package com.mortals.xhx.module.baseset.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.baseset.dao.BasesetDao;
import com.mortals.xhx.module.baseset.model.BasesetEntity;
import com.mortals.xhx.module.baseset.service.BasesetService;
import com.mortals.xhx.module.hotword.model.HotwordEntity;
import com.mortals.xhx.module.hotword.model.HotwordQuery;
import com.mortals.xhx.module.hotword.service.HotwordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * BasesetService
 * 填单服务基础设置 service实现
 *
 * @author zxfei
 * @date 2022-12-08
 */
@Service("basesetService")
public class BasesetServiceImpl extends AbstractCRUDServiceImpl<BasesetDao, BasesetEntity, Long> implements BasesetService {

    @Autowired
    private HotwordService hotwordService;


    @Override
    protected void findAfter(BasesetEntity params, PageInfo pageInfo, Context context, List<BasesetEntity> list) throws AppException {
        list.forEach(item -> {
            HotwordQuery hotwordQuery = new HotwordQuery();
            hotwordQuery.siteId(item.getSiteId());
            List<OrderCol> orderColList = new ArrayList<>();
            orderColList.add(new OrderCol("wordsSource", OrderCol.ASCENDING));
            orderColList.add(new OrderCol("searchCount", OrderCol.DESCENDING));
            hotwordQuery.setOrderColList(orderColList);
            List<HotwordEntity> hotwordEntities = hotwordService.find(hotwordQuery);
            item.setHotwordList(hotwordEntities);
        });
        super.findAfter(params, pageInfo, context, list);
    }

    @Override
    protected void saveAfter(BasesetEntity entity, Context context) throws AppException {
        updateAndSaveHotWords(entity, context);
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(BasesetEntity entity, Context context) throws AppException {
        updateAndSaveHotWords(entity, context);
        super.updateAfter(entity, context);
    }

    private void updateAndSaveHotWords(BasesetEntity entity, Context context) {
        if(!ObjectUtils.isEmpty(entity.getHotwordList())){
            //更新新增热门词汇，差集删除不在列表中的
            List<HotwordEntity> allHotWords = hotwordService.find(new HotwordQuery().siteId(entity.getSiteId()));
            //求差集 删除
            List<HotwordEntity> delList = this.subList(allHotWords, entity.getHotwordList());
            if(!ObjectUtils.isEmpty(delList)){
                hotwordService.removeList(delList, context);
            }
            entity.getHotwordList().forEach(item -> {
                HotwordEntity hotwordEntity = hotwordService.selectOne(new HotwordQuery().siteId(item.getSiteId()).hotwords(item.getHotwords()));
                if (!ObjectUtils.isEmpty(hotwordEntity)) {
                    item.setId(hotwordEntity.getId());
                    item.setSearchCount(hotwordEntity.getSearchCount());
                    item.setUpdateTime(new Date());
                    hotwordService.update(item, context);
                }else{
                    item.setCreateUserId(this.getContextUserId(context));
                    item.setCreateTime(new Date());
                    hotwordService.save(item, context);
                }
            });
        }
    }



    public List<HotwordEntity> subList(List<HotwordEntity> firstList, List<HotwordEntity> secondList) {
        Set<String> secondSet = secondList.parallelStream().map(e -> e.getHotwords()).collect(Collectors.toSet());
        return firstList.parallelStream().filter(item -> !secondSet.contains(item.getHotwords())).collect(Collectors.toList());
    }
}