package com.ssyf.module.auth.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ssyf.module.auth.domain.entity.Role;
import com.ssyf.module.auth.domain.query.AssignQuery;
import com.ssyf.module.auth.domain.query.RoleQuery;
import com.ssyf.module.auth.domain.vo.RoleVo;
import com.ssyf.common.core.util.EntityUtil;
import com.ssyf.common.security.helper.ClientContextHelper;
import com.ssyf.common.security.domain.ClientInfo;
import com.ssyf.plugs.mysql.helper.PageEnhanceHelper;
import com.ssyf.common.web.aop.RequestSynchronization;

import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.module.auth.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* com.ssyf.module.auth.entity.Role模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="权限-角色接口")
@RestController
@RequestMapping("/auth/role")
public class RoleAuthController {

    @Autowired
    private RoleService baseService;

    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<RoleVo> addOne(@Validated @RequestBody RoleQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        Role menuRole = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(menuRole , RoleVo.class));
    }

    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid , @Validated @RequestBody RoleQuery query){
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"更新数据不能为空");
        }
        this.baseService.updateByUid(uid ,query);
        return Result.success();
    }

    @RequestSynchronization
    @ApiOperation(value = "删除数据", notes = "uid删除")
    @PostMapping(value = "/removeByUid/{uid}")
    public Result removeByUid(@PathVariable String uid){
        this.baseService.removeByUid(uid);
        return Result.success();
    }

    @ApiOperation(value = "/查询指定数据" ,notes = "uid查询")
    @PostMapping(value = "/getOneByUid/{uid}")
    public Result<PageInfo<RoleVo>> getOneByUid(@PathVariable String uid) {
        return Result.success(BeanUtil.toBean(this.baseService.getOneByUid(uid) ,RoleVo.class));
    }

    @ApiOperation(value = "/查询分页数据", notes = "分页查询")
    @PostMapping(value = "/queryPage")
    public Result<PageInfo<RoleVo>> queryPage(@Validated @RequestBody RoleQuery query) {
        PageInfo<Role> temp = this.baseService.getPage(query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp ,RoleVo.class));
    }

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryList")
    public Result<PageInfo<RoleVo>> queryList(@Validated @RequestBody RoleQuery query) {
        List<Role> menuRoleList = this.baseService.getList(query);
        List<RoleVo> t = EntityUtil.copyProperties(menuRoleList, RoleVo.class);
        return Result.success(t);
    }

    @ApiOperation(value = "/查询关联数据", notes = "查询关联数据")
    @PostMapping(value = "/queryRelation")
    public Result<RoleVo> queryRelation(@Validated @RequestBody RoleQuery query){
        ClientInfo clientInfo = ClientContextHelper.getClientInfo();
        List<Role> roleList = this.baseService.getRelationListByUserUid(clientInfo.getId().toString() ,query);
        List<RoleVo> t = EntityUtil.copyProperties(roleList, RoleVo.class);
        return Result.success(t);
    }

    @ApiOperation(value = "查询关联数据")
    @PostMapping(value = "/queryRelationByUserUid/{userUid}")
    public Result queryRelationByUserUid(@PathVariable String userUid ,@Validated @RequestBody RoleQuery query){
        List<Role> roleList = this.baseService.getRelationListByUserUid(userUid,query);
        List<RoleVo> result = EntityUtil.copyProperties(roleList ,RoleVo.class);
        return Result.success(result);
    }

    /**
     * 查询可见数据;
     * @param query
     * @return
     */
    @ApiOperation(value = "/查询可见数据", notes = "查询可见数据")
    @PostMapping(value = "/queryVisible")
    public Result<RoleVo> queryVisible(@Validated @RequestBody RoleQuery query){
        ClientInfo clientInfo = ClientContextHelper.getClientInfo();
        List<Role> menuList = this.baseService.getVisibleByUserUid(clientInfo.getId().toString() ,query);
        List<RoleVo> t = EntityUtil.copyProperties(menuList, RoleVo.class);
        return Result.success(t);
    }

    @ApiOperation(value = "/重置用户关联角色分享权限", notes = "重置用户关联角色分享权限")
    @PostMapping(value = "/resetUserRelationShare")
    public Result resetUserRelationShare(@Validated @RequestBody AssignQuery query){
        this.baseService.resetUserRelationShare(query.getId() ,query.getAssignIds());
        return Result.success();
    }

    @RequestSynchronization
    @ApiOperation(value = "分配菜单并删除旧菜单")
    @PostMapping(value = "/assignMenuPermissionAndRemoveOld")
    public Result assignAndRemoveOld(@Validated @RequestBody AssignQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        this.baseService.assignMenuPermissionAndRemoveOld(query.getId() ,query.getAssignIds());
        return Result.success();
    }
}
