package com.hzs.member.base.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.core.utils.StringUtils;
import com.hzs.common.domain.member.detail.CuMemberAchieveLog;
import com.hzs.common.security.utils.SecurityUtils;
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.detail.service.ICuMemberServiceLogService;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
import com.hzs.member.handlebusiness.service.ICuHandleBusinessService;
import com.hzs.system.base.ICountryServiceApi;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IGradeServiceApi;
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;

/**
 * @description:
 * @author: zhang jing
 * @date: 2023/3/28 16:57
 * @param:
 * @return:
 **/
@Slf4j
@DubboService
public class ICuMemberAchieveLogServiceProvider implements ICuMemberAchieveLogServiceApi {

    @Autowired
    private ICuMemberServiceLogService memberServiceLogService;
    @Autowired
    private ICuHandleBusinessService handleBusinessService;
    @DubboReference
    private IGradeServiceApi gradeServiceApi;
    @DubboReference
    private IAwardsServiceApi awardsServiceApi;
    @DubboReference
    private ICountryServiceApi countryServiceApi;
    @Autowired
    private ICuMemberAchieveLogService achieveLogService;
    @Autowired
    private ICuMemberAchieveLogService cuMemberAchieveLogService;
    @DubboReference
    private IServiceChargeServiceApi serviceChargeServiceApi;
    @Autowired
    private ICuMemberBaseService cuMemberBaseService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;
    /**
     * 解析注水业绩诉求信息
     *
     * @param analysisBusinessAppealParam 业务信息
     * @return R
     */
    @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.enumTransactionByDBToTag(analysisBusinessAppealParam.getToLanguageTag(),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);
    }



}
