package com.saye.hospitalgd.scheduler.job;

import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.scheduler.jobMethod.*;
import com.saye.hospitalgd.service.ThirdFtpConfigService;
import com.saye.hospitalgd.service.ThirdSftpConfigService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 从银行接口下载对账信息并存入数据库
 * @date 2021/8/30 16:03
 */
public class BankGetData implements Job {

    @Autowired
    private ThirdFtpConfigService thirdFtpConfigService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String id = StringDUtil.changeNullToEmpty(jobDataMap.get("id"));
        String name = StringDUtil.changeNullToEmpty(jobDataMap.get("name"));
        String trade_date = StringDUtil.changeNullToEmpty(jobDataMap.get("trade_date"));

        System.out.println("/****************************定时任务[" + name + "]开始执行*********************************/");

        try {
            HashMap<Object, Object> searchMap = new HashMap<>();
            searchMap.put("is_active", "1");
            HashMap<Object, Object> wlifSearchMap = new HashMap<>();
            wlifSearchMap.put("FUBS", "1");
            List<HashMap<Object, Object>> thirdFtpConfigList = thirdFtpConfigService.findThirdFtpConfigList(searchMap);
            List<HashMap<Object, Object>> wlConfigList = thirdFtpConfigService.findWLIF(searchMap);

            for (int i = 0; i < thirdFtpConfigList.size(); i++) {


                HashMap<Object, Object> hashMap = thirdFtpConfigList.get(i);
                String execute_class = StringDUtil.changeNullToEmpty(hashMap.get("EXECUTE_CLASS"));


                //商户对账单
                if ("BankGetDataBySH".equals(execute_class)){
                    Thread thread = new Thread(() -> new BankGetDataBySH().getDate(id,name,trade_date,hashMap));
                    thread.start();
                }

                //商户POS对账单
                if ("BankGetDataBySHPOS".equals(execute_class)){
                    Thread thread = new Thread(() -> new BankGetDataBySHPOS().getDate(id,name,trade_date,hashMap));
                    thread.start();
                }

                // 银行商户银行卡对账单
                if ("BankGetDataBySHYLK".equals(execute_class)) {
                    Thread thread = new Thread(() -> new BankGetDataBySHYLK().getDate(id, name, trade_date, hashMap));
                    thread.start();
                }

                // 商户银联外卡对账单
                if ("BankGetDataBySHYLWK".equals(execute_class)) {
                    Thread thread = new Thread(() -> new BankGetDataBySHYLWK().getDate(id, name, trade_date, hashMap));
                    thread.start();
                }

                // 微信账单
                if ("BankGetDataByWXAPI".equals(execute_class)) {
                    Thread thread = new Thread(() -> new BankGetDataByWXAPI().getDate(id, name, trade_date, hashMap));
                    thread.start();
                }
            }
            for (int i = 0; i < wlConfigList.size(); i++) {

                HashMap<Object, Object> hashMap = wlConfigList.get(i);
                String execute_class = StringDUtil.changeNullToEmpty(hashMap.get("EXECUTE_CLASS"));

                //建行龙支付
                if ("BankGetDataMethodByJHLZF".equals(execute_class)) {
                    Thread thread = new Thread(() -> new BankGetDataMethodByJHLZF().getDate(id, name, trade_date, hashMap));
                    thread.start();
                }
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description: 手动执行一次任务
     * @author thuang
     * @date 2021/9/22 15:07
     * @version 1.0
     */
    public static HashMap<Object, Object> oneExecute(String id, String name, String trade_date, HashMap<Object, Object> hashMap) {

        String execute_class = StringDUtil.changeNullToEmpty(hashMap.get("EXECUTE_CLASS"));

        //建行龙支付
        if ("BankGetDataMethodByJHLZF".equals(execute_class)) {
            return new BankGetDataMethodByJHLZF().getDate(id, name, trade_date, hashMap);
        }

        //商户对账单
        if ("BankGetDataBySH".equals(execute_class)){
            return new BankGetDataBySH().getDate(id,name,trade_date,hashMap);
        }

        //商户POS对账单
        if ("BankGetDataBySHPOS".equals(execute_class)){
            return new BankGetDataBySHPOS().getDate(id,name,trade_date,hashMap);
        }

        // 银行商户银联卡对账单
        if ("BankGetDataBySHYLK".equals(execute_class)) {
            return new BankGetDataBySHYLK().getDate(id, name, trade_date, hashMap);
        }

        // 商户银联外卡对账单
        if ("BankGetDataBySHYLWK".equals(execute_class)) {
            return new BankGetDataBySHYLWK().getDate(id, name, trade_date, hashMap);
        }

        // 微信账单
        if ("BankGetDataByWXAPI".equals(execute_class)) {
            return new BankGetDataByWXAPI().getDate(id, name, trade_date, hashMap);
        }
//
//        //商户营销联盟对账单
//        if ("BankGetDataBySHYXLM".equals(execute_class)){
//            return new BankGetDataBySHYXLM().getDate(id,name,trade_date,hashMap);
//        }

        return null;
    }

}
