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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesStationService;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesStation;
import cn.estsh.i3plus.pojo.mes.repository.MesStationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description : 站点信息
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/12 13:56
 * @Modify:
 **/
@Service
public class MesStationServiceImpl implements IMesStationService {

    @Autowired
    private MesStationRepository mesStationRepository;


    @Override
    public MesStation getMesStationByEquipmentCode(String organizeCode, String equipmentCode) {
        if (StringUtil.isEmptyAndNull(organizeCode) || StringUtil.isEmptyAndNull(organizeCode)) {
            return null;
        }
        return mesStationRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.EQUIPMENT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), equipmentCode});
    }

    @Override
    public MesStation checkMesStationByEquipmentCode(String organizeCode, String equipmentCode) {
        MesStation mesStation = this.getMesStationByEquipmentCode(organizeCode, equipmentCode);
        if (Objects.isNull(mesStation)) {
            MesPcnException.throwMesBusiException("设备【%s】站点信息未维护", equipmentCode);
        }
        return mesStation;
    }

    @Override
    public List<String> queryAllEquipmentCodeFromMesStation(String organizeCode) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        List<MesStation> mesStationList = mesStationRepository.findByHqlWhere(ddlPackBean);
        List<String> equipmentCodeList = new ArrayList<>();
        if (CollectionUtils.isEmpty(mesStationList)) {
            return equipmentCodeList;
        }else {
            equipmentCodeList = mesStationList.stream().map(MesStation::getEquipmentCode).distinct().collect(Collectors.toList());
            return equipmentCodeList;
        }
    }

    @Override
    public ListPager<MesStation> queryMesStationByPager(MesStation mesStation, Pager pager) {
        DdlPackBean ddlPackBean  = DdlPackBean.getDdlPackBean(mesStation.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(mesStation.getStation(), "station", ddlPackBean);
        DdlPreparedPack.getNumEqualPack(mesStation.getStationType(), "stationType", ddlPackBean);
        DdlPreparedPack.getStringLikerPack(mesStation.getEquipmentCode(), "equipmentCode", ddlPackBean);
        pager = PagerHelper.getPager(pager, mesStationRepository.findByHqlWhereCount(ddlPackBean));
        List<MesStation> mesStationList = mesStationRepository.findByHqlWherePage(ddlPackBean, pager);
        return new ListPager<>(mesStationList, pager);
    }

}
