
package com.tmerclub.cloud.user.controller.app;

import com.tmerclub.cloud.api.payment.vo.PaySettlementConfigVO;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.api.user.vo.UserApiVO;
import com.tmerclub.cloud.common.constant.ConfigNameConstant;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.Arith;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.user.constant.LevelChangeReasonEnum;
import com.tmerclub.cloud.user.constant.LevelTypeEnum;
import com.tmerclub.cloud.user.constant.RecruitStatusEnum;
import com.tmerclub.cloud.user.dto.UserLevelRechargeOrderDTO;
import com.tmerclub.cloud.user.mapper.UserLevelTermMapper;
import com.tmerclub.cloud.user.model.UserExtension;
import com.tmerclub.cloud.user.model.UserLevelLog;
import com.tmerclub.cloud.user.service.*;
import com.tmerclub.cloud.user.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 会员等级表
 *
 * @author YXF
 * @date 2020-12-08 10:16:53
 */
@RestController("appUserLevelController")
@RequestMapping("/userLevel")
@Tag(name = "app-会员等级表")
public class UserLevelController {

    @Autowired
    private UserLevelService userLevelService;

    @Autowired
    private UserExtensionService userExtensionService;

    @DubboReference
    private ConfigFeignClient configFeignClient;

    @Autowired
    private UserLevelLogService userLevelLogService;

    @Autowired
    private UserLevelTermService userLevelTermService;

    @Autowired
    private UserLevelTermMapper userLevelTermMapper;

    @Autowired
    private UserService userService;

    @GetMapping("/getMemberInfo")
    @Operation(summary = "获取用户会员中心信息", description = "获取用户会员中心信息")
    public ServerResponseEntity<UserMemberInfoVO> getMemberInfo() {
        Long userId = AuthUserContext.get().getUserId();
        UserApiVO user = userService.getByUserId(userId);
        // 查询用户基本信息名称加头像
        UserMemberInfoVO userMemberInfoVO = new UserMemberInfoVO();
        userMemberInfoVO.setNickName(user.getNickName());
        userMemberInfoVO.setPic(user.getPic());
        userMemberInfoVO.setExpireTime(user.getVipEndTime());
        // 查询用户扩展信息
        UserExtension userExtension = userExtensionService.getByUserId(userId);
        userMemberInfoVO.setScore(userExtension.getScore());
        userMemberInfoVO.setGrowth(userExtension.getGrowth());
        //设置用户的会员等级信息
        setLevelInformation(userMemberInfoVO, userExtension);
        userMemberInfoVO.setSignInCount(userExtension.getSignDay());
        // 查询成长值配置信息
        String growthConfigValue = configFeignClient.getConfig(ConfigNameConstant.GROWTH_CONFIG).getData();
        GrowthCompleteConfigVO growthCompleteConfigVO = Json.parseObject(growthConfigValue, GrowthCompleteConfigVO.class);
        userMemberInfoVO.setShopScore(growthCompleteConfigVO.getBuyOrder());

        return ServerResponseEntity.success(userMemberInfoVO);
    }

    @GetMapping("/ua/levelList")
    @Operation(summary = "会员等级列表信息", description = "会员等级列表信息")
    @Parameter(name = "levelType", description = "会员等级类型0为根据会员自身情况 1为付费", required = true)
    public ServerResponseEntity<List<UserLevelVO>> levelList(Integer levelType) {
        List<UserLevelVO> userLevelVOList = userLevelService.list(levelType);
        return ServerResponseEntity.success(userLevelVOList);
    }

    /**
     * 等级页展示
     *
     * @return 等级页展示
     */
    @GetMapping("/getLevelShow")
    @Operation(summary = "会员中心页展示", description = "会员中心页展示")
    public ServerResponseEntity<String> getLevelShow() {
        String growthDescriptionValue = configFeignClient.getConfig(ConfigNameConstant.MEMBER_GROWTH_DESCRIPTION).getData();
        if (Objects.isNull(growthDescriptionValue)) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.success(growthDescriptionValue);
    }


    @GetMapping("/getUserLevelTerm")
    @Operation(summary = "会员等级期数表", description = "会员等级期数表列表")
    @Parameter(name = "userLevelId", description = "用户会员等级id", required = true)
    public ServerResponseEntity<List<UserLevelTermVO>> getUserLevelTerm(Long userLevelId) {
        List<UserLevelTermVO> levelTermVOList = userLevelTermService.getAmountAndTypeByUserLevelId(userLevelId);
        int monthAmount = userLevelTermMapper.getMonthAmount(userLevelId);

        for (UserLevelTermVO levelTermVO : levelTermVOList) {
            switch (levelTermVO.getTermType()) {
                case 0:
                    levelTermVO.setOriginalPrice(monthAmount);
                    levelTermVO.setDiscount(Arith.div(levelTermVO.getNeedAmount(), levelTermVO.getOriginalPrice(), 2));
                    break;
                case 1:
                    levelTermVO.setOriginalPrice(monthAmount * 3);
                    levelTermVO.setDiscount(Arith.div(levelTermVO.getNeedAmount(), levelTermVO.getOriginalPrice(), 2));
                    break;
                case 2:
                    levelTermVO.setOriginalPrice(monthAmount * 12);
                    levelTermVO.setDiscount(Arith.div(levelTermVO.getNeedAmount(), levelTermVO.getOriginalPrice(), 2));
                    break;
                default:
                    break;
            }
        }
        return ServerResponseEntity.success(levelTermVOList);
    }

