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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.friday.custody.dto.*;
import com.friday.custody.service.basicService.impl.*;
import com.friday.custody.service.tenantService.impl.TrustAuthorizationServiceImpl;
import com.friday.custody.service.trustService.citicZhongXin.service.OutsourceZhongXinService;
import com.friday.custody.service.trustService.cmsc.service.OutsourceCmscServiceImpl;
import com.friday.custody.service.trustService.gtja.service.OutsourceGuoAnServiceImpl;
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.zx.service.OutSourceZxServiceImpl;
import com.friday.custody.context.OutsourceServiceContext;
import com.friday.custody.entity.ProductDataEntity;
import com.friday.custody.entity.TenantEmailAuthorizationInfoEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.exception.RRException;
import com.friday.custody.req.BasicQueryReq;
import com.friday.custody.service.basicService.InvokeRecordService;
import com.friday.custody.service.logicService.LogicInternalService;
import com.friday.custody.service.tenantService.TenantService;
import com.friday.custody.service.trustService.OutsourceService;
import com.friday.custody.utils.R;
import com.friday.custody.vo.TenantEscrowAuthorizationInfoVO;
import com.friday.custody.vo.TenantInfoVO;
import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.custody.dto.*;
import com.friday.operation.api.custody.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

import static com.friday.base.common.response.DubboResult.CLIENT_EXCEPTION;

@Service
@Slf4j
public class LogicInternalServiceImpl implements LogicInternalService {
    @Autowired
    private TenantService tenantService;
    @Autowired
    private TenantEscrowAuthorizationInfoServiceImpl tenantEscrowAuthorizationInfoService;

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

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


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

    @Resource(name = "outsourceService")
    private OutsourceService outsourceService;

    @Resource
    private OutSourceMailServiceImpl outSourceMailService;

    @Autowired
    private OutsourceGuoAnServiceImpl outsourceGuoAnService;


    @Autowired
    private OutsourceCmscServiceImpl outsourceCmscService;

    @Autowired
    private OutsourceZhongXinService outsourceZhongXinService;

    @Autowired
    private OutSourceSWServiceImpl outSourceSWService;

    @Resource(name = "tenantEscrowAuthorizationInfoService")
    private TenantEscrowAuthorizationInfoServiceImpl tenantEscrowAuthorizationInfoServiceimpl;

    @Autowired
    private OutsourceHtscServiceImpl outsourceHtscService;

    @Autowired
    private DataDictionaryZxFndBasicInfoCyServiceImpl dataDictionaryZxFndBasicInfoCyService;

    @Autowired
    private OutSourceZxServiceImpl outSourceZxService;

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

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

    @Autowired
    private TenantEmailAuthorizationInfoServiceImpl tenantEmailInfoService;
    @Autowired
    private DataDictionaryTrustServiceImpl dataDictionaryTrustService;
    @Autowired
    TenantInfoServiceImpl tenantInfoService;
    @Autowired
    TrustAuthorizationServiceImpl trustAuthorizationService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveTenant(TenantInfoVO vo) {
        TenantInfoDto dto = TenantInfoDto.builder()
                .tenantName(vo.getAdmin())
                .accountName(vo.getAccountName())
                .accountLevel(vo.getAccountLevel())
                .contactPerson(vo.getContactPerson())
                .permissions(vo.getPermissions())
                .build();
        return tenantService.addTenant(dto);
    }

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

    @Override
    public R saveEscrowAuthorization(String tenantName, String accountKey, TenantEscrowAuthorizationInfoVO vo) {
        //1 校验数据
        RRException.isNotTrue(StringUtils.isBlank(tenantName), RetCodeEnum.PARAM_ERROR.getCode(), "租户名不能为空！");
        RRException.isNotTrue(StringUtils.isBlank(accountKey), RetCodeEnum.PARAM_ERROR.getCode(), "用户key不能为空！");
        //2 vo转dto
        TenantEscrowAuthorizationInfoDto dto = TenantEscrowAuthorizationInfoDto.builder()
                .tenantName(tenantName)
                .contactPerson(vo.getContactPerson())
                .trustNo(vo.getTrustNo() + "")
                .appKey(vo.getAppKey())
                .appSecret(vo.getAppSecret())
                .companyId(vo.getCompanyId())
                .files(vo.getFiles())
                .build();
        //3 执行保存操作
        R r=  tenantService.saveEscrowAuthorization(tenantName, accountKey, dto);
        if ((Integer) r.get("code") == RetCodeEnum.SUCCESS.getCode()) {
            //如果成功 则调用初始化数据接口
            CompletableFuture.supplyAsync(() -> {
                outsourceService.initTenantData(Collections.singletonList(dto));
                return null;
            }, initTenantDataThreadPool);
        }
        return r;
    }

