package org.jeecg.modules.adweb.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.jeecg.modules.adweb.enterprise.entity.AdwebSysUserPermission;
import org.jeecg.modules.adweb.enterprise.mapper.AdwebSysUserPermissionMapper;
import org.jeecg.modules.adweb.enterprise.service.SysUserPermissionService;
import org.jeecg.modules.system.entity.SysPermission;
import org.jeecg.modules.system.service.ISysPermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysUserPermissionServiceImpl
    extends ServiceImpl<AdwebSysUserPermissionMapper, AdwebSysUserPermission>
    implements SysUserPermissionService {

  @Resource private ISysPermissionService sysPermissionService;

  /**
   * 删除原有权限，赋予新的权限
   *
   * @param userId 用户id
   * @param permissionIds 给用户分配的权限
   */
  @Override
  @Transactional
  public void editUserPermission(String userId, String permissionIds) {
    QueryWrapper<AdwebSysUserPermission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("user_id", userId);
    this.remove(queryWrapper);

    List<AdwebSysUserPermission> permissions = new ArrayList<>();
    String[] permissionIdArr = permissionIds.split(",");
    for (String permissionId : permissionIdArr) {
      permissions.add(new AdwebSysUserPermission(userId, permissionId));
    }
    // 如果没有首页权限，要增加首页权限
    SysPermission indexMenu =
        sysPermissionService
            .list(new LambdaQueryWrapper<SysPermission>().eq(SysPermission::getName, "首页"))
            .get(0);
    if (!permissionIds.contains(indexMenu.getId())) {
      permissions.add(new AdwebSysUserPermission(userId, indexMenu.getId()));
    }
    // 以下两个菜单为默认拥有的权限
    //    List<SysPermission> fatherPermission =
    //        sysPermissionService.list(
    //            new LambdaQueryWrapper<SysPermission>()
    //                .in(SysPermission::getUrl, Arrays.asList("/account", "/tools")));
    //
    //    List<String> fatherPermissionIds =
    //        fatherPermission.stream().map(SysPermission::getId).collect(Collectors.toList());
    //
    //    List<SysPermission> subPermission =
    //        sysPermissionService.list(
    //            new LambdaQueryWrapper<SysPermission>()
    //                .in(SysPermission::getParentId, fatherPermissionIds));

    //    for (SysPermission permission : fatherPermission) {
    //      permissions.add(new AdwebSysUserPermission(userId, permission.getId()));
    //    }
    //    for (SysPermission permission : subPermission) {
    //      permissions.add(new AdwebSysUserPermission(userId, permission.getId()));
    //    }
    this.saveBatch(permissions);
  }

  /**
   * 获取用户菜单权限
   *
   * @param userId 用户id
   */
  @Override
  public List<SysPermission> getPermissionsByUserId(String userId) {
    QueryWrapper<AdwebSysUserPermission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("user_id", userId);
    List<AdwebSysUserPermission> permissions = this.list(queryWrapper);

    List<String> ids =
        permissions.stream()
            .map(AdwebSysUserPermission::getPermissionId)
            .collect(Collectors.toList());
    QueryWrapper<SysPermission> permissionQueryWrapper = new QueryWrapper<>();
    permissionQueryWrapper.in("id", ids);
    permissionQueryWrapper.eq("del_flag", 0);
    permissionQueryWrapper.orderByAsc("sort_no");
    return sysPermissionService.list(permissionQueryWrapper);
  }
}
