|
@@ -0,0 +1,168 @@
|
|
|
|
+package cn.com.qmth.examcloud.tool.task.basic;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.tool.task.Task;
|
|
|
|
+import lombok.Data;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
|
|
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
+
|
|
|
|
+import java.io.Serializable;
|
|
|
|
+import java.sql.PreparedStatement;
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 数据割接:
|
|
|
|
+ * 用户与学习中心、课程、考试等数据维度的默认权限初始化
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+public class InitUserDataRule implements Task {
|
|
|
|
+
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(InitUserDataRule.class);
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void start(String params) {
|
|
|
|
+ log.info("task start... " + params);
|
|
|
|
+
|
|
|
|
+ StringBuilder querySql = new StringBuilder()
|
|
|
|
+ .append("select id,root_org_id,org_id from ec_b_user ")
|
|
|
|
+ .append("where id > %s ")
|
|
|
|
+ .append("order by id asc limit 0,100");
|
|
|
|
+
|
|
|
|
+ Long nextMinId = 0L;
|
|
|
|
+ while (true) {
|
|
|
|
+ log.info("query nextMinId:" + nextMinId);
|
|
|
|
+ List<UserInfo> list = jdbcTemplate.query(String.format(querySql.toString(), nextMinId),
|
|
|
|
+ new BeanPropertyRowMapper(UserInfo.class));
|
|
|
|
+
|
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ nextMinId = list.get(list.size() - 1).getId();
|
|
|
|
+
|
|
|
|
+ this.initUserDataRulesForDefaultStatus(list);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("task end...");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initUserDataRulesForDefaultStatus(List<UserInfo> list) {
|
|
|
|
+ List<UserDataRuleInfo> todoList = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ int existSize = 0;
|
|
|
|
+ for (UserInfo user : list) {
|
|
|
|
+ for (DataRuleType type : DataRuleType.values()) {
|
|
|
|
+ UserDataRuleInfo rule = new UserDataRuleInfo();
|
|
|
|
+ rule.setRootOrgId(user.getRootOrgId());
|
|
|
|
+ rule.setUserId(user.getId());
|
|
|
|
+ rule.setType(type);
|
|
|
|
+ rule.setRefId(-1L);
|
|
|
|
+ rule.setEnabled(true);
|
|
|
|
+
|
|
|
|
+ if (this.existUserDataRule(rule)) {
|
|
|
|
+ existSize++;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ todoList.add(rule);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info(String.format("baseUserSize = %s, existSize = %s, todoSize = %s \n",
|
|
|
|
+ list.size(), existSize, todoList.size()));
|
|
|
|
+ this.addUserDataRules(todoList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean existUserDataRule(UserDataRuleInfo rule) {
|
|
|
|
+ String countSql = String.format(
|
|
|
|
+ "select count(1) from ec_b_user_data_rule where user_id = %s and type = '%s' and ref_id = %s",
|
|
|
|
+ rule.getUserId(), rule.getType().name(), rule.getRefId());
|
|
|
|
+ Long total = jdbcTemplate.queryForObject(countSql, Long.class);
|
|
|
|
+ return total > 0 ? true : false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void addUserDataRules(List<UserDataRuleInfo> list) {
|
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder insertSql = new StringBuilder()
|
|
|
|
+ .append("insert into ec_b_user_data_rule ")
|
|
|
|
+ .append("(user_id,type,ref_id,root_org_id,enabled,creation_time,update_time) ")
|
|
|
|
+ .append("values (?,?,?,?,?,?,?)");
|
|
|
|
+
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+
|
|
|
|
+ jdbcTemplate.batchUpdate(insertSql.toString(), new BatchPreparedStatementSetter() {
|
|
|
|
+ @Override
|
|
|
|
+ public void setValues(PreparedStatement ps, int n) throws SQLException {
|
|
|
|
+ UserDataRuleInfo row = list.get(n);
|
|
|
|
+ ps.setLong(1, row.getUserId());
|
|
|
|
+ ps.setString(2, row.getType().name());
|
|
|
|
+ ps.setLong(3, row.getRefId());
|
|
|
|
+ ps.setLong(4, row.getRootOrgId());
|
|
|
|
+ ps.setBoolean(5, row.getEnabled());
|
|
|
|
+ ps.setString(6, sdf.format(new Date()));
|
|
|
|
+ ps.setString(7, sdf.format(new Date()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int getBatchSize() {
|
|
|
|
+ return list.size();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Data
|
|
|
|
+ static class UserInfo implements Serializable {
|
|
|
|
+
|
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
|
+
|
|
|
|
+ private Long id;
|
|
|
|
+
|
|
|
|
+ private Long rootOrgId;
|
|
|
|
+
|
|
|
|
+ private Long orgId;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Data
|
|
|
|
+ static class UserDataRuleInfo implements Serializable {
|
|
|
|
+
|
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
|
+
|
|
|
|
+ private Long rootOrgId;
|
|
|
|
+
|
|
|
|
+ private Long userId;
|
|
|
|
+
|
|
|
|
+ private DataRuleType type;
|
|
|
|
+
|
|
|
|
+ private Long refId;
|
|
|
|
+
|
|
|
|
+ private Boolean enabled;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ enum DataRuleType {
|
|
|
|
+
|
|
|
|
+ ORG,
|
|
|
|
+
|
|
|
|
+ EXAM,
|
|
|
|
+
|
|
|
|
+ COURSE;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|