    @PostMapping("/buyVip")
    @Operation(summary = "购买付费会员", description = "付费 or 续费，成为付费会员")
    public ServerResponseEntity<Long> save(@Valid @RequestBody UserLevelRechargeOrderDTO userLevelRechargeOrderDTO) {

        // 从缓存获取用户充值信息
        UserLevelVO userLevelVO = userLevelService.getByUserLevelId(userLevelRechargeOrderDTO.getUserLevelId());
        if (userLevelVO == null || !Objects.equals(LevelTypeEnum.PAY_USER.value(), userLevelVO.getLevelType())) {
            return ServerResponseEntity.showFailMsg("订单异常，请刷新页面重试");
        }
        if (Objects.equals(userLevelVO.getRecruitStatus(), RecruitStatusEnum.STOP_RECRUIT.value())) {
            return ServerResponseEntity.showFailMsg("付费会员等级为：" + userLevelVO.getLevelName() + "，已停止招募");
        }

        List<UserLevelTermVO> userLevelTerms = userLevelVO.getUserLevelTerms();

        UserLevelTermVO userLevelTerm = null;
        for (UserLevelTermVO userLevelTermVo : userLevelTerms) {
            if (Objects.equals(userLevelTermVo.getLevelTermId(), userLevelRechargeOrderDTO.getLevelTermId())) {
                userLevelTerm = userLevelTermVo;
            }
        }
        if (userLevelTerm == null) {
            return ServerResponseEntity.showFailMsg("订单异常，请刷新页面重试");
        }

        Long userId = AuthUserContext.get().getUserId();
        // 缓存中获取用户信息
        UserApiVO user = userService.getByUserId(userId);
        // 已付费会员不能购买其他等级的会员，请到期后重新购买
        if (Objects.equals(user.getLevelType(), LevelTypeEnum.PAY_USER.value()) && !Objects.equals(user.getVipLevel(), userLevelVO.getLevel())) {
            return ServerResponseEntity.showFailMsg("已付费会员不能购买其他等级的会员，请到期后重新购买");
        }
        UserLevelVO userlevel = userLevelService.getByUserLevelId(userLevelRechargeOrderDTO.getUserLevelId());
        UserLevelLog userLevelLog = new UserLevelLog();
        userLevelLog.setUserId(userId);
        userLevelLog.setPayAmount(userLevelTerm.getNeedAmount());
        userLevelLog.setAfterLevel(userLevelVO.getLevel());
        userLevelLog.setAfterLevelType(userLevelVO.getLevelType());
        userLevelLog.setBeforeLevel(user.getVipLevel() == null ? user.getLevel() : user.getVipLevel());
        userLevelLog.setBeforeLevelType(user.getLevelType());
        userLevelLog.setIsPayed(0);
        userLevelLog.setLevelChangeReason(Objects.equals(user.getVipLevel(), userLevelVO.getLevel()) ? LevelChangeReasonEnum.RENEW_VIP.value() : LevelChangeReasonEnum.BUY_VIP.value());
        userLevelLog.setLevelIoType(Objects.equals(user.getVipLevel(), userLevelVO.getLevel()) ? 0 : 1);
        userLevelLog.setTermType(userLevelTerm.getTermType());
        userLevelLog.setLevelName(userlevel.getLevelName());
        // 补充支付系统类型
        ServerResponseEntity<String> configResponse = configFeignClient.getConfig(Constant.PAY_SETTLEMENT_CONFIG);
        if (!configResponse.isSuccess()) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        PaySettlementConfigVO paySettlementConfigVO = Json.parseObject(configResponse.getData(), PaySettlementConfigVO.class);
        userLevelLog.setPaySysType(paySettlementConfigVO.getPaySettlementType());
        userLevelLogService.save(userLevelLog);
        return ServerResponseEntity.success(userLevelLog.getLevelLogId());
    }

    @GetMapping("/rightsByLevelTypeList")
    @Operation(summary = "根据会员类型查权益", description = "0普通会员 1付费会员")
    @Parameter(name = "levelType", description = " 会员等级类型0为根据会员自身情况 1为付费")
    public List<UserRightsVO> rightsByLevelTypeList(Integer levelType) {
        return userLevelService.listRightsByLevelType(levelType, null);
    }

    private void setLevelInformation(UserMemberInfoVO userMemberInfoVO, UserExtension userExtension) {
        // 查询用户当前普通会员的等级信息
        UserLevelVO ordinaryLevel = userLevelService.getOneByTypeAndLevel(LevelTypeEnum.ORDINARY_USER.value(), userExtension.getLevel());
        userMemberInfoVO.setUserOrdinaryLevel(ordinaryLevel);
        //用户当前的付费会员信息
        if (Objects.nonNull(userExtension.getVipLevel()) && Objects.equals(userExtension.getLevelType(), LevelTypeEnum.PAY_USER.value())) {
            UserLevelVO payedLevel = userLevelService.getOneByTypeAndLevel(LevelTypeEnum.PAY_USER.value(), userExtension.getVipLevel());
            userMemberInfoVO.setUserPayedLevel(payedLevel);
        }
    }
}
