package cn.estsh.i3plus.ext.mes.apiservice.controller.busi.bu;

import cn.estsh.i3plus.ext.mes.api.base.bu.IBuWorkTimeDetailService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.BuWorkTimeDetailModel;
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.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.web.bind.annotation.*;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-09-27 13:06
 */
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/bu/work-time-detail")
@Api(tags = "作业时间明细")
public class MesWorkTimeDetailController extends MesBaseController {

    @Autowired
    private IBuWorkTimeDetailService workTimeDetailService;

    @GetMapping(value = "/find-work-time-detail-by-pager")
    @ApiOperation(value = "分页查询作业时间信息", notes = "分页查询作业时间信息")
    public ResultBean findWorkTimeDetailByPager(BuWorkTimeDetailModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workTimeDetailService.queryWorkTimeDetailByPager(model,pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增作业时间信息", notes = "新增作业时间信息")
    public ResultBean insertBuWorkTimeDetailModel(MesShiftProdCenterRecord model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_TIME, model.getWorkTime())
                    .notNull(MesExtConstWords.PROD_CODE, model.getProdCode())
                    .notNull(MesExtConstWords.WORKER_QTY, model.getWorkerQty())
                    .notNull(MesExtConstWords.SHIFT_CODE, model.getShiftCode())
                    .notNull(MesExtConstWords.START_TIME, model.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, model.getEndTime());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            workTimeDetailService.insertWorkTimeDetail(model);
            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 = "/update")
    @ApiOperation(value = "编辑作业时间信息", notes = "编辑作业时间信息")
    public ResultBean updateWorkTimeDetail(MesShiftProdCenterRecord model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.WORKER_QTY, model.getWorkerQty())
                    .notNull(MesExtConstWords.START_TIME, model.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, model.getEndTime());
            workTimeDetailService.updateWorkTimeDetail(model);
            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 = "/delete")
    @ApiOperation(value = "启用/禁用作业时间信息", notes = "启用/禁用作业时间信息")
    public ResultBean doValid(Long id) {
        try {
            workTimeDetailService.deleteWorkTimeDetail(id, 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);
        }
    }
}
