package com.slodon.b2b2c.model.goods;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.goods.GoodsSearchWordsReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsSearchWordsWriteMapper;
import com.slodon.b2b2c.goods.example.GoodsSearchWordsExample;
import com.slodon.b2b2c.goods.pojo.GoodsSearchWords;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 高频搜索词表model
 */
@Component
@Slf4j
public class GoodsSearchWordsModel {
    @Resource
    private GoodsSearchWordsReadMapper goodsSearchWordsReadMapper;

    @Resource
    private GoodsSearchWordsWriteMapper goodsSearchWordsWriteMapper;

    /**
     * 新增高频搜索词表
     *
     * @param goodsSearchWords
     * @return
     */
    public Integer saveGoodsSearchWords(GoodsSearchWords goodsSearchWords) {
        int count = goodsSearchWordsWriteMapper.insert(goodsSearchWords);
        if (count == 0) {
            throw new MallException("添加高频搜索词表失败，请重试");
        }
        return count;
    }

    /**
     * 根据wordsId删除高频搜索词表
     *
     * @param wordsId wordsId
     * @return
     */
    public Integer deleteGoodsSearchWords(Long wordsId) {
        if (StringUtils.isEmpty(wordsId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = goodsSearchWordsWriteMapper.deleteByPrimaryKey(wordsId);
        if (count == 0) {
            log.error("根据wordsId：" + wordsId + "删除高频搜索词表失败");
            throw new MallException("删除高频搜索词表失败,请重试");
        }
        return count;
    }

    /**
     * 根据wordsId更新高频搜索词表
     *
     * @param goodsSearchWords
     * @return
     */
    public Integer updateGoodsSearchWords(GoodsSearchWords goodsSearchWords) {
        if (StringUtils.isEmpty(goodsSearchWords.getWordsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsSearchWordsWriteMapper.updateByPrimaryKeySelective(goodsSearchWords);
        if (count == 0) {
            log.error("根据wordsId：" + goodsSearchWords.getWordsId() + "更新高频搜索词表失败");
            throw new MallException("更新高频搜索词表失败,请重试");
        }
        return count;
    }

    /**
     * 根据wordsId获取高频搜索词表详情
     *
     * @param wordsId wordsId
     * @return
     */
    public GoodsSearchWords getGoodsSearchWordsByWordsId(Long wordsId) {
        return goodsSearchWordsReadMapper.getByPrimaryKey(wordsId);
    }

    /**
     * 根据条件获取高频搜索词表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsSearchWords> getGoodsSearchWordsList(GoodsSearchWordsExample example, PagerInfo pager) {
        List<GoodsSearchWords> goodsSearchWordsList;
        if (pager != null) {
            pager.setRowsCount(goodsSearchWordsReadMapper.countByExample(example));
            goodsSearchWordsList = goodsSearchWordsReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsSearchWordsList = goodsSearchWordsReadMapper.listByExample(example);
        }
        return goodsSearchWordsList;
    }

    /**
     * 记录搜索词
     * @param searchWords 搜索词
     * @param goodsNum 搜索词查询到的商品数
     */
    @Async
    public void recordSearchWords(String searchWords, String webSite, Integer goodsNum) {
        //查询是否已有记录
        GoodsSearchWordsExample example = new GoodsSearchWordsExample();
        example.setWordsContent(searchWords);
        example.setWebSite(webSite);
        List<GoodsSearchWords> list = goodsSearchWordsReadMapper.listByExample(example);
        if (CollectionUtils.isEmpty(list)){
            //没有记录，新增一条
            GoodsSearchWords goodsSearchWords = new GoodsSearchWords();
            goodsSearchWords.setWebSite(webSite);
            goodsSearchWords.setWordsContent(searchWords);
            goodsSearchWords.setFullPy(PinyinUtil.getPinyin(searchWords,""));
            goodsSearchWords.setSimplePy(goodsSearchWords.getFullPy().substring(0,1));
            goodsSearchWords.setResource(2);
            goodsSearchWords.setCreateTime(new Date());
            goodsSearchWords.setSearchGoodsNum(goodsNum);
            goodsSearchWords.setSearchFrequency(1);
            goodsSearchWordsWriteMapper.insert(goodsSearchWords);
            return;
        }
        //有记录，更新
        GoodsSearchWords goodsSearchWords = list.get(0);
        goodsSearchWords.setSearchGoodsNum(goodsNum);
        goodsSearchWords.setSearchFrequency(goodsSearchWords.getSearchFrequency() + 1);
        goodsSearchWordsWriteMapper.updateByPrimaryKeySelective(goodsSearchWords);
    }

    /**
     * 搜索词排行
     * @param startTime
     * @param endTime
     * @param type
     * @param webSite
     * @param pager
     * @return
     */
    public List<String> getSearchWordsRank(Date startTime, Date endTime, String type, String webSite, PagerInfo pager) {
        return goodsSearchWordsReadMapper.getSearchWordsRank(startTime, endTime, type, webSite, pager);
    }
}