package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yzt.airconditionersystem.bean.Permission;
import com.yzt.airconditionersystem.bean.RolePermission;
import com.yzt.airconditionersystem.bean.User;
import com.yzt.airconditionersystem.bean.dto.RolePermissionDTO;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author TzZach
 * @since 2022-06-24 17:34
 */
@Controller
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    @PostMapping("/role/permission/add")
    @ResponseBody
    public Result addRolePermission(@RequestBody RolePermissionDTO rolePermissionDTO) {
        List<Integer> permissionIds = new ArrayList<>();
        iteratePermissions(rolePermissionDTO.getPermissions(), permissionIds);
        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (Integer permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(rolePermissionDTO.getRoleId());
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        rolePermissionService.saveBatch(rolePermissionList);
        return Result.success("添加成功");
    }

    @PostMapping("/role/permission/edit")
    @ResponseBody
    public Result editRolePermission(@RequestBody RolePermissionDTO rolePermissionDTO) {
        Integer roleId = rolePermissionDTO.getRoleId();
        List<Integer> checkedIds = new ArrayList<>();
        iteratePermissions(rolePermissionDTO.getPermissions(), checkedIds);
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> rolePermissions = rolePermissionService.list(queryWrapper);
        List<Integer> permissionsIds = rolePermissions.stream()
                .map(rolePermission -> rolePermission.getPermissionId())
                .collect(Collectors.toList());
        //需要插入到数据库的permissionId
        List<Integer> insertIds = checkedIds.stream()
                .filter(id -> !permissionsIds.contains(id))
                .collect(Collectors.toList());
        List<RolePermission> insertRolePermission = insertIds.stream()
                .map(permissionId -> {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.setRoleId(roleId);
                    rolePermission.setPermissionId(permissionId);
                    return rolePermission;
                })
                .collect(Collectors.toList());
        //需要从数据库中删除的permissionId
        List<Integer> removeIds = permissionsIds.stream()
                .filter(id -> !checkedIds.contains(id))
                .collect(Collectors.toList());
        if (!insertRolePermission.isEmpty()) {
            rolePermissionService.saveBatch(insertRolePermission);
        }
        if (!removeIds.isEmpty()) {
            queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RolePermission::getRoleId, roleId)
                    .in(RolePermission::getPermissionId, removeIds);
            rolePermissionService.remove(queryWrapper);
        }
        return Result.success("修改成功");
    }

    /**
     * 获取被选中的节点及其子节点的id
     * @param permissions
     * @param ids
     */
    private void iteratePermissions(List<Permission> permissions, List<Integer> ids) {
        for (Permission permission : permissions) {
            if (permission.getChildren() != null) {
                iteratePermissions(permission.getChildren(), ids);
            }
            ids.add(permission.getId());
        }
    }


}
