edb_info_calculate_tcz.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 AddCalculateTcz(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. fmt.Println("err:", err.Error())
  21. _ = to.Rollback()
  22. } else {
  23. _ = to.Commit()
  24. }
  25. }()
  26. fmt.Println("req.EdbInfoId:", req.EdbInfoId)
  27. if req.EdbInfoId <= 0 {
  28. edbInfo := new(EdbInfo)
  29. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_TCZ
  30. edbInfo.SourceName = "同差值"
  31. edbInfo.EdbCode = edbCode
  32. edbInfo.EdbName = req.EdbName
  33. edbInfo.EdbNameSource = req.EdbName
  34. edbInfo.Frequency = req.Frequency
  35. edbInfo.Unit = req.Unit
  36. edbInfo.ClassifyId = req.ClassifyId
  37. edbInfo.SysUserId = sysUserId
  38. edbInfo.SysUserRealName = sysUserRealName
  39. edbInfo.CreateTime = time.Now()
  40. edbInfo.ModifyTime = time.Now()
  41. edbInfo.UniqueCode = uniqueCode
  42. edbInfo.CalculateFormula = req.Formula
  43. edbInfo.EdbType = 2
  44. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  45. if tmpErr != nil {
  46. return edbInfoId, tmpErr
  47. }
  48. edbInfoId = int(newEdbInfoId)
  49. //calculateItem := new(EdbInfoCalculateTcz)
  50. //calculateItem.CreateTime = time.Now()
  51. //calculateItem.ModifyTime = time.Now()
  52. //calculateItem.Sort = 1
  53. //calculateItem.EdbCode = edbCode
  54. //calculateItem.EdbInfoId = edbInfoId
  55. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  56. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  57. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  58. //calculateItem.FromSource = fromEdbInfo.Source
  59. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  60. //
  61. //_, err = to.Insert(calculateItem)
  62. //if err != nil {
  63. // return edbInfoId, err
  64. //}
  65. //关联关系
  66. {
  67. calculateMappingItem := new(EdbInfoCalculateMapping)
  68. calculateMappingItem.CreateTime = time.Now()
  69. calculateMappingItem.ModifyTime = time.Now()
  70. calculateMappingItem.Sort = 1
  71. calculateMappingItem.EdbCode = edbCode
  72. calculateMappingItem.EdbInfoId = edbInfoId
  73. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  74. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  75. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  76. calculateMappingItem.FromSource = fromEdbInfo.Source
  77. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  78. calculateMappingItem.FromTag = ""
  79. calculateMappingItem.Source = edbInfo.Source
  80. calculateMappingItem.SourceName = edbInfo.SourceName
  81. _, err = to.Insert(calculateMappingItem)
  82. if err != nil {
  83. return
  84. }
  85. }
  86. } else {
  87. edbInfoId = req.EdbInfoId
  88. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SUB_SOURCE_EDB)
  89. fmt.Println("dataTableName:", dataTableName)
  90. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  91. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  92. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  93. if err != nil {
  94. return edbInfoId, err
  95. }
  96. }
  97. edbInfoIdStr := strconv.Itoa(edbInfoId)
  98. //计算数据
  99. var condition string
  100. var pars []interface{}
  101. condition += " AND edb_info_id=? "
  102. if req.EdbInfoId <= 0 {
  103. pars = append(pars, req.FromEdbInfoId)
  104. } else {
  105. pars = append(pars, fromEdbInfo.EdbInfoId)
  106. }
  107. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  108. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, fromEdbInfo.SubSource, 0)
  109. if err != nil {
  110. return edbInfoId, err
  111. }
  112. var dateArr []string
  113. dataMap := make(map[string]*EdbInfoSearchData)
  114. for _, v := range dataList {
  115. dateArr = append(dateArr, v.DataTime)
  116. dataMap[v.DataTime] = v
  117. }
  118. fmt.Println("Frequency:", fromEdbInfo.Frequency)
  119. addSql := ` INSERT INTO edb_data_calculate_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  120. var isAdd bool
  121. existMap := make(map[string]string)
  122. for _, av := range dateArr {
  123. //fmt.Println("dateArr:", ak, av)
  124. currentItem := dataMap[av]
  125. if currentItem != nil {
  126. //当前日期
  127. currentDate, err := time.Parse(utils.FormatDate, av)
  128. if err != nil {
  129. return edbInfoId, err
  130. }
  131. //上一年的日期
  132. preDate := currentDate.AddDate(-1, 0, 0)
  133. preDateStr := preDate.Format(utils.FormatDate)
  134. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  135. //dataTime, _ := time.Parse(utils.FormatDate, date)
  136. if _, ok := existMap[edbCode+av]; !ok {
  137. timestamp := currentDate.UnixNano() / 1e6
  138. timestampStr := fmt.Sprintf("%d", timestamp)
  139. val := TczSub(currentItem.Value, findItem.Value)
  140. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  141. isAdd = true
  142. }
  143. existMap[edbCode+av] = av
  144. } else {
  145. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  146. nextDateDay := preDate
  147. preDateDay := preDate
  148. for i := 0; i <= 35; i++ {
  149. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  150. if findItem, ok := dataMap[nextDateDayStr]; ok { //下一年同期->下一个月找到
  151. if _, ok := existMap[edbCode+av]; !ok {
  152. timestamp := currentDate.UnixNano() / 1e6
  153. timestampStr := fmt.Sprintf("%d", timestamp)
  154. val := TczSub(currentItem.Value, findItem.Value)
  155. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  156. isAdd = true
  157. }
  158. existMap[edbCode+av] = av
  159. break
  160. } else {
  161. preDateDayStr := preDateDay.Format(utils.FormatDate)
  162. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  163. if _, ok := existMap[edbCode+av]; !ok {
  164. timestamp := currentDate.UnixNano() / 1e6
  165. timestampStr := fmt.Sprintf("%d", timestamp)
  166. val := TczSub(currentItem.Value, findItem.Value)
  167. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  168. isAdd = true
  169. }
  170. existMap[edbCode+av] = av
  171. break
  172. }
  173. }
  174. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  175. preDateDay = preDateDay.AddDate(0, 0, -1)
  176. }
  177. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  178. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  179. if _, ok := existMap[edbCode+av]; !ok {
  180. timestamp := currentDate.UnixNano() / 1e6
  181. timestampStr := fmt.Sprintf("%d", timestamp)
  182. val := TczSub(currentItem.Value, findItem.Value)
  183. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  184. isAdd = true
  185. }
  186. existMap[edbCode+av] = av
  187. break
  188. }
  189. } else {
  190. nextDateDay := preDate
  191. preDateDay := preDate
  192. for i := 0; i < 35; i++ {
  193. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  194. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  195. if _, ok := existMap[edbCode+av]; !ok {
  196. timestamp := currentDate.UnixNano() / 1e6
  197. timestampStr := fmt.Sprintf("%d", timestamp)
  198. val := TczSub(currentItem.Value, findItem.Value)
  199. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  200. isAdd = true
  201. }
  202. existMap[edbCode+av] = av
  203. break
  204. } else {
  205. preDateDayStr := preDateDay.Format(utils.FormatDate)
  206. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  207. if _, ok := existMap[edbCode+av]; !ok {
  208. timestamp := currentDate.UnixNano() / 1e6
  209. timestampStr := fmt.Sprintf("%d", timestamp)
  210. val := TczSub(currentItem.Value, findItem.Value)
  211. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  212. isAdd = true
  213. }
  214. existMap[edbCode+av] = av
  215. break
  216. } else {
  217. }
  218. }
  219. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  220. preDateDay = preDateDay.AddDate(0, 0, -1)
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if isAdd {
  227. addSql = strings.TrimRight(addSql, ",")
  228. _, err = to.Raw(addSql).Exec()
  229. if err != nil {
  230. return edbInfoId, err
  231. }
  232. }
  233. return
  234. }
  235. func TczSub(a, b float64) string {
  236. af := decimal.NewFromFloat(float64(a))
  237. fmt.Println(af)
  238. bf := decimal.NewFromFloat(float64(b))
  239. val, _ := af.Sub(bf).Float64()
  240. valStr := utils.SubFloatToString(val, 4)
  241. return valStr
  242. }