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

import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.user.constant.LevelTypeEnum;
import com.tmerclub.cloud.user.dto.UserAdminDTO;
import com.tmerclub.cloud.user.dto.UserLevelDTO;
import com.tmerclub.cloud.user.service.UserLevelService;
import com.tmerclub.cloud.user.service.UserService;
import com.tmerclub.cloud.user.vo.UserLevelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
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("platformUserLevelController")
@RequestMapping("/p/userLevel")
@Tag(name = "平台-会员等级表")
public class UserLevelController {

    @Autowired
    private UserLevelService userLevelService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    @Operation(summary = "获取会员等级表列表", description = "分页获取会员等级表列表")
    public ServerResponseEntity<List<UserLevelVO>> list(@RequestParam("levelType") Integer levelType) {
        List<UserLevelVO> userLevels = userLevelService.list(levelType);
        return ServerResponseEntity.success(userLevels);
    }

    @GetMapping
    @Operation(summary = "获取会员等级表", description = "根据id获取会员等级表")
    public ServerResponseEntity<UserLevelVO> getById(@RequestParam Long userLevelId) {
        UserLevelVO userLevelVO = userLevelService.getByUserLevelId(userLevelId);
        return ServerResponseEntity.success(userLevelVO);
    }

    @PutMapping
    @Operation(summary = "更新会员等级表", description = "更新会员等级表")
    public ServerResponseEntity<Void> saveOrUpdate(@Valid @RequestBody UserLevelDTO userLevelDTO) {
        if (userLevelDTO.getLevelType() == 1 && userLevelDTO.getUserLevelTerms().size() == 0) {
            return ServerResponseEntity.showFailMsg("等级期数表不能为空");
        }
        if (userLevelDTO.getLevelType() == 1) {
            //付费会员没有成长值限制
            userLevelDTO.setNeedGrowth(0);
        }
        userLevelService.saveOrUpdate(userLevelDTO);
        userLevelService.removeLevelCache(userLevelDTO.getUserLevelId(), userLevelDTO.getLevelType(), userLevelDTO.getLevel());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除会员等级表", description = "根据会员等级表id删除会员等级表")
    public ServerResponseEntity<Void> delete(@RequestParam Long userLevelId) {
        UserLevelVO userLevelVO = userLevelService.getByUserLevelId(userLevelId);
        if (Objects.equals(userLevelVO.getLevelType(), LevelTypeEnum.PAY_USER.value())) {
            Integer userNum = userService.countUserByLevel(userLevelVO.getLevel(), userLevelVO.getLevelType());
            if (userNum > 0) {
                //如果有用户是该付费会员等级，则不允许删除该等级
                throw new LuckException("存在属于该等级的用户，该等级无法删除！");
            }
        }
        userLevelService.deleteByUserLevelId(userLevelId);
        userLevelService.removeLevelCache(userLevelVO.getUserLevelId(), userLevelVO.getLevelType(), userLevelVO.getLevel());
        return ServerResponseEntity.success();
    }

    @PutMapping("/recruitStatus")
    @Operation(summary = "付费会员，是否可以招募会员；1可以招募，0停止招募", description = "付费会员，是否可以招募会员；1可以招募，0停止招募")
    public ServerResponseEntity<Void> recruitStatus(@Valid @RequestBody UserLevelDTO userLevelDTO) {
        Long userLevelId = userLevelDTO.getUserLevelId();
        if (!Objects.equals(userLevelDTO.getLevelType(), LevelTypeEnum.PAY_USER.value())) {
            // 非付费会员，不可以使用招募
            throw new LuckException("非付费会员，不可以使用招募！");
        }
        userLevelService.updateRecruitStatus(userLevelDTO);
        userLevelService.removeLevelCache(userLevelId, userLevelDTO.getLevelType(), userLevelDTO.getLevel());
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateUserLevel")
    @Operation(summary = "新增/更新用户会员等级", description = "新增/更新用户会员等级")
    public ServerResponseEntity<Void> updateUserLevel() {
        userLevelService.updateUserLevel();
        userLevelService.removeLevelListCache(LevelTypeEnum.ORDINARY_USER.value());
        return ServerResponseEntity.success();
    }

    @PutMapping("/batchUserGrowth")
    @Operation(summary = "批量修改会员成长值", description = "批量修改会员成长值")
    public ServerResponseEntity<Boolean> batchUserGrowth(@RequestBody @Valid UserAdminDTO userAdminDTO) {
        userLevelService.batchUpdateGrowth(userAdminDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/batchUserScore")
    @Operation(summary = "批量修改会员积分", description = "批量修改会员积分")
    public ServerResponseEntity<Boolean> batchUserScore(@RequestBody @Valid UserAdminDTO userAdminDTO) {
        userLevelService.batchUserScore(userAdminDTO);
        return ServerResponseEntity.success();
    }
}