    @Override
    public R queryAuthorization(String tenantName, String accountKey) {
        //1 校验数据
        RRException.isNotTrue(StringUtils.isBlank(tenantName), RetCodeEnum.PARAM_ERROR.getCode(), "租户名不能为空！");
        RRException.isNotTrue(StringUtils.isBlank(accountKey), RetCodeEnum.PARAM_ERROR.getCode(), "用户key不能为空！");
        //2 执行查询操作
        //todo
        return tenantService.queryAuthorization(tenantName, accountKey);
    }

    @Override
    public R initHtscDataByScheduled() {
       return outsourceHtscService.initDataByScheduled();
    }

    @Override
    public R initGuoAnData() {
        R<List<TenantEscrowAuthorizationInfoDto>> listR = tenantEscrowAuthorizationInfoService.queryAuthorizations();
        List<TenantEscrowAuthorizationInfoDto> data = listR.getData();
        if(data!=null){
            for (TenantEscrowAuthorizationInfoDto item : data) {
                if(item.getTrustNo().equals("31")){
                    outsourceGuoAnService.getDataFromGTJA(item,12);
                }
            }
        }
        return R.ok("国泰君安初始化完成！");
    }

    @Override
    public R initZhaoShangData(String mouth) {
        R<List<TenantEscrowAuthorizationInfoDto>> listR = tenantEscrowAuthorizationInfoService.queryAuthorizations();
        List<TenantEscrowAuthorizationInfoDto> data = listR.getData();
        if(data!=null){
            for (TenantEscrowAuthorizationInfoDto item : data) {
                if(item.getTrustNo().equals("31")){
                    int mouthInt = 12;
                    if(mouth ==null){
                        try{
                            mouthInt = Integer.valueOf(mouth);
                        }catch (Exception e){
                            log.error(e.getMessage());
                            mouthInt = 12;
                        }
                    }
                    outsourceCmscService.getDataFromCMSC(item,mouthInt);
                }
            }
        }
        return R.ok("招商初始化完成！");
    }

    /**
     * 每天定时更新数据
     * @return
     */
    @Override
    public R updateDataDailyByScheduled() {
        try {
            //1 先去查询授权表 获得所有授权的客户信息
            //2 执行查询
            return outsourceService.updateDataDaily(tenantService.queryAuthorizations().getData());
        } catch (Exception e) {
            log.error("执行每日更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每日更新定时器失败！");
        }
    }

    public R updateZhongXinDataDailyByScheduled() {

        CompletableFuture.supplyAsync(() -> {
            try {
                //1 先去查询授权表 获得所有授权的客户信息
                List<TenantEscrowAuthorizationInfoDto> data = tenantService.queryAuthorizations().getData();
                log.info("updateZhongXinDataDailyByScheduled_租户信息{}",JSON.toJSONString(data));
                for (TenantEscrowAuthorizationInfoDto tenant : data) {
                    if( !Objects.isNull(tenant) && "35".equals(tenant.getTrustNo())){
                        //2 执行查询
                        outsourceZhongXinService.updateSingleDataByAppointedScheduled(tenant);
                    }
                }

            } catch (Exception e) {
                log.error("执行每日更新定时器失败！" , e);
                return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每日更新定时器失败！");
            }
            return null;
        }, updateZhongXinDataPool);
        log.info("中信每天早晚12点定时任务已提交");
        return R.ok();

    }

