base_from_adjust.go 11 KB

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