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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesMediaFileCfgService;
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.MesMediaFileCfg;
import cn.estsh.i3plus.pojo.mes.repository.MesMediaFileCfgRepository;
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;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MesMediaFileCfgService implements IMesMediaFileCfgService {

    @Autowired
    private MesMediaFileCfgRepository mediaFileCfgRepository;

    @Override
    public MesMediaFileCfg getMediaFileCfg(String organizeCode, Long id) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(id)) return null;
        MesMediaFileCfg mediaFileCfg = mediaFileCfgRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), id});
        return (null == mediaFileCfg || StringUtils.isEmpty(mediaFileCfg.getFileUrl())) ? null : mediaFileCfg;
    }

    @Override
    public MesMediaFileCfg getMediaFileCfg(String organizeCode, String idStr) {
        Long id = null;
        try { id = Long.valueOf(idStr);} catch (NumberFormatException e) {}
        return getMediaFileCfg(organizeCode, id);
    }

    @Override
    public String getMediaFileUrl(String organizeCode, Long id) {
        MesMediaFileCfg mediaFileCfg = getMediaFileCfg(organizeCode, id);
        return (null == mediaFileCfg || StringUtils.isEmpty(mediaFileCfg.getFileUrl())) ? null : mediaFileCfg.getFileUrl();
    }

    @Override
    public String getMediaFileUrl(String organizeCode, String idStr) {
        MesMediaFileCfg mediaFileCfg = getMediaFileCfg(organizeCode, idStr);
        return (null == mediaFileCfg || StringUtils.isEmpty(mediaFileCfg.getFileUrl())) ? null : mediaFileCfg.getFileUrl();
    }

    @Override
    public List<MesMediaFileCfg> getMediaFileCfgList(String organizeCode, List<Long> idList) {
        if (StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(idList)) return null;
        if (idList.size() != 1) idList = idList.stream().filter(o -> !StringUtils.isEmpty(o)).distinct().collect(Collectors.toList());
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (idList.size() == 1) DdlPreparedPack.getNumEqualPack(idList.get(0), MesPcnExtConstWords.ID, packBean);
        else DdlPreparedPack.getInPackList(idList, MesPcnExtConstWords.ID, packBean);
        return mediaFileCfgRepository.findByHqlWhere(packBean);
    }

    @Override
    public Map<Long, String> getMediaFileCfgMap(String organizeCode, List<Long> idList) {
        if (StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(idList)) return null;
        List<MesMediaFileCfg> mediaFileCfgList = getMediaFileCfgList(organizeCode, idList);
        if (CollectionUtils.isEmpty(mediaFileCfgList)) return null;
        return mediaFileCfgList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getFileUrl()))).collect(Collectors.toMap(MesMediaFileCfg::getId, MesMediaFileCfg::getFileUrl));
    }
}
