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

import cn.estsh.i3plus.ext.mes.api.base.IShiftProdCenterService;
import cn.estsh.i3plus.ext.mes.pojo.model.ShiftProdCenterModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "产线班次固定作业内容控制层")
public class ShiftProdCenterController extends MesBaseController {

    @Autowired
    private IShiftProdCenterService shiftProdCenterService;

    @GetMapping(value = "/shift-prod-center/query")
    @ApiOperation(value = "按条件分页查询产线班次固定作业内容信息")
    public ResultBean queryShiftProdCenterByPager(ShiftProdCenterModel shiftProdCenterModel, Pager pager) {
        try {
            shiftProdCenterModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(shiftProdCenterService.queryShiftProdCenterByPager(shiftProdCenterModel, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/shift-prod-center/insert")
    @ApiOperation(value = "新增产线班次固定作业内容信息")
    public ResultBean insertShiftProdCenter(ShiftProdCenterModel shiftProdCenterModel) {
        try {
            ValidatorBean.beginValid(shiftProdCenterModel)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, shiftProdCenterModel.getWorkCenterCode())
                    .notNull(MesExtConstWords.SHIFT_CODE, shiftProdCenterModel.getShiftCode())
                    .notNull(MesExtConstWords.SHIFT_NAME, shiftProdCenterModel.getShiftName())
                    .notNull(MesExtConstWords.PROD_CODE, shiftProdCenterModel.getProdCode())
                    .notNull(MesExtConstWords.PROD_TYPE, shiftProdCenterModel.getProdType())
                    .notNull(MesExtConstWords.START_TIME, shiftProdCenterModel.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, shiftProdCenterModel.getEndTime());

            shiftProdCenterModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            shiftProdCenterModel.setCreateUser(AuthUtil.getSessionUser().getUserName());
            shiftProdCenterService.insertShiftProdCenter(shiftProdCenterModel);

            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/shift-prod-center/update")
    @ApiOperation(value = "更新产线班次固定作业内容信息")
    public ResultBean updateShiftProdCenter(ShiftProdCenterModel shiftProdCenterModel) {
        try {
            ValidatorBean.beginValid(shiftProdCenterModel)
                    .notNull(MesExtConstWords.ID, shiftProdCenterModel.getId())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, shiftProdCenterModel.getWorkCenterCode())
                    .notNull(MesExtConstWords.SHIFT_CODE, shiftProdCenterModel.getShiftCode())
                    .notNull(MesExtConstWords.PROD_TYPE, shiftProdCenterModel.getProdType())
                    .notNull(MesExtConstWords.START_TIME, shiftProdCenterModel.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, shiftProdCenterModel.getEndTime());

            shiftProdCenterModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            shiftProdCenterModel.setModifyUser(AuthUtil.getSessionUser().getUserName());
            shiftProdCenterService.updateShiftProdCenter(shiftProdCenterModel);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/shift-prod-center/status-update")
    @ApiOperation(value = "根据id启用或禁用产线班次固定作业内容信息")
    public ResultBean updateShiftProdCenterValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            shiftProdCenterService.updateShiftProdCenterValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/shift-prod-center/delete")
    @ApiOperation(value = "根据id删除产线班次固定作业内容信息")
    public ResultBean deleteShiftProdCenterById(Long id) {
        try {
            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            shiftProdCenterService.deleteShiftProdCenterById(id, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("删除成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
