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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesOrgService;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesOrg;
import cn.estsh.i3plus.pojo.mes.repository.MesOrgRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 责任人区域关系
 * @Author: gsz
 * @Date: 2024/6/5 10:16
 * @Modify:
 */
@Service
@Slf4j
public class MesOrgServiceImpl implements IMesOrgService {

    @Autowired
    private MesOrgRepository mesOrgRDao;

    @Override
    public List<MesOrg> queryMesOrg(MesOrg bean) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(bean.getInventoryLocationCode(), "inventoryLocationCode", packBean);
        if(StringUtil.isEmpty(bean.getResponsibleParty())){
            DdlPreparedPack.getIsNotNull( "responsibleParty", packBean);
        }else {
            DdlPreparedPack.getStringEqualPack(bean.getResponsibleParty(), "responsibleParty", packBean);
        }
        packBean.setOrderByStr(bean.orderBy());

        return mesOrgRDao.findByHqlWhere(packBean);
    }

    @Override
    public List<MesOrg> queryMesOrgSplit(MesOrg mesOrg) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(mesOrg.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesOrg.getInventoryLocationCode(), "inventoryLocationCode", packBean);
        if(StringUtil.isEmpty(mesOrg.getResponsibleParty())){
            DdlPreparedPack.getIsNotNull( "responsibleParty", packBean);
        }else {
            DdlPreparedPack.getStringEqualPack(mesOrg.getResponsibleParty(), "responsibleParty", packBean);
        }
        packBean.setOrderByStr(mesOrg.orderBy());
        List<MesOrg> mesOrgList = mesOrgRDao.findByHqlWhere(packBean);
        List<MesOrg> resultList = new ArrayList<>();
        for (MesOrg org : mesOrgList) {
            String responsibleParty = org.getResponsibleParty();
            if (StringUtil.isEmpty(responsibleParty)) {
                resultList.add(org);
            }else {
                if (responsibleParty.contains(",")) {
                    String[] split = responsibleParty.split(",");
                    for (int i = 0; i < split.length; i++) {
                        MesOrg mesOrg1 = new MesOrg();
                        mesOrg1.setId(org.getId());
                        mesOrg1.setOrganizeCode(org.getOrganizeCode());
                        mesOrg1.setInventoryLocationCode(org.getInventoryLocationCode());
                        mesOrg1.setInventoryLocationName(org.getInventoryLocationName());
                        mesOrg1.setResponsibleParty(split[i]);
                        resultList.add(mesOrg1);
                    }
                }else {
                    resultList.add(org);
                }
            }
        }
        return resultList;
    }

}
