package com.friday.custody.controller;

import com.alibaba.fastjson.JSON;
import com.friday.custody.entity.*;
import com.friday.custody.service.basicService.*;
import com.friday.custody.service.basicService.impl.*;
import com.friday.custody.service.logicService.LogicExternalService;
import com.friday.custody.service.logicService.impl.DaasBuriedPointRecordInfoServiceImpl;
import com.friday.custody.service.basicService.impl.AccountInfoServiceImpl;
import com.friday.custody.service.basicService.impl.FundInfoServiceImpl;
import com.friday.custody.service.basicService.impl.FundNetValueInfoServiceImpl;
import com.friday.custody.utils.R;
import com.friday.custody.utils.TranferUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description
 * @Author k0051745
 * @Date: 2022/9/23
 */
@RestController
@Slf4j
@RequestMapping("/custody/tranfer")
public class DataTranferController{
    @Autowired
    private LogicExternalService logicExternalService;
    @Autowired
    private DaasBuriedPointRecordInfoServiceImpl daasBuriedPointService;

    @Autowired
    private FundInfoServiceImpl fundInfoService;
    @Autowired
    private MjAccountBalanceInfoService mjAccountBalanceInfoService;
    @Autowired
    private MjAccountSeqInfoService mjAccountSeqInfoService;
    @Autowired
    private CustShareInfoService custShareInfoService;
    @Autowired
    private FundTradeInfoService fundTradeInfoService;
    @Autowired
    private InvestorInfoService investorInfoService;
    @Autowired
    private FundOpenInfoService fundOpenInfoService;
    @Autowired
    private InvestorBankInfoService investorBankInfoService;

    @Autowired
    private FundProfitInfoService fundProfitInfoService;

    @Autowired
    private CustShareDetailInfoService custShareDetailInfoService;

    @Autowired
    private AccountInfoServiceImpl accountInfoService;

    @Autowired
    private TgAccountSeqInfoServiceImpl tgAccountSeqInfoService;

    @Autowired
    private TgAccountBalanceInfoServiceImpl tgAccountBalanceInfoService;

    @Autowired
    private FundFeeDataInfoService fundFeeDataInfoService;

    @Autowired
    private PullDataFailureLogService pullDataFailureLogService;

    @Autowired
    private DataDictionaryZxFndBasicInfoCyService dataDictionaryZxFndBasicInfoCyService;

    @Autowired
    private InvokeRecordService invokeRecordService;

