package cn.com.tenlion.bigdata.service.bigdatatemplate.impl;

import cn.com.tenlion.bigdata.dao.bigdatatemplate.IBigdataTemplateDao;
import cn.com.tenlion.bigdata.dao.bigdatatemplatearea.IBigdataTemplateAreaDao;
import cn.com.tenlion.bigdata.pojo.dtos.bigdatatemplate.BigdataTemplateDTO;
import cn.com.tenlion.bigdata.pojo.dtos.bigdatatemplate.BigdataTemplateFileDTO;
import cn.com.tenlion.bigdata.pojo.dtos.bigdatatemplatearea.BigdataTemplateAreaDTO;
import cn.com.tenlion.bigdata.pojo.dtos.bigdatatemplatearea.BigdataTemplateAreaFileDTO;
import cn.com.tenlion.bigdata.pojo.vos.bigdatatemplate.BigdataTemplateContentVO;
import cn.com.tenlion.bigdata.pojo.vos.bigdatatemplate.BigdataTemplateVO;
import cn.com.tenlion.bigdata.service.bigdatatemplate.IBigdataTemplateService;
import cn.com.tenlion.bigdata.service.bigdatatemplatearea.IBigdataTemplateAreaService;
import cn.com.tenlion.bigdata.util.BigDataResult;
import cn.com.tenlion.freemarker.pojo.vos.templaterecord.TemplateRecordVO;
import cn.com.tenlion.freemarker.util.TemplateBuilderUtil;
import cn.com.tenlion.freemarker.util.TemplateCreateData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.util.concurrent.AbstractService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SaveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.exceptions.UpdateException;
import ink.wgink.module.dictionary.service.IDataService;
import ink.wgink.module.dictionary.service.impl.DataServiceImpl;
import ink.wgink.module.file.service.IFileService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.pos.FilePO;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.date.DateUtil;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: BigdataTemplateServiceImpl
 * @Description: 大数据展示的模板管理
 * @Author: WenG
 * @Date: 2020-10-20 10:49
 * @Version: 1.0
 **/
@Service
public class BigdataTemplateServiceImpl extends DefaultBaseService implements IBigdataTemplateService {

    @Autowired
    private IBigdataTemplateDao bigdataTemplateDao;

    @Autowired
    private IBigdataTemplateAreaDao bigdataTemplateAreaDao;

    @Autowired
    private IBigdataTemplateAreaService iBigdataTemplateAreaService;

    @Autowired
    private IFileService iFileService;

    @Autowired
    private IDataService iDataService;

