package com.huimv.admin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huimv.admin.system.entity.ScreenSysAccountGroup;
import com.huimv.admin.system.entity.ScreenSysGroup;
import com.huimv.admin.system.service.IScreenSysAccountGroupService;
import com.huimv.admin.system.service.IScreenSysGroupService;
import com.huimv.admin.system.service.ISysAccountMultilevelService;
import com.huimv.common.token.TokenSign;
import com.huimv.common.utils.Result;
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;


@RestController
@RequestMapping("/screen/group")
public class ScreenSysGroupController {

    @Autowired
    private IScreenSysGroupService groupService;

    @Autowired
    private IScreenSysAccountGroupService accountGroupService;
    @Autowired
    private ISysAccountMultilevelService accountMultilevelService;

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


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

    @RequestMapping("/remove")
    public Result removeGroup(@RequestParam("groupId") Integer groupId) {

        accountGroupService.remove(new QueryWrapper<ScreenSysAccountGroup>().in("group_id",groupId));
        return groupService.removeGroup(groupId);
    }

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