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

import cn.estsh.i3plus.ext.mes.api.base.IShiftProdService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProd;
import cn.estsh.i3plus.ext.mes.pojo.model.ShiftProdModel;
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 ShiftProdController extends MesBaseController {

    @Autowired
    private IShiftProdService shiftProdService;

    @GetMapping(value = "/shift-prod/query")
    @ApiOperation(value = "按条件分页查询班次作业项目信息")
    public ResultBean queryShiftProdByPager(ShiftProdModel shiftProd, Pager pager) {
        try {
            shiftProd.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(shiftProdService.queryShiftProdByPager(shiftProd, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/shift-prod/insert")
    @ApiOperation(value = "新增班次作业项目信息")
    public ResultBean insertShiftProd(MesShiftProd shiftProd) {
        try {
            ValidatorBean.beginValid(shiftProd)
                    .notNull(MesExtConstWords.PROD_CODE, shiftProd.getProdCode())
                    .notNull(MesExtConstWords.PROD_NAME, shiftProd.getProdName())
                    .notNull(MesExtConstWords.IS_PROD_TIME, shiftProd.getIsProdTime())
                    .notNull(MesExtConstWords.PROD_CATEGORY, shiftProd.getProdCategory())
                    .notNull(MesExtConstWords.PROD_TYPE, shiftProd.getProdType());

            shiftProd.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            shiftProd.setCreateUser(AuthUtil.getSessionUser().getUserName());
            shiftProdService.insertShiftProd(shiftProd);

            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/update")
    @ApiOperation(value = "更新班次作业项目信息")
    public ResultBean updateShiftProd(MesShiftProd shiftProd) {
        try {
            ValidatorBean.beginValid(shiftProd)
                    .notNull(MesExtConstWords.ID, shiftProd.getId())
                    .notNull(MesExtConstWords.PROD_NAME, shiftProd.getProdName())
                    .notNull(MesExtConstWords.IS_PROD_TIME, shiftProd.getIsProdTime())
                    .notNull(MesExtConstWords.PROD_CATEGORY, shiftProd.getProdCategory());

            shiftProd.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            shiftProd.setModifyUser(AuthUtil.getSessionUser().getUserName());
            shiftProdService.updateShiftProd(shiftProd);
            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/status-update")
    @ApiOperation(value = "根据id启用或禁用班次作业项目信息")
    public ResultBean updateShiftProdValidById(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();
            }
            shiftProdService.updateShiftProdValidById(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/delete")
    @ApiOperation(value = "根据id删除班次作业项目信息")
    public ResultBean deleteShiftProdById(Long id) {
        try {
            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            shiftProdService.deleteShiftProdById(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);
        }
    }

}