    @Override
    public SuccessResult saveBigdataTemplate(BigdataTemplateVO bigdataTemplateVO) throws Exception {
        saveBigdataTemplateInfo(null, bigdataTemplateVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveBigdataTemplateByToken(String token, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        saveBigdataTemplateInfo(token, bigdataTemplateVO);
        return new SuccessResult();
    }

    @Override
    public String saveBigdataTemplateReturnId(BigdataTemplateVO bigdataTemplateVO) throws Exception {
        return saveBigdataTemplateInfoReturnId(null, bigdataTemplateVO);
    }

    @Override
    public String saveBigdataTemplateByTokenReturnId(String token, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        return saveBigdataTemplateInfoReturnId(token, bigdataTemplateVO);
    }

    /**
     * 新增大数据展示的模板管理
     *
     * @param token
     * @param bigdataTemplateVO
     * @throws Exception
     */
    private void saveBigdataTemplateInfo(String token, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        saveBigdataTemplateInfoReturnId(token, bigdataTemplateVO);
    }

    /**
     * 新增大数据展示的模板管理
     *
     * @param token
     * @param bigdataTemplateVO
     * @return bigdataTemplateId
     * @throws Exception
     */
    private String saveBigdataTemplateInfoReturnId(String token, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        String bigdataTemplateId = UUIDUtil.getUUID();
        /**
         * 判断编码是否已经存在
         */
        BigdataTemplateDTO dto = bigdataTemplateDao.getBigdataTemplateByCode(bigdataTemplateVO.getBigdataTemplateCode());
        if(dto != null) {
            throw new SaveException("该编码已经存在");
        }
        bigdataTemplateVO.setBigdataTemplateContent("<style type=\"text/css\">\n" +
                "\t/* \n" +
                "\t* 边框样式Start\n" +
                "\t*/\n" +
                "\t." + bigdataTemplateVO.getBigdataTemplateCode()+ "{\n" +
                "\t\tposition: relative;\n" +
                "\t\tborder: 1px solid #0bc4e9;\n" +
                "\t\tbackground: rgba(0,72,115,0.28);\n" +
                "\t}\n" +
                "\t." + bigdataTemplateVO.getBigdataTemplateCode()+ ":hover{\n" +
                "\t\tbox-shadow:\n" +
                "\t\t-5px 0px 2px rgba(255,255,255,0.1),   \n" +
                "\t\t0px -5px 2px rgba(255,255,255,0.1),  \n" +
                "\t\t5px 0px 2px rgba(255,255,255,0.1),  \n" +
                "\t\t0px 5px 2px rgba(255,255,255,0.1);\n" +
                "\t\tbackground: rgba(255,255,255,0.1);\n" +
                "\t}\n" +
                "\t/** \n" +
                "\t* 边框样式End\n" +
                "\t*/\n" +
                "</style>\n" +
                "<script type=\"text/javascript\">\n" +
                "$(window).load(function(){\n" +
                "\t// 背景粒子特效,如果不需要请注释掉\n" +
                "\tinitParticlesJs();\n" +
                "});\n" +
                "\n" +
                "var initParticlesJs = function() {\n" +
                "\tparticlesJS('container',\n" +
                "\t{\n" +
                "\t\t\"particles\": {\n" +
                "\t\t\t\"number\": {\n" +
                "\t\t\t\t\"value\": 160,//数量\n" +
                "\t\t\t\t\"density\": {\n" +
                "\t\t\t\t\t\"enable\": true, //启用粒子的稀密程度\n" +
                "\t\t\t\t\t\"value_area\": 800 //区域散布密度大小\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\t\"color\": {\n" +
                "\t\t\t\t\"value\": \"#362cff\" //原子的颜色\n" +
                "\t\t\t},\n" +
                "\t\t\t\"shape\": {\n" +
                "\t\t\t\t\"type\": \"circle\", //原子的形状 \"circle\" ,\"edge\" ,\"triangle\" ,\"polygon\" ,\"star\" ,\"image\" ,[\"circle\", \"triangle\", \"image\"]\n" +
                "\t\t\t\t\"stroke\": {\n" +
                "\t\t\t\t\t\"width\": 0, //原子的宽度\n" +
                "\t\t\t\t\t\"color\": \"#362cff\" //原子颜色\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"polygon\": {\n" +
                "\t\t\t\t\t\"nb_sides\": 5 // 原子的多边形边数\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\t\"opacity\": {\n" +
                "\t\t\t\t\"value\": 1, //不透明度\n" +
                "\t\t\t\t\"random\": true, //随机不透明度\n" +
                "\t\t\t\t\"anim\": {\n" +
                "\t\t\t\t\t\"enable\": true, //渐变动画\n" +
                "\t\t\t\t\t\"speed\": 1, // 渐变动画速度\n" +
                "\t\t\t\t\t\"opacity_min\": 0, //渐变动画不透明度\n" +
                "\t\t\t\t\t\"sync\": true\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\t\"size\": {\n" +
                "\t\t\t\t\"value\": 3, //原子大小\n" +
                "\t\t\t\t\"random\": true, // 原子大小随机\n" +
                "\t\t\t\t\"anim\": {\n" +
                "\t\t\t\t\t\"enable\": false, // 原子渐变\n" +
                "\t\t\t\t\t\"speed\": 4, //原子渐变速度\n" +
                "\t\t\t\t\t\"size_min\": 0.3,\n" +
                "\t\t\t\t\t\"sync\": false\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\t\"line_linked\": {\n" +
                "\t\t\t\t\"enable\": true, //连接线  -- 网状\n" +
                "\t\t\t\t\"distance\": 150, //连接线距离\n" +
                "\t\t\t\t\"color\": \"#ffffff\", //连接线颜色\n" +
                "\t\t\t\t\"opacity\": 0.4, //连接线不透明度\n" +
                "\t\t\t\t\"width\": 1 //连接线的宽度\n" +
                "\t\t\t},\n" +
                "\t\t\t\"move\": {\n" +
                "\t\t\t\t\"enable\": true, //原子移动\n" +
                "\t\t\t\t\"speed\": 8, //原子移动速度\n" +
                "\t\t\t\t\"direction\": \"none\", //原子移动方向   \"none\" ,\"top\" ,\"top-right\" ,\"right\" ,\"bottom-right\" ,\"bottom\" ,\"bottom-left\" ,\"left\" ,\"top-left\"\n" +
                "\t\t\t\t\"random\": true, //移动随机方向\n" +
                "\t\t\t\t\"straight\": false, //直接移动\n" +
                "\t\t\t\t\"out_mode\": \"out\", //是否移动出画布\n" +
                "\t\t\t\t\"bounce\": false, //是否跳动移动\n" +
                "\t\t\t\t\"attract\": {\n" +
                "\t\t\t\t\t\"enable\": true, // 原子之间吸引\n" +
                "\t\t\t\t\t\"rotateX\": 600, //原子之间吸引X水平距离\n" +
                "\t\t\t\t\t\"rotateY\": 600  //原子之间吸引Y水平距离\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"interactivity\": {\n" +
                "\t\t\t\"detect_on\": \"canvas\", //原子之间互动检测 \"canvas\", \"window\"\n" +
                "\t\t\t\"events\": {\n" +
                "\t\t\t\t\"onhover\": {\n" +
                "\t\t\t\t\t\"enable\": true, //悬停\n" +
                "\t\t\t\t\t\"mode\": \"bubble\" //悬停模式      \"grab\"抓取临近的,\"bubble\"泡沫球效果,\"repulse\"击退效果,[\"grab\", \"bubble\"]\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"onclick\": {\n" +
                "\t\t\t\t\t\"enable\": false,  //点击效果\n" +
                "\t\t\t\t\t\"mode\": \"repulse\"  //点击效果模式   \"push\" ,\"remove\" ,\"bubble\" ,\"repulse\" ,[\"push\", \"repulse\"]\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\t\"resize\": true // 互动事件调整\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"modes\": {\n" +
                "\t\t\t\t\t\"grab\": {\n" +
                "\t\t\t\t\t\t\"distance\": 100, //原子互动抓取距离\n" +
                "\t\t\t\t\t\t\"line_linked\": {\n" +
                "\t\t\t\t\t\t\"opacity\": 0.8  //原子互动抓取距离连线不透明度\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"bubble\": {\n" +
                "\t\t\t\t\t\"distance\": 250, //原子抓取泡沫效果之间的距离\n" +
                "\t\t\t\t\t\"size\": 4, // 原子抓取泡沫效果之间的大小\n" +
                "\t\t\t\t\t\"duration\": 2, //原子抓取泡沫效果之间的持续事件\n" +
                "\t\t\t\t\t\"opacity\": 1, //原子抓取泡沫效果透明度\n" +
                "\t\t\t\t\t\"speed\": 3\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"repulse\": {\n" +
                "\t\t\t\t\t\"distance\": 400, //击退效果距离\n" +
                "\t\t\t\t\t\"duration\": 0.4 //击退效果持续事件\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"push\": {\n" +
                "\t\t\t\t\t\"particles_nb\": 4 //粒子推出的数量\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"remove\": {\n" +
                "\t\t\t\t\t\"particles_nb\": 2\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"retina_detect\": true\n" +
                "\t});\n" +
                "}\n" +
                "</script>");
        Map<String, Object> params = HashMapUtil.beanToMap(bigdataTemplateVO);
        params.put("bigdataTemplateId", bigdataTemplateId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        bigdataTemplateDao.saveBigdataTemplate(params);
        return bigdataTemplateId;
    }

    @Override
    public SuccessResult removeBigdataTemplate(String ids) throws RemoveException {
        removeBigdataTemplateInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeBigdataTemplateByToken(String token, String ids) throws RemoveException {
        removeBigdataTemplateInfo(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除大数据展示的模板管理
     *
     * @param token
     * @param ids
     */
    private void removeBigdataTemplateInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("bigdataTemplateIds", Arrays.asList(ids.split("_")));
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        bigdataTemplateDao.removeBigdataTemplate(params);
    }

    @Override
    public void deleteBigdataTemplate(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("bigdataTemplateIds", Arrays.asList(ids.split("_")));
        bigdataTemplateDao.deleteBigdataTemplate(params);
    }

    @Override
    public SuccessResult updateBigdataTemplate(String bigdataTemplateId, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        updateBigdataTemplateInfo(null, bigdataTemplateId, bigdataTemplateVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateBigdataTemplateByToken(String token, String bigdataTemplateId, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        updateBigdataTemplateInfo(token, bigdataTemplateId, bigdataTemplateVO);
        return new SuccessResult();
    }

    /**
     * 修改大数据展示的模板管理
     *
     * @param token
     * @param bigdataTemplateId
     * @param bigdataTemplateVO
     */
    private void updateBigdataTemplateInfo(String token, String bigdataTemplateId, BigdataTemplateVO bigdataTemplateVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(bigdataTemplateVO);
        params.put("bigdataTemplateId", bigdataTemplateId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        bigdataTemplateDao.updateBigdataTemplate(params);
    }

    @Override
    public BigdataTemplateDTO getBigdataTemplateById(String bigdataTemplateId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("bigdataTemplateId", bigdataTemplateId);
        BigdataTemplateDTO dto = bigdataTemplateDao.getBigdataTemplate(params);
        if(dto == null ) {
            return null;
        }
        List<BigdataTemplateAreaDTO> areaList = iBigdataTemplateAreaService.getBigdataTemplateAreaByBigdataTemplateId(bigdataTemplateId);
        dto.setAreaList(areaList);
        return dto;
    }

    @Override
    public List<BigdataTemplateDTO> listBigdataTemplate(Map<String, Object> params) throws SearchException {
        return bigdataTemplateDao.listBigdataTemplate(params);
    }

    @Override
    public SuccessResultList<List<BigdataTemplateDTO>> listPageBigdataTemplate(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<BigdataTemplateDTO> bigdataTemplateDTOs = bigdataTemplateDao.listBigdataTemplate(page.getParams());
        PageInfo<BigdataTemplateDTO> pageInfo = new PageInfo<>(bigdataTemplateDTOs);
        return new SuccessResultList<>(bigdataTemplateDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer countNumberBigdataTemplate(Map<String, Object> params) throws SearchException {
        Integer count = bigdataTemplateDao.countBigdataTemplate(params);
        return count == null ? 0 : count;
    }

    @Override
    public SuccessResultData<Integer> countBigdataTemplate(Map<String, Object> params) throws SearchException {
        return new SuccessResultData<>(countNumberBigdataTemplate(params));
    }

    @Override
    public SuccessResult buiderBigdataTemplateByBigdataTemplateCode(String bigdataTemplateCode) throws Exception {
        Map<String, Object> params = super.getHashMap(1);
        params.put("bigdataTemplateCode", bigdataTemplateCode);
        // 页面
        BigdataTemplateDTO dto = bigdataTemplateDao.getBigdataTemplateByCode(bigdataTemplateCode);
        // 页面区域
        List<BigdataTemplateAreaDTO> areaList = iBigdataTemplateAreaService.getBigdataTemplateAreaByBigdataTemplateId(dto.getBigdataTemplateId());
        Map<String, String> areaFunctionMap = new LinkedHashMap<String, String>();
        // 封装自定义上传的js和css文件
        for (BigdataTemplateAreaDTO area : areaList) {
            areaFunctionMap.put(area.getBigdataModuleId(), area.getBigdataModuleContent());
            // 查找模块附件
            String[] fileList = area.getTemplateAreaFile().split(",");
            List<BigdataTemplateAreaFileDTO> templateAreaFileDTOList = new ArrayList<BigdataTemplateAreaFileDTO>();
            for (String file : fileList) {
                Map<String, Object> queryParams = super.getHashMap(1);
                queryParams.put("fileId", file);
                FilePO filePO = iFileService.getPO(queryParams);
                if(filePO != null) {
                    BigdataTemplateAreaFileDTO fileDTO = new BigdataTemplateAreaFileDTO();
                    fileDTO.setTemplateAreaFile(file);
                    fileDTO.setTemplateAreaFileType(filePO.getFileType());
                    templateAreaFileDTOList.add(fileDTO);
                }
            }
            area.setFileList(templateAreaFileDTOList);
            // 查询跳转模板
            if(!StringUtils.isEmpty(area.getTemplateAreaJumpTemplateId())) {
                BigdataTemplateDTO bigdataTemplateDTO = getBigdataTemplateById(area.getTemplateAreaJumpTemplateId());
                if(bigdataTemplateDTO != null) {
                    area.setBigdataTemplateJumpUrl(bigdataTemplateDTO.getBigdataTemplateUrl());
                    area.setBigdataTemplateJumpTitle(bigdataTemplateDTO.getBigdataTemplateTitle());
                    area.setBigdataTemplateJumpHeight(bigdataTemplateDTO.getBigdataTemplateHeight());
                    area.setBigdataTemplateJumpWidth(bigdataTemplateDTO.getBigdataTemplateWidth());
                    area.setBigdataTemplateJumpSize(bigdataTemplateDTO.getBigdataTemplateSize());
                }
            }
        }
        // 查找模板附件
        String[] templateFileList = dto.getBigdataTemplateFile().split(",");
        List<BigdataTemplateFileDTO> templateFileDTOList = new ArrayList<BigdataTemplateFileDTO>();
        for (String templateFile : templateFileList) {
            Map<String, Object> queryFileParams = super.getHashMap(1);
            queryFileParams.put("fileId", templateFile);
            FilePO dDDto = iFileService.getPO(queryFileParams);
            if(dDDto != null) {
                BigdataTemplateFileDTO fileDTO = new BigdataTemplateFileDTO();
                fileDTO.setBigdataTemplateFile(dDDto.getFileId());
                fileDTO.setBigdataTemplateFilePath(dDDto.getFilePath());
                fileDTO.setBigdataTemplateFileType(dDDto.getFileType());
                templateFileDTOList.add(fileDTO);
            }
        }
        /**
         * 生成静态文件 ， 引入freemark模块使用生成静态文件
         */
        dto.setFileList(templateFileDTOList);
        dto.setAreaList(areaList);
        Map<String, Object> templateDataMap = HashMapUtil.beanToMap(dto);
        templateDataMap.put("areaFunctionMap", areaFunctionMap);
        TemplateCreateData createData = new TemplateCreateData();
        // C0001编码的模板配置文件是大数据的模板配置文件
        createData.setTemplateCode("C0001");
        createData.setBusinessId(dto.getBigdataTemplateId());
        createData.setBusinessContent(dto.getBigdataTemplateTitle());
        createData.setTemplateDataMap(templateDataMap);
        // 生成模板文件
        TemplateRecordVO recordVo =  TemplateBuilderUtil.getInstance().createFile(createData);
        /**
         * 更新访问路径
         */
        Map<String, Object> updateParams = super.getHashMap(2);
        updateParams.put("bigdataTemplateUrl", recordVo.getTemplateRecordUrl());
        updateParams.put("bigdataTemplateId", dto.getBigdataTemplateId());
        bigdataTemplateDao.updataBigdataTemplateUrl(updateParams);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateBigdataTemplatePublishStatus(String bigdataTemplateId, Boolean bigdataTemplatePublishStatus) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(3);
        params.put("bigdataTemplateId", bigdataTemplateId);
        params.put("bigdataTemplatePublishStatus", bigdataTemplatePublishStatus ? "1" : "0");
        params.put("bigdataTemplatePublishTime", DateUtil.getTime());
        setUpdateInfo(params);
        bigdataTemplateDao.updateBigdataTemplatePublishStatus(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult buiderBigdataTemplateAll() throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(3);
        params.put("bigdataTemplatePublishStatus", "1");
        List<BigdataTemplateDTO> dtos = listBigdataTemplate(params);
        for (BigdataTemplateDTO dto : dtos) {
            buiderBigdataTemplateByBigdataTemplateCode(dto.getBigdataTemplateCode());
        }
        return new SuccessResult();
    }

    @Override
    public BigDataResult querySqlAll(String areaId) throws Exception {
        /**
         * 查询区域对象
         */
        BigDataResult bigDataResult = BigDataResult.getInstance();
        BigdataTemplateAreaDTO dto = iBigdataTemplateAreaService.getBigdataTemplateAreaById(areaId);
        if(bigDataResult == null) {
            bigDataResult.setState("400");
            bigDataResult.setMsg("区域已经不存在");
            return bigDataResult;
        }
        if(!"3".equals(dto.getTemplateAreaSource())) {
            bigDataResult.setState("400");
            bigDataResult.setMsg("该区域类型已变");
            return bigDataResult;
        }
        try{
            // 对SQL进行解密 , 解密成功后再进行查询
            List<Map<String,Object>> list = bigdataTemplateDao.querySqlAll(dto.getTemplateAreaServerData());
            bigDataResult.setList(list);
        }catch(Exception e) {
            bigDataResult.setState("400");
            bigDataResult.setMsg(e.getMessage());
        }
        return bigDataResult;
    }

    @Override
    public BigDataResult querySqCheck(String querySql) throws Exception {
        BigDataResult bigDataResult = BigDataResult.getInstance();
        try{
            // 对SQL进行解密 , 解密成功后再进行查询
            List<Map<String,Object>> list = bigdataTemplateDao.querySqlAll(querySql);
            bigDataResult.setList(list);
        }catch(Exception e) {
            bigDataResult.setState("400");
            bigDataResult.setMsg(e.getMessage());
        }
        return bigDataResult;
    }

    @Override
    public SuccessResult updateBigdataTemplateContent(String bigdataTemplateId, BigdataTemplateContentVO bigdataTemplateContentVO) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("bigdataTemplateId", bigdataTemplateId);
        params.put("bigdataModuleContent", bigdataTemplateContentVO.getBigdataTemplateContent());
        if(StringUtils.isEmpty(bigdataTemplateContentVO.getBigdataTemplateContent())) {
            throw new UpdateException("代码内容不能为空!");
        }
        setUpdateInfo(params);
        bigdataTemplateDao.updateBigdataTemplateContent(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult copyBigdataTemplate(String id, String bigdataTemplateCode, String bigdataTemplateTitle) throws Exception {
        BigdataTemplateDTO dto = bigdataTemplateDao.getBigdataTemplateByCode(bigdataTemplateCode);
        if(dto != null) {
            throw new SaveException("该编码已经存在");
        }
        // 查出模板
        String bigdataTemplateId = UUIDUtil.getUUID();
        // 查出区域
        BigdataTemplateDTO contentDto = getBigdataTemplateById(id);
        List<BigdataTemplateAreaDTO> areaList = contentDto.getAreaList();
        for (BigdataTemplateAreaDTO areaDTO : areaList) {
            // 新页面属性
            areaDTO.setBigdataTemplateId(bigdataTemplateId);
            areaDTO.setBigdataTemplateTitle(bigdataTemplateTitle);
            areaDTO.setBigdataTemplateAreaId(UUIDUtil.getUUID());
            Map<String, Object> params = HashMapUtil.beanToMap(areaDTO);
            setSaveInfo(params);
            bigdataTemplateAreaDao.saveBigdataTemplateArea(params);
        }
        contentDto.setBigdataTemplateCode(bigdataTemplateCode);
        contentDto.setBigdataTemplateTitle(bigdataTemplateTitle);
        contentDto.setBigdataTemplateId(bigdataTemplateId);
        contentDto.setBigdataTemplateContent("<style type=\"text/css\">\n" +
        "\t/** \n" +
        "\t* 边框样式Start\n" +
        "\t* 不对文字区域(W0001)进行边框修饰 \n" +
        "\t." + contentDto.getBigdataTemplateCode() + " :not(.W0001){\n" +
        "\t\tposition: relative;\n" +
        "\t\tborder: 1px solid #0bc4e9;\n" +
        "\t\tbackground: rgba(0,72,115,0.28);\n" +
        "\t}\n" +
        "\t." + contentDto.getBigdataTemplateCode() + " :not(.W0001) :before{\n" +
        "\t\tcontent: '';\n" +
        "\t\tposition: absolute;\n" +
        "\t\twidth: 80%;\n" +
        "\t\theight: 100%;\n" +
        "\t\tbottom: -1px;\n" +
        "\t\ttop:-1px;\n" +
        "\t\tleft:10%;\n" +
        "\t\tborder-bottom: 1px solid #007297;\n" +
        "\t\tborder-top: 1px solid #007297;\n" +
        "\t\ttransition: all 0.5s;\n" +
        "\t}\n" +
        "\t." + contentDto.getBigdataTemplateCode() + " :not(.W0001) :after{\n" +
        "\t\tcontent: '';\n" +
        "\t\tposition: absolute;\n" +
        "\t\twidth: 100%;\n" +
        "\t\theight: 80%;\n" +
        "\t\tleft: -1px;\n" +
        "\t\tright:-1px;\n" +
        "\t\ttop:10%;\n" +
        "\t\tborder-left: 1px solid  #007297;\n" +
        "\t\tborder-right: 1px solid  #007297;\n" +
        "\t\ttransition: all 0.5s;\n" +
        "\t}\n" +
        "\t." + contentDto.getBigdataTemplateCode() + " :not(.W0001) :hover::before{\n" +
        "\t\twidth: 0%;\n" +
        "\t\t}\n" +
        "\t." + contentDto.getBigdataTemplateCode() + " :not(.W0001) :hover::after{\n" +
        "\t\theight: 0%;\n" +
        "\t}\n" +
        "\t." + contentDto.getBigdataTemplateCode() + " :not(.W0001) :hover{\n" +
        "\t\tbox-shadow:\n" +
        "\t\t-5px 0px 2px rgba(255,255,255,0.1), \n" +
        "\t\t0px -5px 2px rgba(255,255,255,0.1), \n" +
        "\t\t5px 0px 2px rgba(255,255,255,0.1),  \n" +
        "\t\t0px 5px 2px rgba(255,255,255,0.1);  \n" +
        "\t\tbackground: rgba(255,255,255,0.1);  \n" +
        "\t}\n" +
        "\t* 边框样式End\n" +
        "\t*/\n" +
        "\t.W0001{\n" +
        "\t\tborder: 0px;\n" +
        "\t\tborder-left: 0px;\n" +
        "\t\tborder-right: 0px;\n" +
        "</style>\n" +
        "<script type=\"text/javascript\">\n" +
        "\t/**\n" +
        "\t * 此处可以自定义一些函数对页面元素进行操作\n" +
        "\t */ \n" +
        "</script>");
        Map<String, Object> save = HashMapUtil.beanToMap(contentDto);
        setSaveInfo(save);
        bigdataTemplateDao.saveBigdataTemplate(save);
        return new SuccessResult();
    }

}