package com.tmerclub.cloud.coupon.feign;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.coupon.constant.CouponType;
import com.tmerclub.cloud.api.coupon.constant.PutOnStatus;
import com.tmerclub.cloud.api.coupon.dto.BindCouponDTO;
import com.tmerclub.cloud.api.coupon.dto.ReceiveCouponDTO;
import com.tmerclub.cloud.api.coupon.feign.CouponFeignClient;
import com.tmerclub.cloud.api.coupon.vo.CouponDataVO;
import com.tmerclub.cloud.api.coupon.vo.CouponUserCountDataVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.product.vo.SpuCouponAppVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.coupon.constant.CouponStatus;
import com.tmerclub.cloud.coupon.constant.SuitableProdType;
import com.tmerclub.cloud.coupon.dto.CouponDTO;
import com.tmerclub.cloud.coupon.manager.CouponStockManager;
import com.tmerclub.cloud.coupon.model.Coupon;
import com.tmerclub.cloud.coupon.service.CouponService;
import com.tmerclub.cloud.coupon.service.CouponUserService;
import com.tmerclub.cloud.coupon.vo.CouponAppVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author tmerclub
 * @date 2020/12/17
 */
@DubboService
public class CouponFeignController implements CouponFeignClient {

    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponUserService couponUserService;
    @Autowired
    private CouponStockManager couponStockManager;

    @Override
    public ServerResponseEntity<List<CouponDataVO>> getCouponListByCouponIds(List<Long> couponIds) {
        List<Coupon> coupons = couponService.getCouponListByCouponIds(couponIds);
        List<CouponDataVO> couponDataList = BeanUtil.mapAsList(coupons, CouponDataVO.class);
        return ServerResponseEntity.success(couponDataList);
    }

    @Override
    public ServerResponseEntity<List<CouponDataVO>> getCouponListByCouponIdsAndPutOnStatus(List<Long> couponIds, Integer putOnStatus) {
        List<Coupon> coupons = couponService.getCouponListByCouponIdsAndPutOnStatus(couponIds, putOnStatus);
        return ServerResponseEntity.success(BeanUtil.mapAsList(coupons, CouponDataVO.class));
    }

    @Override
    public ServerResponseEntity<Void> batchBindCouponByCouponIds(BindCouponDTO bindCouponDTO) {
        couponService.batchBindCouponByCouponIds(bindCouponDTO.getCouponIds(), bindCouponDTO.getUserId());
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Long> countCanUseCoupon() {
        return ServerResponseEntity.success(couponUserService.countCanUseCoupon(AuthUserContext.get().getUserId()));
    }

    @Override
    public ServerResponseEntity<List<SpuCouponAppVO>> couponOfSpuDetail(Long shopId, Long spuId) {
        // 获取已投放优惠券
        List<CouponAppVO> shopCoupons = couponService.getShopCouponList(shopId);
        List<CouponAppVO> coupons = couponService.getShopCouponList(Constant.PLATFORM_SHOP_ID);
        coupons.addAll(shopCoupons);
        if (CollUtil.isEmpty(coupons)) {
            return ServerResponseEntity.success(new ArrayList<>(0));
        }
        coupons.sort(Comparator.comparingLong(CouponAppVO::getCashCondition));
        List<CouponAppVO> couponList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (CouponAppVO coupon : coupons) {
            if (Objects.equals(coupon.getSuitableProdType(), SuitableProdType.ALL_SPU.value()) || (coupon.getSpuIds().contains(spuId))) {
                if (Objects.equals(coupon.getStatus(), CouponStatus.NO_OVERDUE.value()) && Objects.equals(coupon.getPutonStatus(), PutOnStatus.PUT_ON.value())) {
                    // 正在投放的优惠券的库存
                    Integer stock = couponStockManager.getStockByCouponId(coupon.getCouponId());
                    coupon.setStocks(stock);
                }
                couponList.add(coupon);
            }
            if (couponList.size() >= Constant.SIZE_OF_THREE) {
                break;
            }
        }
        return ServerResponseEntity.success(BeanUtil.mapAsList(couponList, SpuCouponAppVO.class));
    }

    @Override
    public ServerResponseEntity<CouponUserCountDataVO> countCouponUserByUserId(Long userId, Long shopId) {
        CouponUserCountDataVO couponUserCountDataVO = couponUserService.countCouponUserByUserId(userId, shopId);
        return ServerResponseEntity.success(couponUserCountDataVO);
    }

    @Override
    public ServerResponseEntity<List<Long>> countMemberCouponByParam(Long shopId, Date startTime, Date endTime) {
        return ServerResponseEntity.success(couponUserService.countMemberCouponByParam(shopId, startTime, endTime));
    }

    @Override
    public ServerResponseEntity<Void> handleSpuOffline(List<Long> spuIds, List<Long> shopIds) {
        couponService.handleSpuOffline(spuIds, shopIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> handleShopOffline(List<Long> shopIds) {
        couponService.handleShopOffline(shopIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<Long>> userReceiveCouponCount(ReceiveCouponDTO receiveCouponDTO) {
        if (CollUtil.isEmpty(receiveCouponDTO.getUserIds())) {
            return ServerResponseEntity.success(new ArrayList<>());
        }
        List<Long> receiveCouponList = couponUserService.userReceiveCouponCount(receiveCouponDTO);
        return ServerResponseEntity.success(receiveCouponList);
    }

    @Override
    public ServerResponseEntity<Integer> getStockByCouponId(Long couponId) {
        return ServerResponseEntity.success(couponStockManager.getStockByCouponId(couponId));
    }

    @Override
    public void sendUserCoupon(Long shopId, Long userId, Long reduceAmount) {
        // 创建优惠卷
        CouponDTO couponDTO = new CouponDTO();
        couponDTO.setShopId(shopId);
        couponDTO.setCouponName("球杆置换代金卷");
        couponDTO.setSuitableProdType(SuitableProdType.ALL_SPU.value());
        couponDTO.setLimitNum(1);
        couponDTO.setCashCondition(reduceAmount + 1);
        couponDTO.setCouponType(CouponType.C2M.value());
        couponDTO.setReduceAmount(reduceAmount);
        couponDTO.setPutonStatus(PutOnStatus.PUT_ON.value());
        couponDTO.setGetWay(1);
        couponDTO.setValidTimeType(2);
        couponDTO.setAfterReceiveDays(0);
        couponDTO.setValidDays(3652);
        couponDTO.setTotalStock(1);
        couponDTO.setStocks(1);
        Long save = couponService.save(couponDTO);
        // 绑定优惠卷
        couponService.batchBindCouponByCouponIds(Collections.singletonList(save), userId);
    }
}
