package com.hzs.system.common.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EAwards;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.domain.system.base.BdCurrency;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.base.service.IBdCountryService;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.common.ICommonServiceApi;
import com.hzs.system.common.dto.MemberDataDTO;
import com.hzs.system.common.dto.RetailMemberDataDTO;
import com.hzs.system.config.service.IBdAwardsService;
import com.hzs.system.config.service.IBdGradeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 公用dubbo接口
 */
@DubboService
public class CommonServiceProvider implements ICommonServiceApi {

    @Autowired
    private IBdCountryService iBdCountryService;
    @Autowired
    private IBdGradeService iBdGradeService;
    @Autowired
    private IBdAwardsService iBdAwardsService;
    @Autowired
    private IBdCurrencyService iBdCurrencyService;
    @Autowired
    private ICurrencyServiceApi iCurrencyServiceApi;

    @Override
    public R<MemberDataDTO> getMemberData(MemberDataDTO memberDataDTO) {
        // 自然国
        if (null != memberDataDTO.getPkCountry()) {
            BdCountry bdCountry = iBdCountryService.getCountry(memberDataDTO.getPkCountry());
            if (null != bdCountry) {
                memberDataDTO.setPkCountryVal(bdCountry.getShortName());
                memberDataDTO.setCountryCircularIcon(bdCountry.getNationalFlag1());
                memberDataDTO.setCountrySquareIcon(bdCountry.getNationalFlag2());
                memberDataDTO.setPkSettleCountryVal(bdCountry.getShortName());
            }
        }
        // 结算国
        if (null != memberDataDTO.getPkSettleCountry()) {
            BdCountry bdSettleCountry = iBdCountryService.getCountry(memberDataDTO.getPkSettleCountry());
            if (null != bdSettleCountry) {
                memberDataDTO.setPkSettleCountryVal(bdSettleCountry.getShortName());
                memberDataDTO.setSettleCountryCircularIcon(bdSettleCountry.getNationalFlag1());
                memberDataDTO.setSettleCountrySquareIcon(bdSettleCountry.getNationalFlag2());
                memberDataDTO.setSettleCountryImg(bdSettleCountry.getImg());
                memberDataDTO.setPkSettleCountryVal(bdSettleCountry.getShortName());
            }

            // 汇率
            CurrencyDTO currencyDTO = iCurrencyServiceApi.getCurrency(memberDataDTO.getPkSettleCountry()).getData();
            memberDataDTO.setExchangeRate(currencyDTO.getInExchangeRate());
        }

        // 等级
        if (null != memberDataDTO.getPkGrade()) {
            BdGrade bdGrade = iBdGradeService.getBdGrade(memberDataDTO.getPkGrade());
            if (null != bdGrade) {
                memberDataDTO.setPkGrade(bdGrade.getPkTransaction());
                memberDataDTO.setGradeValue(bdGrade.getGradeValue());
                memberDataDTO.setGradeIcon(bdGrade.getImage());
            }
        }
        // 奖衔
        if (null != memberDataDTO.getPkAwards()) {
            BdAwards bdAwards = iBdAwardsService.getBdAwardsOne(memberDataDTO.getPkAwards());
            if (null != bdAwards) {
                memberDataDTO.setPkAwards(bdAwards.getPkTransaction());
                if (EAwards.MEMBER.getValue() != bdAwards.getAwardsValue()) {
                    // 奖衔如果是会员级别不返回图标
                    memberDataDTO.setAwardsIcon(bdAwards.getImage());
                }
            }
        }
        // 荣誉奖衔
        if (null != memberDataDTO.getPkMaxAwards()) {
            BdAwards bdAwards = iBdAwardsService.getBdAwardsOne(memberDataDTO.getPkMaxAwards());
            if (null != bdAwards) {
                memberDataDTO.setPkMaxAwards(bdAwards.getPkTransaction());
                if (EAwards.MEMBER.getValue() != bdAwards.getAwardsValue()) {
                    // 奖衔如果是会员级别不返回图标
                    memberDataDTO.setMaxAwardsIcon(bdAwards.getImage());
                }
            }
        }
        // 币种
        if (null != memberDataDTO.getPkSettleCountry()) {
            BdCurrency bdCurrency = iBdCurrencyService.getCurrency(memberDataDTO.getPkSettleCountry());
            if (null != bdCurrency) {
                memberDataDTO.setCurrencyIcon(bdCurrency.getSymbol());
            }
        }

        return R.ok(memberDataDTO);
    }

