package com.friday.custody.service.trustService.impl;

import com.alibaba.fastjson.JSON;
import com.friday.base.common.response.DubboResult;
import com.friday.custody.service.trustService.XingYe.service.OutSourceXYServiceImpl;
import com.friday.custody.service.trustService.anxin.service.OutSourceAnXinServiceImpl;
import com.friday.custody.service.trustService.citicZhongXin.service.OutsourceZhongXinService;
import com.friday.custody.service.trustService.cmsc.service.OutsourceCmscServiceImpl;
import com.friday.custody.service.trustService.csc.service.OutSourceCSCServiceImpl;
import com.friday.custody.service.trustService.ebscn.service.OutsourceEbscnServiceImpl;
import com.friday.custody.service.trustService.gf.service.OutSourceGFServiceImpl;
import com.friday.custody.service.trustService.gtja.service.OutsourceGuoAnServiceImpl;
import com.friday.custody.service.trustService.guojindaofu.service.impl.GuoJinDaoFuServiceImpl;
import com.friday.custody.service.trustService.guosen.service.impl.OutsourceGuosenServiceImpl;
import com.friday.custody.service.trustService.haitong.service.OutsourceHaiTongServiceImpl;
import com.friday.custody.service.trustService.htsc.service.OutsourceHtscServiceImpl;
import com.friday.custody.service.trustService.mailHandleService.logicService.OutSourceMailServiceImpl;
import com.friday.custody.service.trustService.swhy.service.OutSourceSWServiceImpl;
import com.friday.custody.service.trustService.yinhe.service.OutSourceYinHeServiceImpl;
import com.friday.custody.service.trustService.zhongtai.service.OutSourceZhongTaiServiceImpl;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.service.trustService.OutsourceService;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

@Service("outsourceService")
@Slf4j
public class OutsourceServiceImpl implements OutsourceService {
    @Autowired
    OutsourceHtscServiceImpl htscService;
    @Autowired
    OutsourceGuosenServiceImpl guosenService;
    @Autowired
    OutsourceGuoAnServiceImpl guoAnService;
    @Autowired
    OutsourceHaiTongServiceImpl haiTongService;
    @Autowired
    OutSourceXYServiceImpl xyService;
    @Resource(name = "pullDataThreadPool")
    private ExecutorService pullDataThreadPool;

    @Resource(name = "updateData1hPool")
    private ExecutorService updateData1hPool;

    @Resource(name = "updateData30minPool")
    private ExecutorService updateData30minPool;

    @Autowired
    OutsourceCmscServiceImpl cmscService;
    @Autowired
    OutSourceCSCServiceImpl cscService;
    @Autowired
    OutSourceGFServiceImpl gfService;

    @Autowired
    OutsourceZhongXinService zhongXinService;
    @Autowired
    OutSourceSWServiceImpl swService;

    @Autowired
    OutsourceEbscnServiceImpl ebscnService;
    @Autowired
    OutSourceAnXinServiceImpl anXinService;


    @Autowired
    OutSourceMailServiceImpl mailService;

    @Autowired
    private OutSourceYinHeServiceImpl yinHeService;
    @Autowired
    private OutSourceZhongTaiServiceImpl zhongTaiService;
    @Resource(name = "updateNetValuePool")
    private ExecutorService updateNetValuePool;
    @Autowired
    private GuoJinDaoFuServiceImpl guoJinDaoFuService;
    /**
     * 此处为授权时初始化数据 三年内的托管数据
     *
     * @param tenants
     * @return
     */
    @Override
    public R initTenantData(List<TenantEscrowAuthorizationInfoDto> tenants) {

        //1 根据trust_no判断调用哪个service
        tenants.forEach(tenant -> {
            OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
            //此处为单个授权 若之后有批量授权  建议采用异步编排
            if (!Objects.isNull(outsourceService)) {
                outsourceService.initTenantDataSingle(tenant);
            }
        });

        return null;
    }

    @Override
    public R initTenantDataSingle(TenantEscrowAuthorizationInfoDto tenant) {
        return R.ok();
    }

    @Override
    public R updateDataDaily(List<TenantEscrowAuthorizationInfoDto> tenants) {
        //1 根据trust_no判断调用哪个service
        tenants.forEach(tenant -> {
            CompletableFuture.supplyAsync(() -> {
                OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
                //此处为单个
                if (!Objects.isNull(outsourceService)) {
                    outsourceService.updateSingleDataDaily(tenant);
                }
                return null;
            },pullDataThreadPool);
        });
        log.info("每日更新成功！");
        return R.ok("每日更新成功!");
    }

    private OutsourceService getOutsourceServiceByTrustNo(TenantEscrowAuthorizationInfoDto tenant) {

        switch (tenant.getTrustNo()) {
            case "41" :
                return zhongTaiService;//中泰
            case "43" :
                return anXinService;
            case "33" :
                return yinHeService;
            case "42" :
                //光大
                return ebscnService;
            //长江
            case "54":
            //中金国际
            case "39":
            //平安银行
            case "14":
            //第一创业
            case "97":
            //国金道富
            case "98":
                return guoJinDaoFuService;
            //长城
            case "53":
            //邮件解析
            case "44":
                return mailService;
            case "45":
                //申万
                return swService;
            case "30":
                //广发
                return gfService;
            case "38":
                //中信建投证券
                return cscService;
            case "36":
                //兴业证券
                return xyService;
            case "21":
                //海通证券
                return haiTongService;
            case "22":
                //国信证券
                return guosenService;
            case "25":
                //招商
                return cmscService;
            case "31":
                //国泰君安
                return guoAnService;
            case "34":
                //华泰证券: 因华泰证券有访问时间限制：非交易时间（9:00-15:30不可访问）
                // 故华泰的数据初始化是将任务放入队列 给定时器在下午四点执行队列中的任务实现
                return htscService;
            case "35":
                //中信
                return zhongXinService;
            default:
                return null;
        }
    }

