package cn.pluss.platform.controller.userRole;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.GuideLabel;
import cn.pluss.platform.entity.UserRole;
import cn.pluss.platform.user.UserRoleService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户角色表 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2022-01-13
 */
@Controller
@RequestMapping("/userRole")
public class UserRoleController extends BaseModelController<UserRoleService, UserRole> {

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

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

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/save")
    @Override
    public Result<String> save(@RequestBody UserRole entity) {
        if(StringUtil.isEmpty(entity.getRoleName())){
            return ResultGenerator.genFailResult("角色名称不能为空！", null);
        }
        if(StringUtil.isEmpty(entity.getRoleCode())){
            return ResultGenerator.genFailResult("角色Code不能为空", null);
        }
        try {
            return super.save(entity);
        }catch (Exception e){
            return ResultGenerator.genFailResult("角色Code已存在，请更换！");
        }
    }
}
