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

import cn.estsh.i3plus.ext.mes.api.base.IEsopExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEsopExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEsopReadRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.BatchIdModel;
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.*;
import org.springframework.web.multipart.MultipartFile;

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

    @Autowired
    private IEsopExtService esopExtService;

    @GetMapping(value = "/sop-ext/query")
    @ApiOperation(value = "按条件分页查询SOP信息")
    public ResultBean queryEsopByPager(MesEsopExt esopExt, Pager pager) {
        try {
            esopExt.setOrganizeCode(AuthUtil.getOrganize().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.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("上传的文件为null，后台未能接收到")
                        .build();
            }

            ValidatorBean.beginValid(esopExt)
                    .notNull(MesExtConstWords.BC_SEQ, esopExt.getBcSeq())
                    .notNull(MesExtConstWords.BC_TIMES, esopExt.getBcTimes());

            esopExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            esopExt.setCreateUser(AuthUtil.getSessionUser().getUserName());
            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(MesExtConstWords.ID, esopExt.getId())
                    .notNull(MesExtConstWords.BC_SEQ, esopExt.getBcSeq())
                    .notNull(MesExtConstWords.BC_TIMES, esopExt.getBcTimes());

            esopExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            esopExt.setModifyUser(AuthUtil.getSessionUser().getUserName());
            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/batch/status-update")
    @ApiOperation(value = "根据id批量启用或禁用SOP信息")
    public ResultBean updateBatchEsopValidById(@RequestBody BatchIdModel model) {
        try {
            if (null == model || null == model.getIds()) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            esopExtService.updateBatchEsopValidById(model.getIds(), model.getStatus(), 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);
        }
    }

    @GetMapping(value = "/sop-read-record/query")
    @ApiOperation(value = "按条件分页查询SOP阅读记录")
    public ResultBean queryEsopReadRecordByPager(MesEsopReadRecord esopReadRecord, Pager pager) {
        try {
            esopReadRecord.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(esopExtService.queryEsopReadRecordByPager(esopReadRecord, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sop-ext/file-batch/upload")
    @ApiOperation(value = "批量上传SOP文件")
    public ResultBean doBatchUploadEsopFile(MultipartFile[] files, String lotNo) {
        try {
            if (files == null || StringUtils.isEmpty(lotNo)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("缺少入参,先选择导入批次号后选择文件上传")
                        .build();
            }
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(esopExtService.doBatchUploadEsopFile(
                            files, lotNo, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
