Roc 2 年之前
父节点
当前提交
4bf667a211
共有 1 个文件被更改,包括 23 次插入0 次删除
  1. 23 0
      models/edb_data_calculate_hcz.go

+ 23 - 0
models/edb_data_calculate_hcz.go

@@ -241,8 +241,10 @@ func refreshAllCalculateHcz(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo *
 		return err
 	}
 	existDataMap := make(map[string]string)
+	removeDateMap := make(map[string]string) //需要移除的日期
 	for _, v := range existDataList {
 		existDataMap[v.DataTime] = v.Value
+		removeDateMap[v.DataTime] = v.DataTime
 	}
 	fmt.Println("existDataMap:", existDataMap)
 	addSql := ` INSERT INTO edb_data_calculate_hcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
@@ -259,6 +261,9 @@ func refreshAllCalculateHcz(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo *
 			if currentItem != nil && preItem != nil {
 				existKey := edbCode + currentItem.DataTime
 				if _, ok := existMap[existKey]; !ok {
+					// 有计算出来值,那么就从待删除指标中移除
+					delete(removeDateMap, currentItem.DataTime)
+
 					currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
 					timestamp := currentDate.UnixNano() / 1e6
 					timestampStr := fmt.Sprintf("%d", timestamp)
@@ -292,6 +297,24 @@ func refreshAllCalculateHcz(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo *
 			return err
 		}
 	}
+
+	if len(removeDateMap) > 0 {
+		removeDateList := make([]string, 0) //需要移除的日期
+		for k := range removeDateMap {
+			removeDateList = append(removeDateList, k)
+		}
+		removeDateStr := strings.Join(removeDateList, `","`)
+		removeDateStr = `"` + removeDateStr + `"`
+		//如果拼接指标变更了,那么需要删除所有的指标数据
+		tableName := GetEdbDataTableName(source)
+		sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
+
+		_, err = to.Raw(sql, edbInfoId).Exec()
+		if err != nil {
+			err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
+			return
+		}
+	}
 	return
 }