package com.kxs.transfer.api.task;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxs.transfer.api.model.KxsDtsErrorLog;
import com.kxs.transfer.api.model.KxsDtsLog;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.KxsDtsErrorLogService;
import com.kxs.transfer.api.service.KxsDtsLogService;
import com.kxs.transfer.api.service.product.*;
import com.kxs.transfer.api.service.product.impl.KxsMachineTrackService;
import com.kxs.transfer.api.service.product.impl.KxsShopOrderService;
import com.kxs.transfer.api.service.store.KxsMachineRecycleService;
import com.kxs.transfer.api.service.store.KxsWarehouseService;
import com.kxs.transfer.api.service.sys.KxsMorningService;
import com.kxs.transfer.api.service.sys.KxsSchoolStudyService;
import com.kxs.transfer.api.service.sys.KxsSysMsgService;
import com.kxs.transfer.api.service.user.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 任务作业
 *
 * @author 没秃顶的码农
 * @date 2024-04-10
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class taskJob {

    private final KxsDtsLogService kxsDtsLogService;
    private final KxsDtsErrorLogService kxsDtsErrorLogService;

    private final KxsUserService kxsUserService;
    private final KxsUserPresetLogService kxsUserPresetLogService;
    private final KxsUserAddressService kxsUserAddressService;
    private final KxsUserAmountService kxsUserAmountService;
    private final KxsUserAdvanceService kxsUserAdvanceService;
    private final KxsUserAmountLogService kxsUserAmountLogService;
    private final KxsUserWithdrawalService kxsUserWithdrawalService;
    private final KxsLeaderService kxsLeaderService;
    private final KxsPartnerService kxsPartnerService;
    private final KxsMorningService kxsMorningService;
    private final KxsSchoolStudyService kxsSchoolStudyService;
    private final KxsSysMsgService kxsSysMsgService;
    private final KxsBankChangeLogService kxsBankChangeLogService;
    private final KxsUserByStageService kxsUserByStageService;
    private final KxsLeaderAmountLogService kxsLeaderAmountLogService;

    private final KxsMachineService kxsMachineService;

    //产品模块
    private final KxsShopGoodsService kxsShopGoodsService;
    private final KxsMachinePledgeService kxsMachinePledgeService;
    private final KxsMachineRatioService kxsMachineRatioService;
    private final KxsTicketService kxsTicketService;
    private final KxsTicketTransferService kxsTicketTransferService;
    private final KxsMerchantService kxsMerchantService;
    private final KxsMachineTransferService kxsMachineTransferService;
    private final KxsMachineTrackService kxsMachineTrackService;
    private final KxsShopOrderService kxsShopOrderService;

    //仓库模块
    private final KxsWarehouseService kxsWarehouseService;
    private final KxsMachineRecycleService kxsMachineRecycleService;


    /**
     * 同步数据失败重新消费
     */
    @Scheduled(fixedDelay = 1000 * 60 * 5)
//    @Scheduled(fixedDelay = 1000 * 5)
    public void tradeRepeatEntry(){
        List<KxsDtsErrorLog> list = kxsDtsErrorLogService.list(Wrappers.<KxsDtsErrorLog>lambdaQuery().in(KxsDtsErrorLog::getOperation, OperationType.INSERT, OperationType.DELETE).last("limit 100"));

        for (KxsDtsErrorLog kxsDtsErrorLog : list) {
            try {
                //仓库担保记录
                if ("StoreHouseAmountPromiss".equals(kxsDtsErrorLog.getTableName())) {
                    kxsWarehouseService.changeCreditData(JSON.parseObject(kxsDtsErrorLog.getContent(), DMLData.class));
                    //删除本条记录
                    kxsDtsErrorLogService.removeById(kxsDtsErrorLog.getId());
                }
                //商品订单同步
                if ("Orders".equals(kxsDtsErrorLog.getTableName())) {
                    kxsShopOrderService.changeData(JSON.parseObject(kxsDtsErrorLog.getContent(), DMLData.class));
                    //删除本条记录
                    kxsDtsErrorLogService.removeById(kxsDtsErrorLog.getId());
                }
                //商品订单详情
                if ("OrderProduct".equals(kxsDtsErrorLog.getTableName())) {
                    kxsShopOrderService.changeInfoData(JSON.parseObject(kxsDtsErrorLog.getContent(), DMLData.class));
                    //删除本条记录
                    kxsDtsErrorLogService.removeById(kxsDtsErrorLog.getId());
                }
                //机具表
                if ("PosMachinesTwo".equals(kxsDtsErrorLog.getTableName())) {
                    kxsMachineService.changeData(JSON.parseObject(kxsDtsErrorLog.getContent(), DMLData.class));
                    //删除本条记录
                    kxsDtsErrorLogService.removeById(kxsDtsErrorLog.getId());
                }
                //合伙人账户余额变动记录表
                if ("AmountRecordNew".equals(kxsDtsErrorLog.getTableName()) ) {
                    kxsPartnerService.changeAccountLogData(JSON.parseObject(kxsDtsErrorLog.getContent(), DMLData.class));
                    //删除本条记录
                    kxsDtsErrorLogService.removeById(kxsDtsErrorLog.getId());
                }








            } catch (Exception e) {
                log.error("重复消费新增数据的队列消费失败",e);
            }





        }


    }

    /**
     * 由于每天同步数据量太大，必须每天清除一次日志
     * 删除 DTS 日志
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void removeDtsLogBeforeDay(){
        LocalDateTime oneDayAgo = LocalDateTime.now().minusDays(1);
        kxsDtsLogService.remove(Wrappers.<KxsDtsLog>lambdaQuery().lt(KxsDtsLog::getCreateTime,oneDayAgo));
    }

}
