Jelajahi Sumber

增加标签删除以及计时

LiTaohong 7 tahun lalu
induk
melakukan
e6c6b4e747

+ 1 - 1
patient-co/patient-co-figure-label/pom.xml

@ -81,7 +81,7 @@
        <dependency>
            <groupId>com.yihu</groupId>
            <artifactId>common-data-es-starter</artifactId>
            <version>1.1.0</version>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.yihu</groupId>

+ 68 - 0
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/controller/DataOperationController.java

@ -0,0 +1,68 @@
package com.yihu.wlyy.figure.label.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.base.es.config.ElastricSearchHelper;
import com.yihu.wlyy.figure.label.util.ConstantUtil;
import com.yihu.wlyy.figure.label.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * @author litaohong on 2018/5/3
 * @project patient-co-management
 */
@RestController
@RequestMapping(value = "/data", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "ES数据操作控制器")
public class DataOperationController extends BaseController{
    private Logger logger = LoggerFactory.getLogger(DataOperationController.class);
    @Autowired
    private ElastricSearchHelper elastricSearchHelper;
    @Autowired
    ObjectMapper objectMapper;
    @ApiOperation(value = "根据dictCode删除es数据")
    @RequestMapping(value = "deteleByDictCode", method = RequestMethod.POST)
    public String deleteBydictCode( @ApiParam(name = "dictCode", value = "dictCode", required = true)@RequestParam(value = "dictCode", required = true) String dictCode) throws JobExecutionException {
       long count = deleteData(dictCode);
       return success("删除成功,共计"+ count +"条");
    }
    public long  deleteData(String dictCode){
        long start = System.currentTimeMillis();
        TimeUtil.start(logger,"start delete",start);
        List<Map<String, Object>>  list = new ArrayList<>();
        String sql = "select * from " + ConstantUtil.figure_label_es_index + "/" +  ConstantUtil.figure_label_es_type + " where dictCode ='" + dictCode + "' limit 10000";
        list.addAll(elastricSearchHelper.excuceSQL(sql));
        long count = list.size();
        while (list.size() > 0){
            delete(list);
            list = elastricSearchHelper.excuceSQL(sql);
            count+= list.size();
        }
        TimeUtil.finish(logger,"finish delete",start,System.currentTimeMillis());
        return count;
    }
    public void delete(List<Map<String, Object>> list){
        elastricSearchHelper.delete(ConstantUtil.figure_label_es_index,ConstantUtil.figure_label_es_type,list);
    }
}

+ 5 - 1
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/convert/ConvertHelper.java

