package com.hzs.member.base.provider;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
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.domain.member.detail.CuMemberServiceLog;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.base.ICuAwardsControlServiceApi;
import com.hzs.member.base.dto.CuAwardsControlDTO;
import com.hzs.member.base.service.ICuMemberAchieveLogService;
import com.hzs.member.detail.service.ICuMemberServiceLogService;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
import com.hzs.system.config.IServiceChargeServiceApi;
import com.hzs.system.config.dto.ServiceChargeDTO;
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;

/**
 * 真实奖衔控制涉及dubbo服务
 */
@DubboService
public class CuAwardsControlServiceProvider implements ICuAwardsControlServiceApi {

    @Autowired
    private ICuMemberServiceLogService memberServiceLogService;
    @Autowired
    private ICuMemberAchieveLogService cuMemberAchieveLogService;
    @DubboReference
    private IServiceChargeServiceApi serviceChargeServiceApi;
    @Autowired
    private ICuMemberBaseService cuMemberBaseService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @Override
    public R<List<CuAwardsControlDTO>> awardsControlList(AnalysisBusinessAppealParam analysisBusinessAppealParam) {
        CuMemberServiceLog memberServiceLog = new CuMemberServiceLog();
        List<CuAwardsControlDTO> list = new ArrayList<>();
        memberServiceLog.setBusinessNo(analysisBusinessAppealParam.getBusinessNo());
        QueryWrapper<CuMemberServiceLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BUSINESS_NO", analysisBusinessAppealParam.getBusinessNo());
        List<CuMemberServiceLog> msList = memberServiceLogService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(msList)) {
            //数据库内容翻译
            Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            Integer pkCountry = msList.get(0).getPkCountry();
            for (CuMemberServiceLog cs : msList) {
                Integer maxSecond = cuMemberAchieveLogService.selectByMaxApproveNumber(cs.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);
                CuAwardsControlDTO control = JSONObject.parseObject(cs.getNewValue(), CuAwardsControlDTO.class);
                control.setCost(serviceChargeDTOR.getData().getCost());
                control.setPkAccount(serviceChargeDTOR.getData().getPkAccount());
                Integer pkAccountKey = cuMemberBaseService.translateAccountValue(serviceChargeDTOR.getData().getPkAccount());
                control.setPkAccountKeyVal(transactionMap.get(pkAccountKey));
                control.setPkMember(cs.getPkMember());
                list.add(control);
            }
        }
        return R.ok(list);
    }
}
