edb_info_calculate_tbz.go 11 KB

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