    @Autowired
    private FundNetValueInfoServiceImpl fundNetValueInfoService;
    @Autowired
    private TranferUtils tranferUtils;
    /**
     * 21,25
     * @param map
     * @return
     */
    @PostMapping("/fundInfo")
    @ApiOperation("用于中转更新fundInfo数据")
    public R fundInfo(@RequestBody HashMap map){
        log.info("DataTranferController:fundInfo:{}", JSON.toJSONString(map));
        /*List<FundInfoEntity> fundInfoEntities= tranferUtils.getEntityDataByMap(map);
        R r = fundInfoService.tranferAndSaveDataBatch(fundInfoEntities);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    /**
     * 21
     * @param map
     * @return
     */
    @PostMapping("/fundProfitInfo")
    @ApiOperation("用于中转更新fundProfitInfo数据")
    public R fundProfitInfo(@RequestBody HashMap map){
        log.info("DataTranferController:fundProfitInfoEntities:{}", JSON.toJSONString(map));
        /*List<FundProfitInfoEntity> fundProfitInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = fundProfitInfoService.tranferAndSaveDataBatch(fundProfitInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    /**
     * 21
     * @param map
     * @return
     */
    @PostMapping("/custShareDetailInfo")
    @ApiOperation("用于中转更新custShareDetailInfo数据")
    public R custShareDetailInfo(@RequestBody HashMap map){
        log.info("DataTranferController:custShareDetailInfo:{}", JSON.toJSONString(map));
        /*List<CustShareDetailInfoEntity> CustShareDetailInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = custShareDetailInfoService.tranferAndSaveDataBatch(CustShareDetailInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    /**
     * 25
     * @param map
     * @return
     */
    @PostMapping("/accountInfo")
    @ApiOperation("用于中转更新accountInfo数据")
    public R accountInfo(@RequestBody HashMap map){
        log.info("DataTranferController:accountInfoEntities:{}", JSON.toJSONString(map));
        //List<AccountInfoEntity> accountInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r= tranferUtils.getEntityDataByMap(map);
        //R r = accountInfoService.tranferAndSaveDataBatch(accountInfoEntitys);
        return r;
    }

    /**
     * 25
     * @param map
     * @return
     */
    @PostMapping("/tgAccountSeqInfo")
    @ApiOperation("用于中转更新tgAccountSeqInfo数据")
    public R tgAccountSeqInfo(@RequestBody HashMap map){
        log.info("DataTranferController:map:{}", JSON.toJSONString(map));
        /*List<TgAccountSeqInfoEntity> tgAccountSeqInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = tgAccountSeqInfoService.tranferAndSaveDataBatch(tgAccountSeqInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    /**
     * 21,25
     * @param map
     * @return
     */
    @PostMapping("/tgAccountBalanceInfo")
    @ApiOperation("用于中转更新tgAccountBalanceInfo数据")
    public R tgAccountBalanceInfo(@RequestBody HashMap map){
        log.info("DataTranferController:map:{}", JSON.toJSONString(map));
        /*List<TgAccountBalanceInfoEntity> tgAccountBalanceInfoEntity= tranferUtils.getEntityDataByMap(map);
        R r = tgAccountBalanceInfoService.tranferAndSaveDataBatch(tgAccountBalanceInfoEntity);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    /**
     * 21,25
     * @param map
     * @return
     */
    @PostMapping("/fundFeeDataInfo")
    @ApiOperation("用于中转更新fundFeeDataInfo数据")
    public R fundFeeDataInfo(@RequestBody HashMap map){
        log.info("DataTranferController:fundFeeDataInfoEntities:{}", JSON.toJSONString(map));
        /*List<FundFeeDataInfoEntity> fundFeeDataInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = fundFeeDataInfoService.tranferAndSaveDataBatch(fundFeeDataInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    /**
     *
     * @param pullDataFailureLog
     * @return
     */
    @PostMapping("/pullDataFailureLog")
    @ApiOperation("用于中转更新pullDataFailureLog数据")
    public R pullDataFailureLog(@RequestBody PullDataFailureLogEntity pullDataFailureLog){
        log.info("DataTranferController:pullDataFailureLog:{}", JSON.toJSONString(pullDataFailureLog));
        R r = pullDataFailureLogService.tranferAndSaveDataBatch(pullDataFailureLog);
        return r;
    }

    /**
     *
     * @param admin
     * @param trustNo
     * @return
     */
    @PostMapping("/getFundCodesFromFundInfo")
    @ApiOperation("用于中转从基金基础信息表中获取基金编码数据")
    public R getFundCodesFromFundInfo(@RequestHeader("admin") @ApiParam(name = "admin", value = "租户名") String admin,
                                      @RequestHeader("trustNo") @ApiParam(name = "trustNo", value = "托管编号") String trustNo){

        log.info("DataTranferController:getFundCodesFromFundInfo:admin={},trustNo={}", admin,trustNo);
        List<String> fundCodeList = fundInfoService.getFundCodeList(trustNo, admin);

        Map map = new HashMap<String,List<String>>();
        map.put("data",fundCodeList);
        return R.ok(map);
    }

    /**
     *
     * @param admin
     * @param trustNo
     * @return
     */
    @PostMapping("/getFundCodesFromAccountInfo")
    @ApiOperation("用于中转从账户表中获取基金编码数据")
    public R getFundCodesFromAccountInfo(@RequestHeader("admin")String admin,
                                      @RequestHeader("trustNo")String trustNo){

        log.info("DataTranferController:getFundCodesFromAccountInfo:admin={},trustNo={}", admin,trustNo);
        List<String> fundCodeList = accountInfoService.getFundCodeList(trustNo, admin);

        Map map = new HashMap<String,List<String>>();
        map.put("data",fundCodeList);
        return R.ok(map);
    }

    /**
     *  select fund_code from `custody_fund_info`
     *  where inner_fund_code = #{innerFundCode}
     *      and trust_no = #{trustNo} limit 1
     * @param admin
     * @param trustNo
     * @return
     */
    @PostMapping("/getInnerFundCodeAsKeyFundCodeAsValueMap")
    @ApiOperation("用于中转获取基金信息")
    public R getInnerFundCodeAsKeyFundCodeAsValueMap(@RequestHeader("admin")String admin,
                                         @RequestHeader("trustNo")String trustNo){

        log.info("DataTranferController:getInnerFundCodeAsKeyFundCodeAsValueMap:admin={},trustNo={}", admin,trustNo);
        Map result = new HashMap<String,Map<String,String>>();

        List<FundInfoEntity>  list= fundInfoService.getInnerFundCodeAsKeyFundCodeAsValueMap(trustNo, admin);
        log.info("DataTranferController:getInnerFundCodeAsKeyFundCodeAsValueMap:return{}",JSON.toJSONString(list));
        result.put("data",list);
        return R.ok(result);
    }

    @PostMapping("/getFundCodeAndAccountByTgAccountSeqInfo")
    @ApiOperation("用于中转获取托管流水表查询")
    public R getFundCodeAndAccountByTgAccountSeqInfo(@RequestHeader("admin") @ApiParam("租户名(登记编号)") String admin,
                                                     @RequestHeader("trustNo") @ApiParam("托管编码") String trustNo){
        Map result = new HashMap<String,Map<String,String>>();

        log.info("DataTranferController:getFundCodeAndAccountByTgAccountSeqInfo:admin={},trustNo={}", admin,trustNo);
        List<TgAccountSeqInfoEntity> list = tgAccountSeqInfoService.getFundCodeAndAccountList(trustNo, admin);
        log.info("DataTranferController:getFundCodeAndAccountByTgAccountSeqInfo:return{}",JSON.toJSONString(list));
        result.put("data",list);
        return R.ok(result);
    }

    @PostMapping("/getBankAccoListByTgAccountBalanceInfo")
    @ApiOperation("用于中转获取托管账户信息")
    public R getBankAccoListByTgAccountBalanceInfo(@RequestHeader("admin") @ApiParam("租户名(登记编号)") String admin,
                                                     @RequestHeader("trustNo") @ApiParam("托管编码") String trustNo){
        Map result = new HashMap<String,Map<String,String>>();

        log.info("DataTranferController:getBankAccoListByTgAccountBalanceInfo:admin={},trustNo={}", admin,trustNo);
        List<String> bankAccoList = tgAccountBalanceInfoService.getBankAccoList(trustNo, admin);
        log.info("DataTranferController:getBankAccoListByTgAccountBalanceInfo:return{}",JSON.toJSONString(bankAccoList));
        result.put("data",bankAccoList);
        return R.ok(result);
    }



    /**
     * @return
     */
    @PostMapping("/dataDictionaryZxFndBasicInfoCy")
    @ApiOperation("用于中转查询资讯数据字典")
    public R dataDictionaryZxFndBasicInfoCy(){
        log.info("DataTranferController:dataDictionaryZxFndBasicInfoCy");
        List<DataDictionaryZxFndBasicInfoCyEntity> fndBasicInfoCyEntities = dataDictionaryZxFndBasicInfoCyService.listAll();

        Map map = new HashMap<String,List<String>>();
        map.put("data",fndBasicInfoCyEntities);
        return R.ok(map);
    }

    /**
     *
     * map： key是fundName，value是fundCode
     * @param admin
     * @param trustNo
     * @return
     */
    @PostMapping("/getFundCodeAndFundNameAndInnerFundCodeFromFundInfo")
    @ApiOperation("用于中转通过托管编码和租户查询对应基金基础信息")
    public R getFundCodeAndFundNameAndInnerFundCodeFromFundInfo(@RequestHeader("admin")String admin,
                                                @RequestHeader("trustNo")String trustNo){
        log.info("DataTranferController:getFundCodeAndFundNameAndInnerFundCodeFromFundInfo:admin={},trustNo={}", admin,trustNo);

        List<FundInfoEntity>  data= fundInfoService.getFundCodeAndFundNameAndInnerFundCodeFromFundInfo(admin,trustNo);

        Map<String,Object> result = new HashMap<>();
        result.put("data",data);
        return R.ok(result);
    }

    /**
     *
     * @return
     */
    @PostMapping("/saveInvokeRecord")
    @ApiOperation("用于中转保存调用上游数据信息")
    public R saveInvokeRecord(@RequestBody InvokeRecordEntity invokeRecordEntity){
        log.info("DataTranferController:saveInvokeRecord:{}",JSON.toJSONString(invokeRecordEntity));
        boolean b = invokeRecordService.saveInvokeRecord(invokeRecordEntity);

        if(b){
            return R.ok("success");
        }else {
            return R.error();
        }
    }

    @PostMapping("/mjAccountBalanceInfo")
    @ApiOperation("用于中转更新mjAccountBalanceInfo数据")
    public R mjAccountBalanceInfo(@RequestBody HashMap map){
        log.info("DataTranferController:mjAccountBalanceInfo:{}", JSON.toJSONString(map));
        /*List<MjAccountBalanceInfoEntity> MjAccountBalanceInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = mjAccountBalanceInfoService.tranferAndSaveDataBatch(MjAccountBalanceInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    @PostMapping("/mJAccountSeqInfo")
    @ApiOperation("用于中转更新mjAccountSeqInfo数据")
    public R mjAccountSeqInfo(@RequestBody HashMap map){
        log.info("DataTranferController:mjAccountSeqInfo:{}", JSON.toJSONString(map));
        /*List<MjAccountSeqInfoEntity> MjAccountSeqInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = mjAccountSeqInfoService.tranferAndSaveDataBatch(MjAccountSeqInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    @PostMapping("/custShareInfo")
    @ApiOperation("用于中转更新custShareInfo数据")
    public R custShareInfo(@RequestBody HashMap map){
        log.info("DataTranferController:custShareInfo:{}", JSON.toJSONString(map));
        /*List<CustShareInfoEntity> custShareInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = custShareInfoService.tranferAndSaveDataBatch(custShareInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }


    @PostMapping("/fundNetValueInfo")
    @ApiOperation("用于中转更新fundNetValueInfo数据")
    public R fundNetValueInfo(@RequestBody HashMap map){
        log.info("DataTranferController:map:{}", JSON.toJSONString(map));
        /*List<FundNetValueInfoEntity> fundNetValueInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = fundNetValueInfoService.tranferAndSaveDataBatch(fundNetValueInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    @PostMapping("/fundTradeInfo")
    @ApiOperation("用于中转更新fundTradeInfo数据")
    public R fundTradeInfo(@RequestBody HashMap map){
        log.info("DataTranferController:map:{}", JSON.toJSONString(map));
        /*List<FundTradeInfoEntity> fundTradeInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = fundTradeInfoService.tranferAndSaveDataBatch(fundTradeInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }
    @PostMapping("/fundOpenInfo")
    @ApiOperation("用于中转更新fundOpenInfo数据")
    public R fundOpenInfo(@RequestBody HashMap map){
        log.info("DataTranferController:investorInfo:{}", JSON.toJSONString(map));
        /*List<FundOpenInfoEntity> fundOpenInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = fundOpenInfoService.tranferAndSaveDataBatch(fundOpenInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }
    @PostMapping("/investorInfo")
    @ApiOperation("用于中转更新investorInfo数据")
    public R investorInfo(@RequestBody HashMap map){
        log.info("DataTranferController:investorInfo:{}", JSON.toJSONString(map));
        /*List<InvestorInfoEntity> investorInfoEntities= tranferUtils.getEntityDataByMap(map);
        R r = investorInfoService.tranferAndSaveDataBatch(investorInfoEntities);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }

    @PostMapping("/investorBankInfo")
    @ApiOperation("用于中转更新investorBankInfo数据")
    public R investorBankInfo(@RequestBody HashMap map){
        log.info("DataTranferController:investorBankInfo:{}", JSON.toJSONString(map));
        /*List<InvestorBankInfoEntity> investorBankInfoEntitys= tranferUtils.getEntityDataByMap(map);
        R r = investorBankInfoService.tranferAndSaveDataBatch(investorBankInfoEntitys);*/
        R r= tranferUtils.getEntityDataByMap(map);
        return r;
    }
}