package com.zfire.jiasm.syncdata.download.basedata;


import com.zfire.jiasm.syncdata.data.Token;
import com.zfire.jiasm.syncdata.plus.entity.ItfGreeToFaDownloadTask;
import com.zfire.jiasm.syncdata.plus.entity.ItfGreeToFaSynTaskList;
import com.zfire.jiasm.syncdata.plus.service.ItfGreeToFaDownloadTaskService;
import com.zfire.jiasm.syncdata.plus.service.ItfGreeToFaSynTaskListService;
import com.zfire.jiasm.syncdata.service.MasterDownloadService;
import com.zfire.jiasm.syncdata.utils.TokenCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**定时执行一次 师傅端使用 两表配合使用 只需修改其中一个表
 * select * from itf_gree_to_fa_download_task;
 * select * from itf_gree_to_fa_syn_task_list
 */
@Slf4j
@Component
public class MasterDownloadTask {
    @Resource
    MasterDownloadService masterDownloadService;
    @Resource
    TokenCenter tokenCenter;
    @Resource
    ItfGreeToFaDownloadTaskService itfGreeToFaDownloadTaskService;
    @Resource
    ItfGreeToFaSynTaskListService itfGreeToFaSynTaskListService;


    @Scheduled(fixedDelayString = "${masterdownloadrate}")
    public void process() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Token token = tokenCenter.getCurrentToken();
            List<ItfGreeToFaDownloadTask> recs = new ArrayList<>();
            recs = itfGreeToFaDownloadTaskService.lambdaQuery()
                    .eq(ItfGreeToFaDownloadTask::getNeedDownload,1)
                    .orderByAsc(ItfGreeToFaDownloadTask::getId).list();

            for (ItfGreeToFaDownloadTask item : recs) {
                int reqId =item.getId();
                log.info("开始下载基础资料信息: \uF0B7{}",sdf.format(new Date()));
                try {
                    Integer downloadTaskType = item.getDownloadTaskType();
                    String srcTabNames =item.getSrcTabName();
                    String destTabNames =item.getDestTabName();
                    String belongCompany=item.getBelongCompany();
                    String[] arySrcTabName = srcTabNames.split("\\Q|\\E");
                    String[] aryDestTabName = destTabNames.split("\\Q|\\E");

                    masterDownloadService.batchDownload(token, arySrcTabName, aryDestTabName,belongCompany,reqId,downloadTaskType);
                    log.info("格力品牌基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));
                } catch (Exception ex) {
                    log.error("执行基础资料下载任务[" + reqId + "]发生错误:"+ex.getMessage());
                }
            }
        } catch (Exception ex) {
            log.error("ִ执行基础资料下载发生错误", ex);
        }
    }


    @Scheduled(fixedDelayString = "${masterrate}")
    public void processData() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            List<ItfGreeToFaSynTaskList> recs =  itfGreeToFaSynTaskListService.lambdaQuery()
                    .eq(ItfGreeToFaSynTaskList::getSynTaskType,9002)
                    .eq(ItfGreeToFaSynTaskList::getSynStatus,0).list();

            // 数据落本地表
            for (ItfGreeToFaSynTaskList item : recs) {
                int reqId =item.getId();
                log.info("开始处理基础资料信息: \uF0B7{}",sdf.format(new Date()));
                try {
                    masterDownloadService.masterProcessData(item.getSynTaskNo(),item.getId());
                    log.info("格力品牌基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));
                } catch (Exception ex) {
                    log.error("执行基础资料处理任务[" + reqId + "]发生错误:"+ex.getMessage());
                    processError(item, ex);
                }
            }
        } catch (Exception ex) {
            log.error("ִ执行基础资料处理发生错误", ex);
        }
    }

    private void processError(ItfGreeToFaSynTaskList item, Exception ex) {
        item.setSynTime(new Date());
        item.setSynErrMsg("数据处理失败");
        if (ex.getMessage() !=null){
            if (ex.getMessage().length()>255){
                item.setSynErrMsg(ex.getMessage().substring(0,255));
            } else {
                item.setSynErrMsg(ex.getMessage());
            }
        }
        itfGreeToFaSynTaskListService.updateById(item);
    }


}
