base_from_adjust.go 12 KB

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