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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesCustomerPartService;
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.MesCustPartInvoiced;
import cn.estsh.i3plus.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.repository.MesCustPartInvoicedRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesCustomerPartRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @PROJECT_NAME: i3plus-mes-yfai
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2024-09-12 20:41
 */
@Service
public class MesCustomerPartService implements IMesCustomerPartService {

    @Autowired
    private MesCustomerPartRepository customerPartRepository;
    @Autowired
    private MesCustPartInvoicedRepository partInvoicedRao;


    @Override
    public MesCustomerPart getMesCustomerPartByPartNo(String erpPartNo, String custCode, String organizeCode) {

        if (StringUtils.isEmpty(erpPartNo) || StringUtils.isEmpty(custCode) || StringUtils.isEmpty(organizeCode)) return null;

        return customerPartRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ERP_PART_NO, MesPcnExtConstWords.CUST_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), erpPartNo, custCode});

    }

    @Override
    public MesCustomerPart getMesCustomerPart(String organizeCode,String partNo) {
        if (StringUtils.isEmpty(partNo)|| StringUtils.isEmpty(organizeCode)) return null;

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "erpPartNo", ddlPackBean);
        return customerPartRepository.getByProperty(ddlPackBean);
    }

    @Override
    public boolean isExistCustomerPart(String organizeCode, String partNo, String custPartNo) {
        DdlPackBean invoicedPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", invoicedPackBean);
        DdlPreparedPack.getStringEqualPack(custPartNo, "custPartNo", invoicedPackBean);
        if (partInvoicedRao.isExitByHql(invoicedPackBean)) {
            return true;
        }

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "erpPartNo", packBean);
        DdlPreparedPack.getStringEqualPack(custPartNo, "custPartNo", packBean);
        return customerPartRepository.isExitByHql(packBean);
    }

    public String getCustPartNo(String organizeCode, String partNo) {
        if (StringUtils.isEmpty(partNo)|| StringUtils.isEmpty(organizeCode)) return "";

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", ddlPackBean);
        MesCustPartInvoiced invoiced = partInvoicedRao.getByProperty(ddlPackBean);
        if (invoiced != null && !StringUtils.isEmpty(invoiced.getCustPartNo())) {
            return invoiced.getCustPartNo();
        }

        MesCustomerPart customerPart = getMesCustomerPart(organizeCode, partNo);
        return customerPart != null ? customerPart.getCustPartNo() : "";
    }
}
