package com.changfa.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changfa.base.BaseController;
import com.changfa.constants.ApiResult;
import com.changfa.enums.StatusEnum;
import com.changfa.custom.ExCondition;
import com.changfa.dto.RoleDto;
import com.changfa.entity.TRole;
import com.changfa.exception.BuzEx;
import com.changfa.service.ITRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wwz
 * @since 2022-06-24
 */
@RestController
@Api(tags = "角色APi")
@RequestMapping(value = "/role",method = RequestMethod.POST)
public class RoleController extends BaseController {

    @Autowired
    private ITRoleService roleService;


    @ApiOperation(value = "角色列表")
    @RequestMapping("/queryRolePage")
    @ApiResponses(@ApiResponse(code=200,message = "请求成功",response = TRole.class))
    public ApiResult queryRolePage(@RequestBody RoleDto roleDto,@ApiIgnore Page page){
        try{
            page.setCurrent(roleDto.getPageNum());
            page.setSize(roleDto.getPageSize());
            ExCondition condition = new ExCondition();
            condition.like("role_name",roleDto.getRoleName());
            condition.eq("logic_del",0);
            condition.orderByAsc("order_no");
            return ApiResult.success( this.roleService.listPage(condition,page));
        }catch (BuzEx buzEx){
            return ApiResult.error(buzEx.getStatusEnum());

        }catch (Exception e){

        }
        return ApiResult.error(StatusEnum.FAIL);

    }

    @ApiOperation(value = "删除角色")
    @RequestMapping("/deleteRole")
    public ApiResult deleteRole(@RequestParam("roleId")Integer roleId){
        try{
            if(this.roleService.deleteRoleById(roleId)){
                return ApiResult.success();
            }
        }catch (BuzEx buzEx){
            return ApiResult.error(buzEx.getStatusEnum());

        }catch (Exception e){

        }
        return ApiResult.error(StatusEnum.FAIL);

    }

    @ApiOperation(value = "新增修改角色")
    @RequestMapping("/saveOrUpdateRole")
    public ApiResult saveOrUpdateRole(@RequestBody @Valid TRole tRole, @ApiIgnore Errors errors){
        try{
            if(errors.hasErrors()){
                return ApiResult.error(errors.getFieldError().getDefaultMessage());
            }
            if(tRole.getId() != null){
                tRole.fillUpdate();
            }else{
                tRole.fillCreated();
            }
            ExCondition wrapper = new ExCondition();
            wrapper.eq("role_name",tRole.getRoleName());
            wrapper.ne("id",tRole.getId());
            if(this.roleService.count(wrapper) > 0){
                return ApiResult.error(StatusEnum.ROLE_NAME_EXISTS);
            }
            if(this.roleService.saveOrUpdate(tRole)){
                return ApiResult.success();
            };
        }catch (BuzEx buzEx){
            return ApiResult.error(buzEx.getStatusEnum());

        }catch (Exception e){

        }
        return ApiResult.error(StatusEnum.FAIL);
    }


    @ApiOperation(value = "获取所有角色信息")
    @RequestMapping("/getAllRole")
    @ApiResponses(@ApiResponse(code=200,message = "请求成功",response = TRole.class))
    public ApiResult getAllRole(){
        try{
            ExCondition condition = new ExCondition();
            condition.select("id as roleId,role_name roleName");
            return ApiResult.success( this.roleService.listMaps(condition));
        }catch (BuzEx buzEx){
            return ApiResult.error(buzEx.getStatusEnum());

        }catch (Exception e){

        }
        return ApiResult.error(StatusEnum.FAIL);

    }

}
