package com.friday.custody.controller;

import com.friday.base.common.response.DubboResult;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.service.basicService.impl.TenantEscrowAuthorizationInfoServiceImpl;
import com.friday.custody.service.logicService.LogicInternalService;
import com.friday.custody.service.trustService.citicZhongXin.service.OutsourceZhongXinService;
import com.friday.custody.service.trustService.citicZhongXin.utils.ZhongXinHttpUtil;
import com.friday.custody.service.trustService.guojindaofu.service.impl.GuoJinDaoFuServiceImpl;
import com.friday.custody.utils.R;
import com.friday.operation.api.custody.dto.AuthorizationQueryDTO;
import com.friday.operation.api.custody.service.ITrustAuthorizationService;
import com.friday.operation.api.custody.vo.TrustAuthorizationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
public class ZhongXinVerifyController {
    @Autowired
    private ZhongXinHttpUtil zhongXinHttpUtil;
    @Autowired
    private TenantEscrowAuthorizationInfoServiceImpl tenantEscrowAuthorizationInfoService;
    @Autowired
    private OutsourceZhongXinService outsourceZhongXinService;

//    @GetMapping("/custody/internal/getZhongXinFundValueData")
//    public R verifyFundValueDataFromApi(String fundCode, String beginDate, String endDate){
//        R<List<TenantEscrowAuthorizationInfoDto>> authInfoList = tenantEscrowAuthorizationInfoService.queryAuthorizations();
//        List<TenantEscrowAuthorizationInfoDto> authInfoListData = authInfoList.getData();
//        Optional<TenantEscrowAuthorizationInfoDto> authInfoDto = authInfoListData.stream().filter(t -> "35".equals(t.getTrustNo()) && "P1002894".equals(t.getTenantName())).findFirst();
//        if (authInfoDto.isPresent()){
//            TenantEscrowAuthorizationInfoDto tenant = authInfoDto.get();
//            String token = zhongXinHttpUtil.getToken(tenant);
//            HashMap<String, Object> params = new HashMap<>();
//            params.put("fundCode", fundCode);
//            params.put("beginDate",beginDate);
//            params.put("endDate",endDate);
//            String jsonString = JSON.toJSONString(params);
//
//            Map<String, Object> res = zhongXinHttpUtil.sendMessage(jsonString, ZhongXinConstants.FUND_VALUE_TABLE_DATA_INFO, tenant, tenant.getTrustNo(), token);
//            return R.ok(res);
//        }
//        return R.error();
//    }
//    @GetMapping("/custody/internal/getZhongXinFundValueDataV2")
//    public R verifyFundValueDataFromApi(int loop){
//        R<List<TenantEscrowAuthorizationInfoDto>> authInfoList = tenantEscrowAuthorizationInfoService.queryAuthorizations();
//        List<TenantEscrowAuthorizationInfoDto> authInfoListData = authInfoList.getData();
//        Optional<TenantEscrowAuthorizationInfoDto> authInfoDto = authInfoListData.stream().filter(t -> "35".equals(t.getTrustNo()) && "P1002894".equals(t.getTenantName())).findFirst();
//        if (authInfoDto.isPresent()){
//            TenantEscrowAuthorizationInfoDto tenant = authInfoDto.get();
//            String token = zhongXinHttpUtil.getToken(tenant);
//            outsourceZhongXinService.processFundValueTableInfoForApiV2(tenant,"35",token,loop);
//        }
//        return R.ok("fund value data saving....");
//    }

    @Autowired
    private LogicInternalService logicInternalService;
    @Resource(name = "tenantEscrowAuthorizationInfoService")
    private TenantEscrowAuthorizationInfoServiceImpl tenantEscrowAuthorizationInfoServiceimpl;
    @Autowired
    private GuoJinDaoFuServiceImpl guoJinDaoFuService;
    @Autowired
    private ITrustAuthorizationService authorizationService;

    @PostMapping("/custody/internal/updateAt7")
    public R updateDataAt7Am(){
        logicInternalService.updateDataDailyByScheduled();
        return R.ok();
    }

    @GetMapping("/custody/internal/updateGuoJin")
    public R updateGuoJinAll(Integer type){
        TenantEscrowAuthorizationInfoDto tenant = tenantEscrowAuthorizationInfoServiceimpl.queryAuthorizationByTrustNo("98");
        if (type == null) {
            guoJinDaoFuService.initTenantDataSingle(tenant);
        } else {
            guoJinDaoFuService.updateSingleDataDaily(tenant);
        }
        return R.ok();
    }

    @PostMapping("/custody/internal/getEmailConfig")
    public R getEmailConfig(@RequestBody AuthorizationQueryDTO type){
        DubboResult<ArrayList<TrustAuthorizationVO>> result = authorizationService.queryAuthorizationInfo(type);
        ArrayList<TrustAuthorizationVO> object = result.getObject();
        HashMap<String, Object> map = new HashMap<>(5);
        map.put("data", object);
        return R.ok(map);
    }
}
