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

import cn.estsh.i3plus.ext.mes.pcn.api.base.bu.IBuShiftProdCenterRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesShiftExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesShiftProd;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.MesPcnBaseController;
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.*;

import java.util.Map;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN+"/bu-ext")
@Api(tags = "停开机记录")
public class BuShiftProdCenterRecordController extends MesPcnBaseController {

    @Autowired
    private IBuShiftProdCenterRecordService shiftProdCenterRecordService;

    @GetMapping(value = "/shift-ext-all/query")
    @ApiOperation(value = "查询班次所有有效数据")
    public ResultBean queryShiftAll(MesShiftExt shiftExt) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(shiftProdCenterRecordService.queryShiftAll(shiftExt));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/shift-prod-center/query-by-center")
    @ApiOperation(value = "查询停开机记录页面展示数据")
    public ResultBean queryShiftProdCenterData(MesShiftExt shiftExt) {
        try {
            ValidatorBean.beginValid(shiftExt)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, shiftExt.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.AREA_CODE, shiftExt.getAreaCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, shiftExt.getWorkCenterCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(shiftProdCenterRecordService.queryShiftProdCenterData(shiftExt));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/shift-prod-center/stop")
    @ApiOperation(value = "停开机记录-结束班次")
    public ResultBean doShiftProdCenterStop(MesWorkCenterExt workCenter, String partNo, String notReachReason) {
        try {
            ValidatorBean.beginValid(workCenter)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, workCenter.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.AREA_CODE, workCenter.getAreaCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, workCenter.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, workCenter.getModifyUser());
            return shiftProdCenterRecordService.doShiftProdCenterStop(workCenter, partNo, notReachReason);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/shift-prod-center/start")
    @ApiOperation(value = "停开机记录-启动班次")
    public ResultBean doShiftProdCenterStart(MesWorkCenterExt workCenter) {
        try {
            ValidatorBean.beginValid(workCenter)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, workCenter.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.AREA_CODE, workCenter.getAreaCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, workCenter.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.SHIFT_CODE, workCenter.getShiftCode())
                    .notNull(MesPcnExtConstWords.WORKER_QTY, workCenter.getWorkerQty())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, workCenter.getModifyUser());

            shiftProdCenterRecordService.doShiftProdCenterStart(workCenter);

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

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

    @PostMapping(value = "/shift-prod-center-record/insert")
    @ApiOperation(value = "新增停开机记录")
    public ResultBean insertShiftProdCenterRecord(MesShiftProdCenterRecord record) {
        try {
            ValidatorBean.beginValid(record)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, record.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, record.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.SHIFT_CODE, record.getShiftCode())
                    .notNull(MesPcnExtConstWords.SHIFT_NAME, record.getWorkerQty())
                    .notNull(MesPcnExtConstWords.WORK_TIME, record.getWorkTime())
                    .notNull(MesPcnExtConstWords.PROD_CODE, record.getProdCode())
                    .notNull(MesPcnExtConstWords.PROD_NAME, record.getProdName())
                    .notNull(MesPcnExtConstWords.PROD_CATEGORY, record.getProdCategory())
                    .notNull(MesPcnExtConstWords.IS_PROD_TIME, record.getIsProdTime())
                    .notNull(MesPcnExtConstWords.PROD_TYPE, record.getProdType())
                    .notNull(MesPcnExtConstWords.START_TIME, record.getStartTime())
                    .notNull(MesPcnExtConstWords.END_TIME, record.getEndTime())
                    .notNull(MesPcnExtConstWords.WORKER_QTY, record.getWorkerQty())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, record.getModifyUser());
            shiftProdCenterRecordService.insertShiftProdCenterRecord(record);
            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-record/update")
    @ApiOperation(value = "编辑停开机记录")
    public ResultBean updateShiftProdCenterRecord(MesShiftProdCenterRecord record) {
        try {
            ValidatorBean.beginValid(record)
                    .notNull(MesPcnExtConstWords.ID, record.getId())
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, record.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, record.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.SHIFT_CODE, record.getShiftCode())
                    .notNull(MesPcnExtConstWords.WORK_TIME, record.getWorkTime())
                    .notNull(MesPcnExtConstWords.PROD_TYPE, record.getProdType())
                    .notNull(MesPcnExtConstWords.START_TIME, record.getStartTime())
                    .notNull(MesPcnExtConstWords.END_TIME, record.getEndTime())
                    .notNull(MesPcnExtConstWords.WORKER_QTY, record.getWorkerQty())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, record.getModifyUser());
            shiftProdCenterRecordService.updateShiftProdCenterRecord(record);
            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-record/delete")
    @ApiOperation(value = "根据id删除停开机记录")
    public ResultBean deleteShiftProdCenterRecodeById(Long id, String modifyUser) {
        try {
            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            shiftProdCenterRecordService.deleteShiftProdCenterRecodeById(id, modifyUser);
            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-record/form-andon/save")
    @ApiOperation(value = "安灯保存停开机记录")
    public ResultBean saveShiftProdCenterRecordFromAndon(@RequestBody Map<String, Object> params) {
        try {
            return ResultBean.success("新增成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(shiftProdCenterRecordService.saveShiftProdCenterRecordFromAndon(params));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/shift-prod-start-time/query")
    @ApiOperation(value = "根据生产类型获取开始时间")
    public ResultBean queryStartTime(MesShiftProdCenterRecord record) {
        try {
            ValidatorBean.beginValid(record)
                    .notNull(MesPcnExtConstWords.SHIFT_CODE, record.getShiftCode())
                    .notNull(MesPcnExtConstWords.WORK_TIME, record.getWorkTime())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, record.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.PROD_TYPE, record.getProdType())
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, record.getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(shiftProdCenterRecordService.queryStartTime(record));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
