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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesEquipmentExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesConfigService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionRecordService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipment;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionRecord;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Description : 业务信息
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/8/6 20:45
 * @Modify:
 **/
@RestController
@Api(tags = "业务信息")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/busi")
@Slf4j
public class MesBusiController {

    @Autowired
    private IMesProduceSnExtService mesProduceSnExtService;

    @Autowired
    private IMesProductionRecordService mesProductionRecordService;

    @Autowired
    private IMesConfigService mesConfigService;

    @Autowired
    private IMesEquipmentExtService mesEquipmentExtService;

    @GetMapping("/mesProduceSn/query")
    @ApiOperation(value = "查询条码信息")
    public ResultBean queryMesProduceSn(MesProduceSn mesProduceSn, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(mesProduceSnExtService.queryMesProduceSn(mesProduceSn, pager));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/mesProductionRecord/query")
    @ApiOperation(value = "查询加工记录信息PDA报表")
    public ResultBean queryMesProductionRecordReport(MesProductionRecord mesProductionRecord) {
        try {
            ValidatorBean.checkNotNull(mesProductionRecord.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(mesProductionRecord.getProductSn(), "零件条码不能为空");
            return mesProductionRecordService.queryMesProductionRecordReport(mesProductionRecord.getOrganizeCode(), mesProductionRecord.getProductSn());
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/mesConfig/query")
    @ApiOperation(value = "查询系统配置")
    public ResultBean queryMesConfig(MesConfig mesConfig) {
        try {
            ValidatorBean.checkNotNull(mesConfig.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(mesConfig.getCfgCode(), "配置代码不能为空");
            return ResultBean.success("查询成功").setResultList(mesConfigService.findMesConfigList(mesConfig.getOrganizeCode(), mesConfig.getCfgCode()));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/mesEquipment/getByEquipId")
    @ApiOperation(value = "根据设备ID")
    public ResultBean queryMesEquipment(String organizeCode, Integer equipId) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            ValidatorBean.checkNotNull(equipId, "设备ID不能为空");
            MesEquipment mesEquipmentByEquipId = mesEquipmentExtService.getMesEquipmentByEquipId(organizeCode, equipId);
            if(Objects.isNull(mesEquipmentByEquipId)){
                MesPcnException.throwMesBusiException("设备ID【%s】设备信息不存在",equipId);
            }
            return ResultBean.success("查询成功").setResultObject(mesEquipmentByEquipId);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
