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

import cn.estsh.i3plus.ext.mes.api.base.IShiftExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftExt;
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 ShiftExtController extends MesBaseController {

    @Autowired
    private IShiftExtService shiftExtService;

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

    @PostMapping(value = "/shift-ext/insert")
    @ApiOperation(value = "新增班次信息")
    public ResultBean insertShift(MesShiftExt shiftExt) {
        try {
            ValidatorBean.beginValid(shiftExt)
                    .notNull(MesExtConstWords.SHIFT_CODE, shiftExt.getShiftCode())
                    .notNull(MesExtConstWords.SHIFT_NAME, shiftExt.getShiftName())
                    .notNull(MesExtConstWords.AREA_CODE, shiftExt.getAreaCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, shiftExt.getWorkCenterCode())
                    .notNull(MesExtConstWords.START_TIME, shiftExt.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, shiftExt.getEndTime())
                    .notNull(MesExtConstWords.WORKER_QTY, shiftExt.getWorkerQty());

            shiftExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            shiftExt.setCreateUser(AuthUtil.getSessionUser().getUserName());
            shiftExtService.insertShift(shiftExt);

            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-ext/update")
    @ApiOperation(value = "更新班次信息")
    public ResultBean updateShift(MesShiftExt shiftExt) {
        try {
            ValidatorBean.beginValid(shiftExt)
                    .notNull(MesExtConstWords.ID, shiftExt.getId())
                    .notNull(MesExtConstWords.SHIFT_NAME, shiftExt.getShiftName())
                    .notNull(MesExtConstWords.START_TIME, shiftExt.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, shiftExt.getEndTime())
                    .notNull(MesExtConstWords.WORKER_QTY, shiftExt.getWorkerQty());

            shiftExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            shiftExt.setModifyUser(AuthUtil.getSessionUser().getUserName());
            shiftExtService.updateShift(shiftExt);
            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-ext/status-update")
    @ApiOperation(value = "根据id启用或禁用班次信息")
    public ResultBean updateShiftValidById(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();
            }
            shiftExtService.updateShiftValidById(id, status, 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);
        }
    }

}
