Quellcode durchsuchen

Merge branch 'dev' of lyr/patient-co-management into dev

lyr vor 8 Jahren
Ursprung
Commit
896f4fc7f1

+ 105 - 0
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/entity/UserPortrait.java

@ -0,0 +1,105 @@
package com.yihu.wlyy.analysis.entity;
import javax.persistence.*;
/**
 * 用户画像实体类
 *
 * Created by lyr-pc on 2017/2/20.
 */
@Entity
@Table(name = "wlyy_user_portrait")
public class UserPortrait {
    // 主键ID
    private Long id;
    // 居民code
    private String userCode;
    // 姓名
    private String userName;
    // 身份证号
    private String idcard;
    // 标签类型
    private String type;
    // 标签名称
    private String typeName;
    // 子标签类型
    private String subType;
    // 子标签名称
    private String subTypeName;
    // 标签值
    private String value;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUserCode() {
        return userCode;
    }
    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getIdcard() {
        return idcard;
    }
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getTypeName() {
        return typeName;
    }
    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
    public String getSubType() {
        return subType;
    }
    public void setSubType(String subType) {
        this.subType = subType;
    }
    public String getSubTypeName() {
        return subTypeName;
    }
    public void setSubTypeName(String subTypeName) {
        this.subTypeName = subTypeName;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

+ 25 - 0
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/etl/ETLConstantData.java

@ -0,0 +1,25 @@
package com.yihu.wlyy.analysis.etl;
/**
 * Created by lyr-pc on 2017/2/20.
 */
public class ETLConstantData {
    /**
     * 性别
     *
     * @return
     */
    public static String sexName(int sex) {
        switch (sex) {
            case 1:
                return "男";
            case 2:
                return "女";
            default:
                return "其他";
        }
    }
}

+ 5 - 4
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/etl/ILogTransform.java

@ -1,8 +1,9 @@
package com.yihu.wlyy.analysis.etl;
package com.yihu.wlyy.analysis.etl;
import com.yihu.wlyy.analysis.model.DataModel;
import com.yihu.wlyy.analysis.model.emun.BusinessType;
import org.json.JSONObject;
import com.yihu.wlyy.analysis.entity.UserPortrait;
import net.sf.json.JSONObject;
import java.util.List;
/**
/**
 * 日志信息提取分析
 * 日志信息提取分析
@ -16,7 +17,7 @@ public interface ILogTransform {
     *
     *
     * @param log
     * @param log
     */
     */
    void transform(JSONObject log);
    List<UserPortrait> transform(JSONObject log) throws Exception;
    /**
    /**
     * 获取日志类型
     * 获取日志类型

+ 20 - 7
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/etl/LogDataTransform.java

@ -1,11 +1,13 @@
package com.yihu.wlyy.analysis.etl;
package com.yihu.wlyy.analysis.etl;
import org.json.JSONObject;
import com.yihu.wlyy.analysis.entity.UserPortrait;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.File;
import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map;
/**
/**
@ -52,18 +54,25 @@ public class LogDataTransform {
     *
     *
     * @param log
     * @param log
     */
     */
    public void transform(JSONObject log) {
    public List<UserPortrait> transform(JSONObject log) {
        try {
        try {
            ILogTransform transform = transforms.get(log.getString("logType"));
            String logType = log.getString("logType");
            if (logType.equals("1")) {
                String businessType = log.getJSONObject("data").getString("businessType");
                ILogTransform transform = transforms.get(businessType);
            if (transform == null) {
                logger.error("logType:" + log.getString("logType") + " transform can not find");
            } else {
                transform.transform(log);
                if (transform == null) {
                    logger.error("logType:" + log.getString("logType") + " transform can not find");
                } else {
                    return transform.transform(log);
                }
            }
            }
            return null;
        } catch (Exception e) {
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
            logger.error(e.getMessage());
            logger.error(e.getMessage());
            return null;
        }
        }
    }
    }
@ -107,4 +116,8 @@ public class LogDataTransform {
            logger.error("log transform init failed:" + e.getMessage());
            logger.error("log transform init failed:" + e.getMessage());
        }
        }
    }
    }
    public static void main(String[] args) {
        LogDataTransform.getLogTransform();
    }
}
}

+ 6 - 4
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/etl/transform/ConsultLogTransform.java

