package com.usoftchina.bi.server.service.dashboard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.usoftchina.bi.core.base.*;
import com.usoftchina.bi.core.exception.MyException;
import com.usoftchina.bi.core.jdbc.DynamicDataSourceContextHolder;
import com.usoftchina.bi.core.utils.CollectionUtils;
import com.usoftchina.bi.core.utils.EncryUtil;
import com.usoftchina.bi.server.dao.chart.ChartsConfigMapper;
import com.usoftchina.bi.server.dao.dashboard.DashboardsMapper;
import com.usoftchina.bi.server.dao.dataSource.DataConnectorMapper;
import com.usoftchina.bi.server.dao.user.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.usoftchina.bi.server.model.bo.DashOrder;
import com.usoftchina.bi.server.model.bo.Screen;
import com.usoftchina.bi.server.model.bo.ShareReqBO;
import com.usoftchina.bi.server.model.po.ChartConfig;
import com.usoftchina.bi.server.model.po.Dashboards;
import com.usoftchina.bi.server.model.po.DataConnector;
import com.usoftchina.bi.server.model.vo.configVo.ChangeOrderInfo;
import com.usoftchina.bi.server.model.vo.configVo.ColumnRenameInfo;
import com.usoftchina.bi.server.model.vo.configVo.DashboardsInfo;
import com.usoftchina.bi.server.model.vo.dataVo.*;
import com.usoftchina.bi.server.service.chart.ChartsUtilService;
import com.usoftchina.bi.core.utils.GetTokenDataUtil;
import com.usoftchina.bi.server.service.common.MessageLogService;
import com.usoftchina.bi.server.service.dataSource.DataConnectorService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.format.CellFormatType;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class DashboardsService {
    @Autowired
    DashboardsMapper dashboardsMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ChartsUtilService chartsUtilService;
    @Autowired
    private DataConnectorService dataConnectorService;
    @Autowired
    private ChartsConfigMapper chartsConfigMapper;
    @Autowired
    private MessageLogService messageLogService;
    @Autowired
    private DefaultDashboardService defaultDashboardService;
    @Autowired
    private DataConnectorMapper dataConnectorMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(DashboardsService.class);

    /*
    保存报表
     */
    public RepEntity setDashboards(String token, DashboardsInfo dashboardsInfo) {
        //校验报表编号是否已存在
        String code = validCode(dashboardsInfo.getBdCode(), dashboardsInfo.getId());
        dashboardsInfo.setBdCode(code);
        Map<String, String> stringMap = GetTokenDataUtil.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        String name = stringMap.get("name");
        Dashboards dashboards = new Dashboards();
        BeanUtils.copyProperties(dashboardsInfo, dashboards);
        dashboards.setCreateBy(name);
        dashboards.setCreateId(userId);
        if (dashboardsInfo.getFilters() == null){
            dashboardsInfo.setFilters("");
        }
        dashboardsMapper.setDashboards(dashboards);
        int id = dashboards.getId();
        //保存报表图表关联关系
        String chartIds = dashboardsInfo.getChartIds();
        if (!StringUtils.isEmpty(chartIds)) {
            dashboardsMapper.saveDashboardChartRelation(id, chartIds.split(","));
        }
        return new RepEntity(RepCode.success, id);
    }

    /*
    更新报表
     */
    public RepEntity updateDashboards(String token, DashboardsInfo dashboardsInfo) {
        //校验报表编号是否已存在
        String code = validCode(dashboardsInfo.getBdCode(), dashboardsInfo.getId());
        dashboardsInfo.setBdCode(code);
        Map<String, String> stringMap = GetTokenDataUtil.getTokenData(token);
        int userId = Integer.parseInt(stringMap.get("id"));
        String name = stringMap.get("name");
        int createId = dashboardsMapper.getCreateIdById(dashboardsInfo.getId());
        if (userId != createId) {
            return new RepEntity(RepCode.NoAuthority);
        }
        Dashboards dashboards = new Dashboards();
        BeanUtils.copyProperties(dashboardsInfo, dashboards);
        dashboards.setCreateBy(name);
        dashboards.setCreateId(userId);
        if (dashboardsInfo.getFilters() == null){
            dashboardsInfo.setFilters("");
        }
        dashboardsMapper.updateDashboards(dashboards);
        //更新报表图表关系
        int dashboardId = dashboardsInfo.getId();
        String chartIds = dashboardsInfo.getChartIds();
        String oldChartIds = dashboardsMapper.getChartIdsById(dashboardId);
        if (!StringUtils.isEmpty(oldChartIds)) {
            if (StringUtils.isEmpty(chartIds)) {    //旧不为空 & 新为空
                dashboardsMapper.deleteDashboardChartRelation(dashboardId);
            }else {                                 //旧不为空 & 新不为空
                if (!oldChartIds.equals(dashboardsInfo.getChartIds())) {
                    dashboardsMapper.deleteDashboardChartRelation(dashboardId);
                    dashboardsMapper.saveDashboardChartRelation(dashboardId, chartIds.split(","));
                }
            }
        }else {
            //旧为空  & 新不为空
            if (!StringUtils.isEmpty(chartIds)) {
                dashboardsMapper.saveDashboardChartRelation(dashboardId, chartIds.split(","));
            }
        }
        return new RepEntity(RepCode.success);
    }

    /**
     * 校验编号是否存在
     * @param code
     * @param id
     */
    private String validCode(String code, int id) {
        if (!StringUtils.isEmpty(code)) {
            int count = dashboardsMapper.countBdCodeById(code, id);
            if (count > 0) {
                throw new MyException("编号" + code + "已存在");
            }
            return code;
        }else {
            return generateUUCode(id);
        }
    }

    /**
     * 创建报表唯一编号
     * @param id
     * @return
     */
    private String generateUUCode(int id){
        String UUcode = UUID.randomUUID().toString();
        UUcode = UUcode.substring(UUcode.lastIndexOf("-") + 1);
        int count = dashboardsMapper.countBdCodeById(UUcode, id);
        if (count > 0) {
            generateUUCode(id);
        }
        return UUcode;
    }

    /*
    删除报表
     */
    public RepEntity delDashboards(String token, Integer id) {
        int userId = BaseContextHolder.getUserId();
        int createId = dashboardsMapper.getCreateIdById(id);
        if (userId != createId) {
            return new RepEntity(RepCode.NoAuthority);
        }
        Dashboards dashboards = dashboardsMapper.getDashboards(userId, id);
        String name = null;
        if (!ObjectUtils.isEmpty(dashboards)) {
            name = dashboards.getBdName();
        }
        dashboardsMapper.delDashboards(id);
        //删除报表-图表关联关系
        dashboardsMapper.deleteDashboardChartRelation(id);
        //删除报表-首页默认报表
        defaultDashboardService.delete(id);
        messageLogService.delete("报表", name, BaseContextHolder.getUserName(), null);
        return new RepEntity(RepCode.success);
    }

    /*
    查看报表
     */
    public RepEntity getListDashboards(String token, TestPage testPage) {
        int id = BaseContextHolder.getUserId();
        String role = BaseContextHolder.getRole();
        List<Dashboards> getListDashboards = new ArrayList<>();
        if (RoleConstant.SUPER_ADMIN.getRole().equals(role)) {
            getListDashboards = dashboardsMapper.getListDashboardsWithoutLimit(testPage.enablePaging());
        }else {
            getListDashboards = dashboardsMapper.getListDashboards(id, testPage.enablePaging());
        }
        PageInfo<Dashboards> pageInfo = new PageInfo<>(getListDashboards);
        return new RepEntity(RepCode.success, pageInfo);
    }

    /*
    查看单个报表
     */
    public RepEntity getDashboards(String token, int id) {
        int userId = BaseContextHolder.getUserId();
        String role = BaseContextHolder.getRole();
        Dashboards dashboards = new Dashboards();
        if (RoleConstant.SUPER_ADMIN.getRole().equals(role)) {
            dashboards = dashboardsMapper.getDashboardsWithoutLimit(id);
        }else {
            dashboards = dashboardsMapper.getDashboards(userId, id);
        }
        if (dashboards == null){
            return new RepEntity(RepCode.DashboardNonExistent);
        }
        return new RepEntity(RepCode.success, dashboards);
    }

    /**
     * 通过code查看单个报表
     * @param code
     * @return
     */
    public RepEntity getDashboardByCode(String code) {
        code = validAndDes(code);
        Dashboards dashboard = dashboardsMapper.getDashboardByCode(code);
        if (ObjectUtils.isEmpty(dashboard)) {
            return new RepEntity(RepCode.DashboardNonExistent.getCode(), String.format(RepCode.DashboardNotExist.getMsg(), code), null);
        }
        Dashboards dashboards = dashboardsMapper.getDashboards(dashboard.getCreateId(), dashboard.getId());
        if (dashboards == null){
            return new RepEntity(RepCode.DashboardNonExistent);
        }
        return new RepEntity(RepCode.success, dashboards);
    }

    private String validAndDes(String code) {
        String message = EncryUtil.decryptBiCode(code);
        String[] params = message.split("&");
        if (params.length == 2) {
            if (System.currentTimeMillis() - Long.valueOf(params[1]) > 15 * 60 * 1000) {
                LOGGER.error("now={}, params[1]={}, diff={}", System.currentTimeMillis(), params[1], System.currentTimeMillis()-Long.valueOf(params[1]));
                throw new MyException("请求超时");
            }
            return params[0];
        }else {
            throw new MyException("请求参数个数错误");
        }
    }

    /**
     * 生成分享链接参数
     * @param shareReqBO
     * @return
     */
    public RepEntity shareDashBoard(ShareReqBO shareReqBO) {
        Long endTime = new Double(System.currentTimeMillis() + shareReqBO.getDelay() * 24 * 60 * 60 * 1000).longValue();
        String data = "id=" + shareReqBO.getId() + "&endTime=" + endTime;
        String encodeData = new String(EncryUtil.encryptBiCode(data));
        return new RepEntity(RepCode.success, encodeData);
    }

    /**
     * 链接参数解析
     * @param data
     * @return
     */
    private Map<String, String> parseParams(String data) {
        String decodeData = new String(EncryUtil.decryptBiCode(data));
        Map<String, String> map = new HashMap<>();
        int firstEndIndex = decodeData.indexOf("&");
        map.put("id", decodeData.substring(3, firstEndIndex));
        map.put("endTime", decodeData.substring(firstEndIndex + 9));
        return map;
    }

    /**
     * 通过分享链接获取报表信息
     * @param data
     * @return
     */
    public RepEntity getSharedDashboard(String data){
        Map<String, String> params = parseParams(data);
        Long endTime = Long.valueOf(params.get("endTime"));
        if (System.currentTimeMillis() > endTime) {
            throw new MyException("分享链接已过期");
        }
        int id = Integer.valueOf(params.get("id"));
        int creatorId = dashboardsMapper.getCreateIdById(id);
        Dashboards dashboards = dashboardsMapper.getDashboards(creatorId, id);
        if (dashboards == null){
            return new RepEntity(RepCode.DashboardNonExistent);
        }
        return new RepEntity(RepCode.success, dashboards);
    }

    /*
    转交报表
     */
    public RepEntity changeDashOrder(String token, ChangeOrderInfo changeOrderInfo) {
        int userId = BaseContextHolder.getUserId();
        String role = BaseContextHolder.getRole();
        int createId = dashboardsMapper.getCreateIdById(changeOrderInfo.getId());
        if (userId != createId && !RoleConstant.SUPER_ADMIN.getRole().equals(role)) {
            return new RepEntity(RepCode.NoAuthority);
        }
        String name = userMapper.getName(changeOrderInfo.getUserId());
        dashboardsMapper.changeDashOrder(name, changeOrderInfo.getUserId(), changeOrderInfo.getId());
        return new RepEntity(RepCode.success);
    }

    /*
    查看报表分发对象
     */
    public RepEntity getDashOrder(String token, int dashId) {
        int userId = BaseContextHolder.getUserId();
        int createId = dashboardsMapper.getCreateIdById(dashId);
        if (userId != createId) {
            return new RepEntity(RepCode.NoAuthority);
        }

        List<DashOrder> userNames = new ArrayList<>();
        List<DashOrder> groupNames = new ArrayList<>();
        List<String> type = dashboardsMapper.getType(dashId);
        Iterator isType = type.iterator();
        while (isType.hasNext()){
            String ty = String.valueOf(isType.next());
            if ("1".equals(ty)){
                userNames = dashboardsMapper.getOrderName(dashId);
            }else {
                groupNames = dashboardsMapper.getOrderGroupName(dashId);
            }
        }
        DashboardOrderInfo dashboardOrderInfo = new DashboardOrderInfo();
        dashboardOrderInfo.setUserNames(userNames);
        dashboardOrderInfo.setGroupNames(groupNames);
        return new RepEntity(RepCode.success, dashboardOrderInfo);
    }

    /**
     * 复制报表
     * @param dashboardCopyInfo
     * @return
     */
    public RepEntity copy(String token, DashboardCopyInfo dashboardCopyInfo) {
        Map<String,String> userData = GetTokenDataUtil.getTokenData(token);
        int userId = Integer.valueOf(userData.get("id"));
        String userName = userData.get("name");
        int dashboardId = dashboardCopyInfo.getDashboardId(),
            dataSourceId = dashboardCopyInfo.getDataSourceId();
        List<String> idList = new ArrayList<>();
        Map<String, String> idMap = new HashMap<>();
        //复制图表
        String chartIds = dashboardsMapper.getChartIdsById(dashboardId);
        if (!StringUtils.isEmpty(chartIds)) {
            String[] idArray = chartIds.split(",");
            for (int i = 0, len = idArray.length; i < len; i++) {
                ChartConfig chartConfig = chartsConfigMapper.getOneChart(Integer.valueOf(idArray[i]));
                int oldChartId = chartConfig.getChartId();
                //校验 要复制的图标是否已在目标账套
                List<Map<String, Object>> chartMap = chartsConfigMapper.checkOldChart(oldChartId,chartConfig.getDataId());
                if(chartMap.size()==0){
                    chartConfig.setFrom_id(oldChartId);
                    chartConfig.setFrom_data_id(chartConfig.getDataId());
                    chartConfig.setChartName(chartConfig.getChartName() + dashboardCopyInfo.getLastName());
                    chartConfig.setCreateBy(userName);
                    chartConfig.setCreateId(userId);
                    String oldFetchConfig = chartConfig.getFetchConfig();
                    //判断数据源是否已复制过
                    Integer DataConnectId = chartsConfigMapper.getDataConnectId(chartConfig.getDataId());
                    int dataId = 0;
                    if(DataConnectId==null || DataConnectId==0){
                        dataId = dataConnectorService.copyDataSource(token, new DataSourceCopyInfo(chartConfig.getDataId(), dataSourceId ,dashboardCopyInfo.getLastName())).getData();
                    }else{
                        dataId = DataConnectId;
                        chartConfig.setFrom_data_id(dataId);
                    }
                    chartConfig.setDataId(dataId);
                    chartsConfigMapper.insertCharts(chartConfig);
                    int newChartId = chartConfig.getChartId();
                    //新ID更新到FetchConfig
                    JSONArray jsonArray = JSONObject.parseArray("["+oldFetchConfig+"]");
                    jsonArray.getJSONObject(0).put("id",newChartId);
                    chartsConfigMapper.updateChartFetchConfig(JSONObject.toJSONString(jsonArray.getJSONObject(0)),newChartId);
                    idMap.put(String.valueOf(oldChartId), String.valueOf(newChartId));
                    idList.add(String.valueOf(newChartId));
                }else{
                    String newChartId = String.valueOf(chartMap.get(0).get("ID"));
                    idMap.put(String.valueOf(oldChartId), newChartId);
                    idList.add(newChartId);
                }
            }
        }
        //复制报表
        int dashboardCreatorId = dashboardsMapper.getCreateIdById(dashboardId);
        Dashboards dashboards = dashboardsMapper.getDashboards(dashboardCreatorId, dashboardId);
        dashboards.setBdName(dashboards.getBdName() + dashboardCopyInfo.getLastName());
        JSONArray jsonArray = JSON.parseArray(dashboards.getBdConfiguration());
        for (int i = 0, len = jsonArray.size(); i < len; i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            if (!"richText".equals(json.getString("viewType"))) {
                String newId = idMap.get(json.getString("code"));
                json.put("code", newId);
                json.put("chartCode", newId);
            }
        }
        dashboards.setBdConfiguration(JSON.toJSONString(jsonArray));
        dashboards.setCreateBy(userName);
        dashboards.setCreateId(userId);
        dashboards.setDemo(false);
        if(StringUtils.hasText(dashboardCopyInfo.getNewCode()) && dashboardCopyInfo.getNewCode().length()>0){
            dashboards.setBdCode(dashboardCopyInfo.getNewCode());
        }else{
            dashboards.setBdCode(generateUUCode(0));
        }

        dashboardsMapper.setDashboards(dashboards);
        String[] ids = String.join(",", idList).split(",");
        dashboardsMapper.saveDashboardChartRelation(dashboards.getId(), ids);
        return new RepEntity(RepCode.success, dashboards.getId());
    }

    /**
     * 导出报表数据
     * @param dashboardExportInfo
     * @return
     */
    public HSSFWorkbook export(DashboardExportInfo dashboardExportInfo){
        HSSFWorkbook workbook = new HSSFWorkbook();
        int dashboardId = dashboardExportInfo.getDashboardId();
        List<DataSourceExportInfo> dataSourceExportInfoList = dashboardExportInfo.getData();
        for (DataSourceExportInfo dataInfo : dataSourceExportInfoList) {
            RepEntity<DataConnector> repEntity = dataConnectorService.getConnector(dataInfo.getDataSourceId());
            if (repEntity.getCode() == 200) {
                DataConnector dataConnector = repEntity.getData();
                String tableName = dataConnector.getLoadObject();
                List<Screen> screenList = dataInfo.getFilter();
                String condition = parseFilter(screenList);
                DynamicDataSourceContextHolder.setDataSourceType(dataConnector.getDbConfig().getId());
                List<Map<String, Object>> dataList = dataConnectorMapper.getExportData(tableName, condition);
                DynamicDataSourceContextHolder.clearDataSourceType();
                List<ColumnRenameInfo> columnRenameInfo = JSON.parseArray(dataConnector.getColumnConfig(), ColumnRenameInfo.class);
                createExcelSheet(workbook, dataConnector.getDataName(), dataList, columnRenameInfo);
            }
        }
        return workbook;
    }


    private void createExcelSheet(HSSFWorkbook workbook, String sheetName, List<Map<String, Object>> dataList, List<ColumnRenameInfo> columnRenameInfo) {
        //添加sheet
        HSSFSheet sheet = workbook.createSheet(sheetName);
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell;
        HSSFCellStyle cellStyle = getCellStyle(workbook);
        //设置标题
        for (int i = 0, len = columnRenameInfo.size(); i < len; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(cellStyle);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(columnRenameInfo.get(i).getColumnLable());
        }
        //设置数据
        for (int i = 0, len = dataList.size(); i < len; i++) {
            row = sheet.createRow(i + 1);
            Map<String, Object> data = dataList.get(i);
            for (int j = 0, size = columnRenameInfo.size(); j < size; j++) {
                cell = row.createCell(j);
                int dataType = convertToCellType(columnRenameInfo.get(j).getColumnType());
                Object value = data.get(columnRenameInfo.get(j).getColumnName());
                if (9 == dataType) {
                    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                    Date dataValue = null;
                    try {
                        dataValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(value));
                    } catch (ParseException e) {
                        continue;
                    }
                    cell.setCellValue(dataValue);
                }else {
                    cell.setCellType(dataType);
                    cell.setCellValue(StringUtils.isEmpty(value) ? "" : String.valueOf(value));
                }
            }
        }

    }

    private int convertToCellType(String columnType) {
        switch (columnType) {
            case "categorical":
                return HSSFCell.CELL_TYPE_STRING;
            case "Timestamp":
                return 9;
            case "BigDecimal":
                return HSSFCell.CELL_TYPE_NUMERIC;
            case "scale":
                return HSSFCell.CELL_TYPE_NUMERIC;
            default:
                return HSSFCell.CELL_TYPE_STRING;
        }
    }

    private HSSFCellStyle getCellStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("仿宋_GB2312");// 字体
        font.setFontHeightInPoints((short) 12);// 字号
        font.setBoldweight(new Short("8"));
        font.setColor((short)64);// 颜色
        style.setFont(font);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        return style;
    }

    private String parseFilter(List<Screen> screenList){
        StringBuilder condition = new StringBuilder(" where 1=1 ");
        if (CollectionUtils.isEmpty(screenList)) {
            return null;
        }
        for (Screen screen : screenList) {
            String columnName = screen.getColumnName();
            String columnType = screen.getColumnType();
            String symbol = screen.getSymbol();
            String value = screen.getValue();
            if (columnType != "time" && !("time".equals(columnType))) {
                String symbVal = getSymbAndVal(symbol, value, columnType);
                condition.append(" and " + columnName + " " + symbVal);
            }else if (columnType == "time" || "time".equals(columnType)) {
                String symbVal = getTimeSymbAndVal(symbol, value);
                condition.append(" and " + columnName + " " + symbVal);
            }
        }
        return condition.toString();
    }

    private String getTimeSymbAndVal(String symbol, String value) {
        String values = "" + value;
        String tar = "";
        if ("between".equals(symbol)) {
            String[] str = value.split(",");
            String str1 = str[0];
            String str2 = str[1];
            tar = "between to_date('" + str1 + "','yyyy-MM-dd') and to_date('" + str2 + "','yyyy-MM-dd')";
        } else {
            tar = symbol + " '" + values + "'";
        }
        return tar;
    }

    private String getSymbAndVal(String symbol, String value, String columnType) {
        String values = "" + value;
        String tar = "";
        if ("contain".equals(symbol)) {
            if ("categorical".equals(columnType)) {
                tar = "in " + getContainsCate(value);
            } else {
                tar = "like '%" + values + "%'";
            }
        } else if ("notContain".equals(symbol)) {
            if ("categorical".equals(columnType)) {
                tar = "not in " + getContainsCate(value);
            } else {
                tar = "not like '%" + values + "%'";
            }
        } else if ("startsWith".equals(symbol)) {
            tar = "like '" + values + "%'";
        } else if ("endsWith".equals(symbol)) {
            tar = "like '%" + values + "'";
        } else if ("null".equals(symbol)) {
            tar = "is null";
        } else if ("notNull".equals(symbol)) {
            tar = "is not null";
        } else if ("between".equals(symbol)) {
            tar = "";
            String[] str = value.split(",");
            String str1 = str[0];
            String str2 = str[1];
            tar = "between '" + str1 + "' and '" + str2 + "'";
        } else if("notInclude".equals(symbol)) {
            tar = "not like '%" + values + "%'";
        } else if("include".equals(symbol)){
            tar = "like '%" + values + "%'";
        } else if("equals".equals(symbol)){
            tar = "=" + " '" + values + "'";
        } else if("notEquals".equals(symbol)){
            tar = "!=" + " '" + values + "'";
        }else {
            tar = symbol + " '" + values + "'";
        }
        return tar;
    }

    private String getContainsCate(String value) {
        List<String> val = new ArrayList<>();
        CollectionType javaType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
        try {
            val = objectMapper.readValue(value, javaType);   //这里不需要强制转换
        } catch (IOException e) {
            e.printStackTrace();
        }

        String valueString = "";
        Iterator isList = val.iterator();
        while (isList.hasNext()) {
            String v = String.valueOf(isList.next());
            valueString = valueString + ", '" + v + "'";
        }
        valueString = valueString.replaceFirst(",", "(") + ")";
        return valueString;
    }

}