    @Override
    public R<RetailMemberDataDTO> getRetailMemberData(RetailMemberDataDTO memberDataDTO) {
        // 国家
        if (null != memberDataDTO.getPkSettleCountry()) {
            BdCountry bdSettleCountry = iBdCountryService.getCountry(memberDataDTO.getPkSettleCountry());
            if (null != bdSettleCountry) {
                memberDataDTO.setPkSettleCountryVal(bdSettleCountry.getShortName());
                memberDataDTO.setSettleCountryCircularIcon(bdSettleCountry.getNationalFlag1());
                memberDataDTO.setSettleCountrySquareIcon(bdSettleCountry.getNationalFlag2());
            }

            // 汇率
            CurrencyDTO currencyDTO = iCurrencyServiceApi.getCurrency(memberDataDTO.getPkSettleCountry()).getData();
            memberDataDTO.setExchangeRate(currencyDTO.getInExchangeRate());
        }

        // 等级
        if (null != memberDataDTO.getPkGrade()) {
            BdGrade bdGrade = iBdGradeService.getBdGrade(memberDataDTO.getPkGrade());
            if (null != bdGrade) {
                memberDataDTO.setGradeValue(bdGrade.getGradeValue());
                memberDataDTO.setGradeIcon(bdGrade.getImage());
                memberDataDTO.setGradeVal(bdGrade.getGradeName());
            }
        }
        // 当月奖衔
//        if (null != memberDataDTO.getPkAwards()) {
//            BdAwards bdAwards = iBdAwardsService.getBdAwardsOne(memberDataDTO.getPkAwards());
//            if (null != bdAwards) {
//                memberDataDTO.setAwardsVal(bdAwards.getAwardsName());
//                if (EAwards.MEMBER.getValue() != bdAwards.getAwardsValue()) {
//                    // 奖衔如果是会员级别不返回图标
//                    memberDataDTO.setAwardsIcon(bdAwards.getImage());
//                }
//            }
//        }
//        // 荣誉奖衔
//        if (null != memberDataDTO.getPkMaxAwards()) {
//            BdAwards bdAwards = iBdAwardsService.getBdAwardsOne(memberDataDTO.getPkMaxAwards());
//            if (null != bdAwards) {
//                if (EAwards.MEMBER.getValue() == bdAwards.getAwardsValue()) {
//                    memberDataDTO.setMaxAwardsVal(memberDataDTO.getAwardsVal());
//                    memberDataDTO.setMaxAwardsIcon(memberDataDTO.getAwardsIcon());
//                } else {
//                    memberDataDTO.setMaxAwardsVal(bdAwards.getAwardsName());
//                    memberDataDTO.setMaxAwardsIcon(bdAwards.getImage());
//                }
//            }
//        }
//        // 分红奖衔
//        if (null != memberDataDTO.getPkRangeAwards()) {
//            BdAwards bdAwards = iBdAwardsService.getBdAwardsOne(memberDataDTO.getPkRangeAwards());
//            if (null != bdAwards) {
//                if (bdAwards.getAwardsValue() > EAwards.HIGH_MANAGER.getValue()) {
//                    // 前4级不显示分红奖衔
//                    memberDataDTO.setRangeAwardsVal(bdAwards.getAwardsName());
//                    memberDataDTO.setRangeAwardsIcon(bdAwards.getImage());
//                }
//            }
//        }
        // 币种
        if (null != memberDataDTO.getPkSettleCountry()) {
            BdCurrency bdCurrency = iBdCurrencyService.getCurrency(memberDataDTO.getPkSettleCountry());
            if (null != bdCurrency) {
                memberDataDTO.setCurrencyIcon(bdCurrency.getSymbol());
            }
        }

        return R.ok(memberDataDTO);
    }

}
