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

import com.friday.custody.dto.TenantEscrowAuthorizationInfoDto;
import com.friday.custody.entity.MjAccountSeqInfoEntity;
import com.friday.custody.service.basicService.impl.MjAccountSeqInfoServiceImpl;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
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.*;

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

    private List<MjAccountSeqInfoEntity> getMJAccountSeqInfoData(TenantEscrowAuthorizationInfoDto tenant, List<Object> remoteDatas, String trustNo) {
        //1、获取字典表数据
        //2、封装数据
        List<MjAccountSeqInfoEntity> result = new ArrayList<>();
        HashMap<String, String> remoteDataMap;
        Date now = new Date();
        for (Object remoteData : remoteDatas) {
            remoteDataMap = (HashMap<String, String>) remoteData;
            String paymentTime = remoteDataMap.get("tradeDate");
            String transferDate = null;
            String transferTime = null;
            //1 todo transferDate 格式错误 （格式：2017-04-26 16:53:14.0）
            if (StringUtils.isNotBlank(paymentTime)) {
                String[] split = paymentTime.split(" ");
                transferDate = split[0].replace("-", "");
                transferTime = split[1].split("\\.")[0];
            }
            result.add(MjAccountSeqInfoEntity.builder()
                    .opAccountName(remoteDataMap.get("accountName"))
                    .opAccount(remoteDataMap.get("accountNumber"))
                    .fundCode(remoteDataMap.get("amacCode"))
                    .bankName(remoteDataMap.get("collectBank"))
                    .account(remoteDataMap.get("collectNumber"))
                    .updateDate(DateFormaterUtil.removeZero(remoteDataMap.get("createTime")))
                    .summary(remoteDataMap.get("digest"))
                    .innerFundCode(remoteDataMap.get("fundCode"))
                    .fundName(remoteDataMap.get("fundName"))
                    .seqNo(StrUtil.getSeqNoWithoutNoRandom(remoteDataMap.get("id")))
                    .amount(BigDecimalUtil.obj2BigDecimal(remoteDataMap.get("money")))
                    .remark(remoteDataMap.get("remark"))
                    .transferDate(transferDate)
                    .transferTime(DateFormaterUtil.removeZero(transferTime))
                    .transferInOut("1")
                    .trustNo(trustNo)
                    .admin(tenant.getTenantName())
                    .createTime(now)
                    .updateTime(now)
                    .build());
        }
        return result;

    }
}
