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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesWorkOrderCutService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.*;
import cn.estsh.i3plus.pojo.mes.repository.*;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-yfai
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2024-09-07 10:57
 */
@Service
public class MesWorkOrderCutService implements IMesWorkOrderCutService {

    @Autowired
    private MesWorkOrderCutRepository workOrderCutRepository;

    @Autowired
    private MesWorkOrderCutDetailRepository workOrderCutDetailRepository;

    @Autowired
    private MesWorkOrderCutMaterialRepository workOrderCutMaterialRepository;

    @Autowired
    private MesCutSchemeRepository cutSchemeRepository;

    @Autowired
    private MesCutSchemeFgRepository cutSchemeFgRepository;

    @Autowired
    private MesCutSchemeEquipmentRepository cutSchemeEquipmentRepository;

    @Override
    public MesWorkOrderCut getMesWorkOrderCut(String cutWorkOrderNo, String organizeCode) {
        return workOrderCutRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CUT_WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cutWorkOrderNo});
    }

    @Override
    public List<MesWorkOrderCutDetail> queryMesWorkOrderCutDetailList(String cutWorkOrderNo, String organizeCode) {
        return workOrderCutDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CUT_WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cutWorkOrderNo});
    }

    @Override
    public MesCutScheme getCutScheme(String cutCode, String organizeCode) {
        return cutSchemeRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CUT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cutCode});
    }

    @Override
    public List<MesCutScheme> queryCutSchemeList(List<String> cutCodeList, String organizeCode) {
        if (CollectionUtils.isEmpty(cutCodeList) || StringUtils.isEmpty(organizeCode)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (cutCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(cutCodeList.get(0), MesPcnExtConstWords.CUT_CODE, packBean);
        else DdlPreparedPack.getInPackList(cutCodeList, MesPcnExtConstWords.CUT_CODE, packBean);
        return cutSchemeRepository.findByHqlWhere(packBean);
    }

    @Override
    public List<MesCutSchemeFg> queryCutSchemeFgList(String cutCode, String organizeCode) {

        if (StringUtils.isEmpty(organizeCode)) return Collections.emptyList();

        return cutSchemeFgRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CUT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cutCode});
    }

    @Override
    public List<MesWorkOrderCutMaterial> queryCutOrderMaterialList(String cutWorkOrder, String organizeCode) {

        if (StringUtils.isEmpty(organizeCode)) return Collections.emptyList();

        return workOrderCutMaterialRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CUT_WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cutWorkOrder});
    }

    @Override
    public MesCutSchemeEquipment getCutSchemeEquipment(String cutCode, String equipmentCode, String organizeCode) {

        return cutSchemeEquipmentRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.CUT_CODE, MesPcnExtConstWords.EQUIPMENT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), cutCode, equipmentCode});

    }
}
