edb_info_calculate_tbz.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/shopspring/decimal"
  6. "hongze/hongze_task/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type EdbInfoCalculateTbz struct {
  12. EdbInfoCalculateTbzId int `orm:"column(edb_info_calculate_tbz_id);pk"`
  13. EdbInfoId int `description:"指标id"`
  14. EdbCode string `description:"指标编码"`
  15. FromEdbInfoId int `description:"计算指标id"`
  16. FromEdbCode string `description:"计算指标编码"`
  17. FromEdbName string `description:"计算指标名称"`
  18. FromSource int `description:"计算指标来源"`
  19. FromSourceName string `description:"计算指标来源名称"`
  20. FromTag string `description:"来源指标标签"`
  21. Sort int `description:"计算指标名称排序"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. func TbzDiv(a, b float64) string {
  26. af := decimal.NewFromFloat(float64(a))
  27. bf := decimal.NewFromFloat(float64(b))
  28. val, _ := af.Div(bf).Float64()
  29. val = val - 1
  30. valStr := utils.SubFloatToString(val, 4)
  31. return valStr
  32. }
  33. //刷新同比值数据
  34. func RefreshCalculateTbz(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. tx,err:=o.Begin()
  37. if err!=nil {
  38. return err
  39. }
  40. defer func() {
  41. if err != nil {
  42. tx.Rollback()
  43. } else {
  44. tx.Commit()
  45. }
  46. }()
  47. if err != nil {
  48. return
  49. }
  50. edbInfoIdStr := strconv.Itoa(edbInfoId)
  51. //计算数据
  52. //计算数据
  53. var condition string
  54. var pars []interface{}
  55. condition += " AND edb_info_id=? "
  56. pars = append(pars, fromEdbInfo.EdbInfoId)
  57. if startDate != "" {
  58. condition += " AND data_time>=? "
  59. pars = append(pars, startDate)
  60. }
  61. if endDate != "" {
  62. condition += " AND data_time<=? "
  63. pars = append(pars, endDate)
  64. }
  65. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  66. if err != nil {
  67. return err
  68. }
  69. var dateArr []string
  70. dataMap := make(map[string]*EdbInfoSearchData)
  71. for _, v := range dataList {
  72. dateArr = append(dateArr, v.DataTime)
  73. dataMap[v.DataTime] = v
  74. }
  75. addSql := ` INSERT INTO edb_data_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  76. var isAdd bool
  77. for ak, av := range dateArr {
  78. fmt.Println(ak, av)
  79. currentItem := dataMap[av]
  80. if currentItem != nil {
  81. //当前日期
  82. currentDate, err := time.Parse(utils.FormatDate, av)
  83. if err != nil {
  84. return err
  85. }
  86. //上一年的日期
  87. preDate := currentDate.AddDate(-1, 0, 0)
  88. preDateStr := preDate.Format(utils.FormatDate)
  89. if findItem, ok := dataMap[preDateStr]; ok && findItem.Value != 0 { //上一年同期找到
  90. //dataTime, _ := time.Parse(utils.FormatDate, date)
  91. timestamp := currentDate.UnixNano() / 1e6
  92. timestampStr := fmt.Sprintf("%d", timestamp)
  93. val := TbzDiv(currentItem.Value, findItem.Value)
  94. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  95. if err != nil && err.Error() != utils.ErrNoRow() {
  96. return err
  97. }
  98. if count <= 0 {
  99. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  100. isAdd = true
  101. } else {
  102. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  108. continue
  109. } else {
  110. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  111. nextDateDay := preDate.AddDate(0, 1, 0)
  112. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  113. preDateDay := preDate.AddDate(0, -1, 0)
  114. preDateDayStr := preDateDay.Format(utils.FormatDate)
  115. for i := 0; i <= 6; i++ {
  116. if i >= 1 {
  117. nextDateDay = nextDateDay.AddDate(0, 0, i)
  118. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  119. }
  120. if findItem, ok := dataMap[nextDateDayStr]; ok && findItem.Value != 0 { //上一年同期->下一个月找到
  121. timestamp := currentDate.UnixNano() / 1e6
  122. timestampStr := fmt.Sprintf("%d", timestamp)
  123. val := TbzDiv(currentItem.Value, findItem.Value)
  124. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  125. if err != nil && err.Error() != utils.ErrNoRow() {
  126. return err
  127. }
  128. if count <= 0 {
  129. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  130. isAdd = true
  131. } else {
  132. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  133. if err != nil {
  134. return err
  135. }
  136. }
  137. break
  138. } else {
  139. if i >= 1 {
  140. preDateDay = preDate.AddDate(0, 0, -i)
  141. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  142. }
  143. if findItem, ok := dataMap[preDateDayStr]; ok && findItem.Value != 0 { //上一年同期->上一个月找到
  144. timestamp := currentDate.UnixNano() / 1e6
  145. timestampStr := fmt.Sprintf("%d", timestamp)
  146. val := TbzDiv(currentItem.Value, findItem.Value)
  147. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  148. if err != nil && err.Error() != utils.ErrNoRow() {
  149. return err
  150. }
  151. if count <= 0 {
  152. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  153. isAdd = true
  154. } else {
  155. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  156. if err != nil {
  157. return err
  158. }
  159. }
  160. break
  161. }
  162. }
  163. }
  164. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  165. if findItem, ok := dataMap[preDateStr]; ok && findItem.Value != 0 { //上一年同期->下一个月找到
  166. timestamp := currentDate.UnixNano() / 1e6
  167. timestampStr := fmt.Sprintf("%d", timestamp)
  168. val := TbzDiv(currentItem.Value, findItem.Value)
  169. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  170. if err != nil && err.Error() != utils.ErrNoRow() {
  171. return err
  172. }
  173. if count <= 0 {
  174. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  175. isAdd = true
  176. } else {
  177. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  178. if err != nil {
  179. return err
  180. }
  181. }
  182. break
  183. }
  184. } else {
  185. nextDateDay := preDate
  186. preDateDay := preDate
  187. for i := 0; i < 35; i++ {
  188. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  189. if findItem, ok := dataMap[nextDateDayStr]; ok && findItem.Value != 0 { //上一年同期->下一个月找到
  190. timestamp := currentDate.UnixNano() / 1e6
  191. timestampStr := fmt.Sprintf("%d", timestamp)
  192. val := TbzDiv(currentItem.Value, findItem.Value)
  193. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  194. if err != nil && err.Error() != utils.ErrNoRow() {
  195. return err
  196. }
  197. if count <= 0 {
  198. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  199. isAdd = true
  200. } else {
  201. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. break
  207. } else {
  208. preDateDayStr := preDateDay.Format(utils.FormatDate)
  209. if findItem, ok := dataMap[preDateDayStr]; ok && findItem.Value != 0 { //上一年同期->上一个月找到
  210. timestamp := currentDate.UnixNano() / 1e6
  211. timestampStr := fmt.Sprintf("%d", timestamp)
  212. val := TbzDiv(currentItem.Value, findItem.Value)
  213. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  214. if err != nil && err.Error() != utils.ErrNoRow() {
  215. return err
  216. }
  217. if count <= 0 {
  218. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  219. isAdd = true
  220. } else {
  221. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  222. if err != nil {
  223. return err
  224. }
  225. }
  226. break
  227. }
  228. }
  229. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  230. preDateDay = preDateDay.AddDate(0, 0, -1)
  231. }
  232. }
  233. }
  234. }
  235. }
  236. if isAdd {
  237. addSql = strings.TrimRight(addSql, ",")
  238. _, err = tx.Raw(addSql).Exec()
  239. if err != nil {
  240. return err
  241. }
  242. }
  243. return
  244. }
  245. type EdbInfoCalculateTbzDetail struct {
  246. EdbInfoCalculateTbzId int `orm:"column(edb_info_calculate_tbz_id);pk"`
  247. EdbInfoId int `description:"指标id"`
  248. EdbCode string `description:"指标编码"`
  249. FromEdbInfoId int `description:"计算指标id"`
  250. FromEdbCode string `description:"计算指标编码"`
  251. FromEdbName string `description:"计算指标名称"`
  252. FromSource int `description:"计算指标来源"`
  253. FromSourceName string `description:"计算指标来源名称"`
  254. FromTag string `description:"来源指标标签"`
  255. Sort int `description:"计算指标名称排序"`
  256. CreateTime time.Time `description:"创建时间"`
  257. ModifyTime time.Time `description:"修改时间"`
  258. StartDate string `description:"开始日期"`
  259. EndDate string `description:"结束日期"`
  260. }
  261. func GetEdbInfoCalculateTbzDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  262. o := orm.NewOrmUsingDB("data")
  263. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  264. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  265. WHERE a.edb_info_id=? `
  266. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  267. return
  268. }
  269. func GetEdbDataCalculateTbzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  270. o := orm.NewOrmUsingDB("data")
  271. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_tbz WHERE edb_code=? AND data_time=? `
  272. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  273. return
  274. }
  275. func ModifyEdbDataCalculateTbz(edbInfoId int64, dataTime, value string) (err error) {
  276. o := orm.NewOrmUsingDB("data")
  277. sql := ` UPDATE edb_data_calculate_tbz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  278. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  279. return
  280. }