package io.gisbi.application.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.gisbi.application.module.domain.Module;
import io.gisbi.application.module.mapper.ModuleMapper;
import io.gisbi.application.module.service.IModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gisbi.engine.utils.DateUtils;
import io.gisbi.utils.AuthUtils;
import io.gisbi.visualization.dao.auto.entity.DataVisualizationInfo;
import io.gisbi.visualization.manage.CoreVisualizationManage;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 应用_系统模块 服务实现类
 * </p>
 *
 * @author zhengsl
 * @since 2025-03-25
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements IModuleService {
    @Resource
    private CoreVisualizationManage coreVisualizationManage;
    private DataVisualizationInfo buildDataVisualizationInfo(Module module) {
    DataVisualizationInfo visualizationInfo = new DataVisualizationInfo();
    visualizationInfo.setId(Long.parseLong(module.getId()));
    visualizationInfo.setAppId(module.getAppId());
    visualizationInfo.setName(module.getName());
    visualizationInfo.setPid(Long.parseLong(module.getPid()));
    visualizationInfo.setNodeType("leaf");
    visualizationInfo.setType("dataV");
    visualizationInfo.setCanvasStyleData("{\"width\":1920,...}"); // 建议将该 JSON 字符串抽为常量或配置项
    visualizationInfo.setComponentData("[]");
    return visualizationInfo;
}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createModule(Module module) {
        boolean isok=this.save(module);
        if(isok){
            if(module.getType().equals("03"))//GIS大屏页面和数据大屏页面
            {
                DataVisualizationInfo visualizationInfo = new DataVisualizationInfo();
                visualizationInfo.setId(Long.parseLong(module.getId()));
                visualizationInfo.setAppId(module.getAppId());
                visualizationInfo.setName(module.getName());
                visualizationInfo.setPid(Long.parseLong(module.getPid()));
                visualizationInfo.setNodeType("leaf");
                visualizationInfo.setType("dataV");
                visualizationInfo.setCanvasStyleData("{\"width\":1920,\"height\":1080,\"refreshBrowserEnable\":false,\"refreshBrowserUnit\":\"minute\",\"refreshBrowserTime\":5,\"refreshViewEnable\":false,\"refreshViewLoading\":true,\"refreshUnit\":\"minute\",\"refreshTime\":5,\"popupAvailable\":true,\"popupButtonAvailable\":true,\"suspensionButtonAvailable\":false,\"screenAdaptor\":\"widthFirst\",\"dashboardAdaptor\":\"keepHeightAndWidth\",\"scale\":60,\"scaleWidth\":60,\"scaleHeight\":60,\"backgroundColorSelect\":true,\"backgroundImageEnable\":false,\"backgroundType\":\"backgroundColor\",\"background\":\"\",\"openCommonStyle\":true,\"opacity\":1,\"fontSize\":14,\"fontFamily\":\"PingFang\",\"themeId\":\"10002\",\"color\":\"#fff\",\"backgroundColor\":\"rgba(2, 4, 8, 1)\",\"dashboard\":{\"gap\":\"yes\",\"gapSize\":5,\"showGrid\":false,\"matrixBase\":4,\"resultMode\":\"all\",\"resultCount\":1000,\"themeColor\":\"dark\",\"mobileSetting\":{\"customSetting\":false,\"imageUrl\":null,\"backgroundType\":\"image\",\"color\":\"#fff\"}},\"component\":{\"chartTitle\":{\"show\":true,\"fontSize\":16,\"hPosition\":\"left\",\"vPosition\":\"top\",\"isItalic\":false,\"isBolder\":true,\"remarkShow\":false,\"remark\":\"\",\"fontFamily\":\"\",\"letterSpace\":\"0\",\"fontShadow\":false,\"color\":\"#FFFFFF\",\"remarkBackgroundColor\":\"#5A5C62\"},\"chartColor\":{\"basicStyle\":{\"colorScheme\":\"default\",\"colors\":[\"#1E90FF\",\"#90EE90\",\"#00CED1\",\"#E2BD84\",\"#7A90E0\",\"#3BA272\",\"#2BE7FF\",\"#0A8ADA\",\"#FFD700\"],\"alpha\":100,\"gradient\":false,\"mapStyle\":\"darkblue\",\"areaBaseColor\":\"#5470C6\",\"areaBorderColor\":\"#EBEEF5\",\"gaugeStyle\":\"default\",\"tableBorderColor\":\"#CCCCCC\",\"tableScrollBarColor\":\"rgba(255, 255, 255, 0.5)\",\"zoomButtonColor\":\"#fff\",\"zoomBackground\":\"#000\"},\"misc\":{\"flowMapConfig\":{\"lineConfig\":{\"mapLineGradient\":false,\"mapLineSourceColor\":\"#146C94\",\"mapLineTargetColor\":\"#576CBC\"}},\"nameFontColor\":\"#ffffff\",\"valueFontColor\":\"#5470c6\"},\"tableHeader\":{\"tableHeaderBgColor\":\"#1E90FF\",\"tableHeaderCornerBgColor\":\"#1E90FF\",\"tableHeaderColBgColor\":\"#1E90FF\",\"tableHeaderFontColor\":\"#FFFFFF\",\"tableHeaderCornerFontColor\":\"#FFFFFF\",\"tableHeaderColFontColor\":\"#FFFFFF\"},\"tableCell\":{\"tableItemBgColor\":\"#131E42\",\"tableFontColor\":\"#ffffff\",\"tableItemSubBgColor\":\"#EEEEEE\"}},\"chartCommonStyle\":{\"backgroundColorSelect\":false,\"backdropFilterEnable\":false,\"backgroundImageEnable\":false,\"backgroundType\":\"innerImage\",\"innerImage\":\"board/board_1.svg\",\"outerImage\":null,\"innerPadding\":12,\"borderRadius\":0,\"backdropFilter\":4,\"backgroundColor\":\"#131E42\",\"innerImageColor\":\"#1094E5\"},\"filterStyle\":{\"layout\":\"horizontal\",\"titleLayout\":\"left\",\"labelColor\":\"#ffffff\",\"titleColor\":\"#ffffff\",\"color\":\"#FFFFFF\",\"borderColor\":\"#484747\",\"text\":\"#AFAFAF\",\"bgColor\":\"#131C42\"},\"tabStyle\":{\"headPosition\":\"left\",\"headFontColor\":\"#ffffff\",\"headFontActiveColor\":\"#ffffff\",\"headBorderColor\":\"#000000\",\"headBorderActiveColor\":\"#000000\"},\"seniorStyleSetting\":{\"linkageIconColor\":\"#ffffff\",\"drillLayerColor\":\"#ffffff\",\"pagerColor\":\"#ffffff\"}}}");
                visualizationInfo.setComponentData("[]");
                long id=coreVisualizationManage.preInnerSave(visualizationInfo);
                return true;
            }else if(module.getType().equals("0301")){//数据大屏页面
                DataVisualizationInfo visualizationInfo = new DataVisualizationInfo();
                visualizationInfo.setId(Long.parseLong(module.getId()));
                visualizationInfo.setAppId(module.getAppId());
                visualizationInfo.setName(module.getName());
                visualizationInfo.setPid(Long.parseLong(module.getPid()));
                visualizationInfo.setNodeType("leaf");
                visualizationInfo.setType("dashboard");
                visualizationInfo.setCanvasStyleData("{\"width\":1920,\"height\":1080,\"refreshViewEnable\":false,\"refreshViewLoading\":true,\"refreshUnit\":\"minute\",\"refreshTime\":5,\"scale\":78,\"scaleWidth\":78,\"scaleHeight\":78,\"backgroundColorSelect\":true,\"backgroundImageEnable\":false,\"backgroundType\":\"backgroundColor\",\"background\":\"\",\"openCommonStyle\":true,\"opacity\":1,\"fontSize\":14,\"themeId\":\"10002\",\"color\":\"#000000\",\"backgroundColor\":\"rgba(166, 227, 123, 1)\",\"dashboard\":{\"gap\":\"yes\",\"gapSize\":5,\"resultMode\":\"all\",\"resultCount\":1000,\"themeColor\":\"dark\",\"mobileSetting\":{\"customSetting\":false,\"imageUrl\":null,\"backgroundType\":\"image\",\"color\":\"#000\"},\"showGrid\":false,\"matrixBase\":4},\"component\":{\"chartTitle\":{\"show\":true,\"fontSize\":16,\"hPosition\":\"left\",\"vPosition\":\"top\",\"isItalic\":false,\"isBolder\":true,\"remarkShow\":false,\"remark\":\"\",\"fontFamily\":\"\",\"letterSpace\":\"0\",\"fontShadow\":false,\"color\":\"#FFFFFF\",\"remarkBackgroundColor\":\"#5A5C62\"},\"chartColor\":{\"basicStyle\":{\"colorScheme\":\"default\",\"colors\":[\"#1E90FF\",\"#90EE90\",\"#00CED1\",\"#E2BD84\",\"#7A90E0\",\"#3BA272\",\"#2BE7FF\",\"#0A8ADA\",\"#FFD700\"],\"alpha\":100,\"gradient\":false,\"mapStyle\":\"darkblue\",\"areaBaseColor\":\"#5470C6\",\"areaBorderColor\":\"#EBEEF5\",\"gaugeStyle\":\"default\",\"tableBorderColor\":\"#CCCCCC\",\"tableScrollBarColor\":\"#FFFFFF80\",\"zoomButtonColor\":\"#fff\",\"zoomBackground\":\"#000\"},\"misc\":{\"flowMapConfig\":{\"lineConfig\":{\"mapLineGradient\":false,\"mapLineSourceColor\":\"#146C94\",\"mapLineTargetColor\":\"#576CBC\"}},\"nameFontColor\":\"#ffffff\",\"valueFontColor\":\"#5470c6\"},\"tableHeader\":{\"tableHeaderBgColor\":\"#1E90FF\",\"tableHeaderCornerBgColor\":\"#1E90FF\",\"tableHeaderColBgColor\":\"#1E90FF\",\"tableHeaderFontColor\":\"#FFFFFF\",\"tableHeaderCornerFontColor\":\"#FFFFFF\",\"tableHeaderColFontColor\":\"#FFFFFF\"},\"tableCell\":{\"tableItemBgColor\":\"#131E42\",\"tableFontColor\":\"#FFFFFF\",\"tableItemSubBgColor\":\"#EEEEEE\"}},\"chartCommonStyle\":{\"backgroundColorSelect\":true,\"backdropFilterEnable\":true,\"backgroundImageEnable\":false,\"backgroundType\":\"innerImage\",\"innerImage\":\"board/board_1.svg\",\"outerImage\":null,\"innerPadding\":1,\"borderRadius\":0,\"backdropFilter\":4,\"backgroundColor\":\"rgba(19,28,66,1)\",\"innerImageColor\":\"#1094E5\"},\"filterStyle\":{\"layout\":\"vertical\",\"titleLayout\":\"left\",\"labelColor\":\"#FFFFFF\",\"titleColor\":\"#FFFFFF\",\"color\":\"#FFFFFF\",\"borderColor\":\"#484747\",\"text\":\"#AFAFAF\",\"bgColor\":\"#131C42\"},\"tabStyle\":{\"headPosition\":\"left\",\"headFontColor\":\"#FFFFFF\",\"headFontActiveColor\":\"#FFFFFF\",\"headBorderColor\":\"#131E42\",\"headBorderActiveColor\":\"#131E42\"},\"seniorStyleSetting\":{\"linkageIconColor\":\"#FFFFFF\",\"drillLayerColor\":\"#FFFFFF\",\"pagerColor\":\"#FFFFFFFF\"}},\"fontFamily\":\"PingFang\",\"suspensionButtonAvailable\":false,\"screenAdaptor\":\"widthFirst\",\"dashboardAdaptor\":\"keepHeightAndWidth\",\"refreshBrowserEnable\":false,\"refreshBrowserUnit\":\"minute\",\"refreshBrowserTime\":5,\"popupAvailable\":true,\"popupButtonAvailable\":true}");
                visualizationInfo.setComponentData("[]");
                long id=coreVisualizationManage.preInnerSave(visualizationInfo);
                return true;
            }
        }
        return false;
    }

    // 新增existsChildrenByPid实现
    @Override
    public boolean existsChildrenByPid(String  appId,String pid) {
        return this.count(new LambdaQueryWrapper<Module>()
                .eq(Module::getAppId, appId).eq(Module::getPid, pid)) > 0;
    }

    @Override
    public boolean initAppModules(String appId) {
        // 01:登录页面  02:主框架页面  03:GIS大屏页面  04:二级弹窗页面  05:数据填报页面   06:文档资源页面 09:自定义页面
        List<Module> list=new ArrayList<>();
        list.add(createInitModule(appId,"登录页面","01",1));
        list.add(createInitModule(appId,"主框架页面","02",2));
        list.add(createInitModule(appId,"GIS大屏页面","03",3));
        list.add(createInitModule(appId,"数据看板页面","0301",4));
        list.add(createInitModule(appId,"二级弹窗页面","04",5));
        list.add(createInitModule(appId,"数据填报页面","05",6));
        list.add(createInitModule(appId,"文档资源页面","06",7));
        list.add(createInitModule(appId,"自定义页面","09",9));
        boolean isok=this.saveBatch(list);
        return isok;
    }



    private Module createInitModule(String appId,String name,String type,Integer sort){
        Module initModule=new Module(
                appId,
                name,
                "00",
                1,
                "01",
                type,
                0,
                sort,
                LocalDateTime.now(),
                AuthUtils.getUser().getUserId().toString()
        );
        return initModule;
    }

    @Override
    public boolean deleteModulesByAppID(String appId) {
        if(this.remove(new LambdaQueryWrapper<Module>().eq(Module::getAppId,appId))){
            return true;
        }
        return false;
    }
}