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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IEsopExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEsopExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEsopReadRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
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.i3plus.pojo.mes.bean.MesWorkCell;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN)
@Api(tags = "SOP")
public class EsopExtController extends MesPcnBaseController {

    @Autowired
    private IEsopExtService esopExtService;

    //-------------------------工单SOP----START-------------------------------

    @GetMapping(value = "/sop-ext/query")
    @ApiOperation(value = "按条件分页查询工单SOP信息")
    public ResultBean queryEsopByPager(MesEsopExt esopExt, Pager pager) {
        try {
            ValidatorBean.beginValid(esopExt)
                    .notNull(MesPcnExtConstWords.IS_VALID, esopExt.getIsValid())
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(esopExtService.queryEsopByPager(esopExt, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sop-ext/insert")
    @ApiOperation(value = "新增工单SOP信息")
    public ResultBean insertEsop(MultipartFile file, MesEsopExt esopExt) {
        try {
            if (file == null) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("上传的文件为null，后台未能接收到")
                        .build();
            }

            ValidatorBean.beginValid(esopExt)
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, esopExt.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.PART_NO, esopExt.getPartNo())
                    .notNull(MesPcnExtConstWords.PROCESS_CODE, esopExt.getProcessCode())
                    .notNull(MesPcnExtConstWords.BC_SEQ, esopExt.getBcSeq())
                    .notNull(MesPcnExtConstWords.BC_TIMES, esopExt.getBcTimes())
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, esopExt.getModifyUser());

            esopExtService.insertEsop(file, esopExt);

            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 = "/sop-ext/update")
    @ApiOperation(value = "更新工单SOP信息")
    public ResultBean updateEsop(MultipartFile file, MesEsopExt esopExt) {
        try {
            ValidatorBean.beginValid(esopExt)
                    .notNull(MesPcnExtConstWords.ID, esopExt.getId())
                    .notNull(MesPcnExtConstWords.PROCESS_CODE, esopExt.getProcessCode())
                    .notNull(MesPcnExtConstWords.BC_SEQ, esopExt.getBcSeq())
                    .notNull(MesPcnExtConstWords.BC_TIMES, esopExt.getBcTimes())
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, esopExt.getModifyUser());

            esopExtService.updateEsop(file, esopExt);
            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 = "/sop-ext/delete")
    @ApiOperation(value = "根据id删除工单SOP信息")
    public ResultBean deleteEsopById(Long id, String modifyUser) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            if (StringUtils.isEmpty(modifyUser)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("入参缺少用户信息")
                        .build();
            }
            esopExtService.deleteEsopById(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);
        }
    }

    //-------------------------工单SOP----END-------------------------------


    //-------------------------工位SOP信息播放----START----------------------

    @GetMapping(value = "/bc/sop-ext/query")
    @ApiOperation(value = "SOP信息播放")
    public ResultBean queryBcEsop(MesWorkCell workCell) {
        try {
            ValidatorBean.beginValid(workCell)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, workCell.getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(esopExtService.queryBcEsop(workCell));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/bc/sop-ext/check")
    @ApiOperation(value = "SOP信息播放检查是否被确认")
    public ResultBean checkIsExistEsopReadRecord(MesEsopExt esopExt) {
        try {
            ValidatorBean.beginValid(esopExt)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, esopExt.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, esopExt.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.WORK_CELL_CODE, esopExt.getWorkCellCode())
                    .notNull(MesPcnExtConstWords.PROCESS_CODE, esopExt.getProcessCode())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, esopExt.getModifyUser());
            if (esopExtService.checkIsExistEsopReadRecord(esopExt)) {
                return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                        .setResultObject(LocaleUtil.transferLanguage(esopExt.getModifyUser(), "已确认查阅"));
            } else {
                return ResultBean.fail("操作成功").setCode(ResourceEnumUtil.MESSAGE.FAIL.getCode())
                        .setResultObject(LocaleUtil.transferLanguage(esopExt.getModifyUser(), "请确认查阅"));
            }
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/bc/sop-ext/insert")
    @ApiOperation(value = "SOP信息确认")
    public ResultBean insertEsopReadRecord(MesEsopReadRecord esopReadRecord) {
        try {
            ValidatorBean.beginValid(esopReadRecord)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, esopReadRecord.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.WORK_ORDER_NO, esopReadRecord.getWorkOrderNo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, esopReadRecord.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.WORK_CELL_CODE, esopReadRecord.getWorkCellCode())
                    .notNull(MesPcnExtConstWords.PROCESS_CODE, esopReadRecord.getProcessCode())
                    .notNull(MesPcnExtConstWords.PROCESS_NAME, esopReadRecord.getProcessName())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, esopReadRecord.getModifyUser());

            esopExtService.insertEsopReadRecord(esopReadRecord);

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

    //-------------------------工位SOP信息播放----END------------------------

}
