package com.sundot.airport.system.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.domain.entity.SysRole;
import com.sundot.airport.common.enums.HomePageEnum;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.system.domain.SysAppRole;
import com.sundot.airport.system.mapper.SysAppRoleMapper;
import com.sundot.airport.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sundot.airport.system.mapper.SysAppMapper;
import com.sundot.airport.system.domain.SysApp;
import com.sundot.airport.system.service.ISysAppService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应用Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-20
 */
@Service
public class SysAppServiceImpl implements ISysAppService {
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private SysAppMapper sysAppMapper;
    @Autowired
    private SysAppRoleMapper sysAppRoleMapper;

    /**
     * 查询应用
     *
     * @param appId 应用主键
     * @return 应用
     */
    @Override
    public SysApp selectSysAppByAppId(Long appId) {
        SysApp result = sysAppMapper.selectSysAppByAppId(appId);
        List<SysAppRole> sysAppRoleList = sysAppRoleMapper.selectSysAppRoleByAppId(appId);
        if (CollUtil.isNotEmpty(sysAppRoleList)) {
            List<Long> roleIdList = sysAppRoleList.stream().filter(item -> StrUtil.equals(HomePageEnum.NO.getCode(), item.getHomePage())).map(SysAppRole::getRoleId).distinct().collect(Collectors.toList());
            List<Long> homePageRoleIdList = sysAppRoleList.stream().filter(item -> StrUtil.equals(HomePageEnum.YES.getCode(), item.getHomePage())).map(SysAppRole::getRoleId).distinct().collect(Collectors.toList());
            List<SysRole> sysRoleListAll = sysRoleService.selectRoleListNoPer(new SysRole());
            List<SysRole> sysRoleList = sysRoleListAll.stream().filter(sysRole -> roleIdList.contains(sysRole.getRoleId())).collect(Collectors.toList());
            List<SysRole> homePageSysRoleList = sysRoleListAll.stream().filter(sysRole -> homePageRoleIdList.contains(sysRole.getRoleId())).collect(Collectors.toList());
            result.setSysRoleList(sysRoleList);
            result.setHomePageSysRoleList(homePageSysRoleList);
        }
        return result;
    }

    /**
     * 查询应用列表
     *
     * @param sysApp 应用
     * @return 应用
     */
    @Override
    public List<SysApp> selectSysAppList(SysApp sysApp) {
        return sysAppMapper.selectSysAppList(sysApp);

    }

    /**
     * 移动端查询全部应用列表
     *
     * @param sysApp 应用
     * @return 应用集合
     */
    @Override
    public List<SysApp> listBy(SysApp sysApp) {
        if (StrUtil.isEmpty(sysApp.getHomePage())) {
            sysApp.setHomePage(HomePageEnum.NO.getCode());
        }
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (CollUtil.isEmpty(roles)) {
            return Collections.emptyList();
        }
        List<Long> roleIdList = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
        List<SysAppRole> sysAppRoleList = sysAppRoleMapper.selectSysAppRoleByRoleIds(roleIdList);
        List<Long> appIdList = sysAppRoleList.stream().filter(item -> StrUtil.equals(item.getHomePage(), sysApp.getHomePage())).map(SysAppRole::getAppId).collect(Collectors.toList());
        List<SysApp> result = sysAppMapper.selectSysAppList(sysApp);
        result = result.stream().filter(item -> appIdList.contains(item.getAppId())).collect(Collectors.toList());
        return result;
    }

