package cn.estsh.i3plus.ext.mes.apiservice.controller.base;

import cn.estsh.i3plus.ext.mes.api.base.IMesAccidentDateService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesAccidentDate;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文件描述
 *
 * @author xiangming.liao
 * @date 2021年06月29日 14:13
 */
@RestController
@Api(tags = "事故日期")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/accident-date")
public class MesAccidentDateController extends MesBaseController {

    @Autowired
    private IMesAccidentDateService accidentDateService;

    /**
     * 新增事故日期
     *
     * @param accidentDate
     * @return
     */
    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增事故日期")
    public ResultBean insertAccidentDate(MesAccidentDate accidentDate) {
        try {
            //条件验证
            ValidatorBean.beginValid(accidentDate)
                    .notNull("accidentDate", accidentDate.getAccidentDate())
                    .notNull("accidentStatus", accidentDate.getAccidentStatus());
            return ResultBean.success("新增事故日期成功").setResultObject(accidentDateService.insertAccidentDate(accidentDate))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 启用禁用事故日期
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status-update")
    @ApiOperation(value = "启用禁用事故日期")
    public ResultBean updateStatusAccidentDate(long id,Integer status) {
        try {
            return ResultBean.success("启用/禁用事故日期成功").setResultObject(accidentDateService.updateStatusAccidentDate(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
    /**
     * 修改事故日期
     *
     * @param accidentDate
     * @return
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改事故日期")
    public ResultBean updateAccidentDate(MesAccidentDate accidentDate) {
        try {
            //条件验证
            ValidatorBean.beginValid(accidentDate)
                    .notNull("accidentDate", accidentDate.getAccidentDate())
                    .notNull("accidentStatus", accidentDate.getAccidentStatus());
            return ResultBean.success("修改事故日期成功").setResultObject(accidentDateService.updateAccidentDate(accidentDate))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询事故日期分页
     *
     * @param accidentDate
     * @param pager
     * @return
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "查询事故日期分页")
    public ResultBean queryAccidentDate(MesAccidentDate accidentDate, Pager pager) {
        try {
            return ResultBean.success("查询事故日期成功").setListPager(accidentDateService.queryAccidentDateByPager(accidentDate, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * @Description: 查询最新的事故日期
     * @Param []
     * @return cn.estsh.impp.framework.boot.util.ResultBean
     * @Date 2021/7/1 15:34
     **/
    @GetMapping(value = "/by-newest/query")
    @ApiOperation(value = "查询最新的事故日期")
    public ResultBean queryAccidentDateByNewest() {
        try {
            return ResultBean.success("查询事故日期成功").setResultObject(accidentDateService.queryAccidentDateByNewest())
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
