package com.hzs.retail.member.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.SysConstants;
import com.hzs.common.core.enums.EGrade;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.retail.member.service.IMemberRetailService;
import com.hzs.retail.member.vo.RetailMemberInfoVO;
import com.hzs.system.common.ICommonServiceApi;
import com.hzs.system.common.dto.RetailMemberDataDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 新零售会员服务
 */
@Service
public class MemberRetailServiceImpl implements IMemberRetailService {

    @Autowired
    private ICuMemberService iCuMemberService;

    @DubboReference
    ICommonServiceApi iCommonServiceApi;

    @Override
    public RetailMemberInfoVO getMemberInfo(Long pkMember) {
        CuMemberExt cuMember = iCuMemberService.getMenuMember(pkMember);

        if (null != cuMember) {
            // 调用system服务，获取国家、币种、等级、奖衔等数据
            RetailMemberDataDTO memberDataDTO = iCommonServiceApi.getRetailMemberData(RetailMemberDataDTO.builder()
                    .pkSettleCountry(CountryConstants.CHINA_COUNTRY)
                    .pkGrade(cuMember.getPkSettleGrade())
                    .build()).getData();

            // 会员标记（0=正常会员，1=V0会员，2=V5会员）
            // 正常会员： 指V1以上，可以查看所有功能
            // V0会员： 指V0级别，只有会员专区
            // V5会员： 指V5级别，只有V5显示市场动态等
            int memberSign = 0;
            if (EGrade.START_UP.getValue() == memberDataDTO.getGradeValue()) {
                memberSign = 1;
            }
            if (EGrade.S_VIP.getValue() == memberDataDTO.getGradeValue()) {
                memberSign = 2;
            }

            // 账号控制
            Integer control = null;
            if (SysConstants.CONTROL_MEMBER_LIST.contains(cuMember.getMemberCode())) {
                control = 0;
            }

            // 证件号码
            String idCardAuth = null;
            if (StringUtils.isNotEmpty(cuMember.getIdCard())) {
                idCardAuth = DesensitizedUtil.idCardNum(cuMember.getIdCard(), 6, 6);
            }

            return RetailMemberInfoVO.builder()
                    .memberCode(cuMember.getMemberCode())
                    .memberName(cuMember.getMemberName())
                    .nickName(cuMember.getNickName())
                    .phone(cuMember.getPhone())
                    .headPath(cuMember.getHeadPath())
                    .settleCountryCircularIcon(memberDataDTO.getSettleCountryCircularIcon())
                    .settleCountrySquareIcon(memberDataDTO.getSettleCountrySquareIcon())
                    .exchangeRate(memberDataDTO.getExchangeRate())
                    .pkGradeVal(memberDataDTO.getGradeVal())
                    .gradeIcon(memberDataDTO.getGradeIcon())
//                    .pkAwardsVal(memberDataDTO.getAwardsVal())
//                    .awardsIcon(memberDataDTO.getAwardsIcon())
//                    .pkMaxAwardsVal(memberDataDTO.getMaxAwardsVal())
//                    .maxAwardsIcon(memberDataDTO.getMaxAwardsIcon())
//                    .pkRangeAwardsVal(memberDataDTO.getRangeAwardsVal())
//                    .rangeAwardsIcon(memberDataDTO.getRangeAwardsIcon())
                    .currencyIcon(memberDataDTO.getCurrencyIcon())
                    .memberSign(memberSign)
                    .control(control)
                    .creationTime(cuMember.getCreationTime())
                    .idCardAuth(idCardAuth)
                    .systemType(cuMember.getSystemType())
                    .pkCountry(cuMember.getPkCountry())
                    .pkSettleCountry(cuMember.getPkSettleCountry())
                    .isActivate(cuMember.getIsActivate())
                    .areaName(cuMember.getAreaName())
                    .build();
        }
        return null;
    }

}
