package shop.alien.job.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.store.*;
import shop.alien.entity.store.vo.LifeUserOrderVo;
import shop.alien.job.feign.AlienStoreFeign;
import shop.alien.mapper.*;
import shop.alien.util.common.UniqueRandomNumGenerator;
import shop.alien.util.common.constant.DiscountCouponEnum;
import shop.alien.util.common.constant.OrderStatusEnum;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 团购上下架数据统计
 *
 * @author jyc
 * @date 2023/12/20 14:20
 * @see LifeUserOrder
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class LifeGroupBuyCountJob {

    private final LifeGroupBuyCountMapper lifeGroupBuyCountMapper;
    private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;

    /**
     * 待支付订单超时处理
     */
    @XxlJob("countSxjGroupBuy")
    @Transactional(rollbackFor = Exception.class)
    public void autoCancelUnpaidOrders() {
        List<LifeGroupBuyCount> list = new ArrayList<>();
        //上架数据
        LambdaQueryWrapper<LifeGroupBuyMain> sjQuery = new LambdaQueryWrapper<>();
        sjQuery.eq(LifeGroupBuyMain::getStatus, "5").eq(LifeGroupBuyMain::getDeleteFlag, 0).eq(LifeGroupBuyMain::getGroupType, 1);
        Map<String, List<LifeGroupBuyMain>> sjCollect = lifeGroupBuyMainMapper.selectList(sjQuery).stream().collect(Collectors.groupingBy(LifeGroupBuyMain::getStoreId));
        for (String s : sjCollect.keySet()) {
            LifeGroupBuyCount sj = new LifeGroupBuyCount();
            sj.setStoreId(Integer.valueOf(sjCollect.get(s).get(0).getStoreId()));
            sj.setCountType("2");
            sj.setCountNum(sjCollect.get(s).size());
            sj.setCountDate(new Date());
            list.add(sj);
        }

        //下架数据
        LambdaQueryWrapper<LifeGroupBuyMain> xjQuery = new LambdaQueryWrapper<>();
        xjQuery.in(LifeGroupBuyMain::getStatus, "7", "8").eq(LifeGroupBuyMain::getDeleteFlag, 0).eq(LifeGroupBuyMain::getGroupType, 1);
        Map<String, List<LifeGroupBuyMain>> xjCollect = lifeGroupBuyMainMapper.selectList(xjQuery).stream().collect(Collectors.groupingBy(LifeGroupBuyMain::getStoreId));
        for (String s : xjCollect.keySet()) {
            LifeGroupBuyCount xj = new LifeGroupBuyCount();
            xj.setStoreId(Integer.valueOf(xjCollect.get(s).get(0).getStoreId()));
            xj.setCountType("3");
            xj.setCountNum(xjCollect.get(s).size());
            xj.setCountDate(new Date());
            list.add(xj);
        }
        if (ObjectUtils.isNotEmpty(list)) {
            lifeGroupBuyCountMapper.insertList(list);
        }
    }
}

