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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxTrendAnalysisService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxTrendAnalysisModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
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.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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION: Q管理趋势分析图
 * @USER: xinwang.yi
 * @DATE: 2023-07-03 11:34
 */
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ext/trend-analysis")
@Api(tags = "趋势分析图")
public class SxTrendAnalysisController {

    @Autowired
    private ISxTrendAnalysisService trendAnalysisService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "Q管理项目&工位首检&设备点检 趋势分析")
    public ResultBean queryTrendAnalysis(SxTrendAnalysisModel model, Pager pager) {
        try {
            ValidatorBean.checkNotNull(model.getDataType(), "数据类型不能为空");
            ValidatorBean.checkNotNull(model.getWorkCenterCode(), "产线代码不能为空");
            ValidatorBean.checkNotNull(model.getSpotCheckTimeStart(), "点检时间起始不能为空");
            ValidatorBean.checkNotNull(model.getSpotCheckTimeEnd(), "点检时间截至不能为空");
            ValidatorBean.checkNotNull(model.getManageProjects(), "管理项目不能为空");

            if(MesExtEnumUtil.TREND_ANALYSIS.getByValue(model.getDataType()) == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
                        .setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode())
                        .setErrorDetail("数据类型无效").build();
            }

            switch (MesExtEnumUtil.TREND_ANALYSIS.getByValue(model.getDataType())) {
                case Q_MANAGE:
                case FIRST_ORDER:
                    ValidatorBean.checkNotNull(model.getPartNo(), "物料编码不能为空");
                    ValidatorBean.checkNotNull(model.getProcessCode(), "工序代码不能为空"); break;
                case EQUIP_CHECK:
                    ValidatorBean.checkNotNull(model.getEquNo(), "设备代码不能为空");
                    ValidatorBean.checkNotNull(model.getWorkCellCode(), "工位代码不能为空");
            }

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            Map<String, Object> resultMap = trendAnalysisService.queryTrendAnalysis(model, pager);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(resultMap).setListPager((ListPager) resultMap.get("table"));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/manage-projects/query")
    @ApiOperation(value = "分页查询 Q管理项目 & 工单首检检测信息 & 设备检验项目内容")
    public ResultBean queryQManageProjects(SxTrendAnalysisModel model, Pager pager) {
        try {
            ValidatorBean.checkNotNull(model.getDataType(), "查询数据类型不能为空");
            if(StringUtils.isEmpty(model.getOrganizeCode())) model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(trendAnalysisService.queryManageProjectsByDataType(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/equip/query")
    @ApiOperation(value = "分页查询 设备信息")
    public ResultBean queryEquipByPager(String equNo, String equName, Pager pager) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(trendAnalysisService.queryEquipByPager(equNo, equName, AuthUtil.getOrganize().getOrganizeCode(), pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/equip-by-project-content/query")
    @ApiOperation(value = "分页查询 设备信息")
    public ResultBean queryEquipProjectContent(String workCenterCode, String workCellCode, String projectContent) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(trendAnalysisService.queryEquipByProjectContent(workCenterCode, workCellCode, projectContent, AuthUtil.getOrganize().getOrganizeCode()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
