package shop.alien.job.store;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xxl.job.core.context.XxlJobHelper;
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 shop.alien.entity.store.LawyerConsultationOrder;
import shop.alien.entity.store.LifeUserOrder;
import shop.alien.mapper.LawyerConsultationOrderMapper;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

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

@Slf4j
@Component
@RequiredArgsConstructor
public class LawyerOrderJob {

    private final LawyerConsultationOrderMapper lawyerConsultationOrderMapper;

    /**
     * 接单订单完成处理
     */
    @XxlJob("acceptOrderStatus")
    @Transactional(rollbackFor = Exception.class)
    public void acceptOrderStatus() {
        String param = XxlJobHelper.getJobParam();
        if (StringUtils.isNotEmpty(param)) {
            log.info("开始执行测试待接单订单超时处理任务");
            LambdaQueryWrapper<LawyerConsultationOrder> lambdaQueryWrapper = new LambdaQueryWrapper<LawyerConsultationOrder>()
                    .eq(LawyerConsultationOrder::getOrderStatus, 2)
                    .eq(LawyerConsultationOrder::getDeleteFlag, 0);
            List<LawyerConsultationOrder> lawyerConsultationOrders = lawyerConsultationOrderMapper.selectList(lambdaQueryWrapper);
            List<Integer> collect = lawyerConsultationOrders.stream().map(LawyerConsultationOrder::getId).collect(Collectors.toList());
            if (ObjectUtils.isNotEmpty( collect) && !collect.isEmpty()) {
                LambdaUpdateWrapper<LawyerConsultationOrder> lawyerConsultationOrderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lawyerConsultationOrderLambdaUpdateWrapper.set(LawyerConsultationOrder::getOrderStatus, 3)
                        .in(LawyerConsultationOrder::getId, collect);
                lawyerConsultationOrderMapper.update(null, lawyerConsultationOrderLambdaUpdateWrapper);
                log.info("待接单订单超时处理完成，处理数量：{}", collect.size());
            }
        } else {
            log.info("开始执行待接单订单超时处理任务");
            String format = DateUtils.format(shop.alien.util.common.DateUtils.calcDays(new Date(), -4), "yyyy-MM-dd");
            LambdaQueryWrapper<LawyerConsultationOrder> lambdaQueryWrapper = new LambdaQueryWrapper<LawyerConsultationOrder>()
                    .eq(LawyerConsultationOrder::getOrderStatus, 2)
                    .like(LawyerConsultationOrder::getAcceptOrdersTime, format)
                    .eq(LawyerConsultationOrder::getDeleteFlag, 0);
            List<LawyerConsultationOrder> lawyerConsultationOrders = lawyerConsultationOrderMapper.selectList(lambdaQueryWrapper);
            List<Integer> collect = lawyerConsultationOrders.stream().map(LawyerConsultationOrder::getId).collect(Collectors.toList());
            if (ObjectUtils.isNotEmpty( collect) && !collect.isEmpty()) {
                LambdaUpdateWrapper<LawyerConsultationOrder> lawyerConsultationOrderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lawyerConsultationOrderLambdaUpdateWrapper.set(LawyerConsultationOrder::getOrderStatus, 3)
                        .in(LawyerConsultationOrder::getId, collect);
                lawyerConsultationOrderMapper.update(null, lawyerConsultationOrderLambdaUpdateWrapper);
            }
        }
    }
}