@ -1,9 +1,11 @@
package com.yihu.wlyy.analysis.etl.transform;
package com.yihu.wlyy.analysis.etl.transform;
import com.yihu.wlyy.analysis.entity.UserPortrait;
import com.yihu.wlyy.analysis.etl.BusinessTypeEnum;
import com.yihu.wlyy.analysis.etl.BusinessTypeEnum;
import com.yihu.wlyy.analysis.etl.ILogTransform;
import com.yihu.wlyy.analysis.etl.ILogTransform;
import com.yihu.wlyy.analysis.model.DataModel;
import org.json.JSONObject;
import net.sf.json.JSONObject;
import java.util.List;
/**
/**
 * 咨询日志提取分析
 * 咨询日志提取分析
@ -15,8 +17,8 @@ public class ConsultLogTransform implements ILogTransform {
    private final BusinessTypeEnum logType = BusinessTypeEnum.consult;
    private final BusinessTypeEnum logType = BusinessTypeEnum.consult;
    @Override
    @Override
    public void transform(JSONObject log) {
    public List<UserPortrait> transform(JSONObject log) {
        return null;
    }
    }
    @Override
    @Override

+ 45 - 3
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/etl/transform/RegisterLogTransform.java

@ -1,9 +1,15 @@
package com.yihu.wlyy.analysis.etl.transform;
package com.yihu.wlyy.analysis.etl.transform;
import com.yihu.wlyy.analysis.etl.ETLConstantData;
import com.yihu.wlyy.analysis.etl.ILogTransform;
import com.yihu.wlyy.analysis.etl.ILogTransform;
import com.yihu.wlyy.analysis.etl.BusinessTypeEnum;
import com.yihu.wlyy.analysis.etl.BusinessTypeEnum;
import com.yihu.wlyy.analysis.model.DataModel;
import org.json.JSONObject;
import com.yihu.wlyy.analysis.entity.UserPortrait;
import com.yihu.wlyy.analysis.etl.util.IdcardUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
/**
 * 注册日志信息提取分析
 * 注册日志信息提取分析
@ -15,8 +21,16 @@ public class RegisterLogTransform implements ILogTransform {
    private final BusinessTypeEnum logType = BusinessTypeEnum.register;
    private final BusinessTypeEnum logType = BusinessTypeEnum.register;
    @Override
    @Override
    public void transform(JSONObject log) {
    public List<UserPortrait> transform(JSONObject log) throws Exception {
        JSONObject logData = log.getJSONObject("data");
        JSONObject businessData = logData.getJSONObject("data");
        List<UserPortrait> labelInfoList = new ArrayList<>();
        // 性别
        UserPortrait sexInfo = tranformSex(businessData);
        if (sexInfo != null) labelInfoList.add(sexInfo);
        return labelInfoList;
    }
    }
    @Override
    @Override
@ -29,4 +43,32 @@ public class RegisterLogTransform implements ILogTransform {
        return logType.name();
        return logType.name();
    }
    }
    /**
     * 获取性别信息
     *
     * @param log
     * @return
     */
    private UserPortrait tranformSex(JSONObject log) throws Exception {
        String idcard = log.getString("idcard");
        UserPortrait labelInfo = new UserPortrait();
        int sex = 3;
        if (StringUtils.isNotEmpty(idcard)) {
            sex = IdcardUtil.getSexForIdcard(idcard);
        } else {
            return null;
        }
        labelInfo.setUserCode(log.getString("code"));
        labelInfo.setUserName(log.getString("name"));
        labelInfo.setIdcard(idcard);
        labelInfo.setType("1");
        labelInfo.setTypeName("基础特征");
        labelInfo.setSubType("1");
        labelInfo.setSubTypeName("性别");
        labelInfo.setValue(ETLConstantData.sexName(sex));
        return labelInfo;
    }
}
}

+ 7 - 7
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/util/IdcardUtil.java

