package com.usky.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.system.domain.SysMenu;
import com.usky.system.domain.SysMobileMenu;
import com.usky.system.domain.SysPlatform;
import com.usky.system.domain.SysPlatformMenu;
import com.usky.system.domain.SysMobilePlatformMenu;
import com.usky.system.mapper.SysMenuMapper;
import com.usky.system.mapper.SysPlatformMapper;
import com.usky.system.mapper.SysTenantPlatformMapper;
import com.usky.system.service.SysMobilePlatformMenuService;
import com.usky.system.service.SysPlatformMenuService;
import com.usky.system.service.SysPlatformService;
import com.usky.system.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 平台信息表 服务实现类
 * </p>
 *
 * @author ya
 * @since 2022-05-18
 */
@Service
public class SysPlatformServiceImpl extends AbstractCrudService<SysPlatformMapper, SysPlatform> implements SysPlatformService {

    @Autowired
    private SysPlatformMenuService sysPlatformMenuService;
    @Autowired
    private SysMobilePlatformMenuService sysMobilePlatformMenuService;

    @Override
    public List<SysPlatform> getPlatformBoxList() {
        LambdaQueryWrapper<SysPlatform> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysPlatform::getStatus, "1");
        List<SysPlatform> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public CommonPage<SysPlatformVo> getPlatformList(String platformName, String startTime, String endTime, Integer page, Integer size, Integer id) {
        List<SysPlatformVo> list1 = baseMapper.getPlatformList(platformName, startTime, endTime, null, null, id);
        int total = 0;
        if (list1.size() > 0) {
            total = list1.size();
        }
        Integer current = null;
        if (page != null && size > 0) {
            current = (page - 1) * size;
        }
        List<SysPlatformVo> list = baseMapper.getPlatformList(platformName, startTime, endTime, current, size, id);
        return new CommonPage<>(list, total, size, page);
    }

