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

import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.module.entity.PlayClerkLevelInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkLevelAddVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkLevelEditVo;
import com.starry.admin.modules.clerk.service.IPlayClerkLevelInfoService;
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 javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 店员等级Controller
 *
 * @author admin
 * @since 2024-03-30
 */
@Api(tags = "店员等级管理", description = "店员等级信息管理相关接口，包括查询、新增、修改和删除等操作")
@RestController
@RequestMapping("/clerk/level")
public class PlayClerkLevelInfoController {
    @Resource
    private IPlayClerkLevelInfoService playClerkLevelInfoService;

    /**
     * 查询店员等级列表
     */
    @ApiOperation(value = "查询所有等级", notes = "获取所有店员等级信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkLevelInfoEntity.class, responseContainer = "List")})
    @GetMapping("/listAll")
    public R listAll() {
        return R.ok(playClerkLevelInfoService.selectAll());
    }

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

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

    /**
     * 修改店员等级
     */
    @ApiOperation(value = "修改等级", notes = "修改店员等级信息")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败")})
    @Log(title = "店员等级", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "等级信息", required = true) @Validated @RequestBody PlayClerkLevelEditVo vo) {
        PlayClerkLevelInfoEntity entity = ConvertUtil.entityToVo(vo, PlayClerkLevelInfoEntity.class);
        boolean success = playClerkLevelInfoService.update(entity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

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