edb_info_calculate_tcz.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. "hongze/hongze_task/utils"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type EdbInfoCalculateTcz struct {
  12. EdbInfoCalculateTbzId int `orm:"column(edb_info_calculate_tcz_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 TczSub(a, b float64) string {
  26. af := decimal.NewFromFloat(float64(a))
  27. fmt.Println(af)
  28. bf := decimal.NewFromFloat(float64(b))
  29. val, _ := af.Sub(bf).Float64()
  30. valStr := utils.SubFloatToString(val, 4)
  31. return valStr
  32. }
  33. //刷新同差值数据
  34. func RefreshCalculateTcz(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. fmt.Println(edbInfoId, fromEdbInfo.EdbInfoId)
  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_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  76. var isAdd bool
  77. for _, av := range dateArr {
  78. currentItem := dataMap[av]
  79. if currentItem != nil {
  80. //当前日期
  81. currentDate, err := time.Parse(utils.FormatDate, av)
  82. if err != nil {
  83. return err
  84. }
  85. //上一年的日期
  86. preDate := currentDate.AddDate(-1, 0, 0)
  87. preDateStr := preDate.Format(utils.FormatDate)
  88. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  89. //dataTime, _ := time.Parse(utils.FormatDate, date)
  90. timestamp := currentDate.UnixNano() / 1e6
  91. timestampStr := fmt.Sprintf("%d", timestamp)
  92. val := TczSub(currentItem.Value, findItem.Value)
  93. count, err := GetEdbDataCalculateTczByCodeAndDate(edbCode, av)
  94. if err != nil && err.Error() != utils.ErrNoRow() {
  95. return err
  96. }
  97. if count <= 0 {
  98. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  99. isAdd = true
  100. } else {
  101. err = ModifyEdbDataCalculateTcz(int64(edbInfoId), av, val)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  107. continue
  108. } else {
  109. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  110. nextDateDay := preDate.AddDate(0, 1, 0)
  111. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  112. preDateDay := preDate.AddDate(0, -1, 0)
  113. preDateDayStr := preDateDay.Format(utils.FormatDate)
  114. for i := 0; i <= 6; i++ {
  115. if i >= 1 {
  116. nextDateDay = nextDateDay.AddDate(0, 0, i)
  117. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  118. }
  119. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  120. timestamp := currentDate.UnixNano() / 1e6
  121. timestampStr := fmt.Sprintf("%d", timestamp)
  122. val := TczSub(currentItem.Value, findItem.Value)
  123. count, err := GetEdbDataCalculateTczByCodeAndDate(edbCode, av)
  124. if err != nil && err.Error() != utils.ErrNoRow() {
  125. return err
  126. }
  127. if count <= 0 {
  128. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  129. isAdd = true
  130. } else {
  131. err = ModifyEdbDataCalculateTcz(int64(edbInfoId), av, val)
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. break
  137. } else {
  138. if i >= 1 {
  139. preDateDay = preDate.AddDate(0, 0, -i)
  140. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  141. }
  142. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  143. timestamp := currentDate.UnixNano() / 1e6
  144. timestampStr := fmt.Sprintf("%d", timestamp)
  145. val := TczSub(currentItem.Value, findItem.Value)
  146. count, err := GetEdbDataCalculateTczByCodeAndDate(edbCode, av)
  147. if err != nil && err.Error() != utils.ErrNoRow() {
  148. return err
  149. }
  150. if count <= 0 {
  151. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  152. isAdd = true
  153. } else {
  154. err = ModifyEdbDataCalculateTcz(int64(edbInfoId), av, val)
  155. if err != nil {
  156. return err
  157. }
  158. }
  159. break
  160. }
  161. }
  162. }
  163. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  164. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  165. timestamp := currentDate.UnixNano() / 1e6
  166. timestampStr := fmt.Sprintf("%d", timestamp)
  167. val := TczSub(currentItem.Value, findItem.Value)
  168. count, err := GetEdbDataCalculateTczByCodeAndDate(edbCode, av)
  169. if err != nil && err.Error() != utils.ErrNoRow() {
  170. return err
  171. }
  172. if count <= 0 {
  173. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  174. isAdd = true
  175. } else {
  176. err = ModifyEdbDataCalculateTcz(int64(edbInfoId), av, val)
  177. if err != nil {
  178. return err
  179. }
  180. }
  181. break
  182. }
  183. } else {
  184. nextDateDay := preDate
  185. preDateDay := preDate
  186. for i := 0; i < 35; i++ {
  187. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  188. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  189. timestamp := currentDate.UnixNano() / 1e6
  190. timestampStr := fmt.Sprintf("%d", timestamp)
  191. val := TczSub(currentItem.Value, findItem.Value)
  192. count, err := GetEdbDataCalculateTczByCodeAndDate(edbCode, av)
  193. if err != nil && err.Error() != utils.ErrNoRow() {
  194. return err
  195. }
  196. if count <= 0 {
  197. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  198. isAdd = true
  199. } else {
  200. err = ModifyEdbDataCalculateTcz(int64(edbInfoId), av, val)
  201. if err != nil {
  202. return err
  203. }
  204. }
  205. break
  206. } else {
  207. preDateDayStr := preDateDay.Format(utils.FormatDate)
  208. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  209. timestamp := currentDate.UnixNano() / 1e6
  210. timestampStr := fmt.Sprintf("%d", timestamp)
  211. val := TczSub(currentItem.Value, findItem.Value)
  212. count, err := GetEdbDataCalculateTczByCodeAndDate(edbCode, av)
  213. if err != nil && err.Error() != utils.ErrNoRow() {
  214. return err
  215. }
  216. if count <= 0 {
  217. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  218. isAdd = true
  219. } else {
  220. err = ModifyEdbDataCalculateTcz(int64(edbInfoId), av, val)
  221. if err != nil {
  222. return err
  223. }
  224. }
  225. break
  226. }
  227. }
  228. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  229. preDateDay = preDateDay.AddDate(0, 0, -1)
  230. }
  231. }
  232. }
  233. }
  234. }
  235. fmt.Println("isAdd:", isAdd)
  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 EdbInfoCalculateTczDetail struct {
  246. EdbInfoCalculateTczId int `orm:"column(edb_info_calculate_tcz_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 GetEdbInfoCalculateTczDetail(edbInfoId int) (item *EdbInfoCalculateTczDetail, 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 GetEdbDataCalculateTczByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  270. o := orm.NewOrmUsingDB("data")
  271. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_tcz WHERE edb_code=? AND data_time=? `
  272. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  273. return
  274. }
  275. func ModifyEdbDataCalculateTcz(edbInfoId int64, dataTime, value string) (err error) {
  276. o := orm.NewOrmUsingDB("data")
  277. sql := ` UPDATE edb_data_calculate_tcz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  278. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  279. return
  280. }