base_from_adjust.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/global"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  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. to := global.DEFAULT_DmSQL.Begin()
  34. if err != nil {
  35. return
  36. }
  37. defer func() {
  38. if err != nil {
  39. _ = to.Rollback()
  40. } else {
  41. _ = to.Commit()
  42. }
  43. }()
  44. var edbCode string
  45. if req.EdbInfoId <= 0 {
  46. //指标code生成
  47. //生成指标编码
  48. edbCode, err = utils.GenerateEdbCode(1, "")
  49. if err != nil {
  50. err = errors.New("指标编码生成失败,Err:" + err.Error())
  51. return
  52. }
  53. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  54. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  55. edbInfo = &EdbInfo{
  56. Source: utils.DATA_SOURCE_CALCULATE_ADJUST,
  57. SourceName: "数据调整",
  58. EdbCode: edbCode,
  59. EdbName: utils.TrimStr(req.EdbName),
  60. EdbNameSource: utils.TrimStr(req.EdbName),
  61. Frequency: utils.TrimStr(req.Frequency),
  62. Unit: utils.TrimStr(req.Unit),
  63. ClassifyId: req.ClassifyId,
  64. SysUserId: req.AdminId,
  65. SysUserRealName: req.AdminName,
  66. CreateTime: time.Now(),
  67. ModifyTime: time.Now(),
  68. UniqueCode: uniqueCode,
  69. EdbType: 2,
  70. Sort: GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.EDB_INFO_TYPE),
  71. }
  72. tmpErr := to.Create(edbInfo).Error
  73. if tmpErr != nil {
  74. err = tmpErr
  75. return
  76. }
  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. err = to.Create(calculateMappingItem).Error
  102. if err != nil {
  103. return
  104. }
  105. }
  106. //相关配置
  107. {
  108. var fromEdbEndDate time.Time
  109. if fromEdbInfo.EndDate != `0000-00-00` {
  110. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  111. }
  112. edbAdjustConf := &EdbAdjustConf{
  113. EdbInfoId: edbInfo.EdbInfoId,
  114. SourceEndDate: fromEdbEndDate,
  115. ModifyTime: time.Now(),
  116. CreateTime: time.Now(),
  117. }
  118. //_, err = to.Insert(edbAdjustConf)
  119. err = to.Create(edbAdjustConf).Error
  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. edbInfo.EdbName = utils.TrimStr(req.EdbName)
  131. edbInfo.EdbNameSource = utils.TrimStr(req.EdbName)
  132. edbInfo.Frequency = utils.TrimStr(req.Frequency)
  133. edbInfo.Unit = utils.TrimStr(req.Unit)
  134. edbInfo.ClassifyId = req.ClassifyId
  135. edbInfo.ModifyTime = time.Now()
  136. //_, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime")
  137. err = to.Model(edbInfo).Updates(edbInfo).Error
  138. if err != nil {
  139. return
  140. }
  141. // 指标code
  142. edbCode = edbInfo.EdbCode
  143. // 清除数据
  144. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_ADJUST, edbInfo.SubSource)
  145. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  146. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  147. err = to.Exec(deleteSql, req.EdbInfoId).Error
  148. if err != nil {
  149. return
  150. }
  151. //相关配置
  152. {
  153. // 获取来源指标
  154. calculateMapping, tmpErr := GetEdbInfoCalculateMappingDetail(edbInfo.EdbInfoId)
  155. if tmpErr != nil {
  156. errMsg = `关联指标获取异常`
  157. err = tmpErr
  158. return
  159. }
  160. fromEdbInfo, tmpErr := GetEdbInfoById(calculateMapping.FromEdbInfoId)
  161. if tmpErr != nil {
  162. errMsg = `来源指标不存在`
  163. err = tmpErr
  164. return
  165. }
  166. var fromEdbEndDate time.Time
  167. if fromEdbInfo.EndDate != `0000-00-00` {
  168. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  169. }
  170. // 获取之前的配置
  171. var edbAdjustConf *EdbAdjustConf
  172. sql := ` SELECT * FROM edb_adjust_conf WHERE edb_info_id=? `
  173. err = to.Raw(sql, edbInfo.EdbInfoId).Scan(&edbAdjustConf).Error
  174. if err != nil {
  175. errMsg = "获取指标配置失败"
  176. return
  177. }
  178. // 修改配置的最晚日期
  179. edbAdjustConf.SourceEndDate = fromEdbEndDate
  180. edbAdjustConf.ModifyTime = time.Now()
  181. err = to.Model(edbAdjustConf).Updates(edbAdjustConf).Error
  182. if err != nil {
  183. errMsg = "更新指标配置失败"
  184. return
  185. }
  186. }
  187. }
  188. dataList := req.DataList
  189. dataObj := make(map[string]AddAdjustEdbData)
  190. for _, item := range dataList {
  191. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.Date, time.Local)
  192. if tmpErr != nil {
  193. err = tmpErr
  194. return
  195. }
  196. timestamp := currentDate.UnixNano() / 1e6
  197. timestampStr := fmt.Sprintf("%d", timestamp)
  198. tmpVal, ok := dataObj[item.Date]
  199. if !ok {
  200. tmpVal = AddAdjustEdbData{
  201. Date: item.Date,
  202. TimestampStr: timestampStr,
  203. Value: item.Value,
  204. }
  205. } else {
  206. tmpVal.Value = item.Value
  207. }
  208. dataObj[item.Date] = tmpVal
  209. }
  210. var isAdd bool
  211. dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  212. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  213. for _, item := range dataObj {
  214. //值
  215. val := item.Value
  216. saveVal := utils.SubFloatToString(val, 20)
  217. addSql += GetAddSql(fmt.Sprint(edbInfo.EdbInfoId), edbCode, item.Date, item.TimestampStr, saveVal)
  218. isAdd = true
  219. }
  220. if isAdd {
  221. addSql = strings.TrimRight(addSql, ",")
  222. err = to.Exec(addSql).Error
  223. if err != nil {
  224. return
  225. }
  226. }
  227. return
  228. }
  229. // RefreshAllAdjustEdb 刷新所有 数据调整指标
  230. func RefreshAllAdjustEdb(edbInfo *EdbInfo, fromEdbInfo *EdbInfo) (err error) {
  231. //o := orm.NewOrm()
  232. //to, err := o.Begin()
  233. to := global.DEFAULT_DmSQL
  234. if err != nil {
  235. return
  236. }
  237. defer func() {
  238. if err != nil {
  239. fmt.Println("RefreshAllPythonEdb,Err:" + err.Error())
  240. _ = to.Rollback()
  241. } else {
  242. _ = to.Commit()
  243. }
  244. }()
  245. // 当前数据表名
  246. dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  247. // 获取之前的配置
  248. var edbAdjustConf *EdbAdjustConf
  249. sql := ` SELECT * FROM edb_adjust_conf WHERE edb_info_id=? `
  250. err = to.Raw(sql, edbInfo.EdbInfoId).Scan(&edbAdjustConf).Error
  251. if err != nil {
  252. err = errors.New("获取指标配置失败")
  253. return
  254. }
  255. // 查询关联指标的数据
  256. dataList, err := GetEdbDataListAllByTo(to, fromEdbInfo.Source, fromEdbInfo.SubSource, FindEdbDataListAllCond{
  257. EdbInfoId: fromEdbInfo.EdbInfoId,
  258. StartDataTime: edbAdjustConf.SourceEndDate.Format(utils.FormatDate),
  259. StartDataTimeCond: ">",
  260. }, 1)
  261. if err != nil {
  262. return err
  263. }
  264. // 已经入库的日期数据
  265. existDataMap := make(map[string]float64)
  266. {
  267. existDataList, tmpErr := GetEdbDataListAllByTo(to, edbInfo.Source, edbInfo.SubSource, FindEdbDataListAllCond{
  268. EdbInfoId: edbInfo.EdbInfoId,
  269. StartDataTime: edbAdjustConf.SourceEndDate.Format(utils.FormatDate),
  270. StartDataTimeCond: ">",
  271. }, 1)
  272. if tmpErr != nil {
  273. err = tmpErr
  274. return
  275. }
  276. for _, v := range existDataList {
  277. existDataMap[v.DataTime] = v.Value
  278. }
  279. }
  280. var isAdd bool
  281. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  282. for _, item := range dataList {
  283. currDay := item.DataTime
  284. //值
  285. val := item.Value
  286. saveVal := utils.SubFloatToString(val, 20)
  287. if existVal, ok := existDataMap[currDay]; !ok {
  288. //格式化时间
  289. currentDate, tmpErr := time.Parse(utils.FormatDate, item.DataTime)
  290. if tmpErr != nil {
  291. err = tmpErr
  292. return
  293. }
  294. timestamp := currentDate.UnixNano() / 1e6
  295. timestampStr := fmt.Sprintf("%d", timestamp)
  296. addSql += GetAddSql(fmt.Sprint(edbInfo.EdbInfoId), edbInfo.EdbCode, item.DataTime, timestampStr, saveVal)
  297. isAdd = true
  298. } else {
  299. if existVal != item.Value {
  300. sql = ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  301. sql = fmt.Sprintf(sql, dataTableName)
  302. err = to.Exec(sql, saveVal, edbInfo.EdbInfoId, currDay).Error
  303. if err != nil {
  304. return err
  305. }
  306. }
  307. }
  308. }
  309. if isAdd {
  310. addSql = strings.TrimRight(addSql, ",")
  311. err = to.Exec(addSql).Error
  312. if err != nil {
  313. return
  314. }
  315. // 变更数据调整配置表的实际结束日期
  316. {
  317. var fromEdbEndDate time.Time
  318. if fromEdbInfo.EndDate != `0000-00-00` {
  319. fromEdbEndDate, _ = time.Parse(utils.FormatDate, fromEdbInfo.EndDate)
  320. }
  321. edbAdjustConf.SourceEndDate = fromEdbEndDate
  322. edbAdjustConf.ModifyTime = time.Now()
  323. err = to.Model(edbAdjustConf).Updates(edbAdjustConf).Error
  324. }
  325. }
  326. return
  327. }
  328. // SaveAdjustEdbReq 保存数据调整请求参数(请求指标服务)
  329. type SaveAdjustEdbReq struct {
  330. AdminId int `description:"添加人id"`
  331. AdminName string `description:"添加人名称"`
  332. EdbInfoId int `description:"指标id"`
  333. FromEdbInfoId int `description:"来源指标id"`
  334. EdbName string `description:"指标名称"`
  335. Frequency string `description:"频度"`
  336. Unit string `description:"单位"`
  337. ClassifyId int `description:"分类id"`
  338. DataList []SaveAdjustEdbDataReq `description:"指标对应的数据值"`
  339. }
  340. // SaveAdjustEdbDataReq 保存数据调整请求的数据的参数
  341. type SaveAdjustEdbDataReq struct {
  342. Date string `description:"数据日期"`
  343. Value float64 `description:"数据值"`
  344. }