package cn.pluss.platform.controller.userLevel;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.UserLevel;
import cn.pluss.platform.entity.UserRole;
import cn.pluss.platform.user.UserLevelService;
import cn.pluss.platform.user.UserRoleService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户等级表 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2022-01-13
 */
@Controller
@RequestMapping("/userLevel")
public class UserLevelController extends BaseModelController<UserLevelService, UserLevel> {

    @Resource
    private UserRoleService userRoleService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index(HttpServletRequest request) {
        return "userLevel/list";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        List<UserRole> list = userRoleService.list();
        if (id != null) {
            UserLevel entity = baseService.getOne(new QueryWrapper<UserLevel>().eq("id",id));
            request.setAttribute("entity", entity);
        }
        request.setAttribute("roleList", list);
        return "userLevel/detail";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/save")
    @Override
    public Result<String> save(@RequestBody UserLevel entity) {
        try {
            return super.save(entity);
        }catch (Exception e){
            return ResultGenerator.genFailResult("角色Code和当前等级Code已存在！");
        }
    }
}
