package cn.pluss.platform.job;

import cn.hutool.core.io.FileUtil;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.ryx.RyxService;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

/**
 * 定时任务获取前一天插件订单交易数据
 */
@Configuration
@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class RyxGetPlugInTransOrderJob {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RyxService ryxService;

    @Resource
    private MerchantOrderService merchantOrderService;

    /**
     * 每天早上8点获取前一天的插件订单记录
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    public void getPlugInTransOrderTask(){
        log.info("====================>【瑞银信】获取插件交易订单记录开始<==========================");
        JSONObject result = ryxService.downloadFtpFile(null);
        if(StringUtil.isEmpty(result)){
            log.info("====================>【瑞银信】获取插件交易订单结束，下载文件返回路径为null<==========================");
            return;
        }
        String uploadPath = result.getString("uploadPath");
        String filePath = result.getString("filePath");
        //读取文件
        merchantOrderService.readCSV(filePath);
        //删除文件
        FileUtil.del(uploadPath);
        log.info("====================>【瑞银信】获取插件交易订单记录定时任务结束<==========================");
    }
}
