edb_info_calculate_ljzzy.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // 累计值转月
  12. func AddCalculateLjzzy(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  13. o := orm.NewOrmUsingDB("data")
  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, tmpErr := to.Insert(edbInfo)
  43. if tmpErr != nil {
  44. return int(newEdbInfoId), tmpErr
  45. }
  46. edbInfoId = int(newEdbInfoId)
  47. //calculateItem := new(EdbInfoCalculateLjzzy)
  48. //calculateItem.CreateTime = time.Now()
  49. //calculateItem.ModifyTime = time.Now()
  50. //calculateItem.Sort = 1
  51. //calculateItem.EdbCode = edbCode
  52. //calculateItem.EdbInfoId = edbInfoId
  53. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  54. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  55. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  56. //calculateItem.FromSource = fromEdbInfo.Source
  57. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  58. //_, err = to.Insert(calculateItem)
  59. //if err != nil {
  60. // return edbInfoId, err
  61. //}
  62. //关联关系
  63. {
  64. calculateMappingItem := new(EdbInfoCalculateMapping)
  65. calculateMappingItem.CreateTime = time.Now()
  66. calculateMappingItem.ModifyTime = time.Now()
  67. calculateMappingItem.Sort = 1
  68. calculateMappingItem.EdbCode = edbCode
  69. calculateMappingItem.EdbInfoId = edbInfoId
  70. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  71. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  72. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  73. calculateMappingItem.FromSource = fromEdbInfo.Source
  74. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  75. calculateMappingItem.FromTag = ""
  76. calculateMappingItem.Source = edbInfo.Source
  77. calculateMappingItem.SourceName = edbInfo.SourceName
  78. _, err = to.Insert(calculateMappingItem)
  79. if err != nil {
  80. return
  81. }
  82. }
  83. } else {
  84. edbInfoId = req.EdbInfoId
  85. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_LJZZY)
  86. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  87. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  88. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  89. }
  90. edbInfoIdStr := strconv.Itoa(edbInfoId)
  91. //计算数据
  92. var condition string
  93. var pars []interface{}
  94. condition += " AND edb_info_id=? "
  95. if req.EdbInfoId <= 0 {
  96. pars = append(pars, req.FromEdbInfoId)
  97. } else {
  98. pars = append(pars, fromEdbInfo.EdbInfoId)
  99. }
  100. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  101. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  102. if err != nil {
  103. return edbInfoId, err
  104. }
  105. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  106. dataLen := len(dataList)
  107. for i := 0; i < dataLen; i++ {
  108. item := dataList[i]
  109. //日其中获取年
  110. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  111. if err != nil {
  112. return edbInfoId, err
  113. }
  114. year := itemDate.Year()
  115. month := int(itemDate.Month())
  116. if monthMap, yok := yearMap[year]; yok {
  117. monthMap[month] = item
  118. yearMap[year] = monthMap
  119. } else {
  120. monthMap = make(map[int]*EdbInfoSearchData)
  121. monthMap[month] = item
  122. yearMap[year] = monthMap
  123. }
  124. }
  125. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  126. var isAdd bool
  127. existMap := make(map[string]string)
  128. for yk, yv := range yearMap {
  129. _, oneMonthOk := yv[1]
  130. _, twoMonthOk := yv[2]
  131. if !oneMonthOk && !twoMonthOk {
  132. continue
  133. }
  134. for i := 1; i <= 12; i++ {
  135. fmt.Println(yk, i, yv[i])
  136. dataCurrentItem := yv[i]
  137. fmt.Println("i:", i, yk, dataCurrentItem)
  138. var date string
  139. var val float64
  140. if i == 1 || i == 2 {
  141. if _, mok := yv[1]; mok { //1月有值
  142. if i == 1 {
  143. date = dataCurrentItem.DataTime
  144. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  145. }
  146. if i == 2 {
  147. dataOneItem := yv[1]
  148. if dataCurrentItem != nil {
  149. date = dataCurrentItem.DataTime
  150. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  151. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  152. val, _ = twoMonth.Sub(oneMonth).Float64()
  153. } else {
  154. continue
  155. }
  156. }
  157. } else { //1月无值
  158. dataTwoItem := yv[2]
  159. if i == 1 {
  160. date = strconv.Itoa(yk) + "-01-31"
  161. a := decimal.NewFromFloat(dataTwoItem.Value)
  162. b := decimal.NewFromFloat(2.0)
  163. val, _ = a.Div(b).Float64()
  164. }
  165. if i == 2 {
  166. date = dataCurrentItem.DataTime
  167. a := decimal.NewFromFloat(dataTwoItem.Value)
  168. b := decimal.NewFromFloat(2.0)
  169. val, _ = a.Div(b).Float64()
  170. }
  171. }
  172. } else {
  173. dataPreItem := yv[i-1]
  174. if dataCurrentItem != nil && dataPreItem != nil {
  175. date = dataCurrentItem.DataTime
  176. //val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Sub(decimal.NewFromFloat(dataPreItem.Value)).Float64()
  177. a := decimal.NewFromFloat(dataCurrentItem.Value)
  178. b := decimal.NewFromFloat(dataPreItem.Value)
  179. val, _ = a.Sub(b).Float64()
  180. }
  181. }
  182. if date != "" {
  183. dataTime, _ := time.Parse(utils.FormatDate, date)
  184. timestamp := dataTime.UnixNano() / 1e6
  185. timeStr := fmt.Sprintf("%d", timestamp)
  186. if _, ok := existMap[edbCode+date]; !ok {
  187. addSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, utils.SubFloatToString(val, 4))
  188. isAdd = true
  189. }
  190. existMap[edbCode+date] = date
  191. }
  192. }
  193. }
  194. if isAdd {
  195. addSql = strings.TrimRight(addSql, ",")
  196. _, err = to.Raw(addSql).Exec()
  197. if err != nil {
  198. return edbInfoId, err
  199. }
  200. }
  201. return
  202. }