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

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxProductReportService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdBindRecordExt;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProdBindRecordExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProdBindRecord;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-jx
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-11-13 17:06
 */
@Service
public class JxProductReportService implements IJxProductReportService {

    @Autowired
    private MesPartExtRepository partExtRepository;

    @Autowired
    private MesProdBindRecordExtRepository prodBindRecordExtRepository;

    @Override
    public ListPager<MesProdBindRecord> queryMesProdBindRecordByPager(MesProdBindRecordExt prodBindRecordExtExt, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getMesProdBindRecordExt(prodBindRecordExtExt);
        int count = prodBindRecordExtRepository.findByHqlWhereCount(hqlPack);
        pager = PagerHelper.getPager(pager, count);
        hqlPack.setOrderByStr(prodBindRecordExtExt.orderBy());
        List<MesProdBindRecordExt> prodBindRecordExts = prodBindRecordExtRepository.findByHqlWherePage(hqlPack, pager);
        if (CollectionUtils.isEmpty(prodBindRecordExts)) {
            return new ListPager(prodBindRecordExts, pager);
        }

        // 去重并获取物料代码
        Set<String> itemPartNoSet = prodBindRecordExts.stream().map(MesProdBindRecord::getItemPartNo).collect(Collectors.toSet());
        Set<String> partNoSet = prodBindRecordExts.stream().map(MesProdBindRecord::getPartNo).collect(Collectors.toSet());

        partNoSet.addAll(itemPartNoSet);
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(prodBindRecordExtExt.getOrganizeCode());
        DdlPreparedPack.getInPackList(new ArrayList<>(partNoSet), MesExtConstWords.PART_NO, packBean);
        List<MesPartExt> parts = partExtRepository.findByHqlWhere(packBean);
        Map<String, String> partMap = parts.stream().collect(Collectors.toMap(MesPartExt::getPartNo, MesPartExt::getPartName));

        for (MesProdBindRecordExt mesProdBindRecord : prodBindRecordExts) {
            if (!CollectionUtils.isEmpty(partMap)) {
                if (!StringUtil.isEmpty(mesProdBindRecord.getPartNo()) && partMap.containsKey(mesProdBindRecord.getPartNo())) {
                    mesProdBindRecord.setPartName(partMap.get(mesProdBindRecord.getPartNo()));
                }
                if (!StringUtil.isEmpty(mesProdBindRecord.getItemPartNo()) && partMap.containsKey(mesProdBindRecord.getItemPartNo())) {
                    mesProdBindRecord.setItemPartName(partMap.get(mesProdBindRecord.getItemPartNo()));
                }
            }
            mesProdBindRecord.setActionTypeName(StringUtil.isEmpty(mesProdBindRecord.getActionType()) ?
                    null : MesEnumUtil.MES_ACTION_TYPE.valueOfDescription(mesProdBindRecord.getActionType()));
            mesProdBindRecord.setIsBindKeyName(mesProdBindRecord.getIsBindKey() == null ?
                    null : MesEnumUtil.MES_IS_BIND_KEY.valueOfDescription(mesProdBindRecord.getIsBindKey()));
        }

        return new ListPager(prodBindRecordExts, pager);
    }
}
