package com.yami.shop.coupon.api.listener;

import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.CouponOrderDto;
import com.yami.shop.bean.app.dto.ShopCartOrderDto;
import com.yami.shop.bean.event.SubmitOrderEvent;
import com.yami.shop.bean.order.SubmitOrderOrder;
import com.yami.shop.coupon.comment.dao.CouponUserMapper;
import com.yami.shop.coupon.comment.model.CouponUseRecord;
import com.yami.shop.coupon.comment.service.CouponUseRecordService;
import com.yami.shop.security.api.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("couponSubmitOrderListener")
@AllArgsConstructor
public class SubmitOrderListener {

    private final CouponUserMapper couponUserMapper;
    private final CouponUseRecordService couponUseRecordService;

    /**
     * 修改优惠券为已使用状态
     */
    @EventListener(SubmitOrderEvent.class)
    @Order(SubmitOrderOrder.COUPON)
    public void couponSubmitOrderListener(SubmitOrderEvent event) {

        //待改状态的优惠列表
        List<Long> userCouponIds = Lists.newArrayList();

        List<CouponUseRecord> couponUseRecordList = Lists.newArrayList();
        List<ShopCartOrderDto> shopCartOrders = event.getMergerOrder().getShopCartOrders();


        if(CollectionUtils.isEmpty(shopCartOrders)){
            return ;
        }
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            List<CouponOrderDto> shopCartOrderCoupons = shopCartOrder.getCoupons();

            if (CollectionUtils.isEmpty(shopCartOrderCoupons)) {
                continue;
            }
            // 聚合店铺优惠券
            addCouponUseRecordList(shopCartOrderCoupons, shopCartOrder.getOrderNumber(), couponUseRecordList, userCouponIds);
        }
        // 聚合平台优惠券
        addCouponUseRecordList(event.getMergerOrder().getCoupons(), "all", couponUseRecordList, userCouponIds);


        if (CollectionUtils.isNotEmpty(userCouponIds)) {
            // 批量更新用户优惠券使用状态
            couponUserMapper.batchUpdateUserCouponStatus(2, userCouponIds);
        }
        if (CollectionUtils.isNotEmpty(couponUseRecordList)) {
            // 批量插用户优惠券使用记录
            couponUseRecordService.addCouponUseRecode(couponUseRecordList);
        }

    }

    private void addCouponUseRecordList(List<CouponOrderDto> shopCartOrderCoupons, String orderNumber,List<CouponUseRecord> couponUseRecordList, List<Long> userCouponIds){
        if (CollectionUtils.isEmpty(shopCartOrderCoupons)) {
            return;
        }

        for (CouponOrderDto couponOrderDto : shopCartOrderCoupons) {
            if (couponOrderDto.isChoose() && couponOrderDto.isCanUse()) {
                CouponUseRecord couponUseRecord = new CouponUseRecord();
                couponUseRecord.setAmount(couponOrderDto.getReduceAmount());
                // 获取用户优惠券id
                couponUseRecord.setCouponUserId(couponOrderDto.getCouponUserId());
                couponUseRecord.setUserId(SecurityUtils.getUser().getUserId());
                couponUseRecord.setOrderNumber(orderNumber);
                couponUseRecord.setUseTime(new Date());
                //优惠券记录为冻结状态
                couponUseRecord.setStatus(1);
                couponUseRecordList.add(couponUseRecord);
                userCouponIds.add(couponOrderDto.getCouponUserId());
            }
        }
    }

}
