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

import cn.estsh.i3plus.ext.mes.api.base.IDefectExtService;
import cn.estsh.i3plus.ext.mes.pojo.model.SxDefectModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.mes.bean.MesDefect;
import cn.estsh.i3plus.pojo.mes.bean.MesTypeCfg;
import cn.estsh.i3plus.pojo.mes.repository.MesDefectRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesTypeCfgRepository;
import cn.estsh.i3plus.pojo.mes.sqlpack.MesHqlPack;
import org.springframework.beans.BeanUtils;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Service
public class DefectExtService implements IDefectExtService {

    @Autowired
    private MesDefectRepository defectRepository;

    @Autowired
    private MesTypeCfgRepository typeCfgRepository;

    @Override
    public ListPager<SxDefectModel> queryMesDefectByPager(MesDefect mesDefect, Pager pager) {
        List<SxDefectModel> resultList = new ArrayList<>();

        DdlPackBean hqlPack = MesHqlPack.getMesDefect(mesDefect, mesDefect.getOrganizeCode());
        pager = PagerHelper.getPager(pager, defectRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(mesDefect.orderBy());
        List<MesDefect> defectList = defectRepository.findByHqlWherePage(hqlPack, pager);
        List<MesTypeCfg> typeCfgList = typeCfgRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, "typeCode"},
                new Object[]{mesDefect.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
                        CommonEnumUtil.IS_VAILD.VAILD.getValue(), MesExtEnumUtil.MES_TYPE_CFG_EXT.BAD.getValue()});
        Map<String, List<MesTypeCfg>> typeCfgMap = CollectionUtils.isEmpty(typeCfgList) ? null :
                typeCfgList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getTypeCode()))).collect(Collectors.groupingBy(MesTypeCfg::getBusinessTypeCode));
        if (!CollectionUtils.isEmpty(defectList)) {
            for (MesDefect defectDb : defectList) {
                if (null != defectDb) {
                    SxDefectModel result = new SxDefectModel();
                    BeanUtils.copyProperties(defectDb, result);
                    List<MesTypeCfg> typeCfgs = CollectionUtils.isEmpty(typeCfgMap) ? null : typeCfgMap.get(defectDb.getDefectType());
                    result.setDefectTypeName(CollectionUtils.isEmpty(typeCfgs) ? null : typeCfgs.get(0).getBusinessTypeName());
                    resultList.add(result);
                }
            }
        }
        return new ListPager(resultList, pager);
    }
}
