edb_data_calculate_nszydbpjjs.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "hongze/hongze_edb_lib/utils"
  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. for _, v := range existDataList {
  200. existDataMap[v.DataTime] = v
  201. }
  202. //计算来源数据
  203. var fromCondition string
  204. var fromPars []interface{}
  205. fromCondition += " AND edb_info_id=? "
  206. fromPars = append(fromPars, fromEdbInfo.EdbInfoId)
  207. if startDate != `` {
  208. fromCondition += " AND data_time>=? "
  209. fromPars = append(fromPars, startDate)
  210. }
  211. //fmt.Println("fromPars:", fromPars)
  212. fromDataList, err := GetEdbDataListAllV1ByTo(to, fromCondition, fromPars, fromEdbInfo.Source, 0)
  213. if err != nil {
  214. fmt.Println("from GetEdbDataListAll Err:" + err.Error())
  215. return err
  216. }
  217. var fromDateArr []string
  218. fromDataMap := make(map[string]*EdbInfoSearchDataV1)
  219. for _, v := range fromDataList {
  220. fromDateArr = append(fromDateArr, v.DataTime)
  221. fromDataMap[v.DataTime] = v
  222. }
  223. addSql := ` INSERT INTO edb_data_calculate_nszydpjjs(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  224. var isAdd bool
  225. dataTableName := GetEdbDataTableName(source)
  226. arrLen := len(fromDateArr)
  227. existAddDataMap := make(map[string]string)
  228. for ak, av := range fromDateArr {
  229. //处理第一个值
  230. var valArr []string
  231. if findItem, ok := fromDataMap[av]; ok {
  232. valArr = append(valArr, findItem.Value)
  233. } else {
  234. continue
  235. }
  236. if ak+1 != arrLen {
  237. //处理除第一个值之外的N-1个值
  238. for i := 1; i < formulaInt; i++ {
  239. arrIndex := ak + i
  240. if arrIndex >= arrLen {
  241. break
  242. }
  243. arrVal := fromDateArr[arrIndex]
  244. if findItem, ok := fromDataMap[arrVal]; ok {
  245. valArr = append(valArr, findItem.Value)
  246. } else {
  247. continue
  248. }
  249. }
  250. }
  251. valArrLen := len(valArr)
  252. //var totalVal float64
  253. totalVal := decimal.NewFromFloat(0.00)
  254. for _, v := range valArr {
  255. newDecimal, _ := decimal.NewFromString(v)
  256. totalVal = totalVal.Add(newDecimal)
  257. }
  258. af := totalVal //decimal.NewFromFloat(totalVal)
  259. bf := decimal.NewFromFloat(float64(valArrLen))
  260. val, _ := af.Div(bf).Float64()
  261. valStr := decimal.NewFromFloat(val).RoundCeil(4).String()
  262. if existVal, existOk := existDataMap[av]; !existOk {
  263. currentDate, err := time.ParseInLocation(utils.FormatDate, av, time.Local)
  264. if err != nil {
  265. return err
  266. }
  267. timestamp := currentDate.UnixNano() / 1e6
  268. timestampStr := fmt.Sprintf("%d", timestamp)
  269. if _, existOk := existAddDataMap[av]; !existOk {
  270. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, valStr)
  271. isAdd = true
  272. }
  273. existAddDataMap[av] = av
  274. } else {
  275. existValDecimal, err := decimal.NewFromString(existVal.Value)
  276. existStr := existValDecimal.String()
  277. fmt.Println(existStr, valStr, av)
  278. if existStr != valStr {
  279. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  280. sql = fmt.Sprintf(sql, dataTableName)
  281. _, err = to.Raw(sql, valStr, edbInfoId, av).Exec()
  282. if err != nil {
  283. return err
  284. }
  285. }
  286. }
  287. }
  288. if isAdd {
  289. addSql = strings.TrimRight(addSql, ",")
  290. utils.FileLog.Info("addSql:" + addSql)
  291. _, err = to.Raw(addSql).Exec()
  292. if err != nil {
  293. return err
  294. }
  295. }
  296. return
  297. }