package com.huimv.env.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.env.admin.common.token.TokenSign;
import com.huimv.env.admin.common.utils.Result;
import com.huimv.env.admin.entity.AccountGroup;
import com.huimv.env.admin.entity.Group;
import com.huimv.env.admin.service.IAccountGroupService;
import com.huimv.env.admin.service.IGroupService;
import com.huimv.env.admin.service.ISysAccountMultilevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-06
 */
@RestController
@RequestMapping("/admin/group")
@CrossOrigin
public class SysAccountGroupController {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private IAccountGroupService accountGroupService;
    @Autowired
    private ISysAccountMultilevelService accountMultilevelService;

    @PostMapping("/add")
    public Result addGroup (@RequestBody Group group, HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return groupService.addGroup(group,userId);
    }

    @PostMapping("/edit")
    public Result updateGroup(@RequestBody Group group ){
        return groupService.updateGroup( group);
    }

    @RequestMapping("/remove")
    public Result removeGroup(@RequestParam("groupId") Integer groupId) {
        accountGroupService.remove(new QueryWrapper<AccountGroup>().in("group_id",groupId));
        return groupService.removeGroup(groupId);
    }

    @PostMapping("/list")
    public Result listGroup(@RequestBody Map map , HttpServletRequest request) {
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        List<Integer> allChildId = accountMultilevelService.getAllChildId(userId);
        return groupService.listGroup(map,allChildId);
    }

    //根据角色查权限组
    @PostMapping("/getGroupByUserId")
    public Result getGroupByUserId(@RequestBody Map map){
        Integer userId  =  (Integer) map.get("userId");
        return  groupService.getGroupByUserId(userId);
    }
}
