|
|
@@ -0,0 +1,101 @@
|
|
|
+package com.lc.ibps.task.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.lc.ibps.base.core.util.BeanUtils;
|
|
|
+import com.lc.ibps.base.framework.table.ICommonDao;
|
|
|
+import com.lc.ibps.base.web.context.ContextUtil;
|
|
|
+import com.lc.ibps.org.party.persistence.entity.DefaultPartyUserPo;
|
|
|
+import com.lc.ibps.task.entity.Tzqxdsrwb;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author gaozl
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class BatchDataService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ICommonDao<?> commonDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写在这里是因为 service 包下有配置事务
|
|
|
+ * @param zqrw
|
|
|
+ */
|
|
|
+ public void additionData(Tzqxdsrwb zqrw){
|
|
|
+ try{
|
|
|
+ DefaultPartyUserPo user = new DefaultPartyUserPo();
|
|
|
+ user.setUserId(zqrw.getBianZhiRen());
|
|
|
+ ContextUtil.setCurrentUser(user);
|
|
|
+ List<LinkedHashMap> paramWhere = JSONObject.parseArray(zqrw.getChuShiShuJu(), LinkedHashMap.class);
|
|
|
+
|
|
|
+ // 主表
|
|
|
+ for (LinkedHashMap map : paramWhere) {
|
|
|
+ List<LinkedHashMap> subList = new ArrayList<>();
|
|
|
+ String associatedField = null;
|
|
|
+ String subTableName = null;
|
|
|
+ if ("是".equals(zqrw.getShiFouYouZiBiao())){
|
|
|
+ // 子表code
|
|
|
+ String subTableCode = zqrw.getZiBiaoBiaoMing().substring(2,zqrw.getZiBiaoBiaoMing().indexOf("#"));
|
|
|
+ // 主表与子表关联关系字段
|
|
|
+ associatedField = zqrw.getZiBiaoBiaoMing().substring(zqrw.getZiBiaoBiaoMing().indexOf("#")+1);
|
|
|
+ subTableName = zqrw.getZiBiaoBiaoMing().substring(0,zqrw.getZiBiaoBiaoMing().indexOf("#"));
|
|
|
+ if (map.containsKey(subTableCode)) {
|
|
|
+ // 有子表 1、去除子表集合
|
|
|
+ JSONArray jsonArray = (JSONArray) map.get(subTableCode);
|
|
|
+ subList = JSONObject.parseArray(jsonArray.toJSONString(), LinkedHashMap.class);
|
|
|
+ map.remove(subTableCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String addId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ commonDao.execute(this.buildInsertSql(map, addId, zqrw.getYeWuBiaoMing()));
|
|
|
+
|
|
|
+ // 子表
|
|
|
+ if (BeanUtils.isNotEmpty(subList)){
|
|
|
+ for (LinkedHashMap sub : subList) {
|
|
|
+ String uid = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ if (BeanUtils.isNotEmpty(associatedField)){
|
|
|
+ sub.put(associatedField, addId);
|
|
|
+ }else {
|
|
|
+ sub.put("parent_id_", addId);
|
|
|
+ }
|
|
|
+ commonDao.execute(this.buildInsertSql(sub, uid, subTableName));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("周期任务添加数据发生错误,原始数据ID:"+ zqrw.getId());
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String buildInsertSql(LinkedHashMap map,String uuid,String tableName){
|
|
|
+ map.put("id_", uuid);
|
|
|
+ map.put("create_by_", ContextUtil.getCurrentUserId());
|
|
|
+ map.put("create_time_", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ StringBuilder sql = new StringBuilder("insert into " + tableName + "(");
|
|
|
+ for (Object key : map.keySet()) {
|
|
|
+ sql.append(key).append(",");
|
|
|
+ }
|
|
|
+ sql.delete(sql.length()-1,sql.length());
|
|
|
+ sql.append(") values(");
|
|
|
+ for (Object val : map.values()) {
|
|
|
+ if ("now()".equals(val)){
|
|
|
+ val = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
+ }
|
|
|
+ sql.append("'").append(val).append("'").append(",");
|
|
|
+ }
|
|
|
+ sql.delete(sql.length()-1,sql.length());
|
|
|
+ sql.append(")");
|
|
|
+ return sql.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|