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

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.detail.CuMemberChangeLog;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.base.service.IMemberInfoService;
import com.hzs.member.base.vo.MemberInfoVO;
import com.hzs.member.detail.service.ICuMemberChangeLogService;
import com.hzs.system.common.ICommonServiceApi;
import com.hzs.system.common.dto.MemberDataDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员信息服务
 */
@Service
public class MemberInfoServiceImpl implements IMemberInfoService {

    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuMemberChangeLogService iCuMemberChangeLogService;

    @DubboReference
    ICommonServiceApi iCommonServiceApi;

    @Override
    public MemberInfoVO getMemberInfo(Long memberId) {
        if (null == memberId) {
            return null;
        }

        CuMemberExt cuMember = iCuMemberService.getMenuMember(memberId);
        if (null != cuMember) {
            // 调用system服务，获取国家、币种、等级、奖衔等数据
            MemberDataDTO memberDataDTO = iCommonServiceApi.getMemberData(MemberDataDTO.builder()
                    .pkCountry(cuMember.getPkCountry())
                    .pkSettleCountry(cuMember.getPkSettleCountry())
                    .pkGrade(cuMember.getPkSettleGrade())
                    .pkAwards(cuMember.getPkAwards())
                    .pkMaxAwards(cuMember.getPkMaxAwards())
                    .build())
                    .getData();

            BigDecimal consumeAchieve = cuMember.getConsumeAchieve();
            consumeAchieve = consumeAchieve.multiply(memberDataDTO.getExchangeRate());

            // 会员名称，优先取昵称，没有取名称
            String memberName = StringUtils.isNotEmpty(cuMember.getNickName()) ? cuMember.getNickName() : cuMember.getMemberName();
            // 自然国家
            String pkCountryVal = memberDataDTO.getPkCountryVal();
            // 结算国家
            String pkSettleCountryVal = memberDataDTO.getPkSettleCountryVal();

            // 封装返回信息
            return MemberInfoVO.builder()
                    .memberCode(cuMember.getMemberCode())
                    .memberName(memberName)
                    .registerTime(cuMember.getPayTime())
                    .pkCountry(cuMember.getPkCountry())
                    .pkCountryVal(pkCountryVal)
                    .countryCircularIcon(memberDataDTO.getCountryCircularIcon())
                    .countrySquareIcon(memberDataDTO.getCountrySquareIcon())
                    .pkSettleCountry(cuMember.getPkSettleCountry())
                    .pkSettleCountryVal(pkSettleCountryVal)
                    .settleCountryCircularIcon(memberDataDTO.getSettleCountryCircularIcon())
                    .settleCountrySquareIcon(memberDataDTO.getSettleCountrySquareIcon())
                    .settleCountryLightName(memberDataDTO.getSettleCountryLightName())
                    .settleCountryImg(memberDataDTO.getSettleCountryImg())
                    .pkGradeId(cuMember.getPkSettleGrade())
                    .pkGrade(memberDataDTO.getPkGrade())
                    .gradeIcon(memberDataDTO.getGradeIcon())
                    .pkAwardsId(cuMember.getPkAwards())
                    .pkAwards(memberDataDTO.getPkAwards())
                    .awardsIcon(memberDataDTO.getAwardsIcon())
                    .pkMaxAwardsId(cuMember.getPkMaxAwards())
                    .pkMaxAwards(memberDataDTO.getPkMaxAwards())
                    .maxAwardsIcon(memberDataDTO.getMaxAwardsIcon())
                    .currencyIcon(memberDataDTO.getCurrencyIcon())
                    .registerAuthority(cuMember.getRegisterAuthority())
                    .headPath(cuMember.getHeadPath())
                    .consumeAchieve(consumeAchieve)
                    .payTime(cuMember.getPayTime())
                    .build();
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateMemberInfo(CuMember cuMember, List<CuMemberChangeLog> cuMemberChangeLogList) {
        // 插入会员修改记录
        if (CollectionUtil.isNotEmpty(cuMemberChangeLogList)) {
            iCuMemberChangeLogService.saveBatch(cuMemberChangeLogList);
        }

        // 更新会员信息
        if (null != cuMember) {
            iCuMemberService.updateById(cuMember);
        }
    }

}
