edb_data_calculate_ljzzy.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/shopspring/decimal"
  6. "hongze/hongze_edb_lib/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // AddCalculateLjzzy 累计值转月
  12. func AddCalculateLjzzy(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  13. o := orm.NewOrm()
  14. to, err := o.Begin()
  15. if err != nil {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. _ = to.Rollback()
  21. } else {
  22. _ = to.Commit()
  23. }
  24. }()
  25. if req.EdbInfoId <= 0 {
  26. edbInfo := new(EdbInfo)
  27. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_LJZZY
  28. edbInfo.SourceName = "累计值转月值"
  29. edbInfo.EdbCode = edbCode
  30. edbInfo.EdbName = req.EdbName
  31. edbInfo.EdbNameSource = req.EdbName
  32. edbInfo.Frequency = req.Frequency
  33. edbInfo.Unit = req.Unit
  34. edbInfo.ClassifyId = req.ClassifyId
  35. edbInfo.SysUserId = sysUserId
  36. edbInfo.SysUserRealName = sysUserRealName
  37. edbInfo.CreateTime = time.Now()
  38. edbInfo.ModifyTime = time.Now()
  39. edbInfo.UniqueCode = uniqueCode
  40. edbInfo.CalculateFormula = req.Formula
  41. edbInfo.EdbType = 2
  42. newEdbInfoId, err := to.Insert(edbInfo)
  43. if err != nil {
  44. return int(newEdbInfoId), err
  45. }
  46. 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 = 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. edbInfoId = req.EdbInfoId
  70. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_LJZZY)
  71. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  72. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  73. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  74. if err != nil {
  75. return
  76. }
  77. }
  78. edbInfoIdStr := strconv.Itoa(edbInfoId)
  79. //计算数据
  80. var condition string
  81. var pars []interface{}
  82. condition += " AND edb_info_id=? "
  83. if req.EdbInfoId <= 0 {
  84. pars = append(pars, req.FromEdbInfoId)
  85. } else {
  86. pars = append(pars, fromEdbInfo.EdbInfoId)
  87. }
  88. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  89. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  90. if err != nil {
  91. return edbInfoId, err
  92. }
  93. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  94. dataLen := len(dataList)
  95. for i := 0; i < dataLen; i++ {
  96. item := dataList[i]
  97. //日其中获取年
  98. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  99. if err != nil {
  100. return edbInfoId, err
  101. }
  102. year := itemDate.Year()
  103. month := int(itemDate.Month())
  104. if monthMap, yok := yearMap[year]; yok {
  105. monthMap[month] = item
  106. yearMap[year] = monthMap
  107. } else {
  108. monthMap = make(map[int]*EdbInfoSearchData)
  109. monthMap[month] = item
  110. yearMap[year] = monthMap
  111. }
  112. }
  113. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  114. var isAdd bool
  115. existMap := make(map[string]string)
  116. for yk, yv := range yearMap {
  117. _, oneMonthOk := yv[1]
  118. _, twoMonthOk := yv[2]
  119. if !oneMonthOk && !twoMonthOk {
  120. continue
  121. }
  122. for i := 1; i <= 12; i++ {
  123. fmt.Println(yk, i, yv[i])
  124. dataCurrentItem := yv[i]
  125. fmt.Println("i:", i, yk, dataCurrentItem)
  126. var date string
  127. var val float64
  128. if i == 1 || i == 2 {
  129. if _, mok := yv[1]; mok { //1月有值
  130. if i == 1 {
  131. date = dataCurrentItem.DataTime
  132. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  133. }
  134. if i == 2 {
  135. dataOneItem := yv[1]
  136. if dataCurrentItem != nil {
  137. date = dataCurrentItem.DataTime
  138. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  139. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  140. val, _ = twoMonth.Sub(oneMonth).Float64()
  141. } else {
  142. continue
  143. }
  144. }
  145. } else { //1月无值
  146. dataTwoItem := yv[2]
  147. if i == 1 {
  148. date = strconv.Itoa(yk) + "-01-31"
  149. a := decimal.NewFromFloat(dataTwoItem.Value)
  150. b := decimal.NewFromFloat(2.0)
  151. val, _ = a.Div(b).Float64()
  152. }
  153. if i == 2 {
  154. date = dataCurrentItem.DataTime
  155. a := decimal.NewFromFloat(dataTwoItem.Value)
  156. b := decimal.NewFromFloat(2.0)
  157. val, _ = a.Div(b).Float64()
  158. }
  159. }
  160. } else {
  161. dataPreItem := yv[i-1]
  162. if dataCurrentItem != nil && dataPreItem != nil {
  163. date = dataCurrentItem.DataTime
  164. //val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Sub(decimal.NewFromFloat(dataPreItem.Value)).Float64()
  165. a := decimal.NewFromFloat(dataCurrentItem.Value)
  166. b := decimal.NewFromFloat(dataPreItem.Value)
  167. val, _ = a.Sub(b).Float64()
  168. }
  169. }
  170. if date != "" {
  171. dataTime, _ := time.Parse(utils.FormatDate, date)
  172. timestamp := dataTime.UnixNano() / 1e6
  173. timeStr := fmt.Sprintf("%d", timestamp)
  174. if _, ok := existMap[edbCode+date]; !ok {
  175. addSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, utils.SubFloatToString(val, 4))
  176. isAdd = true
  177. }
  178. existMap[edbCode+date] = date
  179. }
  180. }
  181. }
  182. if isAdd {
  183. addSql = strings.TrimRight(addSql, ",")
  184. _, err = to.Raw(addSql).Exec()
  185. if err != nil {
  186. return edbInfoId, err
  187. }
  188. }
  189. return
  190. }