package com.hzs.member.base.controller.api;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.base.vo.AwardsVo;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.member.base.controller.api
 * @Author: yh
 * @CreateTime: 2023-03-30  10:45
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/member-footmark")
public class MemberFootmarkController {

    @DubboReference
    private IGradeServiceApi gradeServiceApi;
    @DubboReference
    private IAwardsServiceApi awardsServiceApi;
    @DubboReference
    private ICurrencyServiceApi currencyServiceApi;
    @Autowired
    private ICuMemberService memberService;

    /**
     * 成长之路 奖衔
     */
    @GetMapping("grow-up-footmark-awards")
    public AjaxResult growUpFootmarkAwards() {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        CuMember member = memberService.getMember(userId);
        Integer pkAwards = member.getPkAwards();
        R<CurrencyDTO> currencyDto = currencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
        CurrencyDTO currency = currencyDto.getData();
        R<BdAwards> awardsOne = awardsServiceApi.getAwardsOne(pkAwards);

        //  R<List<BdAwards>> awardsDto = awardsServiceApi.queryAwards(pkCountry);
        R<List<BdAwards>> awardsDto = awardsServiceApi.queryByPkCountryAwards(pkCountry);
        // 奖衔级别
        Integer awardsValue = awardsOne.getData().getAwardsValue();
        List<BdAwards> awardsList = awardsDto.getData();
        // 子级名称
        String childLevelName = "";
        // 累计业绩
        BigDecimal achievement = BigDecimal.ZERO;
        // 用户头像
        String thisAvatarUrl = "";
        List<AwardsVo> resultList = new ArrayList<>();
        for (BdAwards bdAwards : awardsList) {
            // 下一级别
            if (bdAwards.getAwardsValue() == (awardsValue - 5)) {
                childLevelName = bdAwards.getAwardsName();

            }
            if (bdAwards.getAwardsValue() > (awardsValue)) {
                bdAwards.setColor("#C0C1C1");
            }

            if (pkAwards.equals(bdAwards.getPkId())) {
                achievement = bdAwards.getCommunityCheck();
                thisAvatarUrl = bdAwards.getImage();
            }


            AwardsVo awardsVo = BeanUtil.copyProperties(bdAwards, AwardsVo.class);
            // 小区业绩
            BigDecimal communityCheck = bdAwards.getCommunityCheck().multiply(currency.getInExchangeRate());
            if (communityCheck.compareTo(new BigDecimal(10000)) > 0) {
                awardsVo.setCommunityCheckStr(communityCheck.divide(new BigDecimal(10000)).stripTrailingZeros().toPlainString() + "万");
            } else {
                awardsVo.setCommunityCheckStr(communityCheck + "");
            }
            resultList.add(awardsVo);
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("childLevelName", childLevelName);
        // 排序
        resultMap.put("awardsList", new ArrayList<>(resultList));
        resultMap.put("thisAwardsName", awardsOne.getData().getAwardsName());
        resultMap.put("thisPkAwards", awardsOne.getData().getPkId());
        resultMap.put("achievement", achievement);
        resultMap.put("thisAvatarUrl", thisAvatarUrl);
        return AjaxResult.success(resultMap);
    }

    /**
     * 成长之路 等级
     */
    @GetMapping("grow-up-footmark-grade")
    public AjaxResult growUpFootmarkGrade() {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        CuMember member = memberService.getMember(userId);
        Integer grade = member.getPkSettleGrade();
        // 当前等级
        String thisGradeName = "";
        // 用户头像
        String thisAvatarUrl = "";
        // 色值
        String color = "";
        // 国际编号
        String globalCode = member.getGlobalCode();
        // 国家编号
        String countryCode = member.getCountryCode();
        //   R<List<GradeDTO>> gradeList = gradeServiceApi.queryGradeConfigByCondition(pkCountry);
        R<CurrencyDTO> currencyDate = currencyServiceApi.getCurrency(pkCountry);
        CurrencyDTO currency = currencyDate.getData();
        R<GradeDTO> gradeDto = gradeServiceApi.getGrade(grade);
        R<List<BdGrade>> gradeList = gradeServiceApi.queryGrowUpFootmarkGrade(pkCountry);

        for (BdGrade g : gradeList.getData()) {

            if (grade.equals(g.getPkId())) {
                thisGradeName = g.getGradeName();
                thisAvatarUrl = g.getImage();
                color = g.getColor();

            }
            if (g.getGradeValue() > gradeDto.getData().getGradeValue()) {
                g.setColor("#C0C1C1");
            }
            // 计算人民币业绩
            g.setStartValue(g.getStartValue().multiply(currency.getInExchangeRate()));
        }

        Map<String, Object> resultMap = new HashMap<>();
        // 国际编号
        resultMap.put("globalCode", globalCode);
        // 国家编号
        resultMap.put("countryCode", countryCode);
        // 颜色
        resultMap.put("color", color);
        // 等级
        resultMap.put("gradeList", gradeList.getData().stream());
        // 当前等级
        resultMap.put("thisGradeName", thisGradeName);
        // 编号
        resultMap.put("memberCode", member.getMemberCode());
        // 会员名称
        resultMap.put("memberName", member.getMemberName());
        // 业绩
        resultMap.put("achievement", member.getConsumeAchieve());
        // 头像
        resultMap.put("thisAvatarUrl", thisAvatarUrl);
        return AjaxResult.success(resultMap);
    }

}
