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.MenuPermission;
import com.ssyf.module.auth.domain.query.AssignQuery;
import com.ssyf.module.auth.domain.query.MenuPermissionQuery;
import com.ssyf.module.auth.domain.vo.MenuPermissionVo;
import com.ssyf.module.auth.service.MenuPermissionService;
import com.ssyf.common.core.util.EntityUtil;
import com.ssyf.common.security.helper.ClientContextHelper;
import com.ssyf.common.security.domain.ClientInfo;
import com.ssyf.common.web.aop.RequestSynchronization;

import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.plugs.mysql.helper.PageEnhanceHelper;
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.Menu模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="权限-菜单接口")
@RestController
@RequestMapping("/auth/menu")
public class MenuPermissionAuthController {

    @Autowired
    private MenuPermissionService baseService;

    /**
     * 新增数据
     *
     * @param query 请求体，包含要新增的数据信息。是一个经过验证的MenuPermissionQuery对象。
     * @return 返回操作结果，如果成功新增，返回新增的数据的详细信息；如果失败，返回错误信息。
     */
    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<MenuPermissionVo> addOne(@Validated @RequestBody MenuPermissionQuery query) {
        // 检查传入的查询对象是否为空
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        // 通过基础服务层新增数据
        MenuPermission menu = this.baseService.addOne(query);
        // 返回新增数据的成功结果，将MenuPermission对象转换为MenuPermissionVo对象后返回
        return Result.success(BeanUtil.copyProperties(menu , MenuPermissionVo.class));
    }


    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid ,@Validated @RequestBody MenuPermissionQuery 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 = "查询指定数据")
    @PostMapping(value = "/getOneByUid/{uid}")
    public Result<MenuPermissionVo> getOneByUid(@PathVariable String uid){
        return Result.success(BeanUtil.toBean(this.baseService.getOneByUid(uid) , MenuPermissionVo.class));
    }

    @ApiOperation(value = "/查询所有数据", notes = "查询所有数据")
    @PostMapping(value = "/queryList")
    public Result<MenuPermissionVo> queryList(@Validated @RequestBody MenuPermissionQuery query){
        List<MenuPermission> menuList = this.baseService.getList(query);
        List<MenuPermissionVo> t = EntityUtil.copyProperties(menuList, MenuPermissionVo.class);
        return Result.success(t);
    }

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

    @ApiOperation(value = "/查询子数据", notes = "查询子数据")
    @PostMapping(value = "/queryListChildren")
    public Result<MenuPermissionVo> queryListChildren(@Validated @RequestBody MenuPermissionQuery query){
        List<MenuPermission> list = this.baseService.getListChildren(query.getParentId() ,query);
        return Result.success(EntityUtil.copyProperties(list, MenuPermissionVo.class));
    }

    @ApiOperation(value = "/分页查询子数据", notes = "分页查询子数据")
    @PostMapping(value = "/queryPageChildren")
    public Result<MenuPermissionVo> queryPageChildren(@Validated @RequestBody MenuPermissionQuery query){
        PageInfo<MenuPermission> temp = this.baseService.getPageChildren(query.getParentId() ,query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp , MenuPermissionVo.class));
    }

    @ApiOperation(value = "/查询下一层级的数据", notes = "查询下一层级的数据")
    @PostMapping(value = "/queryListNextChildren")
    public Result<MenuPermissionVo> queryListNextChildren(@Validated @RequestBody MenuPermissionQuery query){
        List<MenuPermission> list = this.baseService.getListNextChildren(query.getParentId() ,query);
        return Result.success(EntityUtil.copyProperties(list, MenuPermissionVo.class));
    }

    @ApiOperation(value = "/分页查询下一层级的数据", notes = "分页查询下一层级的数据")
    @PostMapping(value = "/queryPageNextChildren")
    public Result<MenuPermissionVo> queryPageNextChildren(@Validated @RequestBody MenuPermissionQuery query){
        PageInfo<MenuPermission> temp = this.baseService.getPageNextChildren(query.getParentId() ,query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp , MenuPermissionVo.class));
    }

    @ApiOperation(value = "/查询顶层数据", notes = "查询顶层数据")
    @PostMapping(value = "/queryListTopData")
    public Result<MenuPermissionVo> queryListTopData(@Validated @RequestBody MenuPermissionQuery query){
        query.setParentId(null);
        List<MenuPermission> list = this.baseService.getListNextChildren(query.getParentId() ,query);
        return Result.success(EntityUtil.copyProperties(list, MenuPermissionVo.class));
    }

    @ApiOperation(value = "/分页查询顶层数据", notes = "分页查询顶层数据")
    @PostMapping(value = "/queryPageTopData")
    public Result<MenuPermissionVo> queryPageTopData(@Validated @RequestBody MenuPermissionQuery query){
        query.setParentId(null);
        PageInfo<MenuPermission> temp = this.baseService.getPageNextChildren(query.getParentId() ,query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp , MenuPermissionVo.class));
    }

    @ApiOperation(value = "查询指定角色的菜单信息")
    @PostMapping(value = "/queryByRole/{roleUid}")
    public Result queryByRole(@PathVariable String roleUid) {
        List<MenuPermission> list = this.baseService.getListByRoleUid(roleUid);
        List<MenuPermissionVo> result = EntityUtil.copyProperties(list  , MenuPermissionVo.class);
        return Result.success(result);
    }

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

    /**
     * 分配给用户的数据;
     * @param query
     * @return
     */
    @ApiOperation(value = "/查询关联数据", notes = "查询关联数据")
    @PostMapping(value = "/queryRelation")
    public Result<MenuPermissionVo> queryRelation(@Validated @RequestBody MenuPermissionQuery query){
        ClientInfo clientInfo = ClientContextHelper.getClientInfo();
        List<MenuPermission> menuList = this.baseService.getRelationListByUserUid(clientInfo.getId().toString() ,query);
        List<MenuPermissionVo> t = EntityUtil.copyProperties(menuList, MenuPermissionVo.class);
        return Result.success(t);
    }

    /**
     * 分配给用户的数据
     * @param query
     * @return
     */
    @ApiOperation(value = "/查询关联数据", notes = "查询关联数据")
    @PostMapping(value = "/queryRelationByUserUid/{userUid}")
    public Result<MenuPermissionVo> queryRelationByUserUid(@PathVariable String userUid , @Validated @RequestBody MenuPermissionQuery query){
        List<MenuPermission> menuList = this.baseService.getRelationListByUserUid(userUid,query);
        List<MenuPermissionVo> t = EntityUtil.copyProperties(menuList, MenuPermissionVo.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();
    }

    /**
     * 重置菜单关联关系
     * @return
     */
    @ApiOperation(value = "/重置关联编码", notes = "重置关联编码")
    @GetMapping(value = "/resetPedigree")
    public Result resetMenuPedigree(){
        long st = System.currentTimeMillis();
        this.baseService.resetMenuPedigree();
        System.err.println("--重置编号耗时：" + (System.currentTimeMillis() - st));
        return Result.success("成功");
    }

}
