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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPartService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
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.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPartSap;
import cn.estsh.i3plus.pojo.mes.repository.MesPartRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPartSapRepository;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description : 物料信息
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/13 15:18
 * @Modify:
 **/
@Service
public class MesPartServiceImpl implements IMesPartService {

    @Autowired
    private MesPartRepository mesPartRDao;

    @Autowired
    private MesPartSapRepository mesPartSapRDao;

    @Override
    public ListPager<MesPart> queryMesPart(MesPart mesPart, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(mesPart.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(mesPart.getPartNo(), "partNo", packBean);
        DdlPreparedPack.getStringLikerPack(mesPart.getPartName(), "partName", packBean);
        pager = PagerHelper.getPager(pager, mesPartRDao.findByHqlWhereCount(packBean));
        List<MesPart> list = mesPartRDao.findByHqlWherePage(packBean, pager);
        return new ListPager<>(list, pager);
    }

    @Override
    public MesPart getMesPartByPartNo(String partNo, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", packBean);
        MesPart mesPart = mesPartRDao.getByProperty(packBean);
        if(Objects.isNull(mesPart)){
            MesPcnException.throwMesBusiException("物料【%s】信息不存在",partNo);
        }
        return mesPart;
    }

    @Override
    public List<MesPart> findMesPartByPartNo(List<String> partNoList, String organizeCode) {
        if(StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(partNoList)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (partNoList.size() == MesPcnExtConstWords.ONE) {
            DdlPreparedPack.getStringEqualPack(partNoList.iterator().next(), "partNo", packBean);
        } else {
            DdlPreparedPack.getInPackList(partNoList, "partNo", packBean);
        }
        return mesPartRDao.findByHqlWhere(packBean);
    }

    @Override
    public MesPartSap getMesPartSapByPartNo(String partNo, String organizeCode) {
        if(StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", packBean);
        MesPartSap mesPartSap = mesPartSapRDao.getByProperty(packBean);
        if(Objects.isNull(mesPartSap)){
            MesPcnException.throwMesBusiException("SAP物料【%s】信息不存在",partNo);
        }
        return mesPartSap;
    }

    @Override
    public List<MesPartSap> findMesPartSapByPartNo(List<String> partNoList, String organizeCode) {
        if(StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(partNoList)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if(partNoList.size() == MesPcnExtConstWords.ONE) {
            DdlPreparedPack.getStringEqualPack(partNoList.iterator().next(),"partNo",packBean);
        }else{
            DdlPreparedPack.getInPackList(partNoList,"partNo",packBean);
        }
        return mesPartSapRDao.findByHqlWhere(packBean);
    }
}
