package com.starry.admin.modules.custom.controller;

import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.custom.module.entity.PlayCustomLevelInfoEntity;
import com.starry.admin.modules.custom.module.vo.PlayCustomLevelAddVo;
import com.starry.admin.modules.custom.module.vo.PlayCustomLevelEditVo;
import com.starry.admin.modules.custom.module.vo.PlayCustomLevelReturnVo;
import com.starry.admin.modules.custom.service.IPlayCustomLevelInfoService;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 顾客等级Controller
 *
 * @author admin
 * @since 2024-05-04
 */
@RestController
@RequestMapping("/custom/level")
@Api(tags = "顾客等级管理", description = "顾客等级信息管理相关接口，包括查询、新增、修改和删除等操作")
public class PlayCustomLevelInfoController {
    @Resource
    private IPlayCustomLevelInfoService playCustomLevelInfoService;

    /**
     * 查询顾客等级列表
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "查询等级列表", notes = "获取所有顾客等级信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayCustomLevelReturnVo.class, responseContainer = "List")})
    public R listAll() {
        List<PlayCustomLevelInfoEntity> list = playCustomLevelInfoService.selectPlayCustomLevelInfoByPage();
        return R.ok(ConvertUtil.entityToVoList(list, PlayCustomLevelReturnVo.class));
    }

    /**
     * 获取顾客等级详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取等级详情", notes = "根据ID获取顾客等级详细信息")
    @ApiImplicitParam(name = "id", value = "等级ID", required = true, paramType = "path", dataType = "String", example = "1")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = PlayCustomLevelInfoEntity.class)})
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(playCustomLevelInfoService.selectPlayCustomLevelInfoById(id));
    }

    /**
     * 新增顾客等级
     */
    // @PreAuthorize("@customSs.hasPermission('custom:level:add')")
    @Log(title = "店员等级", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    @ApiOperation(value = "新增等级", notes = "创建新的顾客等级信息")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败")})
    public R create(@ApiParam(value = "等级信息", required = true) @RequestBody PlayCustomLevelAddVo vo) {
        PlayCustomLevelInfoEntity entity = ConvertUtil.entityToVo(vo, PlayCustomLevelInfoEntity.class);
        int level = playCustomLevelInfoService.selectMaxLevel();
        entity.setLevel(level + 1);
        boolean success = playCustomLevelInfoService.create(entity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改顾客等级
     */
    // @PreAuthorize("@customSs.hasPermission('custom:level:update')")
    @Log(title = "顾客等级", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    @ApiOperation(value = "修改等级", notes = "修改顾客等级信息")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败"),
            @ApiResponse(code = 400, message = "对象不存在")})
    public R update(@ApiParam(value = "等级信息", required = true) @RequestBody PlayCustomLevelEditVo vo) {
        if (playCustomLevelInfoService.selectPlayCustomLevelInfoById(vo.getId()) == null) {
            throw new CustomException("对象不存在");
        }
        PlayCustomLevelInfoEntity entity = ConvertUtil.entityToVo(vo, PlayCustomLevelInfoEntity.class);
        boolean success = playCustomLevelInfoService.update(entity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除店员等级
     */
    @Log(title = "店员等级", businessType = BusinessType.DELETE)
    @DeleteMapping("delMaxLevel")
    @ApiOperation(value = "删除最高等级", notes = "删除系统中的最高等级")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 400, message = "最后一级,不允许删除")})
    public R remove() {
        int level = playCustomLevelInfoService.selectMaxLevel();
        if (level <= 1) {
            throw new CustomException("最后一级,不允许删除");
        }
        playCustomLevelInfoService.delMaxLevelByLevel(level);
        return R.ok();
    }
}
