package com.pcloud.book.applet.biz.impl;

import com.google.common.collect.Lists;

import com.pcloud.appcenter.app.dto.AppDto;
import com.pcloud.book.applet.biz.AppletRecordAggrStatisBiz;
import com.pcloud.book.applet.biz.AppletRecordBiz;
import com.pcloud.book.applet.biz.ServeCollectBiz;
import com.pcloud.book.applet.contants.AppletConstants;
import com.pcloud.book.applet.dao.AppletRecordDao;
import com.pcloud.book.applet.dao.AppletRecordSevenDayDao;
import com.pcloud.book.applet.dto.AppletAppOrProductDTO;
import com.pcloud.book.applet.dto.AppletRecordDTO;
import com.pcloud.book.applet.entity.AppletRecord;
import com.pcloud.book.applet.entity.AppletRecordSevenDay;
import com.pcloud.book.applet.entity.ServeCollect;
import com.pcloud.book.applet.enums.AppletRankTypeEnum;
import com.pcloud.book.applet.enums.AppletRecordTypeEnum;
import com.pcloud.book.applet.enums.AppletSourceTypeEnum;
import com.pcloud.book.base.exception.BookBizException;
import com.pcloud.book.consumer.app.AppConsr;
import com.pcloud.book.consumer.label.LabelConsr;
import com.pcloud.book.consumer.reader.ReaderConsr;
import com.pcloud.book.consumer.resource.ProductConsr;
import com.pcloud.book.consumer.user.AdviserConsr;
import com.pcloud.book.consumer.user.MerchantConsr;
import com.pcloud.book.group.biz.ResourcePageBiz;
import com.pcloud.book.group.constant.ResourcePageConstants;
import com.pcloud.book.group.dao.WeworkTeacherFriendRecordDao;
import com.pcloud.book.group.entity.ResourceRecomManage;
import com.pcloud.book.mapper.clickhouse.AppletRecordCHMapper;
import com.pcloud.book.util.common.YesOrNoEnums;
import com.pcloud.book.util.properties.BookProps;
import com.pcloud.common.page.PageBeanNew;
import com.pcloud.common.page.PageParam;
import com.pcloud.common.utils.BeanUtils;
import com.pcloud.common.utils.DateUtils;
import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.cache.redis.JedisClusterUtils;
import com.pcloud.common.utils.string.StringUtil;
import com.pcloud.readercenter.common.enums.YesOrNoNumEnum;
import com.pcloud.readercenter.userlabel.dto.UserGradeLabelIdDTO;
import com.pcloud.resourcecenter.product.dto.ProductDto;
import com.pcloud.resourcecenter.product.dto.ProductLabelDto;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;