    /**
     * 新增应用
     *
     * @param sysApp 应用
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertSysApp(SysApp sysApp) {
        sysApp.setCreateTime(DateUtils.getNowDate());
        int result = sysAppMapper.insertSysApp(sysApp);
        if (CollUtil.isNotEmpty(sysApp.getSysRoleList())) {
            sysAppRoleMapper.batchAppRole(sysApp.getSysRoleList().stream().map(role -> {
                SysAppRole sysAppRole = new SysAppRole();
                sysAppRole.setAppId(sysApp.getAppId());
                sysAppRole.setRoleId(role.getRoleId());
                sysAppRole.setHomePage(HomePageEnum.NO.getCode());
                return sysAppRole;
            }).collect(Collectors.toList()));
        }
        if (CollUtil.isNotEmpty(sysApp.getHomePageSysRoleList())) {
            sysAppRoleMapper.batchAppRole(sysApp.getHomePageSysRoleList().stream().map(role -> {
                SysAppRole sysAppRole = new SysAppRole();
                sysAppRole.setAppId(sysApp.getAppId());
                sysAppRole.setRoleId(role.getRoleId());
                sysAppRole.setHomePage(HomePageEnum.YES.getCode());
                return sysAppRole;
            }).collect(Collectors.toList()));
        }
        return result;
    }

    /**
     * 修改应用
     *
     * @param sysApp 应用
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateSysApp(SysApp sysApp) {
        sysApp.setUpdateTime(DateUtils.getNowDate());
        int result = sysAppMapper.updateSysApp(sysApp);
        sysAppRoleMapper.deleteSysAppRoleByAppId(sysApp.getAppId());
        if (CollUtil.isNotEmpty(sysApp.getSysRoleList())) {
            sysAppRoleMapper.batchAppRole(sysApp.getSysRoleList().stream().map(role -> {
                SysAppRole sysAppRole = new SysAppRole();
                sysAppRole.setAppId(sysApp.getAppId());
                sysAppRole.setRoleId(role.getRoleId());
                sysAppRole.setHomePage(HomePageEnum.NO.getCode());
                return sysAppRole;
            }).collect(Collectors.toList()));
        }
        if (CollUtil.isNotEmpty(sysApp.getHomePageSysRoleList())) {
            sysAppRoleMapper.batchAppRole(sysApp.getHomePageSysRoleList().stream().map(role -> {
                SysAppRole sysAppRole = new SysAppRole();
                sysAppRole.setAppId(sysApp.getAppId());
                sysAppRole.setRoleId(role.getRoleId());
                sysAppRole.setHomePage(HomePageEnum.YES.getCode());
                return sysAppRole;
            }).collect(Collectors.toList()));
        }
        return result;
    }

    /**
     * 批量删除应用
     *
     * @param appIds 需要删除的应用主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteSysAppByAppIds(Long[] appIds) {
        int result = sysAppMapper.deleteSysAppByAppIds(appIds);
        sysAppRoleMapper.deleteSysAppRoleByAppIds(appIds);
        return result;
    }

    /**
     * 删除应用信息
     *
     * @param appId 应用主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteSysAppByAppId(Long appId) {
        int result = sysAppMapper.deleteSysAppByAppId(appId);
        sysAppRoleMapper.deleteSysAppRoleByAppId(appId);
        return result;
    }

    /**
     * 根据角色ID查询对应应用列表
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Override
    public AjaxResult roleAppSelect(Long roleId) {
        List<SysApp> sysAppList = selectSysAppList(new SysApp());
        List<SysAppRole> sysAppRoleList = sysAppRoleMapper.selectSysAppRoleByRoleId(roleId);
        List<Long> appIdList = sysAppRoleList.stream().filter(sysAppRole -> StrUtil.equals(HomePageEnum.NO.getCode(), sysAppRole.getHomePage())).map(SysAppRole::getAppId).distinct().collect(Collectors.toList());
        List<Long> HomePageAppIdList = sysAppRoleList.stream().filter(sysAppRole -> StrUtil.equals(HomePageEnum.YES.getCode(), sysAppRole.getHomePage())).map(SysAppRole::getAppId).distinct().collect(Collectors.toList());
        AjaxResult result = AjaxResult.success();
        result.put("checkedAppIdList", appIdList);
        result.put("checkedHomePageAppIdList", HomePageAppIdList);
        result.put("sysAppList", sysAppList);
        return result;
    }
}
