kimi
2020-05-18 c8aee7b9bfd79cfd741d7e5692520f4f51a31a86
src/main/java/com/highdatas/mdm/controller/FileController.java
@@ -1,26 +1,28 @@
package com.highdatas.mdm.controller;
import com.alibaba.fastjson.JSONObject;
import com.highdatas.mdm.entity.SysAssemble;
import com.highdatas.mdm.entity.SysAssembleParams;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.mapper.SysViewMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.ISysAssembleDbService;
import com.highdatas.mdm.service.ISysAssembleParamsService;
import com.highdatas.mdm.service.ISysAssembleService;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import com.highdatas.mdm.util.RedisClient;
import com.highdatas.mdm.pojo.Segment;
import com.highdatas.mdm.pojo.kettle.UnBigDataDataSourceInfo;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * @author kimi
@@ -39,6 +41,16 @@
    ISysAssembleParamsService paramsService;
    @Autowired
    RedisClient redisClient;
    @Autowired
    ISysViewService viewService;
    @Autowired
    IMenuMappingService menuMappingService;
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    UnBigDataDataSourceInfo unBigDataDataSourceInfo;
    @RequestMapping(value = "/loadSqlTable", method = RequestMethod.POST)
    @ResponseBody
@@ -123,4 +135,78 @@
        }
    }
    @RequestMapping(value = "/loadMappingFile", method = RequestMethod.POST)
    @ResponseBody
    public Result loadMappingFile(@RequestParam("file")MultipartFile file,@RequestParam String id, @RequestParam String menuId, @RequestParam String field, HttpServletRequest request) {
        SysView sysView = viewService.selectById(id);
        if (sysView == null){
            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
        }
        String mappingTable = sysView.getMappingTable();
        if (StringUtils.isEmpty(mappingTable)){
            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
        }
        boolean exists = unBigDataDataSourceInfo.checkTableExists(mappingTable);
        if (!exists) {
            ArrayList<String> fields = new ArrayList<>();
            fields.add(Constant.ID);
            fields.add(Constant.Code);
            fields.add("pre");
            fields.add("fix");
            boolean created = unBigDataDataSourceInfo.createTable(mappingTable, fields);
            if (!created) {
                return Result.error(CodeMsg.CREATE_ERROR);
            }
        }
        SqlSession session = null;
        try {
            String name = file.getOriginalFilename();
            List<ViewMappingItem> viewMappingByExcel = ExcelUtil.getViewMappingByExcel(file.getInputStream(), name);
            if (viewMappingByExcel.isEmpty()) {
                return Result.error(CodeMsg.EMPTY_ERROR);
            }
            Maintain maintainByMenu = viewService.getMaintainByMenu(sysView, menuId);
            if (maintainByMenu == null) {
                return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
            }
            String changeFieldName = viewService.changeFieldName(maintainByMenu.getTableName(), field);
            Segment segment = new Segment(Constant.Code, changeFieldName);
            tableInfoMapper.delete(mappingTable, segment.toString());
            session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);//关闭session的自动提交
            SysViewMapper mapper = session.getMapper(SysViewMapper.class);
            AtomicInteger idx = new AtomicInteger(0);
            ContentBuilder builder = new ContentBuilder(Constant.COMMA);
            for (ViewMappingItem viewMappingItem : viewMappingByExcel) {
                builder.clear();
                builder.append(DbUtils.quotedStr(changeFieldName));
                builder.append(DbUtils.quotedStr(viewMappingItem.getPre())).append(DbUtils.quotedStr(viewMappingItem.getFix()));
                mapper.insertViewMapping(mappingTable, builder.toString());
                int i = idx.get();
                if (i % 1000 == 0 || i == viewMappingByExcel.size()-1) {
                    //手动每1000个一提交,提交后无法回滚
                    session.commit();
                    session.clearCache();//注意,如果没有这个动作,可能会导致内存崩溃。
                }
                idx.getAndIncrement();
            }
        }catch (Exception e) {
            e.printStackTrace();
            if (session != null) {
                session.rollback();
            }
            return Result.error(new CodeMsg(6002, e.getMessage()));
        }finally {
            if (session != null) {
                session.close();
            }
        }
        return Result.success(null);
    }
}