    @Override
    public Long[] getPlatformMenu(Long platformId) {
        LambdaQueryWrapper<SysPlatformMenu> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysPlatformMenu::getPlatformId, platformId);
        List<SysPlatformMenu> menuList = sysPlatformMenuService.list(queryWrapper);
//        List<SysPlatformMenu> menuList = baseMapper.getMenuListOne(platformId, null);
        Long[] arr = new Long[menuList.size()];
        for (int i = 0; i < menuList.size(); i++) {
            arr[i] = menuList.get(i).getMenuId();
        }
        return arr;
    }

    @Override
    public Long[] getMobilePlatformMenu(Long platformId) {
        LambdaQueryWrapper<SysMobilePlatformMenu> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysMobilePlatformMenu::getPlatformId, platformId);
        List<SysMobilePlatformMenu> menuList = sysMobilePlatformMenuService.list(queryWrapper);
        Long[] arr = new Long[menuList.size()];
        for (int i = 0; i < menuList.size(); i++) {
            arr[i] = menuList.get(i).getMenuId().longValue();
        }
        return arr;
    }


    @Override
    @Transactional
    public void updatePlatformMenu(PlatformMenuVo platformMenuVo) {
        SysPlatform sysPlatform = new SysPlatform();
        sysPlatform.setPlatformName(platformMenuVo.getPlatformName());
        sysPlatform.setIcon(platformMenuVo.getIcon());
        sysPlatform.setOrderNum(platformMenuVo.getOrderNum());
        sysPlatform.setPlatformType(platformMenuVo.getPlatformType());
        if (platformMenuVo.getPlatformId().intValue() == 0) {
            sysPlatform.setCreateBy(SecurityUtils.getUsername());
            sysPlatform.setCreateTime(LocalDateTime.now());
            this.save(sysPlatform);
            Integer fid = sysPlatform.getId();
            platformMenuVo.setPlatformId(fid.longValue());
        }else {
            sysPlatform.setId(platformMenuVo.getPlatformId().intValue());
            sysPlatform.setUpdateBy(SecurityUtils.getUsername());
            sysPlatform.setUpdateTime(LocalDateTime.now());
            this.updateById(sysPlatform);
        }

        List arr3 = new ArrayList();
        Long[] arr = platformMenuVo.getMenuIds();
        //菜单ID
        List<SysMenu> menuIdList = baseMapper.getMenuIdListOne(arr, "F");
        Long arr1[] = new Long[menuIdList.size()];
        for (int i = 0; i < menuIdList.size(); i++) {
            arr1[i] = menuIdList.get(i).getParentId();
        }
        //目录ID
        if(arr1.length > 0){
            List<SysMenu> menuIdListOne = baseMapper.getMenuIdListOne(arr1, "C");
            Long arr2[] = new Long[menuIdListOne.size()];
            for (int i = 0; i < menuIdListOne.size(); i++) {
                arr2[i] = menuIdListOne.get(i).getParentId();
            }
            List<SysMenu> menuIdListTwo = baseMapper.getMenuIdListOne(arr, "C");
            Long arr4[] = new Long[menuIdListTwo.size()];
            for (int i = 0; i < menuIdListTwo.size(); i++) {
                arr4[i] = menuIdListTwo.get(i).getParentId();
            }

            Long[] arr5 = arrayMerge(arr1, arr2);
            Long[] arr6 = arrayMerge(arr5, arr4);
            Long[] arr7 = arrayMerge(arr6, arr);
            for (int i = 0; i < arr7.length; i++) {
                if (!arr3.contains(arr7[i])) {
                    arr3.add(arr7[i]);
                }
            }
        }else{
            List<SysMenu> menuIdListTwo = baseMapper.getMenuIdListOne(arr, "C");
            Long arr4[] = new Long[menuIdListTwo.size()];
            for (int i = 0; i < menuIdListTwo.size(); i++) {
                arr4[i] = menuIdListTwo.get(i).getParentId();
            }

            Long[] arr7 = arrayMerge(arr4, arr);
            for (int i = 0; i < arr7.length; i++) {
                if (!arr3.contains(arr7[i])) {
                    arr3.add(arr7[i]);
                }
            }
        }
        // 删除租户菜单关联
        baseMapper.deletePlatformMenuBy(platformMenuVo.getPlatformId());
        //新增菜单
        if (arr3.size() > 0) {
            for (int i = 0; i < arr3.size(); i++) {
                SysPlatformMenu sysPlatformMenu = new SysPlatformMenu();
                sysPlatformMenu.setMenuId((Long) arr3.get(i));
                sysPlatformMenu.setPlatformId(platformMenuVo.getPlatformId());
                sysPlatformMenuService.save(sysPlatformMenu);
            }
        }

    }

    @Transactional
    public void updateMobilePlatformMenu(MobilePlatformMenuVO mobilePlatformMenuVO) {
        SysPlatform sysPlatform = new SysPlatform();
        if (mobilePlatformMenuVO.getPlatformId().intValue() == 0) {
            sysPlatform.setPlatformName(mobilePlatformMenuVO.getPlatformName());
            sysPlatform.setCreateBy(SecurityUtils.getUsername());
            sysPlatform.setCreateTime(LocalDateTime.now());
            this.save(sysPlatform);
            Integer fid = sysPlatform.getId();
            mobilePlatformMenuVO.setPlatformId(fid.longValue());
        }else {
            sysPlatform.setId(mobilePlatformMenuVO.getPlatformId().intValue());
            sysPlatform.setPlatformName(mobilePlatformMenuVO.getPlatformName());
            sysPlatform.setUpdateBy(SecurityUtils.getUsername());
            sysPlatform.setUpdateTime(LocalDateTime.now());
            this.updateById(sysPlatform);
        }

        List arr3 = new ArrayList();
        Long[] arr = mobilePlatformMenuVO.getMenuIds();
        //菜单ID
        List<SysMobileMenu> menuIdList = baseMapper.getMobileMenuIdListOne(arr, "F");
        Long arr1[] = new Long[menuIdList.size()];
        for (int i = 0; i < menuIdList.size(); i++) {
            arr1[i] = menuIdList.get(i).getParentId();
        }
        //目录ID
        if(arr1.length > 0){
            List<SysMobileMenu> menuIdListOne = baseMapper.getMobileMenuIdListOne(arr1, "C");
            Long arr2[] = new Long[menuIdListOne.size()];
            for (int i = 0; i < menuIdListOne.size(); i++) {
                arr2[i] = menuIdListOne.get(i).getParentId();
            }
            List<SysMobileMenu> menuIdListTwo = baseMapper.getMobileMenuIdListOne(arr, "C");
            Long arr4[] = new Long[menuIdListTwo.size()];
            for (int i = 0; i < menuIdListTwo.size(); i++) {
                arr4[i] = menuIdListTwo.get(i).getParentId();
            }

            Long[] arr5 = arrayMerge(arr1, arr2);
            Long[] arr6 = arrayMerge(arr5, arr4);
            Long[] arr7 = arrayMerge(arr6, arr);
            for (int i = 0; i < arr7.length; i++) {
                if (!arr3.contains(arr7[i])) {
                    arr3.add(arr7[i]);
                }
            }
        }else{
            List<SysMobileMenu> menuIdListTwo = baseMapper.getMobileMenuIdListOne(arr, "C");
            Long arr4[] = new Long[menuIdListTwo.size()];
            for (int i = 0; i < menuIdListTwo.size(); i++) {
                arr4[i] = menuIdListTwo.get(i).getParentId();
            }
            Long[] arr7 = arrayMerge(arr4, arr);
            for (int i = 0; i < arr7.length; i++) {
                if (!arr3.contains(arr7[i])) {
                    arr3.add(arr7[i]);
                }
            }
        }

        // 删除租户菜单关联
        baseMapper.deleteMobilePlatformMenuBy(mobilePlatformMenuVO.getPlatformId());
        //新增菜单
        if (arr3.size() > 0) {
            for (int i = 0; i < arr3.size(); i++) {
                SysMobilePlatformMenu sysMobilePlatformMenu = new SysMobilePlatformMenu();
                sysMobilePlatformMenu.setMenuId(((Long) arr3.get(i)).intValue());
                sysMobilePlatformMenu.setPlatformId(mobilePlatformMenuVO.getPlatformId().intValue());
                sysMobilePlatformMenuService.save(sysMobilePlatformMenu);
            }
        }

    }

    public Long[] arrayMerge(Long[] arr1, Long[] arr2) {
        Long arr3[] = new Long[arr1.length + arr2.length];
        for (int i = 0; i < arr1.length; i++) {
            arr3[i] = arr1[i];
        }
        for (int i = 0; i < arr2.length; i++) {
            arr3[i + arr1.length] = arr2[i];
        }
        return arr3;
    }

    @Override
    public List<SysPlatformVo> getPlatformAllList() {
        List<SysPlatformVo> list1 = baseMapper.getPlatformList(null, null, null, null, null, null);
        return list1;
    }

    /**
     * 通过应用ID删除应用
     *
     * @param platformId 应用ID
     * @return 结果
     */
    @Override
    @Transactional
    public void deletePlatformById(Integer platformId) {
        baseMapper.deleteById(platformId);
    }

}