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.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())
                    .pkAwards(cuMember.getPkAwards())
                    .pkRangeAwards(cuMember.getPkRangeAwards())
                    .pkMaxAwards(cuMember.getPkMaxAwards())
                    .build()).getData();

            // 会员标记（0=正常会员）
            int memberSign = 0;

            // 账号控制
            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())
                    .sharePosterImage(cuMember.getSharePosterImage())
                    .build();
        }
        return null;
    }

    @Override
    public String getSharePosterImage() {
        return iCuMemberService.getSharePosterImage();
    }

}
