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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.AccountInfoEntity;
import com.friday.custody.entity.TgAccountSeqInfoEntity;
import com.friday.custody.service.basicService.impl.AccountInfoServiceImpl;
import com.friday.custody.service.basicService.impl.TgAccountSeqInfoServiceImpl;
import com.friday.custody.service.trustService.guosen.common.GuosenConstants;
import com.friday.custody.service.trustService.guosen.dto.InvokeGuosenRecordDto;
import com.friday.custody.service.trustService.guosen.service.ProcessGuosenDataService;
import com.friday.custody.utils.BigDecimalUtil;
import com.friday.custody.utils.DateFormaterUtil;
import com.friday.custody.utils.StrUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service(GuosenConstants.ACCOUNT_HISTORY_DETAIL_LIST_V2_API)
public class ProcessGuosenTGAccountHistorySeqInfoServiceImpl implements ProcessGuosenDataService {
    @Autowired
    private TgAccountSeqInfoServiceImpl tgAccountSeqInfoService;//f
    @Autowired
    private AccountInfoServiceImpl accountInfoService;
    @Override
    public void processData(InvokeGuosenRecordDto dto) {
        if (!CollectionUtils.isEmpty(dto.getRemoteDatas())) {
            //3、封装成标准表的对象
            List<TgAccountSeqInfoEntity> entities = getTGAccountHistorySeqInfoData(dto.getTenant(),dto.getRemoteDatas() , dto.getTrustNo());
            //4、调用 basicservice 的保存数据接口
            tgAccountSeqInfoService.saveData(null, entities);
            //todo  入库到基金净值信息表（fund_net_value_info）时，需要此响应结果的产品代码、产品名称和估值日期与表中基金内部代码、基金名称和净值日期一致且响应结果中科目代码为“证券投资合计:”的数据，更新表中的资产合计total_asset字段为该值
        }
    }


    private List<TgAccountSeqInfoEntity> getTGAccountHistorySeqInfoData(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo) {
        //1、获取字典表数据
        //2、封装数据
        List<TgAccountSeqInfoEntity> result = new ArrayList<>();
        HashMap<String, String> remoteDataMap;
        Date now = new Date();
        for (Object remoteData : remoteDatas) {
            remoteDataMap = (HashMap<String, String>) remoteData;
            String amount;
            String transferInOut;
            if (!StringUtils.isBlank(remoteDataMap.get("collectionMoney"))) {
                amount = remoteDataMap.get("collectionMoney");
                transferInOut = "1";
            } else {
                amount = remoteDataMap.get("paymentMoney");
                transferInOut = "2";
            }
            AccountInfoEntity entity = accountInfoService.queryParam(remoteDataMap.get("collectNumber"),trustNo);
            result.add(TgAccountSeqInfoEntity.builder()
                    .fundCode(entity.getFundCode())
                    .innerFundCode(entity.getInnerFundCode())
                    .fundName(entity.getFundName())
                    .bankName(entity.getBranchBankName())
                    .accountName(entity.getNameInBank())
                    .transferTime("00:00:00")
                    .seqNo(StrUtil.getSeqNoWithoutNoRandom(remoteDataMap.get("id")))
                    .account(remoteDataMap.get("collectNumber"))
                    .opAccountName(remoteDataMap.get("paymentAccount"))
                    .opAccount(remoteDataMap.get("paymentNumber"))
                    .amount(BigDecimalUtil.obj2BigDecimal(amount))
                    .transferInOut(transferInOut)
                    .balance(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("accountBalance")))
                    .summary(remoteDataMap.get("description"))
                    .transferDate(remoteDataMap.get("occurrenceTime"))
                    .updateDate(DateFormaterUtil.formatterDateTime(remoteDataMap.get("recordTime"),"yyyyMMdd HHmmss","yyyy-MM-dd HH:mm:ss"))
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;

    }
}