    public R updateSWDataDailyByScheduled() {
        log.info("quartz 每30min执行");
        try {
            //1 先去查询授权表 获得所有授权的客户信息
            List<TenantEscrowAuthorizationInfoDto> data = tenantService.queryAuthorizations().getData();
            log.info("updateZhongXinDataDailyByScheduled_租户信息{}",JSON.toJSONString(data));
            for (TenantEscrowAuthorizationInfoDto tenant : data) {
                if( !Objects.isNull(tenant) && "45".equals(tenant.getTrustNo())){
                    //2 执行查询
                    outSourceSWService.updateSingleDataByAppointedScheduled(tenant);
                }
            }

        } catch (Exception e) {
            log.error("执行每日更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每日更新定时器失败！");
        }
        return null;
    }
    public R updateSWMgDataDaily() {
        log.info("每天10点、16点更新申万募集户/托管户余额、流水数据");
        try {
            //1 先去查询授权表 获得所有授权的客户信息
            List<TenantEscrowAuthorizationInfoDto> data = tenantService.queryAuthorizations().getData();
            log.info("updateSWDataOfMg_租户信息{}",JSON.toJSONString(data));
            for (TenantEscrowAuthorizationInfoDto tenant : data) {
                if( !Objects.isNull(tenant) && "45".equals(tenant.getTrustNo())){
                    //2 执行查询
                    outSourceSWService.updateSingleMgData(tenant);
                }
            }

        } catch (Exception e) {
            log.error("执行每日更新申万募集户/托管户余额、流水数据定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每日更新申万募集户/托管户余额、流水数据定时器失败！");
        }
        return null;
    }

    @Autowired
    private DataDictionaryBasicServiceImpl dataDictionaryBasicService;
    @Autowired
    private DataDictionaryTypeCertificateCsdcServiceImpl certificateCsdcService;
    @Autowired
    private DataDictionaryOpenStateServiceImpl openStateService;
    @Override
    public R testDic(String type, String trustNo) {
        DataDictionaryTypeEnum enumByTypeCode = DataDictionaryTypeEnum.getEnumByTypeCode(type);
        if (DataDictionaryTypeEnum.IDENTITY_TYPE_CSDC_SERVICE.getTypeCode().equals(type)) {
            return R.ok("查询成功！").put("data", certificateCsdcService.queryCertificateWithDetail(trustNo));
        }
        if (DataDictionaryTypeEnum.OPEN_STATE_SERVICE.getTypeCode().equals(type)) {
            return R.ok("查询成功！").put("data", openStateService.queryOpenStateWithDetail(trustNo));
        }
        Map<String, String> stringStringMap = dataDictionaryBasicService.queryWithDetail(enumByTypeCode, trustNo);
        return R.ok("查询成功！").put("data", stringStringMap);
    }
    @Autowired
    private OutsourceServiceContext outsourceServiceContext;
    @Override
    public R pullRemoteDataFirstQueueJob() {
        outsourceServiceContext.PullRemoteDataFirstQueueJob();
        return R.ok("pullRemoteDataFirstQueueJob成功！");
    }

    @Autowired
    private InvokeRecordService invokeRecordService;
    @Override
    public R queryOriginData(String tenantName, String accountKey, String type, String trustNo, Integer pageNo, Integer pageSize) {
        //1 校验数据
        RRException.isNotTrue(StringUtils.isBlank(tenantName), RetCodeEnum.PARAM_ERROR.getCode(), "租户名不能为空！");
        RRException.isNotTrue(StringUtils.isBlank(accountKey), RetCodeEnum.PARAM_ERROR.getCode(), "用户key不能为空！");
        RRException.isNotTrue(StringUtils.isBlank(accountKey), RetCodeEnum.PARAM_ERROR.getCode(), "用户type不能为空！");
        RRException.isNotTrue(StringUtils.isBlank(accountKey), RetCodeEnum.PARAM_ERROR.getCode(), "用户trustNo不能为空！");
        checkTenantInfo(tenantName,accountKey);
        BasicQueryReq basicQueryReq = new BasicQueryReq();
        basicQueryReq.setPageNo(pageNo);
        basicQueryReq.setPageSize(pageSize);
        return invokeRecordService.queryByType(tenantName,type,trustNo,basicQueryReq);
    }
    private void checkTenantInfo(String tenantName, String accountKey) {
        R<TenantInfoDto> tenantInfoDtoR = tenantService.queryTenant(TenantInfoDto.builder().tenantName(tenantName).accountKey(accountKey).build());
        if (Objects.isNull(tenantInfoDtoR.getData())) {
            throw new RRException("租户名或密码错误！");
        }
    }

