package com.hzs.member.base.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.base.ICuMemberAchieveLogServiceApi;
import com.hzs.member.base.dto.CuMemberAchieveLogDTO;
import com.hzs.member.base.service.ICuMemberAchieveLogService;
import com.hzs.member.base.vo.CuMemberAchieveLogVO;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
import com.hzs.system.config.IServiceChargeServiceApi;
import com.hzs.system.config.dto.ServiceChargeDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@DubboService
public class ICuMemberAchieveLogServiceProvider implements ICuMemberAchieveLogServiceApi {

    @DubboReference
    IServiceChargeServiceApi serviceChargeServiceApi;

    @Autowired
    private ICuMemberAchieveLogService achieveLogService;
    @Autowired
    private ICuMemberAchieveLogService cuMemberAchieveLogService;
    @Autowired
    private ICuMemberBaseService cuMemberBaseService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @Override
    public R<List<CuMemberAchieveLogDTO>> memberAchieveLog(AnalysisBusinessAppealParam analysisBusinessAppealParam) {
        CuMemberAchieveLogVO cuMemberAchieveLogVO = new CuMemberAchieveLogVO();
        List<CuMemberAchieveLogDTO> list = new ArrayList<>();
        cuMemberAchieveLogVO.setBusinessNo(analysisBusinessAppealParam.getBusinessNo());
        List<CuMemberAchieveLogVO> list1 = achieveLogService.queryTryAchieveLogList(cuMemberAchieveLogVO);
        if (CollectionUtil.isNotEmpty(list1)) {
            //数据库内容翻译
            Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            Integer pkCountry = list1.get(0).getPkCountry();
            for (CuMemberAchieveLogVO cmal : list1) {
                Integer maxSecond = cuMemberAchieveLogService.selectByMaxApproveNumber(cmal.getPkMember(), EApproveStatus.FINISH.getValue());
                //查询扣费金额，账户信息
                ServiceChargeDTO serviceChargeDTO = new ServiceChargeDTO();
                serviceChargeDTO.setType(EApprovalBusiness.MEMBER_ACHIEVE.getValue());
                serviceChargeDTO.setQuantity(maxSecond + 1);
                serviceChargeDTO.setPkCountry(pkCountry);
                R<ServiceChargeDTO> serviceChargeDTOR = serviceChargeServiceApi.getServiceChargeByType(serviceChargeDTO);
                //接口查询金额是否足够
                cmal.setCost(serviceChargeDTOR.getData().getCost());
                cmal.setPkAccount(serviceChargeDTOR.getData().getPkAccount());
                Integer pkAccountKey = cuMemberBaseService.translateAccountValue(serviceChargeDTOR.getData().getPkAccount());
                cmal.setPkAccountKeyVal(transactionMap.get(pkAccountKey));
                cmal.setSettleDateStr(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cmal.getSettleDate()));
                cmal.setDueDateStr(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cmal.getDueDate()));
            }
        }
        list1.forEach(item -> list.add(BeanUtil.copyProperties(item, CuMemberAchieveLogDTO.class)));

        return R.ok(list);
    }

}