@ -49,8 +49,12 @@ public class ConvertHelper {
        if(CollectionUtils.isEmpty(dataModels)){
            return saveModels;
        }
        //标签job未配置
        if(null == flLabelDictJob){
            return saveModels;
        }
        //转换器为空表示不需要转换
        if (flLabelDictJob == null || StringUtils.isEmpty(flLabelDictJob.getConvertClazz())) {
        if (StringUtils.isEmpty(flLabelDictJob.getConvertClazz())) {
            return generateSaveModleWithOutConvert(dataModels,flLabelDictJob.getCategoryId(),sourceType,source);
        }
        List<FlLabelDict> flLabelDictList = jdbcTemplate.query(flLabelDictJob.getSql(), new BeanPropertyRowMapper(FlLabelDict.class));

+ 0 - 48
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/convert/HeightConvert.java

@ -1,48 +0,0 @@
package com.yihu.wlyy.figure.label.convert;
import com.yihu.figure_label.entity.FlLabelDict;
import com.yihu.wlyy.figure.label.model.DataModel;
import com.yihu.wlyy.figure.label.model.SaveModel;
import com.yihu.wlyy.figure.label.util.ConstantUtil;
import com.yihu.wlyy.figure.label.util.IdCardUtil;
import com.yihu.wlyy.figure.label.util.MakeIDUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.*;
/**
 * @author lith on 2018.04.11
 * 身高标签转换器
 */
@Component
public class HeightConvert implements Convert {
    @Autowired
    private ConvertHelper convertHelper;
    @Override
    public List<SaveModel> convert(List<DataModel> modelList,List<FlLabelDict> flLabelDictList,String sourceType,String source) {
        List<SaveModel> saveModels = new ArrayList<>();
        FlLabelDict dict = flLabelDictList.get(0);
        for(DataModel dataModel : modelList){
            SaveModel saveModel = new SaveModel();
            saveModel.setId(MakeIDUtil.makeSaveModelID(dataModel,dataModel.getLabelValue()));
            saveModel.setIdcard(dataModel.getIdcard());
            saveModel.setLabelType(dict.getParentCode());
            saveModel.setLabelCode(dict.getLabelCode());
            saveModel.setLabelName(dict.getLabelName());
            saveModel.setLabelValue(dataModel.getLabelValue() + "cm");
            convertHelper.generateDataSource(dataModel,saveModel,sourceType,source);
            if(org.apache.commons.lang3.StringUtils.isEmpty(saveModel.getSource())){
                return new ArrayList<>();
            }
            saveModel.setCreateTime(DateFormatUtils.format(new Date(), ConstantUtil.date_format));
            saveModels.add(saveModel);
        }
        return saveModels;
    }
}

+ 0 - 52
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/convert/WeightConvert.java

@ -1,52 +0,0 @@
package com.yihu.wlyy.figure.label.convert;
import com.yihu.figure_label.entity.FlLabelDict;
import com.yihu.wlyy.figure.label.model.DataModel;
import com.yihu.wlyy.figure.label.model.SaveModel;
import com.yihu.wlyy.figure.label.util.ConstantUtil;
import com.yihu.wlyy.figure.label.util.MakeIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author litaohong on 2018/4/12
 * @project patient-co-management
 * 体重标签转换器
 */
@Component
public class WeightConvert implements Convert{
    @Autowired
    private ConvertHelper convertHelper;
    @Override
    public List<SaveModel> convert(List<DataModel> modelList, List<FlLabelDict> flLabelDictList, String sourceType, String source) {
        List<SaveModel> saveModels = new ArrayList<>();
        FlLabelDict dict = flLabelDictList.get(0);
        for(DataModel dataModel : modelList){
                    SaveModel saveModel = new SaveModel();
                    saveModel.setId(MakeIDUtil.makeSaveModelID(dataModel,dataModel.getLabelValue()));
                    saveModel.setIdcard(dataModel.getIdcard());
                    saveModel.setLabelType(dict.getParentCode());
                    saveModel.setLabelCode(dict.getLabelCode());
                    saveModel.setLabelName(dict.getLabelName());
                    saveModel.setLabelValue(dataModel.getLabelValue() + "kg");
                    convertHelper.generateDataSource(dataModel,saveModel,sourceType,source);
                    if(StringUtils.isEmpty(saveModel.getSource())){
                         return new ArrayList<>();
                    }
                    saveModel.setCreateTime(DateFormatUtils.format(new Date(), ConstantUtil.date_format));
                    saveModels.add(saveModel);
                }
        return saveModels;
    }
}

+ 8 - 2
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/extract/MysqlExtracter.java

@ -1,6 +1,10 @@
package com.yihu.wlyy.figure.label.extract;
import com.yihu.wlyy.figure.label.model.DataModel;
import com.yihu.wlyy.figure.label.util.ConstantUtil;
import com.yihu.wlyy.figure.label.util.TimeUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -88,13 +92,15 @@ public class MysqlExtracter implements Extracter {
                threadList.add(mutilThreadMysqlExtracter);
            }
            long start = System.currentTimeMillis();
            logger.info("start time:" + String.valueOf(start));
            String logTitle = "therad get data";
            TimeUtil.start(logger,logTitle,start);
            List<Future<List<DataModel>>> futureList = fixedThreadPool.invokeAll(threadList);
            //取回线程执行的结果
            for (Future future : futureList) {
                datas.addAll((List<DataModel>) future.get());
            }
            logger.info("coast time:" + String.valueOf(System.currentTimeMillis() - start));
            long finish = System.currentTimeMillis();
            TimeUtil.finish(logger,logTitle,start,finish);
        } catch (Exception e) {
            logger.error("MutilThreadMysqlExtracter call failed!");
        }finally {

+ 14 - 3
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/storage/Store2ES.java

@ -5,6 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.base.es.config.ElastricSearchHelper;
import com.yihu.wlyy.figure.label.model.SaveModel;
import com.yihu.wlyy.figure.label.util.ConstantUtil;
import com.yihu.wlyy.figure.label.util.TimeUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -17,15 +21,22 @@ import java.util.List;
@Component
public class Store2ES implements Storager {
    private Logger logger = LoggerFactory.getLogger(Store2ES.class);
    @Autowired
    private ElastricSearchHelper elastricSearchHelper;
    @Autowired
    ObjectMapper objectMapper;
    @Override
    public void save(List<SaveModel> modelList) {
        List<String> list = new ArrayList<>();
        int saveCount = 0;
        long start = System.currentTimeMillis();
        String logtitle = "start to loop(save) data,total size:" + modelList.size();
        TimeUtil.start(logger,logtitle,start);
        for(SaveModel saveModel:modelList){
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String str =  objectMapper.writeValueAsString(saveModel);
                list.add(str);
@ -42,7 +53,7 @@ public class Store2ES implements Storager {
                elastricSearchHelper.saveBulkWithCustomId(ConstantUtil.figure_label_es_index, ConstantUtil.figure_label_es_type, list,"id");
            }
        }
        long finish = System.currentTimeMillis();
        TimeUtil.finish(logger,logtitle,start,finish);
    }
}

+ 43 - 0
patient-co/patient-co-figure-label/src/main/java/com/yihu/wlyy/figure/label/util/TimeUtil.java

@ -0,0 +1,43 @@
package com.yihu.wlyy.figure.label.util;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
/**
 * @author litaohong on 2018/5/3
 * @project patient-co-management
 * 计时器工具类
 */
public class TimeUtil{
    public static void start(Logger logger,String title,long start){
        StringBuilder sb = new StringBuilder();
        sb.append(title).append(" start at ")
                .append(DateFormatUtils.format(start, ConstantUtil.date_format));
        logger.info(sb.toString());
    }
    public static void finish(Logger logger,String title,long start,long finishTime){
        StringBuilder sb = new StringBuilder();
        long delta = finishTime - start;
        sb.append(title).append(" finish at ")
                .append(DateFormatUtils.format(start, ConstantUtil.date_format)).append(",coast: ");
        long day = 1000 * 60 * 60 * 24;
        long hour = 1000 * 60 * 60;
        long min = 1000 * 60;
        if(delta > day){
            sb.append(TimeUnit.MILLISECONDS.toSeconds(delta) % 60).append("天");
        }
        if(delta > hour){
            sb.append(TimeUnit.MILLISECONDS.toHours(delta) % 24).append("时");
        }
        if(delta > min){
            sb.append(TimeUnit.MILLISECONDS.toMinutes(delta) % 60).append("分");
        }
        sb.append(TimeUnit.MILLISECONDS.toSeconds(delta) % 60).append("秒");
        logger.info(sb.toString());
    }
}