package com.usky.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.domain.*;
import com.usky.system.mapper.SysTenantPlatformMapper;
import com.usky.system.service.ISysMenuService;
import com.usky.system.service.SysPlatformMenuService;
import com.usky.system.service.SysPlatformService;
import com.usky.system.service.SysTenantPlatformService;
import com.usky.system.service.vo.SysPlatformVo;
import com.usky.system.service.vo.TenantPlatformListVo;
import com.usky.system.service.vo.TenantPlatformMenuVo;
import com.usky.system.service.vo.TenantPlatformVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租户和应用关联表 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-08-14
 */
@Service
public class SysTenantPlatformServiceImpl extends AbstractCrudService<SysTenantPlatformMapper, SysTenantPlatform> implements SysTenantPlatformService {

    @Autowired
    private SysPlatformService sysPlatformService;

    @Autowired
    private SysPlatformMenuService sysPlatformMenuService;

    @Autowired
    private ISysMenuService menuService;

    @Override
    @Transactional
    public void updateTenantPlatform(TenantPlatformVo tenantPlatformVo) {
        Integer[] arr = tenantPlatformVo.getPlatformIds();
        LambdaQueryWrapper<SysTenantPlatform> queryWrapper = Wrappers.lambdaQuery();
        if (tenantPlatformVo.getRequestId().equals(0)){
            queryWrapper.eq(SysTenantPlatform::getTenantId,tenantPlatformVo.getTenantId())
                    .eq(SysTenantPlatform::getIsDefault,1);
            this.remove(queryWrapper);
            LambdaQueryWrapper<SysTenantPlatform> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(SysTenantPlatform::getTenantId,tenantPlatformVo.getTenantId())
                    .eq(SysTenantPlatform::getPlatformId,arr[0]);
            this.remove(queryWrapper1);
            SysTenantPlatform sysTenantPlatform = new SysTenantPlatform();
            sysTenantPlatform.setTenantId(tenantPlatformVo.getTenantId());
            sysTenantPlatform.setPlatformId(arr[0]);
            sysTenantPlatform.setIsDefault(1);
            this.save(sysTenantPlatform);
        }else {
            queryWrapper.eq(SysTenantPlatform::getTenantId,tenantPlatformVo.getTenantId())
                    .ne(SysTenantPlatform::getIsDefault,1);
            this.remove(queryWrapper);
            LambdaQueryWrapper<SysTenantPlatform> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(SysTenantPlatform::getTenantId,tenantPlatformVo.getTenantId())
                    .eq(SysTenantPlatform::getIsDefault,1);
            List<SysTenantPlatform> list = this.list(queryWrapper1);
            if (list.size()<=0){
                throw new BusinessException("没有相关关联数据，请联系管理员");
            }
            for (int i = 0; i < arr.length; i++){
                if (!arr[i].equals(list.get(0).getPlatformId())){
                    SysTenantPlatform sysTenantPlatform1 = new SysTenantPlatform();
                    sysTenantPlatform1.setPlatformId(arr[i]);
                    sysTenantPlatform1.setTenantId(tenantPlatformVo.getTenantId());
                    sysTenantPlatform1.setIsDefault(0);
                    this.save(sysTenantPlatform1);
                }
            }
        }
    }

    @Override
    public List<TenantPlatformListVo> getTenantPlatformList(Integer tenantId) {
        List<TenantPlatformListVo> list2 = new ArrayList<>();;
        LambdaQueryWrapper<SysTenantPlatform> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(SysTenantPlatform::getTenantId,tenantId);
        List<SysTenantPlatform> list = this.list(queryWrapper1);
        List<SysPlatformVo> list1 = sysPlatformService.getPlatformAllList();
        if(CollectionUtils.isNotEmpty(list1)){
            for(int j=0;j<list1.size();j++){
                TenantPlatformListVo tenantPlatformListVo = new TenantPlatformListVo();
                tenantPlatformListVo.setPlatformName(list1.get(j).getPlatformName());
                tenantPlatformListVo.setPlatformId(list1.get(j).getId());
                tenantPlatformListVo.setIcon(list1.get(j).getIcon());
                if(CollectionUtils.isNotEmpty(list)){
                    for(int i=0;i<list.size();i++){
                        if (list1.get(j).getId().equals(list.get(i).getPlatformId())) {
                            tenantPlatformListVo.setIsDefault(list.get(i).getIsDefault());
                            tenantPlatformListVo.setIsSelected(1);
                        }
                    }
                }
                list2.add(tenantPlatformListVo);
            }
        }
        return list2;
    }

    public Map<String, Object> selectTenantPlatform(Integer tenantId){
        LambdaQueryWrapper<SysTenantPlatform> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysTenantPlatform::getTenantId,tenantId);
        List<SysTenantPlatform> list = this.list(queryWrapper);
        List<TenantPlatformMenuVo> list1 = new ArrayList<>();
        Long[] arr = sysPlatformMenuService.selectPlatformMenu(tenantId);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys",arr);
        if (CollectionUtils.isNotEmpty(list)){
            List<Integer> platformIds = new ArrayList<>();
            for(int j=0;j<list.size();j++){
                platformIds.add(list.get(j).getPlatformId());
            }
            LambdaQueryWrapper<SysPlatform> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.in(SysPlatform::getId,platformIds);
            List<SysPlatform> platformList = sysPlatformService.list(queryWrapper1);
            for(int i=0;i<list.size();i++){
                TenantPlatformMenuVo tenantPlatformMenuVo = new TenantPlatformMenuVo();
                tenantPlatformMenuVo.setId(list.get(i).getPlatformId());
                if (CollectionUtils.isNotEmpty(platformList)){
                    for(int k=0;k<platformList.size();k++){
                        if (list.get(i).getPlatformId().equals(platformList.get(k).getId())){
                            tenantPlatformMenuVo.setLabel(platformList.get(k).getPlatformName());
                        }
                    }
                }
                List<SysMenu> menus = sysPlatformMenuService.getPlatformMenuList(list.get(i).getPlatformId());
                tenantPlatformMenuVo.setChildren(menuService.buildMenuTreeSelect(menus));
                list1.add(tenantPlatformMenuVo);
            }
        }
        ajax.put("menus",list1);
        return ajax;
    }
}
