package com.ruoyi.wx.web.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.recovery.domain.RecContent;
import com.ruoyi.framework.recovery.domain.RecSaleInfo;
import com.ruoyi.framework.recovery.domain.RecStores;
import com.ruoyi.framework.recovery.mapper.RecSaleInfoMapper;
import com.ruoyi.framework.recovery.service.IRecContentService;
import com.ruoyi.framework.recovery.service.IRecSaleInfoService;
import com.ruoyi.wx.web.domain.vo.RecStoresVo;
import com.ruoyi.wx.web.domain.vo.SaleInfoVo;
import com.ruoyi.wx.web.mapper.WxHomeMapper;
import com.ruoyi.wx.web.service.IWxHomeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;

@Service
public class WxHomeServiceImpl implements IWxHomeService {
    @Autowired
    private IRecSaleInfoService recSaleInfoService;

    @Autowired
    private IRecContentService recContentService;

    @Autowired
    private RecSaleInfoMapper saleInfoMapper;

    @Autowired
    private WxHomeMapper wxHomeMapper;

    @Autowired
    private RedisCache redisCache;

    private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);

    private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    @SuppressWarnings("unused")
    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(9, 9, 1000, TimeUnit.MILLISECONDS,
            WORK_QUEUE, HANDLER);

    @Override
    public AjaxResult getIndex() throws ExecutionException, InterruptedException {
        AjaxResult result = new AjaxResult();
        // 发售信息
        Callable<List<RecSaleInfo>> saleInfoCallable = () -> recSaleInfoService.selectRecSaleInfoList(new RecSaleInfo());
        // 评论
        Callable<List<RecContent>> contentCallable = () -> recContentService.selectRecContentList(new RecContent());

        FutureTask<List<RecSaleInfo>> saleInfoTask = new FutureTask<>(saleInfoCallable);
        FutureTask<List<RecContent>> contentTask = new FutureTask<>(contentCallable);

        executorService.submit(saleInfoTask);
        executorService.submit(contentTask);
        List<RecContent> contents = contentTask.get();
        for (RecContent content : contents) {
            Object value = redisCache.getCacheMapValue(Constants.THUMBS_UP + content.getId(), "like");
            if (value != null) {
                content.setThumbs(Long.parseLong(value.toString()));
            }
            redisCache.setCacheMapValue(Constants.THUMBS_UP + content.getId(), "like", content.getThumbs());
        }
        result.put("saleInfo", saleInfoTask.get());
        result.put("content", contents);
        return result;
    }

    @Override
    public List<SaleInfoVo> getSaleInfo(Long id) {
        RecSaleInfo saleInfo = saleInfoMapper.selectRecSaleInfoById(id);
        // 获取到商品id，并将商品id转换为Integer到数据库中查询
        List<String> ids = Arrays.asList(saleInfo.getStoreId().split(","));
        // 根据商品获取所有门店信息
        List<RecStores> stores = wxHomeMapper.selectRecStoresByCommondityIds(ids);
        Map<String, List<RecStores>> cityMap = stores.stream().filter(vo -> StringUtils.isNotEmpty(vo.getCity())).collect(Collectors.groupingBy(RecStores::getCity));
        List<SaleInfoVo> result = new ArrayList<>();
        SaleInfoVo saleInfoVo = new SaleInfoVo();
        List<RecStoresVo> cityList = new ArrayList<>();
        BeanUtils.copyProperties(saleInfo, saleInfoVo);
        // 插入门店详情
        cityMap.forEach((key, value) -> {
            RecStoresVo storesVo = new RecStoresVo();
            storesVo.setCity(key);
            storesVo.setStores(value);
            cityList.add(storesVo);
        });
        saleInfoVo.setStores(cityList);
        result.add(saleInfoVo);
        return result;
    }

    @Override
    public List<SaleInfoVo> getSaleCalendar() {
        List<RecSaleInfo> recSaleInfos = saleInfoMapper.selectRecSaleInfoList(new RecSaleInfo());
        List<SaleInfoVo> result = new ArrayList<>();
        for (RecSaleInfo info : recSaleInfos) {
            SaleInfoVo vo = new SaleInfoVo();
            BeanUtils.copyProperties(info, vo);
            result.add(vo);
        }
        return result;
    }

    @Override
    public Long thumbs(Long id) {
        redisCache.increment(Constants.THUMBS_UP + id, "like");
        return redisCache.getCacheMapValue(Constants.THUMBS_UP + id, "like");
    }
}