    @Override
    public R updateSingleDataDaily(TenantEscrowAuthorizationInfoDto tenant) {
        return null;
    }

    @Override
    public R PullRemoteDataFirstQueueJob() {
        return null;
    }

    @Override
    public R initLaterTenantData(String versionStr, List<TenantEscrowAuthorizationInfoDto> tenants) {
        //1 根据trust_no判断调用哪个service
        log.info("initLaterTenantData is begin...the tenants is {}", JSON.toJSONString(tenants));
        tenants.forEach(tenant -> {
            CompletableFuture.supplyAsync(() -> {
                OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
                if (!Objects.isNull(outsourceService)) {
                    outsourceService.initLaterSingleTenantData(versionStr, tenant);
                }
                return null;
            }, pullDataThreadPool);
        });
        log.info("initLaterTenantData is running...");
        return R.ok("拉取数据任务已在进行中...");
    }

    @Override
    public R initLaterSingleTenantData(String versionStr, TenantEscrowAuthorizationInfoDto tenant) {
        return null;
    }

    @Override
    public R initByAppointedTrustNoAndMouth(TenantEscrowAuthorizationInfoDto tenant, int mouth) {
        OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
        if (!Objects.isNull(outsourceService)) {
            log.info("调用信息，实现类={}，tenent={}，mouth={}",outsourceService.getClass().getName(),JSON.toJSONString(tenant),mouth);
            outsourceService.initByAppointedTrustNoAndMouth(tenant, mouth);
        }
        return null;
    }

    @Override
    public R updateData20min(List<TenantEscrowAuthorizationInfoDto> tenants) {
        //1 根据trust_no判断调用哪个service
        tenants.forEach(tenant -> {
            CompletableFuture.supplyAsync(() -> {
                OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
                //此处为单个
                if (!Objects.isNull(outsourceService)) {
                    outsourceService.updateSingleData20min(tenant);
                }
                return null;
            },updateData30minPool);
        });
        log.info("每20min更新成功！");
        return R.ok("每20min更新成功!");
    }

    @Override
    public R updateSingleData20min(TenantEscrowAuthorizationInfoDto tenant) {
        return null;
    }

    @Override
    public R updateData1h(List<TenantEscrowAuthorizationInfoDto> tenants) {
        log.info("updateData1h:查到的授权有：{}",JSON.toJSONString(tenants));
        //1 根据trust_no判断调用哪个service
        tenants.forEach(tenant -> {
            CompletableFuture.supplyAsync(() -> {
                OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
                //此处为单个
                if (!Objects.isNull(outsourceService)) {
                    outsourceService.updateSingleData1h(tenant);
                }
                return null;
            },updateData1hPool);
        });
        log.info("每一小时更新成功！");
        return R.ok("每一小时更新成功!");
    }

    @Override
    public R updateSingleData1h(TenantEscrowAuthorizationInfoDto tenant) {
        return null;
    }

    @Override
    public R updateDataCJ(String date, String flag,TenantEscrowAuthorizationInfoDto tenant) {
        return null;
    }

    @Override
    public R updateNetValueData(List<TenantEscrowAuthorizationInfoDto> tenants) {
        log.info("updateNetValueData:查到的授权有：{}",JSON.toJSONString(tenants));
        //1 根据trust_no判断调用哪个service
        tenants.forEach(tenant -> {
            CompletableFuture.supplyAsync(() -> {
                OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
                //此处为单个
                if (!Objects.isNull(outsourceService)) {
                    outsourceService.updateSingleNetValueData(tenant);
                }
                return null;
            },updateNetValuePool);
        });
        log.info("每8点、12点、20点更新净值表成功！");
        return R.ok("每8点、12点、20点更新净值表成功!");
    }

    @Override
    public R updateSingleNetValueData(TenantEscrowAuthorizationInfoDto tenant) {
        return null;
    }

    @Override
    public DubboResult updateMJBalanceData(TenantEscrowAuthorizationInfoDto tenant) {
        OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
        if (!Objects.isNull(outsourceService)) {
            CompletableFuture.runAsync(() -> outsourceService.updateMJBalanceData(tenant));
        }
        return DubboResult.newSuccess();
    }

    @Override
    public DubboResult updateMJSeqData(TenantEscrowAuthorizationInfoDto tenant) {
        OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
        if (!Objects.isNull(outsourceService)) {
            CompletableFuture.runAsync(() -> outsourceService.updateMJSeqData(tenant));
        }
        return DubboResult.newSuccess();
    }

    @Override
    public DubboResult updateTGBalanceData(TenantEscrowAuthorizationInfoDto tenant) {
        OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
        if (!Objects.isNull(outsourceService)) {
            CompletableFuture.runAsync(() -> outsourceService.updateTGBalanceData(tenant));
        }
        return DubboResult.newSuccess();
    }

    @Override
    public DubboResult updateTGSeqData(TenantEscrowAuthorizationInfoDto tenant) {
        OutsourceService outsourceService = getOutsourceServiceByTrustNo(tenant);
        if (!Objects.isNull(outsourceService)) {
            CompletableFuture.runAsync(() -> outsourceService.updateTGSeqData(tenant));
        }
        return DubboResult.newSuccess();
    }

}