@Service("appletRecordAggrStatisBiz")
public class AppletRecordAggrStatisBizImpl implements AppletRecordAggrStatisBiz {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppletRecordAggrStatisBizImpl.class);

    @Autowired
    private AppletRecordCHMapper appletRecordCHMapper;
    @Autowired
    private AppletRecordBiz appletRecordBiz;
    @Autowired
    private AppletRecordDao appletRecordDao;
    @Autowired
    private WeworkTeacherFriendRecordDao weworkTeacherFriendRecordDao;
    @Autowired
    private ServeCollectBiz serveCollectBiz;
    @Autowired
    private AppletRecordSevenDayDao appletRecordSevenDayDao;
    @Autowired
    private ReaderConsr readerConsr;
    @Autowired
    private LabelConsr labelConsr;
    @Autowired
    private ProductConsr productConsr;
    @Autowired
    private AppConsr appConsr;
    @Autowired
    private AdviserConsr adviserConsr;
    @Autowired
    private MerchantConsr merchantConsr;
    @Autowired
    private ResourcePageBiz resourcePageBiz;

    @Override
    public PageBeanNew getAggrStatis(Long wechatUserId, String keyword, Integer sourceType, Integer rankType, Integer currentPage, Integer numPerPage) {
        String cacheKey = MessageFormat.format("{0}_{1}_{2}", AppletConstants.APPLET_RESOURCE_AGGR_STATIS, sourceType, rankType);
        List<AppletRecordDTO> aggrStatis = JedisClusterUtils.getJsonList(cacheKey, AppletRecordDTO.class);
        if(ListUtils.isEmpty(aggrStatis)){
            return new PageBeanNew(currentPage, numPerPage, 0, Lists.newArrayList());
        }

        if(StringUtils.isNotEmpty(keyword)){
            List<AppletRecordDTO> collect = aggrStatis.stream().filter(x -> x.getFromName().contains(keyword)).collect(Collectors.toList());
            if(CollectionUtil.isEmpty(collect)){
                return new PageBeanNew(currentPage, numPerPage, 0, Lists.newArrayList());
            }
            List<AppletRecordDTO> list = aggrStatis.stream().filter(x -> x.getFromName().contains(keyword)).skip(currentPage * numPerPage).limit(numPerPage).collect(Collectors.toList());
            fillCollect(wechatUserId, list);
            return new PageBeanNew(currentPage, numPerPage, collect.size(), list);
        } else {
            List<AppletRecordDTO> list = aggrStatis.stream().skip(currentPage * numPerPage).limit(numPerPage).collect(Collectors.toList());
            fillCollect(wechatUserId, list);
            return new PageBeanNew(currentPage, numPerPage, aggrStatis.size(), list);
        }
    }

    @Override
    public void syncDataAndUpdateCache(String date){
        fixAppletRecordDataSync4CH(date);
        updateAggrStatisCache();
    }

    /**
     * 将 clickHouse 的资源排行信息加入到 redis 缓存
     */
    @Override
    public void updateAggrStatisCache() {
        for (AppletSourceTypeEnum sourceType : AppletSourceTypeEnum.values()) {
            for (AppletRankTypeEnum rankType : AppletRankTypeEnum.values()) {
                String cacheKey = MessageFormat.format("{0}_{1}_{2}", AppletConstants.APPLET_RESOURCE_AGGR_STATIS, sourceType.value, rankType.value);
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("offset", 0);
                paramMap.put("limit", 100);
                addDateCondition(rankType.value, paramMap);
                addTypeCodeCondition(sourceType.value, paramMap);
                List<AppletRecordDTO> aggrStatis = null;
                // 导师需要在另外表里查询
                if(sourceType.value == AppletSourceTypeEnum.ANSWER_1V1.value){
                    paramMap.put("recordType", AppletRecordTypeEnum.WX_WORK_TEACHER.value);
                    aggrStatis = weworkTeacherFriendRecordDao.getAggrStatis(paramMap);
                } else {
                    aggrStatis = appletRecordCHMapper.getAggrStatis(paramMap);
                }
                // 加载其它数据
                appletRecordBiz.fillAppletRecord(aggrStatis, YesOrNoNumEnum.NO.getValue(), null);
                // 删除部分不存在的数据
                aggrStatis = aggrStatis.stream().filter(x->x.getSourceDelete() == 0).collect(Collectors.toList());
                // 设置缓存
                JedisClusterUtils.setJsonList(cacheKey, aggrStatis, 3600 * 24);
            }
        }
    }

    @Override
    public void fixAppletRecordDataSync4CH(String createDate) {
        if(StringUtil.isEmpty(createDate)){
            createDate = DateUtils.getShortDateStr(DateUtils.getDate(-1));
        }
        // 分批插入的大小
        Integer finalBatchSize = 1000;
        final List<AppletRecord> result = new LinkedList<>();

        appletRecordDao.fixAppletRecordDataSync4CH(createDate, new ResultHandler<AppletRecord>(){
            @Override
            public void handleResult(ResultContext<? extends AppletRecord> resultContext) {
                result.add(resultContext.getResultObject());
                if(result.size() == finalBatchSize){
                    appletRecordCHMapper.batchInsert(result);
                    LOGGER.info("[AppletRecordAggrStatisBizImpl.fixDataSync4CH] CH数据同步，NUM：{},数据满1000条，插入一次", resultContext.getResultCount());
                    result.clear();
                }
            }
        });

        if(CollUtil.isNotEmpty(result)){
            appletRecordCHMapper.batchInsert(result);
        }
        appletRecordCHMapper.optimize();
        LOGGER.info("[AppletRecordAggrStatisBizImpl.fixDataSync4CH] CH数据同步结束");
    }



    private void addDateCondition(Integer rankType, HashMap<String, Object> paramMap) {
        if(rankType == 2){
            // 月榜
            paramMap.put("beginDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayBegin(), -30)));
            paramMap.put("endDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayEnd(), -1)));
        }
        if(rankType == 3){
            // 周榜
            paramMap.put("beginDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayBegin(), -7)));
            paramMap.put("endDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayEnd(), -1)));
        }
    }

    private void addTypeCodeCondition(Integer sourceType, HashMap<String, Object> paramMap) {
        List<Map> recordTypes = Lists.newArrayList();
        HashMap<String, Object> recordTypeMap;
        switch (sourceType){
            case 1:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.PRODUCT.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("VIDEO_SCHEDULE", "AUDIO", "AUDIO_SCHEDULE", "VIDEO", "SCHEDULE", "TUTORIAL"));
                recordTypes.add(recordTypeMap);

                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.APP.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("MATCH_LISTEN", "AUDIO", "LISTEN", "VIDEO", "LIVE_TIMETABLE"));
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
            case 2:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.NEWS.value);
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
            case 3:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.PRODUCT.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("PRETEST", "FORMAT", "LINK", "ARTICLE", "PDF"));
                recordTypes.add(recordTypeMap);

                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.APP.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("GROUP", "ZSCORE", "BOOKCARD", "ITEM_BANK", "SPECIAL", "RECITE_WORD", "BOOK", "WORD_DICTATION", "ORAL_EVALUATION", "BOOK_CLICK", "STROKE_ORDER"));
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
            case 4:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.BOOK_RECOMMEND.value);
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
            case 5:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.PRODUCT.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("VIDEO_SCHEDULE", "AUDIO_SCHEDULE", "PDF"));
                recordTypes.add(recordTypeMap);

                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.APP.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("ANSWER", "TEST_PAPER", "TEACH_RESOURCE", "COURSE_WARE", "PDF"));
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
            case 6:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.ADVISER_GROUP.value);
                recordTypes.add(recordTypeMap);

                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.MODEL_GROUP.value);
                recordTypes.add(recordTypeMap);

                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.THIRD_GROUP.value);
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
            case 7:
                /* recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.WX_WORK_TEACHER.value);
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes); */
                break;
            case 8:
                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.PRODUCT.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("ATTENDANCE_TASK"));
                recordTypes.add(recordTypeMap);

                recordTypeMap = new HashMap<>();
                recordTypeMap.put("recordType", AppletRecordTypeEnum.APP.value);
                recordTypeMap.put("typeCodes", Lists.newArrayList("CLOCK", "WISH", "FLAG", "TEST"));
                recordTypes.add(recordTypeMap);

                paramMap.put("recordTypes", recordTypes);
                break;
        }
    }

    private void fillCollect(Long wechatUserId, List<AppletRecordDTO> list) {
        List<ServeCollect> serveCollectList = serveCollectBiz.getList4RightsSettingByWechatUserId(wechatUserId);
        if (ListUtils.isEmpty(serveCollectList)) {
            return;
        }
        List<ServeCollect> collect4Product = serveCollectList.stream().filter(e -> Objects.equals(e.getServeType(), AppletRecordTypeEnum.PRODUCT.value)).collect(Collectors.toList());
        List<ServeCollect> collect4App = serveCollectList.stream().filter(e -> Objects.equals(e.getServeType(), AppletRecordTypeEnum.APP.value)).collect(Collectors.toList());
        Map<Long, ServeCollect> serveCollectMap4Product = new HashMap<>();
        Map<Long, ServeCollect> serveCollectMap4App = new HashMap<>();
        if (!ListUtils.isEmpty(collect4Product)) {
            serveCollectMap4Product = collect4Product.stream().collect(Collectors.toMap(e -> e.getServeId(), a -> a, (k1, k2) -> k1));
        }
        if (!ListUtils.isEmpty(collect4App)) {
            serveCollectMap4App = collect4App.stream().collect(Collectors.toMap(e -> e.getServeId(), a -> a, (k1, k2) -> k1));
        }
        List<AppletRecordDTO> productOrApps = list.stream().filter(e -> Objects.equals(e.getRecordType(), AppletRecordTypeEnum.PRODUCT.value) ||
                Objects.equals(e.getRecordType(), AppletRecordTypeEnum.APP.value)).collect(Collectors.toList());
        if (!ListUtils.isEmpty(productOrApps)) {
            for (AppletRecordDTO item : productOrApps) {
                if (Objects.equals(item.getRecordType(), AppletRecordTypeEnum.PRODUCT.value) && MapUtils.isNotEmpty(serveCollectMap4Product) &&
                        null != serveCollectMap4Product.get(item.getFromId())) {
                    item.setIsCollect(YesOrNoEnums.YES.getValue());
                } else if (Objects.equals(item.getRecordType(), AppletRecordTypeEnum.APP.value) && MapUtils.isNotEmpty(serveCollectMap4App) &&
                        null != serveCollectMap4App.get(item.getFromId())) {
                    item.setIsCollect(YesOrNoEnums.YES.getValue());
                }
            }
        }
    }


    /**
     * 将 clickHouse 的资源排行加入到 数据库
     */
    @Override
    public void updateH5Resource() {
        //资源广场模块
        for (ResourcePageConstants.ResourceSourceTypeEnum sourceTypeEnum : ResourcePageConstants.ResourceSourceTypeEnum.values()) {
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("offset", 0);
            paramMap.put("limit", 1000);
            if ("pro".equals(BookProps.getSystemEnv())) {
                paramMap.put("beginDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayBegin(), -7)));
            }
            paramMap.put("endDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayEnd(), -1)));
            paramMap.put("sourceKeywords", Arrays.asList(sourceTypeEnum.getKeywords()));

            //资源广场模块搜索-7天内扫码量高的资源
            List<AppletRecordDTO> sevenDaysourceAggrStatis = appletRecordCHMapper.getAggrStatis4H5(paramMap);
            if (ListUtils.isEmpty(sevenDaysourceAggrStatis)) {
                continue;
            }
            List<Long> fromIdList = sevenDaysourceAggrStatis.stream().filter(s->s!=null && s.getFromId()>0).map(AppletRecordDTO::getFromId).collect(Collectors.toList());
            Map<String,Object> map = new HashMap<>();
            map.put("endDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayEnd(), -1)));
            map.put("fromIdList", fromIdList);
            
            //7天内扫码量高的资源的总扫码量
            List<AppletRecordDTO> sourceAggrStatis = appletRecordCHMapper.getAggrStatis4H5(map);
            appletRecordBiz.fillAppletRecord(sourceAggrStatis, YesOrNoNumEnum.NO.getValue(), null);
            sourceAggrStatis = sourceAggrStatis.stream().filter(x -> x.getSourceDelete() == 0).collect(Collectors.toList());
            if (ListUtils.isEmpty(sourceAggrStatis)) {
                continue;
            }
            appletRecordSevenDayDao.deleteBySourceType(sourceTypeEnum.getCode());
            List<AppletRecordSevenDay> serveList = new ArrayList<>();
            BeanUtils.copyListProperties(sourceAggrStatis, serveList,AppletRecordSevenDay.class);
            serveList.forEach(s->s.setSourceType(sourceTypeEnum.getCode()));
            appletRecordSevenDayDao.insert(serveList);
        }
    }

    @Override
    public PageBeanNew getResource4H5(Long wechatUserId, Long officialAccountsId, String keyword, Integer sourceType, Integer currentPage, Integer numPerPage) {
        if (null == sourceType) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少参数");
        }
        //最多查询50个
        Integer maxCount = 50;
        List<AppletRecordDTO> countList;
        Boolean isDefaultList;//默认列表还是关键词实时搜索
        Integer sourceFrom;//来源 育儿/教辅
        if (ResourcePageConstants.ResourceSourceTypeEnum.CHILD.getCode().equals(sourceType)) {
            //育儿宝库
            sourceFrom = ResourcePageConstants.RecommendResourceFromEnum.CHILD_RESOURCE.getCode();
        } else {
            //教辅资料
            sourceFrom = ResourcePageConstants.RecommendResourceFromEnum.TEACHE_RESOURCE.getCode();
        }
        if (StringUtils.isNotEmpty(keyword)) {
            isDefaultList = false;
            //关键词搜索
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("sourceKeywords", Arrays.asList(keyword));
            paramMap.put("offset", 0);
            paramMap.put("limit", maxCount);
            paramMap.put("endDate", DateUtils.formatDate(DateUtils.addDay(DateUtils.getDayEnd(), -1)));
            countList = appletRecordCHMapper.getAggrStatis4H5(paramMap);

        } else {
            isDefaultList = true;
            //无关键词-根据学龄段搜索,优先匹配同年级的资源
            UserGradeLabelIdDTO userGradeLabelIdDTO = readerConsr.getUserGrade(wechatUserId, officialAccountsId);
            //学龄段标签, 年级标签
            Long parentLabel = 1L;
            Long gradeLabelId = null;
            if (null != userGradeLabelIdDTO && null != userGradeLabelIdDTO.getLabelId()) {
                parentLabel = (null == userGradeLabelIdDTO.getParentId() || userGradeLabelIdDTO.getParentId() == 0) ?
                        userGradeLabelIdDTO.getLabelId() : userGradeLabelIdDTO.getParentId();
                gradeLabelId = userGradeLabelIdDTO.getLabelId();
            }
            //年龄段下所有年级
            List<Long> gradeLabelIds = readerConsr.getUserGradeIdByParentId(parentLabel);
            //年级排序,当前年级在前
            List<Long> orderFieldIds = new ArrayList<>();
            if (null != gradeLabelId) {
                orderFieldIds.add(gradeLabelId);
            }
            if (!ListUtils.isEmpty(gradeLabelIds) && null != gradeLabelId && gradeLabelIds.contains(gradeLabelId)) {
                gradeLabelIds.remove(gradeLabelId);
            }
            orderFieldIds.addAll(gradeLabelIds);

            countList = appletRecordSevenDayDao.getResourceByGrade4H5(sourceType, orderFieldIds, maxCount, sourceFrom);
        }
        if (ListUtils.isEmpty(countList)) {
            return new PageBeanNew(currentPage, numPerPage, 0, Lists.newArrayList());
        }
        appletRecordBiz.fillAppletRecord(countList, YesOrNoNumEnum.NO.getValue(), wechatUserId);
        countList = countList.stream().filter(x -> x.getSourceDelete() == 0).collect(Collectors.toList());
        dealServeNamePic(countList, isDefaultList, sourceFrom);
        if (ListUtils.isEmpty(countList)) {
            return new PageBeanNew(currentPage, numPerPage, 0, Lists.newArrayList());
        }
        List<AppletRecordDTO> recordDTOS = countList.stream().skip(currentPage * numPerPage).limit(numPerPage).collect(Collectors.toList());
        fillLabel(recordDTOS);
        PageBeanNew pageBeanNew = new PageBeanNew<>(currentPage, numPerPage, countList.size(), recordDTOS);
        return pageBeanNew;
    }

    /**
     * 处理平台端替换的资源名称封面
     * @author：zhuyajie
     * @date：2021/7/9 17:28
     *  * @param null
     */
    private void dealServeNamePic(List<AppletRecordDTO> recordDTOS, Boolean isDefaultList, Integer sourceFrom) {
        if (ListUtils.isEmpty(recordDTOS)) {
            return;
        }
        if (isDefaultList) {
            //默认列表
            for (AppletRecordDTO appletRecordDTO:recordDTOS){
                AppletAppOrProductDTO appletAppOrProductDTO = appletRecordDTO.getAppletAppOrProductDTO();
                if (!StringUtil.isEmpty(appletRecordDTO.getFromName())) {
                    appletAppOrProductDTO.setServeName(appletRecordDTO.getFromName());
                }
                if (!StringUtil.isEmpty(appletRecordDTO.getServePic())) {
                    appletAppOrProductDTO.setServePic(appletRecordDTO.getServePic());
                }
            }
        } else {
            List<AppletRecordDTO> deleteList = new ArrayList<>();
            //关键词搜索
            List<Long> appIds = recordDTOS.stream().filter(s->s.getRecordType().equals(6)).map(AppletRecordDTO::getFromId).collect(Collectors.toList());
            List<Long> productIds = recordDTOS.stream().filter(s->s.getRecordType().equals(5)).map(AppletRecordDTO::getFromId).collect(Collectors.toList());
            Map<Long,ResourceRecomManage> appMap = resourcePageBiz.mapResourceRecomManage(appIds, "APP", sourceFrom);
            Map<Long,ResourceRecomManage> productMap = resourcePageBiz.mapResourceRecomManage(productIds, "PRODUCT", sourceFrom);
            if (!MapUtils.isEmpty(appMap) || !MapUtils.isEmpty(productMap)) {
                for (AppletRecordDTO appletRecordDTO:recordDTOS){
                    Long fromId = appletRecordDTO.getFromId();
                    AppletAppOrProductDTO appletAppOrProductDTO = appletRecordDTO.getAppletAppOrProductDTO();
                    if (appletRecordDTO.getRecordType().equals(6) && !MapUtils.isEmpty(appMap) && appMap.containsKey(fromId)) {
                        ResourceRecomManage resourceRecomManage = appMap.get(fromId);
                        replaceServe(resourceRecomManage, appletAppOrProductDTO, deleteList, appletRecordDTO);
                    } else if (appletRecordDTO.getRecordType().equals(5) && !MapUtils.isEmpty(productMap) && productMap.containsKey(fromId)) {
                        ResourceRecomManage resourceRecomManage = productMap.get(fromId);
                        replaceServe(resourceRecomManage, appletAppOrProductDTO, deleteList, appletRecordDTO);
                    }
                }
            }
            if (!ListUtils.isEmpty(deleteList)) {
                recordDTOS.removeAll(deleteList);
            }
        }
    }

    private void replaceServe(ResourceRecomManage resourceRecomManage, AppletAppOrProductDTO appletAppOrProductDTO,
                              List<AppletRecordDTO> deleteList, AppletRecordDTO appletRecordDTO) {
        if (null == resourceRecomManage) {
            return;
        }
        if (resourceRecomManage.getShowState() == 0) {
            deleteList.add(appletRecordDTO);
        } else {
            if (!StringUtil.isEmpty(resourceRecomManage.getCoverImg())) {
                appletAppOrProductDTO.setServePic(resourceRecomManage.getCoverImg());
            }
            if (!StringUtil.isEmpty(resourceRecomManage.getServeName())) {
                appletAppOrProductDTO.setServeName(resourceRecomManage.getServeName());
                appletRecordDTO.setFromName(resourceRecomManage.getServeName());
            }
        }
    }

    @Override
    public PageBeanNew getResource4H54Pcloud(Integer currentPage, Integer numPerPage, String query, Integer sourceFrom) {
        Map<String,Object> map = new HashMap<>();
        map.put("sourceTypeList", ResourcePageConstants.RecommendResourceFromEnum.getSourceTypesByCode(sourceFrom));
        map.put("sourceFrom", sourceFrom);
        map.put("query", query);
        PageBeanNew<AppletRecordDTO> pageBeanNew = appletRecordSevenDayDao.listPageNew(new PageParam(currentPage,numPerPage),map,"getList4Pcloud");
        fillResource4Pcloud(pageBeanNew.getRecordList());
        return pageBeanNew;
    }

    /**
     * 填充应用作品信息
     * @author：zhuyajie
     * @date：2021/7/9 17:09
     *  * @param null
     */
    private void fillResource4Pcloud(List<AppletRecordDTO> recordList) {
        if (ListUtils.isEmpty(recordList)) {
            return;
        }
        List<Long> productIds = recordList.stream().filter(appletRecordDTO -> AppletRecordTypeEnum.PRODUCT.value.equals(appletRecordDTO.getRecordType())).map(AppletRecordDTO::getFromId).distinct().collect(Collectors.toList());
        List<Long> appIds = recordList.stream().filter(appletRecordDTO -> AppletRecordTypeEnum.APP.value.equals(appletRecordDTO.getRecordType())).map(AppletRecordDTO::getFromId).distinct().collect(Collectors.toList());
        //应用作品创建人
        Map<Long, AppDto> appDtoMap = appConsr.mapBaseByIds(appIds);
        List<Long> adviserIds = MapUtils.isEmpty(appDtoMap)?new ArrayList<>():appDtoMap.values().stream().map(AppDto::getCreatedUser).distinct().collect(Collectors.toList());
        Map<Long, String> adviserMap = adviserConsr.getNames(adviserIds);
        Map<Long, ProductDto> productDtoMap = productConsr.getProBasesByIds(productIds);
        List<Long> merchantIds = MapUtils.isEmpty(productDtoMap)?new ArrayList<>():productDtoMap.values().stream().map(ProductDto::getCreatedUser).distinct().collect(Collectors.toList());
        Map<Long, String> merchantMap = merchantConsr.getNamesByIdList(merchantIds);
        for (AppletRecordDTO recordDTO : recordList) {
            Long fromId = recordDTO.getFromId();
            if (AppletRecordTypeEnum.APP.value.equals(recordDTO.getRecordType())
                    && !MapUtils.isEmpty(appDtoMap) && appDtoMap.containsKey(fromId)) {
                recordDTO.setServeType("APP");
                AppDto appDto = appDtoMap.get(fromId);
                //平台端没给封面，取原有封面
                if (StringUtil.isEmpty(recordDTO.getServePic())) {
                    recordDTO.setServePic(appDto.getSquareImg());
                }
                recordDTO.setProLabelName(appDto.getProLabelName());
                recordDTO.setDepLabelName(appDto.getDepLabelName());
                recordDTO.setPurLabelName(appDto.getPurLabelName());
                recordDTO.setServeCreateUserId(appDto.getCreatedUser());
                //应用创建人名称
                if (!MapUtils.isEmpty(adviserMap) && adviserMap.containsKey(appDto.getCreatedUser())) {
                    recordDTO.setServeCreateUserName(adviserMap.get(appDto.getCreatedUser()));
                }
            } else if (AppletRecordTypeEnum.PRODUCT.value.equals(recordDTO.getRecordType())
                    && !MapUtils.isEmpty(productDtoMap) && productDtoMap.containsKey(fromId)) {
                recordDTO.setServeType("PRODUCT");
                ProductDto productDto = productDtoMap.get(fromId);
                if (StringUtil.isEmpty(recordDTO.getServePic())) {
                    recordDTO.setServePic(productDto.getCoverImg());
                }
                List<ProductLabelDto> labelDtos = productDto.getProductLabels();
                if (!ListUtils.isEmpty(labelDtos)) {
                    recordDTO.setProLabelName(labelDtos.get(0).getProLabelName());
                    recordDTO.setDepLabelName(labelDtos.get(0).getDepLabelName());
                    recordDTO.setPurLabelName(labelDtos.get(0).getPurLabelName());
                }
                recordDTO.setServeCreateUserId(productDto.getCreatedUser());
                if (!MapUtils.isEmpty(merchantMap) && merchantMap.containsKey(productDto.getCreatedUser())) {
                    recordDTO.setServeCreateUserName(merchantMap.get(productDto.getCreatedUser()));
                }
            }
        }
    }


    private void fillLabel(List<AppletRecordDTO> recordList) {
        if (ListUtils.isEmpty(recordList)) {
            return;
        }
        List<Long> labelIds = new ArrayList<>();
        for (AppletRecordDTO appletRecordDTO : recordList) {
            if (null != appletRecordDTO.getProLabelId()) {
                labelIds.add(appletRecordDTO.getProLabelId());
            }
            if (null != appletRecordDTO.getDepLabelId()) {
                labelIds.add(appletRecordDTO.getDepLabelId());
            }
            if (null != appletRecordDTO.getPurLabelId()) {
                labelIds.add(appletRecordDTO.getPurLabelId());
            }
        }
        if (!ListUtils.isEmpty(labelIds)) {
            Map<Long, String> labelMap = labelConsr.getLabelName(labelIds);
            if (!MapUtils.isEmpty(labelMap)) {
                for(AppletRecordDTO appletRecordDTO : recordList){
                    if (null != appletRecordDTO.getProLabelId() && labelMap.containsKey(appletRecordDTO.getProLabelId())) {
                        appletRecordDTO.setProLabelName(labelMap.get(appletRecordDTO.getProLabelId()));
                    }
                    if (null != appletRecordDTO.getDepLabelId() && labelMap.containsKey(appletRecordDTO.getDepLabelId())) {
                        appletRecordDTO.setDepLabelName(labelMap.get(appletRecordDTO.getDepLabelId()));
                    }
                    if (null != appletRecordDTO.getPurLabelId() && labelMap.containsKey(appletRecordDTO.getPurLabelId())) {
                        appletRecordDTO.setPurLabelName(labelMap.get(appletRecordDTO.getPurLabelId()));
                    }
                }
            }
        }
    }
}