    @Override
    public R initLaterData(String tenantName, String accountKey, String versionStr) {
        RRException.isNotTrue(StringUtils.isBlank(tenantName), RetCodeEnum.PARAM_ERROR.getCode(), "租户名不能为空！");
        RRException.isNotTrue(StringUtils.isBlank(accountKey), RetCodeEnum.PARAM_ERROR.getCode(), "用户key不能为空！");
        checkTenantInfo(tenantName,accountKey);
       return outsourceService.initLaterTenantData(versionStr,tenantService.queryAuthorizations().getData());
    }

    @Override
    public R initByAppointedTrustNoAndMouth( int month, String admin, String trustNo, String appKey) {
        log.info("initByAppointedTrustNoAndMouth参数：trustNo={},mouth={}",trustNo,month);
        List<TenantEscrowAuthorizationInfoDto> data = tenantService.queryAuthorizations().getData();
        if(CollectionUtils.isEmpty(data)){
            log.error("没有注册租户!");
            return R.error("没有注册租户!");
        }
        try {
            List<TenantEscrowAuthorizationInfoDto> theTrustNos = data.stream().filter(item ->
                    trustNo.equals(item.getTrustNo()) &&  admin.equals(item.getTenantName()) && appKey.equals(item.getAppKey())
            ).collect(Collectors.toList());
            Optional<TenantEscrowAuthorizationInfoDto> any = theTrustNos.stream().findAny();
            if (any.isPresent()) {
                TenantEscrowAuthorizationInfoDto theTrustNo = any.get();
                log.info("initByAppointedTrustNoAndMouth调用开始,租户信息="+ JSON.toJSONString(theTrustNo));
                outsourceService.initByAppointedTrustNoAndMouth(theTrustNo,month);
                log.info("initByAppointedTrustNoAndMouth调用结束,租户信息="+ JSON.toJSONString(theTrustNo));
            }
        } catch (Exception e) {
            log.error("执行每日更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每日更新定时器失败！");
        }

        return null;
    }

