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

import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.enums.EGrade;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.self.service.ICuSelfServiceLogService;
import com.hzs.member.self.vo.SelfVO;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 自助服务控制类
 */
@Slf4j
@RequestMapping("/api/self")
@RestController
public class ApiSelfController extends BaseController {

    @Autowired
    private ICuMemberService cuMemberService;
    @Autowired
    private ICuSelfServiceLogService cuSelfServiceLogService;

    @DubboReference
    IGradeServiceApi iGradeServiceApi;

    /**
     * 自助权限查询
     */
    @GetMapping("/authority")
    public AjaxResult validateSelfAuthority() {
        CuMemberExt cuMember = cuMemberService.getMenuMember(SecurityUtils.getUserId());

        // 获取等级
        GradeDTO gradeDTO = iGradeServiceApi.getGrade(cuMember.getPkSettleGrade()).getData();
        if (EGrade.START_UP.getValue() == gradeDTO.getGradeValue()) {
            // 会员等级，需要【实名认证】
            return AjaxResult.success(SelfVO.builder()
                    .existRealName(EYesNo.YES.getIntValue())
                    .build());
        }

        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer showAddress = cuSelfServiceLogService.showHiddenAddress(cuMember.getPkId(), pkCountry);
        SelfVO selfVO = SelfVO.builder()
                .existAddress(showAddress)
                .existOrderSelf(showAddress)
                .selfRevokeOrder(showAddress)
                .existDomicile(cuSelfServiceLogService.showIndexDomicile(pkCountry, cuMember.getPkId()))
                .existRealName(EYesNo.YES.getIntValue())
                .existMemberMonth(EYesNo.YES.getIntValue())
                .build();
        if (!pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            selfVO.setSelfRevokeOrder(EYesNo.NO.getIntValue());
        }

        return AjaxResult.success(selfVO);
    }
}
