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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductVersionService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesProductVersion;
import cn.estsh.i3plus.pojo.mes.repository.MesProductVersionRepository;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @Description : 生产版本
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/4 9:29
 * @Modify:
 **/
@Service
@Slf4j
public class MesProductVersionServiceImpl implements IMesProductVersionService {

    @Autowired
    private MesProductVersionRepository mesProductVersionRepository;

    @Override
    public MesProductVersion getMesProductVersion(String organizeCode, String partNo, String productVersion) {
        if(StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo) ||  StringUtils.isEmpty(productVersion)){
            return null;
        }
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", ddlPackBean);
        DdlPreparedPack.getStringEqualPack(productVersion, "productVersion", ddlPackBean);
        return mesProductVersionRepository.getByProperty(ddlPackBean);
    }

    @Override
    public List<MesProductVersion> findMesProductVersionByPartNo(String organizeCode, String partNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo)) return null;
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", ddlPackBean);
        return mesProductVersionRepository.findByHqlWhere(ddlPackBean);
    }

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