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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpEquipmentCheckService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEpEquipmentCheck;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.BaseController;
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.*;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-13 10:31
 */
@RestController
@Api(tags = "设备点检项目")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ep-ext/equipment-check")
public class EpEquipmentCheckController extends BaseController {

    @Autowired
    private IEpEquipmentCheckService epEquipmentCheckService;

    @GetMapping("/query")
    @ApiOperation("查询")
    public ResultBean queryEquipmentCheckByPager(MesEpEquipmentCheck equipment, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(epEquipmentCheckService.queryByPager(equipment, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/insert")
    @ApiOperation("新增")
    public ResultBean insertEquipmentCheck(MesEpEquipmentCheck equipmentCheck){
        try {
            //条件验证
            ValidatorBean.beginValid(equipmentCheck)
                    .notNull(MesExtConstWords.EQU_NO, equipmentCheck.getEquNo())
                    .notNull(MesExtConstWords.EQU_NAME, equipmentCheck.getEquName())
                    .notNull(MesExtConstWords.CHECK_TYPE, equipmentCheck.getCheckType())
                    .notNull(MesExtConstWords.CHECK_METHOD, equipmentCheck.getCheckMethod())
                    .notNull(MesExtConstWords.RESULT_TYPE, equipmentCheck.getResultType())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, equipmentCheck.getWorkCellCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, equipmentCheck.getWorkCenterCode())
                    .notNull(MesExtConstWords.PROJECT_CONTENT, equipmentCheck.getProjectContent())
                    .notNull(MesExtConstWords.WORK_CELL_NAME, equipmentCheck.getWorkCellName())
            ;
            return ResultBean.success("新增成功").setResultObject(epEquipmentCheckService.insert(equipmentCheck))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("/update")
    @ApiOperation("修改")
    public ResultBean updateEquipmentCheck(MesEpEquipmentCheck equipmentCheck){
        try {
            //条件验证
            ValidatorBean.beginValid(equipmentCheck)
                    .notNull(MesExtConstWords.ID,equipmentCheck.getId())
                    .notNull(MesExtConstWords.EQU_NO, equipmentCheck.getEquNo())
                    .notNull(MesExtConstWords.EQU_NAME, equipmentCheck.getEquName())
                    .notNull(MesExtConstWords.CHECK_TYPE, equipmentCheck.getCheckType())
                    .notNull(MesExtConstWords.CHECK_METHOD, equipmentCheck.getCheckMethod())
                    .notNull(MesExtConstWords.RESULT_TYPE, equipmentCheck.getResultType())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, equipmentCheck.getWorkCellCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, equipmentCheck.getWorkCenterCode())
                    .notNull(MesExtConstWords.PROJECT_CONTENT, equipmentCheck.getProjectContent())
                    .notNull(MesExtConstWords.WORK_CELL_NAME, equipmentCheck.getWorkCellName());
            return ResultBean.success("修改成功").setResultObject(epEquipmentCheckService.update(equipmentCheck))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 根据ID启用或禁用单据规则
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status-update")
    @ApiOperation(value = "根据ID启用或禁用")
    public ResultBean updateEquipmentCheckIsValidById(long id, Integer status) {
        try {
            return ResultBean.success("根据ID启用或禁用").setResultObject(epEquipmentCheckService.updateIsValidById(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