    @Override
    public R initGtjaDataByOne( int month, String admin, String trustNo, String appKey, String type) {
        log.info("initByAppointedTrustNoAndMouth参数：trustNo={},mouth={}",trustNo,month);
        List<TenantEscrowAuthorizationInfoDto> data = tenantService.queryAuthorizations().getData();
        if(CollectionUtils.isEmpty(data)){
            log.error("没有注册租户!");
            return R.error("没有注册租户!");
        }
        try {
            List<TenantEscrowAuthorizationInfoDto> theTrustNos = data.stream().filter(item ->
                    trustNo.equals(item.getTrustNo()) &&  admin.equals(item.getTenantName()) && appKey.equals(item.getAppKey())
            ).collect(Collectors.toList());
            Optional<TenantEscrowAuthorizationInfoDto> any = theTrustNos.stream().findAny();
            if (any.isPresent()) {
                TenantEscrowAuthorizationInfoDto theTrustNo = any.get();
                log.info("initGtjaDataByOne调用开始,租户信息="+ JSON.toJSONString(theTrustNo));
                outsourceGuoAnService.getDataFromGTJAByType(theTrustNo,month, type);
                log.info("initGtjaDataByOne调用结束,租户信息="+ JSON.toJSONString(theTrustNo));
            }
        } catch (Exception e) {
            log.error("initGtjaDataByOne更新失败!！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"initGtjaDataByOne更新失败！");
        }

        return null;
    }

    @Override
    public R updateData20min() {
        try {
            //1 先去查询授权表 获得所有授权的客户信息
            //2 执行查询
            return outsourceService.updateData20min(tenantService.queryAuthorizations().getData());
        } catch (RRException e) {
            log.error("执行每20min更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),e.getMsg());
        }catch (Exception e) {
            log.error("执行每20min更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每20min更新定时器失败！");
        }
    }

    @Override
    public R updateData1h() {
        try {
            //1 先去查询授权表 获得所有授权的客户信息
            //2 执行查询
            return outsourceService.updateData1h(tenantService.queryAuthorizations().getData());
        } catch (RRException e) {
            log.error("执行每一小时更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),e.getMsg());
        } catch (Exception e) {
            log.error("执行每一小时更新定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行每一小时更新定时器失败！");
        }
    }

    @Override
    public R updateDataCJ(String date,String flag) {

        CompletableFuture.supplyAsync(() -> {
            try {
                String trustNo = "54";
                TenantEscrowAuthorizationInfoDto tenant = tenantEscrowAuthorizationInfoServiceimpl.queryAuthorizationByTrustNo(trustNo);
                log.info("teantInfo:"+JSON.toJSONString(tenant));
                if(!Objects.isNull(tenant)){
                    //return outSourceCJService.updateDataCJ(date,flag,tenant);
                }
                return null;
            } catch (Exception e) {
                log.error("获取长江数据失败！" , e);
                return R.error(RetCodeEnum.SYS_ERROR.getCode(),"获取长江数据失败！");
            }
        }, updateDataCJPool);
        return R.ok("长江定时任务已提交");

    }

    @Override
    public R updateDataDicZxFndBasicInfoCy() {
        try {
            String trustNo = "99";
            TenantEscrowAuthorizationInfoDto tenant = tenantEscrowAuthorizationInfoServiceimpl.queryAuthorizationByTrustNo(trustNo);
            return outSourceZxService.processDataDicZxFndBasicInfo(tenant);
        }catch (Exception e){
            log.info("保存dataDictionaryZxFndBasicInfoCy数据失败！", e);
            return R.error("定时更新dataDictionaryZxFndBasicInfoCy数据失败!");
        }
    }

    @Override
    public R initDataZx() {
        try {
            String trustNo = "99";
            TenantEscrowAuthorizationInfoDto tenant = tenantEscrowAuthorizationInfoServiceimpl.queryAuthorizationByTrustNo(trustNo);
            if (!Objects.isNull(tenant)) {
                return outSourceZxService.initTenantDataSingle(tenant);
            }
            return null;
        }catch (Exception e){
            log.error("获取资讯平台数据失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"获取资讯平台数据失败！");
        }
    }

    @Override
    public R updateDataZx() {
        try {
            String trustNo = "99";
            TenantEscrowAuthorizationInfoDto tenant = tenantEscrowAuthorizationInfoServiceimpl.queryAuthorizationByTrustNo(trustNo);
            if (!Objects.isNull(tenant)) {
                return outSourceZxService.updateSingleDataDaily(tenant);
            }
            return null;
        }catch (Exception e) {
            log.error("更新资讯平台数据失败！", e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(), "更新资讯平台数据失败！");
        }
    }

    @Override
    public R updateNetValueData() {
        try {
            //1 先去查询授权表 获得所有授权的客户信息
            //2 执行查询
            return outsourceService.updateNetValueData(tenantService.queryAuthorizations().getData());
        } catch (Exception e) {
            log.error("每天更新8点、12点、20点定时器失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"每天更新8点、12点、20点定时器失败！");
        }
    }

    @Override
    public R updateZhongxTotalAsset(int loopTime) {
        try {
            String trustNo = "35";
            List<TenantEscrowAuthorizationInfoDto> tenants = tenantEscrowAuthorizationInfoServiceimpl.queryAuthorizationsByTrustNo(trustNo);

//            List<TenantEscrowAuthorizationInfoDto> testTenants = tenants.stream().filter(t -> "P1000869".equals(t.getTenantName())).collect(Collectors.toList());
//            List<String> nameList = testTenants.stream().map(t -> t.getTenantName()).collect(Collectors.toList());
//            log.error("updateZhongxTotalAsset--tenantNameList:{}",Arrays.toString(nameList.toArray()));
            tenants.forEach(tenant ->{
                CompletableFuture.supplyAsync(() -> {
                    List<TenantEmailAuthorizationInfoEntity> tenantEmailAuthorizationInfoEntities = tenantEmailInfoService.queryByTenantName(tenant.getTenantName(), tenant.getTrustNo());
                    if(!CollectionUtils.isEmpty(tenantEmailAuthorizationInfoEntities)){
                        for (TenantEmailAuthorizationInfoEntity tenantEmailEntity : tenantEmailAuthorizationInfoEntities) {
                            if (!ObjectUtils.isEmpty(tenantEmailEntity)) {
                                outsourceZhongXinService.updateTotalAsset(tenant, tenantEmailEntity, loopTime);
                                log.info("租户{}更新中信资产合计字段！", tenant.getTenantName());
                            }
                        }
                    }
                    return null;
                },updateZhongxTotalAssetPool);
            });
        } catch (Exception e) {
            log.error("更新中信资产合计失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"更新中信资产合计失败！");
        }
        return R.ok("更新中信资产合计进行中！");
    }

    @Override
    public R initByMail(String date, String admin, String trustNo, String appKey, String flag) {
        log.info("initByMail：admin={},trustNo={},appKey={}",admin,trustNo,appKey);
        List<TenantEscrowAuthorizationInfoDto> data = tenantService.queryAuthorizations().getData();
        if(CollectionUtils.isEmpty(data)){
            log.error("没有注册租户!");
            return R.error("没有注册租户!");
        }
        try {
            List<TenantEscrowAuthorizationInfoDto> theTrustNos = data.stream().filter(item ->
                    trustNo.equals(item.getTrustNo()) &&  admin.equals(item.getTenantName()) && appKey.equals(item.getAppKey())
            ).collect(Collectors.toList());
            Optional<TenantEscrowAuthorizationInfoDto> any = theTrustNos.stream().findAny();
            if (any.isPresent()) {
                TenantEscrowAuthorizationInfoDto theTrustNo = any.get();
                log.info("initByMail调用开始,租户信息="+ JSON.toJSONString(theTrustNo));
                    outSourceMailService.initByMail(theTrustNo,date,flag);
                log.info("initByMail调用结束,租户信息="+ JSON.toJSONString(theTrustNo));
            }
        } catch (Exception e) {
            log.error("initByMail执行失败！" , e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(),"执行initByMail定时器失败！");
        }

        return null;
    }
    @Override
    public List<DataTrustDto> queryTheCustodian(String admin, String trustNo, String appKey) {
        //custody_data_dictionary_trust 托管人
        return  dataDictionaryTrustService.queryAllData(trustNo);

    }
    @Override
    public List<TrustNoMonitorDto> queryAdministrator(String admin, String trustNo, String appKey) {
        //管理人
        return tenantInfoService.queryAllData(admin);
    }
    @Autowired
    AdminInfoServiceImpl adminInfoService;
    @Override
    public List<String> rpaGetAdminNo() {
        List<String> admin = adminInfoService.rpaGetAdminNo();
        return admin;
    }
    @Autowired
    com.friday.custody.service.basicService.impl.ProductDataServiceImpl ProductDataServiceImpl;
    @Override
    public List<ProductDataEntity> saveProductData(JSONObject jsonObject) {
        List<ProductDataEntity> productDataEntities = ProductDataServiceImpl.saveProductData(jsonObject);
        return productDataEntities;
    }

    @Override
    public DubboResult editAuthorization(String admin, String accountKey, AuthorizationDTO dto, MultipartFile file) {
        checkTenantInfo(admin, accountKey);
        if (StringUtils.isBlank(dto.getTrustNo())) {
            return DubboResult.newFailure(CLIENT_EXCEPTION, "托管编码不能为空！");
        }
        if ("25".equals(dto.getTrustNo()) || "42".equals(dto.getTrustNo())) {
            if (ObjectUtil.isEmpty(file)) {
                return DubboResult.newFailure(CLIENT_EXCEPTION, "请上传授权文件！");
            }
            try {
                byte[] bytes = file.getBytes();
                AuthorizationFileDTO fileDTO = dto.getAuthorizationFileDTO();
                if (ObjectUtil.isEmpty(fileDTO)){
                    fileDTO = new AuthorizationFileDTO();
                }
                fileDTO.setFile(bytes);
                dto.setAuthorizationFileDTO(fileDTO);
            } catch (IOException e) {
                log.error("文件转换失败!", e);
            }

        }
        return trustAuthorizationService.editAuthorizationInfo(dto);
    }

    @Override
    public DubboResult<ArrayList<TrustAuthorizationVO>> queryTrustAuthorization(String admin, String accountKey, AuthorizationQueryDTO queryDTO) {
        checkTenantInfo(admin, accountKey);
        return trustAuthorizationService.queryAuthorizationInfo(queryDTO);
    }

    @Override
    public DubboResult deleteTrustAuthorization(String admin, String accountKey, AuthorizationDeleteDTO deleteDTO) {
        checkTenantInfo(admin, accountKey);
        return trustAuthorizationService.deleteAuthorizationInfo(deleteDTO);
    }
}
