package com.cm.common.service.bigdatamodule.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.DateUtil;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.common.dao.bigdatamodule.IBigdataModuleDao;
import com.cm.common.pojo.dtos.bigdatamodule.BigdataModuleDTO;
import com.cm.common.pojo.vos.bigdatamodule.BigdataModuleVO;
import com.cm.common.service.bigdatamodule.IBigdataModuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: BigdataModuleServiceImpl
 * @Description: 大数据展示模块
 * @Author: WenG
 * @Date: 2020-10-19 16:18
 * @Version: 1.0
 **/
@Service
public class BigdataModuleServiceImpl extends AbstractService implements IBigdataModuleService {

    @Autowired
    private IBigdataModuleDao bigdataModuleDao;

    @Override
    public SuccessResult saveBigdataModule(BigdataModuleVO bigdataModuleVO) throws Exception {
        saveBigdataModuleInfo(null, bigdataModuleVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveBigdataModuleByToken(String token, BigdataModuleVO bigdataModuleVO) throws Exception {
        saveBigdataModuleInfo(token, bigdataModuleVO);
        return new SuccessResult();
    }

    @Override
    public String saveBigdataModuleReturnId(BigdataModuleVO bigdataModuleVO) throws Exception {
        return saveBigdataModuleInfoReturnId(null, bigdataModuleVO);
    }

    @Override
    public String saveBigdataModuleByTokenReturnId(String token, BigdataModuleVO bigdataModuleVO) throws Exception {
        return saveBigdataModuleInfoReturnId(token, bigdataModuleVO);
    }

    /**
     * 新增大数据展示模块
     *
     * @param token
     * @param bigdataModuleVO
     * @throws Exception
     */
    private void saveBigdataModuleInfo(String token, BigdataModuleVO bigdataModuleVO) throws Exception {
        saveBigdataModuleInfoReturnId(token, bigdataModuleVO);
    }

    /**
     * 新增大数据展示模块
     *
     * @param token
     * @param bigdataModuleVO
     * @return bigdataModuleId
     * @throws Exception
     */
    private String saveBigdataModuleInfoReturnId(String token, BigdataModuleVO bigdataModuleVO) throws Exception {
        String bigdataModuleId = UUIDUtil.getUUID();
        /**
         * 判断编码是否已经存在
         */
        BigdataModuleDTO dto = bigdataModuleDao.getBigdataModuleByCode(bigdataModuleVO.getBigdataModuleCode());
        if(dto != null) {
            throw new SaveException("该编码已经存在");
        }
        bigdataModuleVO.setBigdataModuleContent("\n<script type=\"text/javascript\">\n\t/**\n" +
                "\t* 将模块的代码封装到此函数中供模板组装调用. \n" +
                "\t* moduleData * 模块数据 {id , data , list , title , params}\n" +
                "\t* serverData * 渲染数据  {data , list , msg , state}\n" +
                "\t* 模块编号 * " + bigdataModuleVO.getBigdataModuleCode() + "\n" +
                "\t* 模块名称 * " + bigdataModuleVO.getBigdataModuleName()+ "\n" +
                "\t* 创建日期 * " + DateUtil.getTime() + "\n" +
                "\t*/\n" +
                "\tfunction "+ bigdataModuleVO.getBigdataModuleCode() +"(moduleData, serverData) {\n" +
                "\t\tconsole.log(moduleData.id + '初始化...');\n" +
                "\t\t// 清除旧的循环任务\n" +
                "\t\techarts.dispose(document.getElementById(moduleData.id));\n" +
                "\t\tvar elem = echarts.init(document.getElementById(moduleData.id));\n" +
                "\t\tvar option = {};\n" +
                "\t\telem.setOption(option);\n\n" +
                "\t}\n</script>");
        Map<String, Object> params = HashMapUtil.beanToMap(bigdataModuleVO);
        params.put("bigdataModuleId", bigdataModuleId);
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        bigdataModuleDao.saveBigdataModule(params);
        return bigdataModuleId;
    }

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

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

    /**
     * 删除大数据展示模块
     *
     * @param token
     * @param ids
     */
    private void removeBigdataModuleInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("bigdataModuleIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        bigdataModuleDao.removeBigdataModule(params);
    }

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

    @Override
    public SuccessResult updateBigdataModule(String bigdataModuleId, BigdataModuleVO bigdataModuleVO) throws Exception {
        updateBigdataModuleInfo(null, bigdataModuleId, bigdataModuleVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateBigdataModuleByToken(String token, String bigdataModuleId, BigdataModuleVO bigdataModuleVO) throws Exception {
        updateBigdataModuleInfo(token, bigdataModuleId, bigdataModuleVO);
        return new SuccessResult();
    }

    /**
     * 修改大数据展示模块
     *
     * @param token
     * @param bigdataModuleId
     * @param bigdataModuleVO
     */
    private void updateBigdataModuleInfo(String token, String bigdataModuleId, BigdataModuleVO bigdataModuleVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(bigdataModuleVO);
        params.put("bigdataModuleId", bigdataModuleId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        bigdataModuleDao.updateBigdataModule(params);
    }

    @Override
    public BigdataModuleDTO getBigdataModuleById(String bigdataModuleId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("bigdataModuleId", bigdataModuleId);
        BigdataModuleDTO dto = bigdataModuleDao.getBigdataModule(params);
        return dto;
    }

    @Override
    public List<BigdataModuleDTO> listBigdataModule(Map<String, Object> params) throws SearchException {
        return bigdataModuleDao.listBigdataModule(params);
    }

    @Override
    public SuccessResultList<List<BigdataModuleDTO>> listPageBigdataModule(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<BigdataModuleDTO> bigdataModuleDTOs = bigdataModuleDao.listBigdataModule(page.getParams());
        PageInfo<BigdataModuleDTO> pageInfo = new PageInfo<>(bigdataModuleDTOs);
        return new SuccessResultList<>(bigdataModuleDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

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

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

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

    @Override
    public SuccessResult updateBigdataModuleSwitch(String bigdataModuleId, Boolean bigdataModuleSwitch) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(3);
        params.put("bigdataModuleId", bigdataModuleId);
        params.put("bigdataModuleSwitch", bigdataModuleSwitch ? "1" : "0");
        setUpdateInfo(params);
        bigdataModuleDao.updateBigdataModuleSwitch(params);
        return new SuccessResult();
    }

}