base_from_adjust.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // EdbDataAdjust 数据调整指标数据结构体
  12. type EdbDataAdjust struct {
  13. EdbDataId int `orm:"column(edb_data_id);pk"`
  14. EdbInfoId int
  15. EdbCode string
  16. DataTime string
  17. Value float64
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. DataTimestamp int64
  21. }
  22. // AddAdjustEdbData 保存数据调整请求的数据的参数
  23. type AddAdjustEdbData struct {
  24. Date string `description:"数据日期"`
  25. TimestampStr string `description:"日期时间戳"`
  26. Value float64 `description:"数据值"`
  27. }
  28. // SaveAdjustEdb 保存数据调整指标
  29. func SaveAdjustEdb(req SaveAdjustEdbReq) (edbInfo *EdbInfo, err error, errMsg string) {
  30. errMsg = `添加指标失败`
  31. o := orm.NewOrm()
  32. to, err := o.Begin()
  33. if err != nil {
  34. return
  35. }
  36. defer func() {
  37. if err != nil {
  38. _ = to.Rollback()
  39. } else {
  40. _ = to.Commit()
  41. }
  42. }()
  43. var edbCode string
  44. if req.EdbInfoId <= 0 {
  45. //指标code生成
  46. randStr := utils.GetRandDigit(4)
  47. edbCode = `C` + time.Now().Format("060102") + randStr
  48. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  49. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  50. edbInfo = &EdbInfo{
  51. Source: utils.DATA_SOURCE_CALCULATE_ADJUST,
  52. SourceName: "数据调整",
  53. EdbCode: edbCode,
  54. EdbName: utils.TrimStr(req.EdbName),
  55. EdbNameSource: utils.TrimStr(req.EdbName),
  56. Frequency: utils.TrimStr(req.Frequency),
  57. Unit: utils.TrimStr(req.Unit),
  58. ClassifyId: req.ClassifyId,
  59. SysUserId: req.AdminId,
  60. SysUserRealName: req.AdminName,
  61. CreateTime: time.Now(),
  62. ModifyTime: time.Now(),
  63. UniqueCode: uniqueCode,
  64. EdbType: 2,
  65. }
  66. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  67. if tmpErr != nil {
  68. err = tmpErr
  69. return
  70. }
  71. edbInfo.EdbInfoId = int(newEdbInfoId)
  72. // 获取来源指标
  73. fromEdbInfo, tmpErr := GetEdbInfoById(req.FromEdbInfoId)
  74. if tmpErr != nil {
  75. err = tmpErr
  76. errMsg = "获取来源指标信息失败"
  77. return
  78. }
  79. //关联关系
  80. {
  81. calculateMappingItem := new(EdbInfoCalculateMapping)
  82. calculateMappingItem.CreateTime = time.Now()
  83. calculateMappingItem.ModifyTime = time.Now()
  84. calculateMappingItem.Sort = 1
  85. calculateMappingItem.EdbCode = edbCode
  86. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  87. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  88. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  89. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  90. calculateMappingItem.FromSource = fromEdbInfo.Source
  91. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  92. calculateMappingItem.FromTag = ""
  93. calculateMappingItem.Source = edbInfo.Source
  94. calculateMappingItem.SourceName = edbInfo.SourceName
  95. _, err = to.Insert(calculateMappingItem)
  96. if err != nil {
  97. return
  98. }
  99. }
  100. //相关配置
  101. {
  102. var fromEdbEndDate time.Time
  103. if fromEdbInfo.EndDate != `0000-00-00` {
  104. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  105. }
  106. edbAdjustConf := &EdbAdjustConf{
  107. EdbInfoId: edbInfo.EdbInfoId,
  108. SourceEndDate: fromEdbEndDate,
  109. ModifyTime: time.Now(),
  110. CreateTime: time.Now(),
  111. }
  112. _, err = to.Insert(edbAdjustConf)
  113. if err != nil {
  114. return
  115. }
  116. }
  117. } else {
  118. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  119. if err != nil {
  120. return
  121. }
  122. // 更新指标信息
  123. edbInfo.EdbName = utils.TrimStr(req.EdbName)
  124. edbInfo.EdbNameSource = utils.TrimStr(req.EdbName)
  125. edbInfo.Frequency = utils.TrimStr(req.Frequency)
  126. edbInfo.Unit = utils.TrimStr(req.Unit)
  127. edbInfo.ClassifyId = req.ClassifyId
  128. edbInfo.ModifyTime = time.Now()
  129. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime")
  130. if err != nil {
  131. return
  132. }
  133. // 指标code
  134. edbCode = edbInfo.EdbCode
  135. // 清除数据
  136. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_ADJUST)
  137. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  138. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  139. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  140. if err != nil {
  141. return
  142. }
  143. //相关配置
  144. {
  145. // 获取来源指标
  146. calculateMapping, tmpErr := GetEdbInfoCalculateMappingDetail(edbInfo.EdbInfoId)
  147. if tmpErr != nil {
  148. errMsg = `关联指标获取异常`
  149. err = tmpErr
  150. return
  151. }
  152. fromEdbInfo, tmpErr := GetEdbInfoById(calculateMapping.FromEdbInfoId)
  153. if tmpErr != nil {
  154. errMsg = `来源指标不存在`
  155. err = tmpErr
  156. return
  157. }
  158. var fromEdbEndDate time.Time
  159. if fromEdbInfo.EndDate != `0000-00-00` {
  160. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  161. }
  162. // 获取之前的配置
  163. var edbAdjustConf *EdbAdjustConf
  164. sql := ` SELECT * FROM edb_adjust_conf WHERE edb_info_id=? `
  165. err = o.Raw(sql, edbInfo.EdbInfoId).QueryRow(&edbAdjustConf)
  166. if err != nil {
  167. errMsg = "获取指标配置失败"
  168. return
  169. }
  170. // 修改配置的最晚日期
  171. edbAdjustConf.SourceEndDate = fromEdbEndDate
  172. edbAdjustConf.ModifyTime = time.Now()
  173. _, err = to.Update(edbAdjustConf, "SourceEndDate", "ModifyTime")
  174. if err != nil {
  175. errMsg = "更新指标配置失败"
  176. return
  177. }
  178. }
  179. }
  180. dataList := req.DataList
  181. dataObj := make(map[string]AddAdjustEdbData)
  182. for _, item := range dataList {
  183. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.Date, time.Local)
  184. if tmpErr != nil {
  185. err = tmpErr
  186. return
  187. }
  188. timestamp := currentDate.UnixNano() / 1e6
  189. timestampStr := fmt.Sprintf("%d", timestamp)
  190. tmpVal, ok := dataObj[item.Date]
  191. if !ok {
  192. tmpVal = AddAdjustEdbData{
  193. Date: item.Date,
  194. TimestampStr: timestampStr,
  195. Value: item.Value,
  196. }
  197. } else {
  198. tmpVal.Value = item.Value
  199. }
  200. dataObj[item.Date] = tmpVal
  201. }
  202. var isAdd bool
  203. dataTableName := GetEdbDataTableName(edbInfo.Source)
  204. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  205. for _, item := range dataObj {
  206. //值
  207. val := item.Value
  208. saveVal := utils.SubFloatToString(val, 20)
  209. addSql += GetAddSql(fmt.Sprint(edbInfo.EdbInfoId), edbCode, item.Date, item.TimestampStr, saveVal)
  210. isAdd = true
  211. }
  212. if isAdd {
  213. addSql = strings.TrimRight(addSql, ",")
  214. _, err = to.Raw(addSql).Exec()
  215. if err != nil {
  216. return
  217. }
  218. }
  219. return
  220. }
  221. // RefreshAllAdjustEdb 刷新所有 数据调整指标
  222. func RefreshAllAdjustEdb(edbInfo *EdbInfo, fromEdbInfo *EdbInfo) (err error) {
  223. o := orm.NewOrm()
  224. to, err := o.Begin()
  225. if err != nil {
  226. return
  227. }
  228. defer func() {
  229. if err != nil {
  230. fmt.Println("RefreshAllPythonEdb,Err:" + err.Error())
  231. _ = to.Rollback()
  232. } else {
  233. _ = to.Commit()
  234. }
  235. }()
  236. // 当前数据表名
  237. dataTableName := GetEdbDataTableName(edbInfo.Source)
  238. // 获取之前的配置
  239. var edbAdjustConf *EdbAdjustConf
  240. sql := ` SELECT * FROM edb_adjust_conf WHERE edb_info_id=? `
  241. err = o.Raw(sql, edbInfo.EdbInfoId).QueryRow(&edbAdjustConf)
  242. if err != nil {
  243. err = errors.New("获取指标配置失败")
  244. return
  245. }
  246. // 查询关联指标的数据
  247. var condition string
  248. var pars []interface{}
  249. condition += " AND edb_info_id=? AND data_time > ? "
  250. pars = append(pars, fromEdbInfo.EdbInfoId, edbAdjustConf.SourceEndDate)
  251. dataList, err := GetEdbDataListAllByTo(to, condition, pars, fromEdbInfo.Source, 1)
  252. if err != nil {
  253. return err
  254. }
  255. // 已经入库的日期数据
  256. existDataMap := make(map[string]float64)
  257. {
  258. var tmpCondition string
  259. var tmpPars []interface{}
  260. condition += " AND edb_info_id=? AND data_time > ? "
  261. pars = append(pars, fromEdbInfo.EdbInfoId, edbAdjustConf.SourceEndDate)
  262. existDataList, tmpErr := GetEdbDataListAllByTo(to, tmpCondition, tmpPars, edbInfo.Source, 1)
  263. if tmpErr != nil {
  264. err = tmpErr
  265. return
  266. }
  267. for _, v := range existDataList {
  268. existDataMap[v.DataTime] = v.Value
  269. }
  270. }
  271. var isAdd bool
  272. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  273. for _, item := range dataList {
  274. currDay := item.DataTime
  275. //值
  276. val := item.Value
  277. saveVal := utils.SubFloatToString(val, 20)
  278. if existVal, ok := existDataMap[currDay]; !ok {
  279. //格式化时间
  280. currentDate, tmpErr := time.Parse(utils.FormatDate, item.DataTime)
  281. if tmpErr != nil {
  282. err = tmpErr
  283. return
  284. }
  285. timestamp := currentDate.UnixNano() / 1e6
  286. timestampStr := fmt.Sprintf("%d", timestamp)
  287. addSql += GetAddSql(fmt.Sprint(edbInfo.EdbInfoId), edbInfo.EdbCode, item.DataTime, timestampStr, saveVal)
  288. isAdd = true
  289. } else {
  290. if existVal != item.Value {
  291. sql = ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  292. sql = fmt.Sprintf(sql, dataTableName)
  293. _, err = to.Raw(sql, saveVal, edbInfo.EdbInfoId, currDay).Exec()
  294. if err != nil {
  295. return err
  296. }
  297. }
  298. }
  299. }
  300. if isAdd {
  301. addSql = strings.TrimRight(addSql, ",")
  302. _, err = to.Raw(addSql).Exec()
  303. if err != nil {
  304. return
  305. }
  306. }
  307. // 变更数据调整配置表的实际结束日期
  308. {
  309. var fromEdbEndDate time.Time
  310. if fromEdbInfo.EndDate != `0000-00-00` {
  311. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  312. }
  313. edbAdjustConf.SourceEndDate = fromEdbEndDate
  314. edbAdjustConf.ModifyTime = time.Now()
  315. _, err = to.Update(edbAdjustConf, "SourceEndDate", "ModifyTime")
  316. }
  317. return
  318. }
  319. // SaveAdjustEdbReq 保存数据调整请求参数(请求指标服务)
  320. type SaveAdjustEdbReq struct {
  321. AdminId int `description:"添加人id"`
  322. AdminName string `description:"添加人名称"`
  323. EdbInfoId int `description:"指标id"`
  324. FromEdbInfoId int `description:"来源指标id"`
  325. EdbName string `description:"指标名称"`
  326. Frequency string `description:"频度"`
  327. Unit string `description:"单位"`
  328. ClassifyId int `description:"分类id"`
  329. DataList []SaveAdjustEdbDataReq `description:"指标对应的数据值"`
  330. }
  331. // SaveAdjustEdbDataReq 保存数据调整请求的数据的参数
  332. type SaveAdjustEdbDataReq struct {
  333. Date string `description:"数据日期"`
  334. Value float64 `description:"数据值"`
  335. }
  336. // FixData 修复数据
  337. func FixData() {
  338. var list []*EdbInfo
  339. o := orm.NewOrm()
  340. sql := ` SELECT * FROM edb_info WHERE source=? `
  341. _, err := o.Raw(sql, 40).QueryRows(&list)
  342. for _, edbInfo := range list {
  343. // 获取来源指标
  344. calculateMapping, tmpErr := GetEdbInfoCalculateMappingDetail(edbInfo.EdbInfoId)
  345. if tmpErr != nil {
  346. fmt.Println(edbInfo.EdbInfoId, "关联指标获取异常;err:", tmpErr)
  347. continue
  348. }
  349. fromEdbInfo, tmpErr := GetEdbInfoById(calculateMapping.FromEdbInfoId)
  350. if tmpErr != nil {
  351. fmt.Println(edbInfo.EdbInfoId, "来源指标不存在;err:", tmpErr)
  352. continue
  353. }
  354. var fromEdbEndDate time.Time
  355. if fromEdbInfo.EndDate != `0000-00-00` {
  356. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  357. }
  358. // 获取之前的配置
  359. edbAdjustConf := &EdbAdjustConf{
  360. EdbInfoId: edbInfo.EdbInfoId,
  361. SourceEndDate: fromEdbEndDate,
  362. ModifyTime: time.Now(),
  363. CreateTime: time.Now(),
  364. }
  365. _, err = o.Insert(edbAdjustConf)
  366. if err != nil {
  367. fmt.Println(edbInfo.EdbInfoId, "添加失败;err:", tmpErr)
  368. continue
  369. }
  370. }
  371. fmt.Println("end")
  372. }