package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxManageCodeMcRecordService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.jx.IJxManageCodeReportDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProduceSnMcRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxManageCodeModel;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxManageCodeResultModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProduceSnExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProduceSnMcRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.repository.MesProduceSnRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-jx
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-12-21 11:57
 */
@Service
public class JxManageCodeReportService implements IJxManageCodeMcRecordService {

    @Autowired
    private IJxManageCodeReportDao manageCodeReportDao;

    @Autowired
    private MesProduceSnMcRecordRepository produceSnMcRecordRepository;

    @Autowired
    private MesWorkOrderRepository workOrderRepository;

    @Override
    public ListPager<JxManageCodeResultModel> queryManageCodeMcRecordReportByPager(JxManageCodeModel manageCodeModel, Pager pager) {

        pager = PagerHelper.getPager(pager, manageCodeReportDao.queryManageCodeMcRecordReportPagerCount(manageCodeModel));
        List<JxManageCodeResultModel> resultList = manageCodeReportDao.queryManageCodeMcRecordReportPager(manageCodeModel , pager);

        if (!CollectionUtils.isEmpty(resultList)) {
            for (JxManageCodeResultModel model : resultList) {
                model.setSnStatusName(MesExtEnumUtil.PRODUCE_SN_STATUS.valueOf(model.getSnStatus()));
            }
        }

        return new ListPager(resultList, pager);
    }

    @Override
    public void doUnbindManageCodeMcRecord(JxManageCodeModel manageCodeModel) {

        MesProduceSnMcRecord produceSnMcRecord = produceSnMcRecordRepository.getById(manageCodeModel.getId());

        if (produceSnMcRecord == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("当前操作资源已下线,请重新查询")
                    .build();
        }

        MesWorkOrder workOrder = workOrderRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.ORDER_NO},
                new Object[]{manageCodeModel.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), produceSnMcRecord.getWorkOrderNo()});

        if (MesExtEnumUtil.WORK_ORDER_STATUS.CLOSE.getValue() != workOrder.getWorkOrderStatus() && MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue() != workOrder.getWorkOrderStatus()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("管理码[%s]关联工单[%s]不为关闭或删除状态,不能强制解绑", produceSnMcRecord.getManageCode(), produceSnMcRecord.getWorkOrderNo())
                    .build();
        }

        produceSnMcRecord.setSnStatus(MesExtEnumUtil.MC_RECORD_SN_STATUS.LINE_OFF.getValue());
        produceSnMcRecord.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelUpdate(produceSnMcRecord, manageCodeModel.getUserInfo());
        produceSnMcRecordRepository.save(produceSnMcRecord);

    }

}
