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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesContainerSnService;
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.api.busi.IMesStationService;
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.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.mes.bean.*;
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.List;
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;

    @Autowired
    private IMesStationService mesStationService;

    @Autowired
    private IMesContainerSnService mesContainerSnService;

    @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);
        }
    }

    @GetMapping("/mesStation/query-all-equipment-code-from-mes-station")
    @ApiOperation(value = "查询站点信息中所有的设备代码")
    public ResultBean queryAllEquipmentCodeFromMesStation(String organizeCode) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂代码不能为空");
            List<String> equipmentCodeList = mesStationService.queryAllEquipmentCodeFromMesStation(organizeCode);
            return ResultBean.success("查询站点信息中所有的设备代码成功").setResultList(equipmentCodeList);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/mesStation/query-mes-station-by-pager")
    @ApiOperation(value = "分页查询站点信息")
    public ResultBean queryMesEquipment(MesStation mesStation, Pager pager) {
        try {
            ValidatorBean.checkNotNull(mesStation.getOrganizeCode(), "工厂代码不能为空");
            ListPager<MesStation> mesStationByPager = mesStationService.queryMesStationByPager(mesStation, pager);
            return ResultBean.success("分页查询站点信息成功").setListPager(mesStationByPager);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/mesContainerSn/query-mes-container-sn-by-pager")
    @ApiOperation(value = "分页查询容器条码")
    public ResultBean queryMesContainerSnByPager(MesContainerSn mesContainerSn, Pager pager) {
        try {
            ValidatorBean.checkNotNull(mesContainerSn.getOrganizeCode(), "工厂代码不能为空");
            ListPager<MesContainerSn> mesContainerSnListPager = mesContainerSnService.queryMesContainerSnByPager(mesContainerSn, pager);
            return ResultBean.success("分页查询容器条码成功").setListPager(mesContainerSnListPager);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
