package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.RowStyle;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.business.domain.ServiceBusiness;
import com.ruoyi.business.domain.ServiceGrant;
import com.ruoyi.business.mapper.ServiceGrantMapper;
import com.ruoyi.business.service.IDatabaseService;
import com.ruoyi.business.service.IServiceBusinessService;
import com.ruoyi.business.service.IServiceGrantService;
import com.ruoyi.common.auth.entity.OauthClientDetails;
import com.ruoyi.common.auth.service.OauthClientDetailsService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.ServiceGrantVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


import static com.ruoyi.common.core.constant.Constants.DELETE_NO;
import static com.ruoyi.common.core.constant.Constants.DELETE_YES;

/**
 * 对外服务授权Service业务层处理
 * 
 * @author wzq
 * @date 2024-01-10
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ServiceGrantServiceImpl extends ServiceImpl<ServiceGrantMapper, ServiceGrant> implements IServiceGrantService {

    private final ServiceGrantMapper serviceGrantMapper;
    private final IServiceBusinessService serviceBusinessService;
    private final OauthClientDetailsService oauthClientDetailsService;
    private final IDatabaseService databaseService;

    /**
     * 查询对外服务授权列表
     * 
     * @param outGrant 对外服务授权
     * @return 对外服务授权
     */
    @Override
    public List<ServiceGrant> selectServiceGrantList(ServiceGrant outGrant)
    {
        LambdaQueryWrapper<ServiceGrant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ServiceGrant::getDeleteFlag, DELETE_NO)
                .like(outGrant.getClientId() != null, ServiceGrant::getClientId,outGrant.getClientId())
                .like(outGrant.getServiceBusinessId() != null, ServiceGrant::getServiceBusinessId,outGrant.getServiceBusinessId())
                .orderByDesc(ServiceGrant::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询对外服务授权详情
     *
     * @param id 对外服务授权主键
     * @return 对外服务授权
     */
    @Override
    public ServiceGrant selectServiceGrantById(Long id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增对外服务授权
     * 
     * @param outGrant 对外服务授权
     * @return 结果
     */
    @Override
    public int insertServiceGrant(ServiceGrant outGrant)
    {
        outGrant
                .setDeleteFlag(DELETE_NO)
                .setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(outGrant);
    }

    /**
     * 修改对外服务授权
     * 
     * @param outGrant 对外服务授权
     * @return 结果
     */
    @Override
    public Boolean updateServiceGrant(ServiceGrant outGrant)
    {
        outGrant.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(outGrant);
    }

    /**
     * 批量删除对外服务授权
     * 
     * @param ids 需要删除的对外服务授权主键
     * @return 结果
     */
    @Override
    public int deleteServiceGrantByIds(Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return updateByIds(list);
    }

    /**
     * 删除对外服务授权信息
     * 
     * @param id 对外服务授权主键
     * @return 结果
     */
    @Override
    public int deleteServiceGrantById(Long id)
    {
        List<Long> list = new ArrayList<>();
        list.add(id);
        return updateByIds(list);
    }

    /**
    * 批量更新
    * @param list ids
    */
    private int updateByIds(List<Long> list) {
        LambdaUpdateWrapper<ServiceGrant> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ServiceGrant::getDeleteBy, SecurityUtils.getUsername())
                .set(ServiceGrant::getDeleteTime, DateUtils.getNowDate())
                .in(ServiceGrant::getId, list)
                .set(ServiceGrant::getDeleteFlag, DELETE_YES);
        return this.baseMapper.updateByIds(updateWrapper);
    }

    @Override
    public int updateGrant(ServiceGrantVo outGrant) {
        outGrant
                .setDeleteFlag(DELETE_NO)
                .setCreateTime(DateUtils.getNowDate());
        // 删除对外服务授权
        this.baseMapper.deleteGrantByClientId(outGrant.getClientId(), outGrant.getBelongSystem());
        return insertGrant(outGrant);
    }

    /**
     * 新增对外服务授权信息
     *
     * @param outGrant 对外服务授权对象
     */
    public int insertGrant(ServiceGrantVo outGrant)
    {
        int rows = 1;
        // 新增用户与角色管理
        List<ServiceGrant> list = new ArrayList<ServiceGrant>();
        for (Long menuId : outGrant.getMenuIds())
        {
            ServiceGrant rm = new ServiceGrant();
            rm.setClientId(outGrant.getClientId());
            rm.setServiceBusinessId(menuId);
            rm.setBelongSystem(outGrant.getBelongSystem());
            rm.setDeptIdPath(outGrant.getDeptIdPath());
            rm.setDeptName(outGrant.getDeptName());
            rm.setCreateUserId(outGrant.getCreateUserId());
            rm.setCreateBy(outGrant.getCreateBy());
            rm.setDeleteFlag(outGrant.getDeleteFlag());
            rm.setCreateTime(outGrant.getCreateTime());
            list.add(rm);
        }
        if (list.size() > 0)
        {
            for (ServiceGrant grant : list){
                this.baseMapper.insert(grant);
            }
        }
        return rows;
    }

    /**
     * 根据客户端id查询菜单树ids信息
     *
     * @param clientId 客户端id
     * @return 选中菜单列表
     */
    @Override
    public List<String> selectServiceBusinessIdByClientId(String clientId, String belongSystem) {
        List<ServiceGrant> grants = this.baseMapper.selectGrantEffective(clientId,belongSystem);
        List<String> list = new ArrayList<>();
        for (ServiceGrant grant : grants) {
            Long serviceBusinessId = grant.getServiceBusinessId();
            list.add(serviceBusinessId.toString());
        }
        return list;
    }

    @Override
    public String generateDocument(OauthClientDetails oAuthClientDetails) {
//        ConfigureBuilder builder = Configure.builder();
        HashMap<String, Object> dataMap = new HashMap<>();
        //获取模板的文件流
        try {
            Path currentPath = Paths.get("");
            Path parentPath = currentPath.toAbsolutePath();
            String filePath = parentPath + "\\DZ-logistics-oauth2\\ruoyi-modules\\ruoyi-system-dataexchange\\src\\main\\java\\com\\ruoyi\\business\\对接文档模板.docx";
            FileInputStream fileInputStream = new FileInputStream(filePath);

            // 为了从auth里查询数据从findById修改成findByIdToAuth
            OauthClientDetails oauthClientDetail = oauthClientDetailsService.findByIdToAuth(oAuthClientDetails.getClientId());
            String information = oauthClientDetail.getAdditionalInformation();
            //匹配联系单位和联系人中间的字符
            Pattern pattern = Pattern.compile("联系单位(.*?)联系人");
            Matcher matcher = pattern.matcher(information);
            String contactUnit = null;
            if (matcher.find()) {
                 contactUnit = matcher.group(1).substring(1, matcher.group(1).length() - 1);
                System.out.println("联系单位: " + contactUnit);
            } else {
                System.out.println("未找到联系单位匹配项");
            }
            //匹配身份证号：后面的18位
            String  idCard =   information.substring(information.indexOf("身份证号") + 5, information.indexOf("身份证号") + 5 + 18);
            //匹配电话：后面的11位
            String phone = information.substring(information.indexOf("电话") + 3, information.indexOf("电话") + 3 + 11);
            String clientId = oauthClientDetail.getClientId();
            ServiceGrant serviceGrant = new ServiceGrant();
            serviceGrant.setClientId(clientId);
            List<ServiceGrant> serviceGrants = serviceGrantMapper.selectServiceGrantList(serviceGrant);
            //遍历serviceGrants
            List<Long> list = new ArrayList<>();
            for (ServiceGrant serviceGrant1 : serviceGrants) {
                list.add(serviceGrant1.getServiceBusinessId());
            }
            //查询serviceBusiness中id在list中的数据
            List<ServiceBusiness> serviceBusinessByIds = serviceBusinessService.getServiceBusinessByIds(list);
            //根据类型判断是查询，输入，有条件查询
            List<ServiceBusiness> selectTable = new ArrayList<>();
            List<ServiceBusiness> inputTable = new ArrayList<>();
            List<ServiceBusiness> whereTable = new ArrayList<>();
            String tableNum1 = "1";
            String tableNum2 = "50000";
            String tableNum3 = "100000";
            for(ServiceBusiness serviceBusiness : serviceBusinessByIds) {
            String type = serviceBusiness.getType();
            if(type.equals("1")) {
                inputTable.add(serviceBusiness);
                // 新增接口文档模板
            }else if(type.equals("2")) {
                // 查询接口文档模板
                selectTable.add(serviceBusiness);
            }else if(type.equals("3")) {
                // 自定义查询接口文档模板
                whereTable.add(serviceBusiness);
            }
        }
            for( ServiceBusiness where : whereTable){
                //    有条件查询接口文档模板
                //自定义有条件查询sql语句
                String resultSql = where.getResultSql();
                // 使用正则表达式提取 SELECT 和 FROM 之间的内容
                Pattern pattern1 = Pattern.compile("SELECT (.*?) FROM");
                Matcher matcher1 = pattern1.matcher(resultSql);

                if (matcher1.find()) {
                    //正则匹配sql要查询的字段
                    String fields = matcher1.group(1);
                    //连接表名
                    String tableName = where.getTableName();
                    //根据表名，获取数据库中表字段和对应的注释
                    List<Map<String, String>> tableFields = databaseService.getTableFields(where.getDatabaseId(), tableName);
                   List<Map<String, String>> result;
                    if(fields.trim().equals("*")){
                        // 查询所有字段
                        result = tableFields;
                    }else{
                        //查询部分字段
                        List<String> fieldList = new ArrayList<>();
                        // 将字段按逗号拆分要查询的部分字段并存入集合中
                        String[] fieldArray = fields.split(",");

                        for (String field : fieldArray) {
                            fieldList.add(field.trim()); // 去除字段前后的空格
                        }
                        result = fieldList.stream()
                                .map(field -> tableFields.stream()
                                        .filter(map -> map.containsKey(field))
                                        .findFirst()
                                        .orElse(null)) // 如果找不到，则返回 null
                                .filter(map -> map != null) // 过滤掉 null
                                .collect(Collectors.toList()); // 收集结果到新的 List 中
                    }
                    // 默认查询接口文档模板,统一一行4列
                    RowRenderData row0 = Rows.of("接口名称", where.getOutName(),null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                    //接口地址
                    RowRenderData row1 = Rows.of("接口地址", "https://www.scdzwl.com:9160/system-dataExchange/outBusiness",null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row2 = Rows.of("类型", "查询","请求方式","Get").center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row3 = Rows.of("参数类型", "表单","返回结果类型","JSON").center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row4 = Rows.of("查询字段",null,null,null).textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row5 = Rows.of("字段列名","字段描述","查询类型","备注").center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row6 = Rows.of("businessId","业务主键",null,"必填").center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row7 = Rows.of("pageNum","分页页码",null,"必填").center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row8 = Rows.of("pageSize","分页条数",null,"必填").center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row9 = Rows.of("结果字段",null,null,null).textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData row10 = Rows.of("字段列名","字段描述",null,"备注").center().textFontFamily("仿宋").textFontSize(12).create();
                    List<RowRenderData> rowList = new ArrayList<>();
                        rowList.add(row0);
                        rowList.add(row1);
                        rowList.add(row2);
                        rowList.add(row3);
                        rowList.add(row4);
                        rowList.add(row5);
                        rowList.add(row6);
                        rowList.add(row7);
                        rowList.add(row8);
                        rowList.add(row9);
                        rowList.add(row10);
                        //从表的第11行开始动态生成
                        for (Map<String, String> res : result) {
                                String name = res.get("name");
                                String remark = res.get("remark");
                                String type = res.get("type");
        //                    for (Map.Entry<String, String> entry : res.entrySet()) {
        //                        RowRenderData rowTemp = Rows.of(entry.getKey(), entry.getValue(), null, null).center().textFontFamily("仿宋").textFontSize(12).create();
                                RowRenderData rowTemp = Rows.of(name, remark, type, null).center().textFontFamily("仿宋").textFontSize(12).create();

                                rowList.add(rowTemp);
        //                    }
                        }

                    RowRenderData emptyRow1 = Rows.of("---------------------", null, null, null).textColor("FFFFFF").textFontSize(18).create();
                    rowList.add(emptyRow1);
                    int lastIndex = rowList.size()-1;
                    // 合并0,1行的1到3列，4，9行的0到3列
                    MergeCellRule rule = MergeCellRule.builder()
                            .map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(0, 3))
                            .map(MergeCellRule.Grid.of(1, 1), MergeCellRule.Grid.of(1, 3))
                            .map(MergeCellRule.Grid.of(4, 0), MergeCellRule.Grid.of(4, 3))
                            .map(MergeCellRule.Grid.of(9, 0), MergeCellRule.Grid.of(9, 3))
                            .map(MergeCellRule.Grid.of(lastIndex, 0), MergeCellRule.Grid.of(lastIndex, 3))
                            .build();
                    int[] width = {20,40,20,20};//四列宽度占比%
                    dataMap.put("table" + tableNum1, Tables.of(rowList.toArray(new RowRenderData[0])).percentWidth("100%",width).mergeRule(rule).create());

                    tableNum1 = String.valueOf(Integer.parseInt(tableNum1) + 1);

                } else {
                    System.out.println("自定义查询sql为空，未找到字段。");
                }

            }
            for( ServiceBusiness select : selectTable){
                //  默认查询接口文档模板
                //连接表名
                String tableName = select.getTableName();
                String queryField = select.getQueryField();
                //根据表名，获取数据库中表字段和对应的注释
                List<Map<String, String>> tableFields = databaseService.getTableFields(select.getDatabaseId(), tableName);
                ObjectMapper objectMapper = new ObjectMapper();
                //fieldList存queryField要查询中的字段
                List<String> fieldList = new ArrayList<>();
                List<LinkedHashMap<String, String>> jsonList = objectMapper.readValue(queryField, new TypeReference<List<LinkedHashMap<String, String>>>(){});
                for (LinkedHashMap<String, String> json : jsonList) {
                    fieldList.add(json.get("field"));
                }
                List<Map<String, String>> result = fieldList.stream()
                        .map(field -> tableFields.stream()
                                .filter(map -> map.containsKey(field))
                                .findFirst()
                                .orElse(null)) // 如果找不到，则返回 null
                        .filter(map -> map != null) // 过滤掉 null
                        .collect(Collectors.toList()); // 收集结果到新的 List 中


                // 默认查询接口文档模板,统一一行4列
                RowRenderData row0 = Rows.of("接口名称", select.getOutName(),null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                //接口地址
                RowRenderData row1 = Rows.of("接口地址", "https://www.scdzwl.com:9160/system-dataExchange/outBusiness",null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row2 = Rows.of("类型", "默认查询","请求方式","Get").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row3 = Rows.of("参数类型", "表单","返回结果类型","JSON").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row4 = Rows.of("查询字段",null,null,null).textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row5 = Rows.of("字段列名","字段描述","查询类型","备注").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row6 = Rows.of("businessId","业务主键",null,"必填").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row7 = Rows.of("pageNum","分页页码",null,"必填").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row8 = Rows.of("pageSize","分页条数",null,"必填").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row9 = Rows.of("结果字段",null,null,null).textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row10 = Rows.of("字段列名","字段描述",null,"备注").center().textFontFamily("仿宋").textFontSize(12).create();
                List<RowRenderData> rowList = new ArrayList<>();
                rowList.add(row0);
                rowList.add(row1);
                rowList.add(row2);
                rowList.add(row3);
                rowList.add(row4);
                rowList.add(row5);
                rowList.add(row6);
                rowList.add(row7);
                rowList.add(row8);
                rowList.add(row9);
                rowList.add(row10);
                //从表的第11行开始动态生成
//                int num = 11;
                for (Map<String, String> res : result) {
                    String name = res.get("name");
                    String remark = res.get("remark");
                    String type = res.get("type");
//                    for (Map.Entry<String, String> entry : res.entrySet()) {
//                        RowRenderData rowTemp = Rows.of(entry.getKey(), entry.getValue(), null, null).center().textFontFamily("仿宋").textFontSize(12).create();
                    RowRenderData rowTemp = Rows.of(name, remark, type, null).center().textFontFamily("仿宋").textFontSize(12).create();

                    rowList.add(rowTemp);
//                    }
                }
                RowRenderData emptyRow1 = Rows.of("---------------------", null, null, null).textColor("FFFFFF").textFontSize(18).create();
                rowList.add(emptyRow1);
                int lastIndex = rowList.size()-1;
                // 合并0,1行的1到3列，4，9行的0到3列
                MergeCellRule rule = MergeCellRule.builder()
                        .map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(0, 3))
                        .map(MergeCellRule.Grid.of(1, 1), MergeCellRule.Grid.of(1, 3))
                        .map(MergeCellRule.Grid.of(4, 0), MergeCellRule.Grid.of(4, 3))
                        .map(MergeCellRule.Grid.of(9, 0), MergeCellRule.Grid.of(9, 3))
                        .map(MergeCellRule.Grid.of(lastIndex, 0), MergeCellRule.Grid.of(lastIndex, 3))
                        .build();
                int[] width = {20,40,20,20};//四列宽度占比%
                dataMap.put("table" + tableNum2, Tables.of(rowList.toArray(new RowRenderData[0])).percentWidth("100%",width).mergeRule(rule).create());

                tableNum2 = String.valueOf(Integer.parseInt(tableNum2) + 1);
            }

            for( ServiceBusiness input : inputTable){
                //  输入接口文档模板
                //连接表名
                String tableName = input.getTableName();

                //根据表名，获取数据库中表字段和对应的注释
                List<Map<String, String>> tableFields = databaseService.getTableFields(input.getDatabaseId(), tableName);




                RowRenderData row0 = Rows.of("接口名称", input.getOutName(),null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                //接口地址
                RowRenderData row1 = Rows.of("接口地址", "https://www.scdzwl.com:9160/system-dataExchange/outBusiness",null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row2 = Rows.of("类型", "输入","请求方式","Post").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row3 = Rows.of("参数类型", "JSON","返回结果类型","JSON").center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row4 = Rows.of("请求头部参数",null,null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row5 = Rows.of("参数名","参数描述","备注",null).center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row6 = Rows.of("businessId","业务主键","必填",null).center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row7 = Rows.of("插入字段",null,null,null).center().textFontFamily("仿宋").textFontSize(12).create();
                RowRenderData row8 = Rows.of("字段列名","字段描述","备注",null).center().textFontFamily("仿宋").textFontSize(12).create();
                List<RowRenderData> rowList = new ArrayList<>();
                rowList.add(row0);
                rowList.add(row1);
                rowList.add(row2);
                rowList.add(row3);
                rowList.add(row4);
                rowList.add(row5);
                rowList.add(row6);
                rowList.add(row7);
                rowList.add(row8);

                //从表的第9行开始动态生成
                for (Map<String, String> res : tableFields) {
                    String name = res.get("name");
                    String remark = res.get("remark");
                    String type = res.get("type");
//                    for (Map.Entry<String, String> entry : res.entrySet()) {
//                        RowRenderData rowTemp = Rows.of(entry.getKey(), entry.getValue(), null, null).center().textFontFamily("仿宋").textFontSize(12).create();
                        RowRenderData rowTemp = Rows.of(name, remark, type, null).center().textFontFamily("仿宋").textFontSize(12).create();

                        rowList.add(rowTemp);
//                    }
                }


                RowRenderData emptyRow1 = Rows.of("---------------------", null, null, null).textColor("FFFFFF").textFontSize(18).create();
                rowList.add(emptyRow1);
                int lastIndex = rowList.size()-1;
                // 合并0,1行的1到3列，4，7行的0到3列,5,6,8行的2到3列
                MergeCellRule rule = MergeCellRule.builder()
                        .map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(0, 3))
                        .map(MergeCellRule.Grid.of(1, 1), MergeCellRule.Grid.of(1, 3))
                        .map(MergeCellRule.Grid.of(4, 0), MergeCellRule.Grid.of(4, 3))
                        .map(MergeCellRule.Grid.of(5, 2), MergeCellRule.Grid.of(5, 3))
                        .map(MergeCellRule.Grid.of(6, 2), MergeCellRule.Grid.of(6, 3))
                        .map(MergeCellRule.Grid.of(7, 0), MergeCellRule.Grid.of(7, 3))
                        .map(MergeCellRule.Grid.of(8, 2), MergeCellRule.Grid.of(8, 3))
                        .map(MergeCellRule.Grid.of(lastIndex, 0), MergeCellRule.Grid.of(lastIndex, 3))
                        .build();
                int[] width = {20,40,20,20};//四列宽度占比%
                dataMap.put("table" + tableNum3, Tables.of(rowList.toArray(new RowRenderData[0])).percentWidth("100%",width).mergeRule(rule).create());

                tableNum3 = String.valueOf(Integer.parseInt(tableNum3) + 1);
            }

            if(whereTable.size()>0){
                dataMap.put("name1", Texts.of("有查询条件查询接口").fontFamily("仿宋").fontSize(16).create());
            }
            if(selectTable.size()>0){
                dataMap.put("name2", Texts.of("默认查询接口").fontFamily("仿宋").fontSize(16).create());
            }
            if(inputTable.size()>0){
                dataMap.put("name3", Texts.of("插入接口").fontFamily("仿宋").fontSize(16).create());
            }
            dataMap.put("idCard", idCard);
            dataMap.put("phone", phone);
            dataMap.put("contactUnit", contactUnit);

        XWPFTemplate template = XWPFTemplate.compile(fileInputStream).render(dataMap);
        //当前用户桌面路径
        File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        // 获取桌面路径
        Path genPath = Paths.get(desktopPath);
        // 在桌面创建文件
        Path outputPath = genPath.resolve("对接文档.docx");


        template.writeAndClose(Files.newOutputStream(outputPath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return "success";


    }
    public List<ServiceBusiness> getServiceBusinessList(ServiceBusiness serviceBusiness) {
        serviceBusiness.setDeleteFlag(DELETE_NO);
        serviceBusiness.setPublishStatus("1"); //已发布
        serviceBusiness.setEffectiveStatus("1"); //生效中
       return serviceBusinessService.selectServiceBusinessList(serviceBusiness);
    }

//    public List<ServiceBusiness> getServiceBusinessByType(ServiceBusiness serviceBusiness) {
//        return serviceBusinessService.selectServiceBusinessList(serviceBusiness);
//    }

}
