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

import cn.estsh.i3plus.ext.mes.api.base.IQManageService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessQManage;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesQManageTaskDetail;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
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.platform.bean.SessionUser;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 13:33 下午
 * @Description:
 **/
@RestController
@Api(tags = "Q管理")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/q-data")
public class QManageController {
    public static final Logger LOGGER = LoggerFactory.getLogger(QManageController.class);

    @Autowired
    private IQManageService qManageService;

    @PostMapping(value = "/mes-process-q-manage/insert")
    @ApiOperation(value = "添加MES_工序Q管理项目信息")
    public ResultBean insertMesProcessQManage(MesProcessQManage mesProcessQManage) {
        try {
            //条件验证
            ValidatorBean.beginValid(mesProcessQManage)
                    .notNull(MesExtConstWords.PROCESS_CODE,mesProcessQManage.getProcessCode())
                    .notNull("manageProjects", mesProcessQManage.getManageProjects())
                    .notNull("screwType", mesProcessQManage.getScrewType())
                    .notNull("tighteningTorque",mesProcessQManage.getTighteningTorque());
            mesProcessQManage.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            qManageService.insertMesProcessQManage(mesProcessQManage);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-process-q-manage/update")
    @ApiOperation(value = "更新MES_工序Q管理项目")
    public ResultBean updateMesProcessQManage(MesProcessQManage mesProcessQManage) {
        try {
            //条件验证
            ValidatorBean.beginValid(mesProcessQManage)
                    .notNull(MesExtConstWords.PROCESS_CODE,mesProcessQManage.getProcessCode())
                    .notNull("manageProjects", mesProcessQManage.getManageProjects())
                    .notNull("screwType", mesProcessQManage.getScrewType())
                    .notNull("tighteningTorque",mesProcessQManage.getTighteningTorque());

            if (mesProcessQManage.getId() == null || mesProcessQManage.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            qManageService.updateMesProcessQManage(mesProcessQManage, 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 = "/mes-process-q-manage/query")
    @ApiOperation(value = "按条件分页查询MES_工序Q管理项目信息", notes = "按条件分页查询MES_工序Q管理项目信息")
    public ResultBean queryMesProcessQManageByPager(MesProcessQManage mesProcessQManage, Pager pager) {
        try {
            mesProcessQManage.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setListPager(qManageService.queryMesProcessQManageByPager(mesProcessQManage, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-process-q-manage/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateMesProcessQManageStatus(String[] idsStr, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(idsStr, true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            qManageService.updateMesProcessQManageStatus(idsStr, status, user.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 = "/mes-qmanage-task-detail/report-query")
    @ApiOperation(value = "Q管理查询报表", notes = "Q管理查询报表")
    public ResultBean queryQmanageTaskDetailReportByPager(MesQManageTaskDetail mesQManageTaskDetail, Pager pager) {
        try {
            mesQManageTaskDetail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setListPager(qManageService.queryQmanageTaskDetailReportByPager(mesQManageTaskDetail, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/ep/mes-qmanage-task-detail/report-query")
    @ApiOperation(value = "EP-Q管理查询报表", notes = "EP-Q管理查询报表")
    public ResultBean queryEpQmanageTaskDetailReportByPager(MesQManageTaskDetail mesQManageTaskDetail, Pager pager) {
        try {
            mesQManageTaskDetail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setListPager(qManageService.queryEpQManageTaskDetailReportByPager(mesQManageTaskDetail, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-qmanage-task-detail/by-id/update")
    @ApiOperation(value = "点击Q管理明细报表中的品质确认和班组确认按钮")
    public ResultBean updateMesQManageTaskDetailById(@RequestBody List<MesQManageTaskDetail> mesQManageTaskDetails) {
        try {
            qManageService.updateMesQManageTaskDetailById(mesQManageTaskDetails, 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);
        }
    }
}
