package com.liquidnet.service.platform.service.impl.candy;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.service.base.OrderCloseMapping;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.candy.dto.CandyCouponRuleDto;
import com.liquidnet.service.candy.dto.CandyUserCouponBasicDto;
import com.liquidnet.service.candy.entity.CandyUserCoupon;
import com.liquidnet.service.candy.mapper.CandyCouponRuleMapper;
import com.liquidnet.service.candy.mapper.CandyUserCouponMapper;
import com.liquidnet.service.platform.utils.QueueUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PlatformCandyUserCouponService extends ServiceImpl<CandyUserCouponMapper, CandyUserCoupon> {
    @Autowired
    private CandyUserCouponMapper candyUserCouponMapper;
    @Autowired
    private CandyCouponRuleMapper candyCouponRuleMapper;
    @Autowired
    private PlatformCandyUserCouponService platformCandyUserCouponService;
    @Autowired
    private QueueUtils queueUtils;

    /**
     * @param uCouponUidList 内容 uCouponId,uid List
     * @param uCouponIdList  内容 uCouponId List
     * @return
     */
    public Boolean useBackCoupon(ArrayList<String> uCouponUidList, ArrayList<String> uCouponIdList) {
        //修改数据库
        CandyUserCoupon userCoupon = CandyUserCoupon.getNew();
        userCoupon.setState(1);
        platformCandyUserCouponService.update(userCoupon, new UpdateWrapper<CandyUserCoupon>().in("ucoupon_id", uCouponIdList));
        //sendRedis
        int forSize = 500;
        int forCount = uCouponUidList.size() % forSize == 0 ? uCouponUidList.size() / forSize : (uCouponUidList.size() / forSize) + 1;
        for (int i = 0; i < forCount; i++) {
            LinkedList<String> mqList = new LinkedList<>();
            for (int y = 0; y < forSize; y++) {
                try {
                    String t = uCouponUidList.get(i * 500 + y);
                    mqList.add(t);
                } catch (Exception e) {
                    break;
                }
            }
            queueUtils.sendMsgByRedis(MQConst.CandyQueue.COUPON_ORDER_BACK.getKey(), OrderCloseMapping.get(mqList));
        }
        return true;
    }

    public List<CandyUserCouponBasicDto> ucouponByUidProcessing(String uid) {
        List<CandyUserCouponBasicDto> basicDtoList = candyUserCouponMapper.selectMultiForUserCouponBasicDto(uid);
        if (!CollectionUtils.isEmpty(basicDtoList)) {
            List<String> couponIdList = basicDtoList.parallelStream().map(CandyUserCouponBasicDto::getCouponId).collect(Collectors.toList());

            List<CandyCouponRuleDto> couponRuleDtoList = candyCouponRuleMapper.selectForCouponRuleDto(couponIdList);

            Map<String, List<CandyCouponRuleDto>> couponRuleDtoListMap = couponRuleDtoList.parallelStream().collect(Collectors.groupingBy(CandyCouponRuleDto::getCouponId));

//            Map<String, List<CandyUserCouponBasicDto>> basicDtoListMap = basicDtoList.parallelStream().collect(Collectors.groupingBy(CandyUserCouponBasicDto::getCouponId));

            basicDtoList.parallelStream().forEach(dto -> {
                dto.setUseRules(couponRuleDtoListMap.get(dto.getCouponId()));
            });
        }

        return null;
    }
}
