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 org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 顾客等级Controller
 *
 * @author admin
 * @since 2024-05-04
 */
@RestController
@RequestMapping("/custom/level")
public class PlayCustomLevelInfoController {
    @Resource
    private IPlayCustomLevelInfoService playCustomLevelInfoService;

    /**
     * 查询顾客等级列表
     */
    @PostMapping("/listAll")
    public R listAll() {
        List<PlayCustomLevelInfoEntity> list = playCustomLevelInfoService.selectPlayCustomLevelInfoByPage();
        return R.ok(ConvertUtil.entityToVoList(list, PlayCustomLevelReturnVo.class));
    }

    /**
     * 获取顾客等级详细信息
     */
    @GetMapping(value = "/{id}")
    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")
    public R create(@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")
    public R update(@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")
    public R remove() {
        int level = playCustomLevelInfoService.selectMaxLevel();
        if (level <= 1) {
            throw new CustomException("最后一级,不允许删除");
        }
        playCustomLevelInfoService.delMaxLevelByLevel(level);
        return R.ok();
    }
}
