package com.friday.custody.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.friday.base.common.response.DubboResult;
import com.friday.custody.dao.TenantEscrowAuthorizationInfoDao;
import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.TenantEscrowAuthorizationInfoEntity;
import com.friday.custody.service.basicService.CdsProductInfoService;
import com.friday.custody.service.basicService.impl.TenantEscrowAuthorizationInfoServiceImpl;
import com.friday.custody.service.logicService.impl.LogicInternalServiceImpl;
import com.friday.custody.service.tenantService.TenantService;
import com.friday.custody.dto.TenantInfoDto;
import com.friday.custody.entity.CdsProductInfoEntity;
import com.friday.custody.exception.RRException;
import com.friday.custody.service.trustService.cmsc.service.OutsourceCmscServiceImpl;
import com.friday.custody.service.trustService.swhy.service.OutSourceSWServiceImpl;
import com.friday.custody.utils.R;
import com.friday.operation.api.custody.dto.FundIndicatorQueryDTO;
import com.friday.operation.api.custody.dto.SharpRatioDTO;
import com.friday.operation.api.custody.service.ICustodyDataService;
import com.friday.operation.api.custody.vo.FundIndicatorVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
public class TesterController {
    @Autowired
    CdsProductInfoService cdsProductInfoService;
    @Autowired
    private TenantService tenantService;
    @Autowired
    private ICustodyDataService custodyDataService;
    @Autowired
    private LogicInternalServiceImpl logicInternalService;
    @Autowired
    private OutsourceCmscServiceImpl outsourceCmscService;
    @Autowired
    private TenantEscrowAuthorizationInfoDao tenantEscrowAuthorizationInfoDao;
    @Resource(name = "tenantEscrowAuthorizationInfoService")
    private TenantEscrowAuthorizationInfoServiceImpl tenantEscrowAuthorizationInfoServiceimpl;

    @GetMapping("/custody/internal/cmsc/getDataFromCMSCWithoutTgMj")
    public R getDataFromCMSCWithoutTgMj(String tenantName, String trustNo) {
        QueryWrapper<TenantEscrowAuthorizationInfoEntity> query = new QueryWrapper<>();
        query.eq("tenant_name", tenantName);
        query.eq("trust_no", trustNo);
        query.orderByDesc("update_time");
        List<TenantEscrowAuthorizationInfoEntity> infoList = tenantEscrowAuthorizationInfoDao.selectList(query);
        if (CollectionUtils.isNotEmpty(infoList)) {
            for (TenantEscrowAuthorizationInfoEntity info : infoList) {
                TenantEscrowAuthorizationInfoDto tenant = new TenantEscrowAuthorizationInfoDto();
                BeanUtils.copyProperties(info, tenant);
                outsourceCmscService.getDataFromCMSCWithoutTgMj(tenant, 1);
            }
        }
        return R.ok();
    }

    @PostMapping("/custody/internal/syncFundInfoFromHtscCds")
    @ApiOperation(value = "此接口用于手动同步资讯基金信息", notes = "此接口用于手动同步资讯基金信息")
    public R syncFundInfoFromHtscCds(@RequestHeader(value = "admin") String tenantName,
                                     @RequestHeader(value = "accountKey") String accountKey) {
        checkTenantInfo(tenantName, accountKey);
            boolean fundCodeByZX = cdsProductInfoService.syncFundInfoFromHtscCds();

            if (fundCodeByZX){
                return R.ok("syncFundInfoFromHtscCds").put("status",fundCodeByZX);
            }
        return R.error();
    }

    @PostMapping("/custody/internal/queryFundInfoFromHtscCds")
    @ApiOperation(value = "此接口用于查询资讯基金信息", notes = "此接口用于查询资讯基金信息")
    public List<CdsProductInfoEntity> queryFundInfoFromHtscCds(@RequestHeader(value = "admin") String tenantName,
                                     @RequestHeader(value = "accountKey") String accountKey) {
        checkTenantInfo(tenantName, accountKey);
        List<CdsProductInfoEntity> list = cdsProductInfoService.list();
        return list;
    }

    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("租户名或密码错误！");
        }
    }

    @PostMapping("/custody/internal/getSharpRatio")
    @ApiOperation(value = "此接口用于手动同步资讯基金信息", notes = "此接口用于手动")
    public R getSharpRatio(@RequestBody FundIndicatorQueryDTO query) {
        DubboResult<FundIndicatorVO<List<SharpRatioDTO>>> result = custodyDataService.getSharpRatio(query);
        return R.ok("result").put("result", result);
    }

    @GetMapping("/custody/internal/saveTenantMapping")
    @ApiOperation(value = "此接口用于手动同步资讯基金信息", notes = "此接口用于手动")
    public R saveTenantMapping(String tenant) {
        DubboResult result = custodyDataService.saveTenantMapping(tenant);
        if (!result.success()) {
            return R.error(result.getMessage());
        }
        return R.ok("result").put("result", result.getObject());
    }

    @GetMapping("/custody/internal/syncOverviewOfPrivateFundFromCds")
    @ApiOperation(value = "此接口用于手动同步资讯基金信息", notes = "此接口用于手动同步资讯基金信息")
    public Object syncOverviewOfPrivateFundFromCds(String admin) {

        return custodyDataService.syncOverviewOfPrivateFundFromCds(admin);
    }
}
