
package com.tmerclub.cloud.order.task;


import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.api.feign.SearchOrderTaskFeignClient;
import com.tmerclub.cloud.api.user.dto.ShopCustomerReqDTO;
import com.tmerclub.cloud.api.user.feign.ShopCustomerFeignClient;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.order.manager.MongoOrderManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 订单定时任务
 *
 * @author tmerclub
 */
@Component
public class OrderTask {

    @Autowired
    private MongoOrderManager mongoOrderManager;
    @DubboReference
    private SearchOrderTaskFeignClient searchOrderTaskFeignClient;
    @DubboReference
    private ShopCustomerFeignClient shopCustomerFeignClient;


    /**
     * 订单结算
     */
    @XxlJob("settledOrder")
    public void settledOrder() {
        ServerResponseEntity<Object> serverResponseEntity = searchOrderTaskFeignClient.settledOrder();
        if (!serverResponseEntity.isSuccess()) {
            throw new LuckException(serverResponseEntity.getMsg());
        }
    }


    /**
     * 确认收货
     */
    @XxlJob("confirmOrder")
    public void confirmOrder() {
        ServerResponseEntity<Object> serverResponseEntity = searchOrderTaskFeignClient.confirmOrder();
        if (!serverResponseEntity.isSuccess()) {
            throw new LuckException(serverResponseEntity.getMsg());
        }
    }

    /**
     * 预售订单，尾款时间过期未支付时的处理
     */
    @XxlJob("balanceTimeExpireOrder")
    public void handleBalanceTimeExpireOrder() {
        ServerResponseEntity<Object> serverResponseEntity = searchOrderTaskFeignClient.handleBalanceTimeExpireOrder();
        if (!serverResponseEntity.isSuccess()) {
            throw new LuckException(serverResponseEntity.getMsg());
        }
    }

    /**
     * 预售订单，尾款时间开始时尾款金额为0的处理
     */
    @XxlJob("balanceTimeStartOrder")
    public void handlerBalanceTimeStartOrder() {
        ServerResponseEntity<Object> serverResponseEntity = searchOrderTaskFeignClient.handlerBalanceTimeStartOrder();
        if (!serverResponseEntity.isSuccess()) {
            throw new LuckException(serverResponseEntity.getMsg());
        }
    }

    /**
     * 0点执行一遍检查采购订单支付状态定时任务
     */
    @XxlJob("checkPurchaseOrderPayStatus")
    public void checkPurchaseOrderPayStatus() {
        ServerResponseEntity<Void> serverResponseEntity = searchOrderTaskFeignClient.checkPurchaseOrderPayStatus();
        if (!serverResponseEntity.isSuccess()) {
            throw new LuckException(serverResponseEntity.getMsg());
        }
    }

    /**
     * 添加店铺会员
     * 每分钟进行一次统计
     */
    @XxlJob("addShopCustomer")
    public void addShopCustomer(String str) {
        Date startTime = RedisUtil.get(CacheNames.SHOP_CUSTOMER_UPDATE_TIME);
        if (Objects.nonNull(startTime)) {
            // 将搜索的时间提前一分钟，防止遗漏掉订单
            startTime = DateUtil.offsetMinute(startTime, -1);
        }
        Date endTime = new Date();

        List<ShopCustomerReqDTO> mongoOrderList = mongoOrderManager.listUserAndShopDataByTime(startTime, endTime);

        if (Objects.nonNull(mongoOrderList)) {
            // 插入店铺会员
            shopCustomerFeignClient.saveBatchByOrders(mongoOrderList);
        }
        RedisUtil.set(CacheNames.SHOP_CUSTOMER_UPDATE_TIME, startTime, Constant.DAY_SECOND);
    }
}
