base_from_adjust.go 11 KB

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