@ -1,4 +1,4 @@
package com.yihu.wlyy.analysis.util;
package com.yihu.wlyy.analysis.etl.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.StringUtils;
@ -59,21 +59,21 @@ public class IdcardUtil {
     * @return
     * @return
     * @throws Exception
     * @throws Exception
     */
     */
    public static String getSexForIdcard(String CardCode)
    public static int getSexForIdcard(String CardCode)
            throws Exception {
            throws Exception {
        String sex = "3";
        int sex = 3;
        if (CardCode.length() == 18) {
        if (CardCode.length() == 18) {
            if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
            if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                sex = "2";
                sex = 2;
            } else {
            } else {
                sex = "1";
                sex = 1;
            }
            }
        } else if (CardCode.length() == 15) {
        } else if (CardCode.length() == 15) {
            String usex = CardCode.substring(14, 15);// 用户的性别
            String usex = CardCode.substring(14, 15);// 用户的性别
            if (Integer.parseInt(usex) % 2 == 0) {
            if (Integer.parseInt(usex) % 2 == 0) {
                sex = "2";
                sex = 2;
            } else {
            } else {
                sex = "1";
                sex = 1;
            }
            }
        }
        }
        return sex;
        return sex;

+ 15 - 2
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/listener/LabelDataListener.java

@ -1,7 +1,10 @@
package com.yihu.wlyy.analysis.listener;
package com.yihu.wlyy.analysis.listener;
import com.yihu.wlyy.analysis.entity.UserPortrait;
import com.yihu.wlyy.analysis.etl.LogDataTransform;
import com.yihu.wlyy.analysis.model.BusinessDataModel;
import com.yihu.wlyy.analysis.model.BusinessDataModel;
import com.yihu.wlyy.analysis.model.OperatorDataModel;
import com.yihu.wlyy.analysis.model.OperatorDataModel;
import com.yihu.wlyy.analysis.repository.UserPortraitDao;
import net.sf.json.JSONObject;
import net.sf.json.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.Logger;
@ -9,7 +12,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Optional;
/**
/**
@ -20,12 +25,15 @@ public class LabelDataListener {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    @Autowired
    private MongoTemplate mongoTemplate;
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserPortraitDao userPortraitDao;
    private static String mongoDb_Business_TableName = "WLYY_BUSINESS_LOG";
    private static String mongoDb_Business_TableName = "WLYY_BUSINESS_LOG";
    private static String mongoDb_Operator_TableName = "WLYY_OPERATOR_LOG";
    private static String mongoDb_Operator_TableName = "WLYY_OPERATOR_LOG";
    //@Scheduled(cron = "0 0/1 * * * ?") //每分钟执行一次
    //@Scheduled(cron = "0 0/1 * * * ?") //每分钟执行一次
    @KafkaListener(topics = "flumeLog1")
    @KafkaListener(topics = "flumeLog1")
    @Transactional
    public void labelData(ConsumerRecord<?, ?> record) {
    public void labelData(ConsumerRecord<?, ?> record) {
        Long startTime = System.currentTimeMillis();
        Long startTime = System.currentTimeMillis();
        logger.debug("Kafka开始消费");
        logger.debug("Kafka开始消费");
@ -36,9 +44,14 @@ public class LabelDataListener {
                JSONObject jsonObject = JSONObject.fromObject(message);
                JSONObject jsonObject = JSONObject.fromObject(message);
                if (jsonObject.has("logType")) {
                if (jsonObject.has("logType")) {
                    String logType = jsonObject.getString("logType");
                    String logType = jsonObject.getString("logType");
                    //根据日志类别保存到mongodb
                    // 根据日志类别保存到mongodb
                    saveLogToMongo(logType, jsonObject);
                    saveLogToMongo(logType, jsonObject);
                    // 日志分析
                    List<UserPortrait> userPortraitList = LogDataTransform.getLogTransform().transform(jsonObject);
                    if(userPortraitList != null && userPortraitList.size() > 0) {
                        // 日志分析结果保存
                        userPortraitDao.save(userPortraitList);
                    }
                    Long endTime = System.currentTimeMillis();
                    Long endTime = System.currentTimeMillis();
                    Long time = startTime - endTime;
                    Long time = startTime - endTime;
                    logger.debug("time(ms):" + time);
                    logger.debug("time(ms):" + time);

+ 11 - 0
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/repository/UserPortraitDao.java

@ -0,0 +1,11 @@
package com.yihu.wlyy.analysis.repository;
import com.yihu.wlyy.analysis.entity.UserPortrait;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by lyr-pc on 2017/2/20.
 */
public interface UserPortraitDao  extends PagingAndSortingRepository<UserPortrait, Long>{
}