edb_data_calculate_nszydbpjjs.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // AddCalculateNszydpjjs N数值移动平均计算
  13. func AddCalculateNszydpjjs(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string, formulaInt int) (edbInfo *EdbInfo, err error) {
  14. o := orm.NewOrm()
  15. to, err := o.Begin()
  16. defer func() {
  17. if err != nil {
  18. fmt.Println("AddCalculateNszydpjjs,Err:" + err.Error())
  19. _ = to.Rollback()
  20. } else {
  21. _ = to.Commit()
  22. }
  23. }()
  24. if req.EdbInfoId <= 0 {
  25. edbInfo = new(EdbInfo)
  26. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_NSZYDPJJS
  27. edbInfo.SourceName = "N数值移动平均计算"
  28. edbInfo.EdbCode = edbCode
  29. edbInfo.EdbName = req.EdbName
  30. edbInfo.EdbNameSource = req.EdbName
  31. edbInfo.Frequency = req.Frequency
  32. edbInfo.Unit = req.Unit
  33. edbInfo.ClassifyId = req.ClassifyId
  34. edbInfo.SysUserId = sysUserId
  35. edbInfo.SysUserRealName = sysUserRealName
  36. edbInfo.CreateTime = time.Now()
  37. edbInfo.ModifyTime = time.Now()
  38. edbInfo.UniqueCode = uniqueCode
  39. edbInfo.CalculateFormula = req.Formula
  40. edbInfo.EdbType = 2
  41. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  42. if tmpErr != nil {
  43. err = tmpErr
  44. return
  45. }
  46. edbInfo.EdbInfoId = int(newEdbInfoId)
  47. //关联关系
  48. {
  49. calculateMappingItem := new(EdbInfoCalculateMapping)
  50. calculateMappingItem.CreateTime = time.Now()
  51. calculateMappingItem.ModifyTime = time.Now()
  52. calculateMappingItem.Sort = 1
  53. calculateMappingItem.EdbCode = edbCode
  54. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  55. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  56. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  57. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  58. calculateMappingItem.FromSource = fromEdbInfo.Source
  59. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  60. calculateMappingItem.FromTag = ""
  61. calculateMappingItem.Source = edbInfo.Source
  62. calculateMappingItem.SourceName = edbInfo.SourceName
  63. _, err = to.Insert(calculateMappingItem)
  64. if err != nil {
  65. return
  66. }
  67. }
  68. } else {
  69. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  70. if err != nil {
  71. return
  72. }
  73. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_NSZYDPJJS)
  74. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  75. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  76. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  77. if err != nil {
  78. return
  79. }
  80. }
  81. //计算数据
  82. err = refreshAllCalculateNszydpjjs(to, edbInfo.EdbInfoId, edbInfo.Source, formulaInt, fromEdbInfo, edbInfo.EdbCode, "")
  83. return
  84. }
  85. // EditCalculateNszydpjjs 修改N数值移动平均计算
  86. func EditCalculateNszydpjjs(edbInfo *EdbInfo, req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo, formulaInt int, oldCalculateFormula string) (err error) {
  87. o := orm.NewOrm()
  88. to, err := o.Begin()
  89. defer func() {
  90. if err != nil {
  91. fmt.Println("EditCalculateNszydpjjs,Err:" + err.Error())
  92. _ = to.Rollback()
  93. } else {
  94. _ = to.Commit()
  95. }
  96. }()
  97. //修改指标信息
  98. edbInfo.EdbName = req.EdbName
  99. edbInfo.EdbNameSource = req.EdbName
  100. edbInfo.Frequency = req.Frequency
  101. edbInfo.Unit = req.Unit
  102. edbInfo.ClassifyId = req.ClassifyId
  103. edbInfo.CalculateFormula = req.Formula
  104. edbInfo.ModifyTime = time.Now()
  105. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
  106. if err != nil {
  107. return
  108. }
  109. var existCondition string
  110. var existPars []interface{}
  111. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  112. existPars = append(existPars, edbInfo.EdbInfoId, req.FromEdbInfoId)
  113. //判断计算指标是否被更换
  114. count, err := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  115. if err != nil && err.Error() != utils.ErrNoRow() {
  116. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  117. return
  118. }
  119. fmt.Println("oldCalculateFormula:", oldCalculateFormula)
  120. fmt.Println("req.Formula:", req.Formula)
  121. fmt.Println("count:", count)
  122. if count > 0 && oldCalculateFormula == req.Formula { // 指标未被替换,同时N值未修改,无需重新计算
  123. return
  124. }
  125. if count <= 0 {
  126. // 需要删除原先的 计算指标关联的,基础指标的关联关系
  127. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  128. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  129. if err != nil {
  130. return
  131. }
  132. // 添加新的关联关系
  133. {
  134. calculateMappingItem := &EdbInfoCalculateMapping{
  135. EdbInfoCalculateMappingId: 0,
  136. EdbInfoId: edbInfo.EdbInfoId,
  137. Source: utils.DATA_SOURCE_CALCULATE_NSZYDPJJS,
  138. SourceName: "N数值移动平均计算",
  139. EdbCode: edbInfo.EdbCode,
  140. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  141. FromEdbCode: fromEdbInfo.EdbCode,
  142. FromEdbName: fromEdbInfo.EdbName,
  143. FromSource: fromEdbInfo.Source,
  144. FromSourceName: fromEdbInfo.SourceName,
  145. FromTag: "",
  146. Sort: 1,
  147. CreateTime: time.Now(),
  148. ModifyTime: time.Now(),
  149. }
  150. _, err = to.Insert(calculateMappingItem)
  151. if err != nil {
  152. return
  153. }
  154. }
  155. }
  156. //清空原有数据
  157. sql := ` DELETE FROM edb_data_calculate_nszydpjjs WHERE edb_info_id = ? `
  158. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  159. if err != nil {
  160. return
  161. }
  162. //计算数据
  163. err = refreshAllCalculateNszydpjjs(to, edbInfo.EdbInfoId, edbInfo.Source, formulaInt, fromEdbInfo, edbInfo.EdbCode, "")
  164. return
  165. }
  166. // RefreshAllCalculateNszydpjjs 刷新全部N数值移动平均计算
  167. func RefreshAllCalculateNszydpjjs(edbInfoId, source, formulaInt int, fromEdbInfo *EdbInfo, edbCode, startDate string) (err error) {
  168. o := orm.NewOrm()
  169. to, err := o.Begin()
  170. defer func() {
  171. if err != nil {
  172. fmt.Println("RefreshCalculateNszydpjjs Err:" + err.Error())
  173. utils.FileLog.Info("RefreshCalculateNszydpjjs Err:" + err.Error())
  174. _ = to.Rollback()
  175. } else {
  176. _ = to.Commit()
  177. }
  178. }()
  179. //计算数据
  180. err = refreshAllCalculateNszydpjjs(to, edbInfoId, source, formulaInt, fromEdbInfo, edbCode, startDate)
  181. return
  182. }
  183. // refreshAllCalculateNszydpjjs 更新全部N数值移动平均计算
  184. func refreshAllCalculateNszydpjjs(to orm.TxOrmer, edbInfoId, source, formulaInt int, fromEdbInfo *EdbInfo, edbCode, startDate string) (err error) {
  185. edbInfoIdStr := strconv.Itoa(edbInfoId)
  186. fmt.Println(edbInfoIdStr)
  187. fmt.Println("refreshAllCalculateNszydpjjs startDate:", startDate)
  188. //计算数据
  189. var condition string
  190. var pars []interface{}
  191. condition += " AND edb_info_id=? "
  192. pars = append(pars, edbInfoId)
  193. existDataList, err := GetEdbDataListAllV1ByTo(to, condition, pars, source, 0)
  194. if err != nil {
  195. fmt.Println("existDataList GetEdbDataListAll Err:" + err.Error())
  196. return err
  197. }
  198. existDataMap := make(map[string]*EdbInfoSearchDataV1)
  199. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  200. for _, v := range existDataList {
  201. existDataMap[v.DataTime] = v
  202. removeDataTimeMap[v.DataTime] = 1
  203. }
  204. //计算来源数据
  205. var fromCondition string
  206. var fromPars []interface{}
  207. fromCondition += " AND edb_info_id=? "
  208. fromPars = append(fromPars, fromEdbInfo.EdbInfoId)
  209. //if startDate != `` {
  210. // fromCondition += " AND data_time>=? "
  211. // fromPars = append(fromPars, startDate)
  212. //}
  213. //fmt.Println("fromPars:", fromPars)
  214. fromDataList, err := GetEdbDataListAllV1ByTo(to, fromCondition, fromPars, fromEdbInfo.Source, 0)
  215. if err != nil {
  216. fmt.Println("from GetEdbDataListAll Err:" + err.Error())
  217. return err
  218. }
  219. var fromDateArr []string
  220. fromDataMap := make(map[string]*EdbInfoSearchDataV1)
  221. for _, v := range fromDataList {
  222. fromDateArr = append(fromDateArr, v.DataTime)
  223. fromDataMap[v.DataTime] = v
  224. }
  225. addSql := ` INSERT INTO edb_data_calculate_nszydpjjs(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  226. var isAdd bool
  227. dataTableName := GetEdbDataTableName(source)
  228. arrLen := len(fromDateArr)
  229. existAddDataMap := make(map[string]string)
  230. for ak, av := range fromDateArr {
  231. //处理第一个值
  232. var valArr []string
  233. if findItem, ok := fromDataMap[av]; ok {
  234. valArr = append(valArr, findItem.Value)
  235. } else {
  236. continue
  237. }
  238. if ak+1 != arrLen {
  239. //处理除第一个值之外的N-1个值
  240. for i := 1; i < formulaInt; i++ {
  241. arrIndex := ak + i
  242. if arrIndex >= arrLen {
  243. break
  244. }
  245. arrVal := fromDateArr[arrIndex]
  246. if findItem, ok := fromDataMap[arrVal]; ok {
  247. valArr = append(valArr, findItem.Value)
  248. } else {
  249. continue
  250. }
  251. }
  252. }
  253. valArrLen := len(valArr)
  254. //var totalVal float64
  255. totalVal := decimal.NewFromFloat(0.00)
  256. for _, v := range valArr {
  257. newDecimal, _ := decimal.NewFromString(v)
  258. totalVal = totalVal.Add(newDecimal)
  259. }
  260. af := totalVal //decimal.NewFromFloat(totalVal)
  261. bf := decimal.NewFromFloat(float64(valArrLen))
  262. val, _ := af.Div(bf).Float64()
  263. valStr := decimal.NewFromFloat(val).RoundCeil(4).String()
  264. if existVal, existOk := existDataMap[av]; !existOk {
  265. currentDate, err := time.ParseInLocation(utils.FormatDate, av, time.Local)
  266. if err != nil {
  267. return err
  268. }
  269. timestamp := currentDate.UnixNano() / 1e6
  270. timestampStr := fmt.Sprintf("%d", timestamp)
  271. if _, existOk := existAddDataMap[av]; !existOk {
  272. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, valStr)
  273. isAdd = true
  274. }
  275. existAddDataMap[av] = av
  276. } else {
  277. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
  278. delete(removeDataTimeMap, av)
  279. existValDecimal, err := decimal.NewFromString(existVal.Value)
  280. existStr := existValDecimal.String()
  281. fmt.Println(existStr, valStr, av)
  282. if existStr != valStr {
  283. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  284. sql = fmt.Sprintf(sql, dataTableName)
  285. _, err = to.Raw(sql, valStr, edbInfoId, av).Exec()
  286. if err != nil {
  287. return err
  288. }
  289. }
  290. }
  291. }
  292. //删除已经不存在的指标数据(由于该指标当日的数据删除了)
  293. {
  294. removeDateList := make([]string, 0)
  295. for dateTime := range removeDataTimeMap {
  296. removeDateList = append(removeDateList, dateTime)
  297. }
  298. removeNum := len(removeDateList)
  299. if removeNum > 0 {
  300. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
  301. _, err = to.Raw(sql, edbInfoId, removeDateList).Exec()
  302. if err != nil {
  303. err = fmt.Errorf("删除不存在的指标数据失败,Err:" + err.Error())
  304. return
  305. }
  306. }
  307. }
  308. if isAdd {
  309. addSql = strings.TrimRight(addSql, ",")
  310. utils.FileLog.Info("addSql:" + addSql)
  311. _, err = to.Raw(addSql).Exec()
  312. if err != nil {
  313. return err
  314. }
  315. }
  316. return
  317. }