edb_info_calculate_tbz.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "hongze/hz_crm_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfoCalculateTbz struct {
  13. EdbInfoCalculateTbzId int `orm:"column(edb_info_calculate_tbz_id);pk"`
  14. EdbInfoId int `description:"指标id"`
  15. EdbCode string `description:"指标编码"`
  16. FromEdbInfoId int `description:"计算指标id"`
  17. FromEdbCode string `description:"计算指标编码"`
  18. FromEdbName string `description:"计算指标名称"`
  19. FromSource int `description:"计算指标来源"`
  20. FromSourceName string `description:"计算指标来源名称"`
  21. FromTag string `description:"来源指标标签"`
  22. Sort int `description:"计算指标名称排序"`
  23. CreateTime time.Time `description:"创建时间"`
  24. ModifyTime time.Time `description:"修改时间"`
  25. }
  26. // 同比值
  27. func AddCalculateTbz(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  28. fmt.Println("AddCalculateTbz")
  29. o := orm.NewOrmUsingDB("data")
  30. to, err := o.Begin()
  31. if err != nil {
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. _ = to.Rollback()
  37. } else {
  38. _ = to.Commit()
  39. }
  40. }()
  41. if req.EdbInfoId <= 0 {
  42. edbInfo := new(EdbInfo)
  43. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_TBZ
  44. edbInfo.SourceName = "同比值"
  45. edbInfo.EdbCode = edbCode
  46. edbInfo.EdbName = req.EdbName
  47. edbInfo.EdbNameSource = req.EdbName
  48. edbInfo.Frequency = req.Frequency
  49. edbInfo.Unit = req.Unit
  50. edbInfo.ClassifyId = req.ClassifyId
  51. edbInfo.SysUserId = sysUserId
  52. edbInfo.SysUserRealName = sysUserRealName
  53. edbInfo.CreateTime = time.Now()
  54. edbInfo.ModifyTime = time.Now()
  55. edbInfo.UniqueCode = uniqueCode
  56. edbInfo.CalculateFormula = req.Formula
  57. edbInfo.EdbType = 2
  58. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  59. if tmpErr != nil {
  60. return edbInfoId, tmpErr
  61. }
  62. edbInfoId = int(newEdbInfoId)
  63. //calculateItem := new(EdbInfoCalculateTbz)
  64. //calculateItem.CreateTime = time.Now()
  65. //calculateItem.ModifyTime = time.Now()
  66. //calculateItem.Sort = 1
  67. //calculateItem.EdbCode = edbCode
  68. //calculateItem.EdbInfoId = edbInfoId
  69. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  70. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  71. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  72. //calculateItem.FromSource = fromEdbInfo.Source
  73. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  74. //
  75. //_, err = to.Insert(calculateItem)
  76. //if err != nil {
  77. // return edbInfoId, err
  78. //}
  79. //关联关系
  80. {
  81. calculateMappingItem := new(EdbInfoCalculateMapping)
  82. calculateMappingItem.CreateTime = time.Now()
  83. calculateMappingItem.ModifyTime = time.Now()
  84. calculateMappingItem.Sort = 1
  85. calculateMappingItem.EdbCode = edbCode
  86. calculateMappingItem.EdbInfoId = edbInfoId
  87. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  88. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  89. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  90. calculateMappingItem.FromSource = fromEdbInfo.Source
  91. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  92. calculateMappingItem.FromTag = ""
  93. calculateMappingItem.Source = edbInfo.Source
  94. calculateMappingItem.SourceName = edbInfo.SourceName
  95. _, err = to.Insert(calculateMappingItem)
  96. if err != nil {
  97. return
  98. }
  99. }
  100. } else {
  101. edbInfoId = req.EdbInfoId
  102. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_TBZ)
  103. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  104. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  105. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  106. }
  107. edbInfoIdStr := strconv.Itoa(edbInfoId)
  108. //计算数据
  109. var condition string
  110. var pars []interface{}
  111. condition += " AND edb_info_id=? "
  112. if req.EdbInfoId <= 0 {
  113. pars = append(pars, req.FromEdbInfoId)
  114. } else {
  115. pars = append(pars, fromEdbInfo.EdbInfoId)
  116. }
  117. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  118. if err != nil {
  119. return edbInfoId, err
  120. }
  121. var dateArr []string
  122. dataMap := make(map[string]*EdbInfoSearchData)
  123. for _, v := range dataList {
  124. dateArr = append(dateArr, v.DataTime)
  125. dataMap[v.DataTime] = v
  126. }
  127. addSql := ` INSERT INTO edb_data_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  128. var isAdd bool
  129. existMap := make(map[string]string)
  130. for _, av := range dateArr {
  131. //fmt.Println(ak, av)
  132. currentItem := dataMap[av]
  133. if currentItem != nil {
  134. //当前日期
  135. currentDate, err := time.Parse(utils.FormatDate, av)
  136. if err != nil {
  137. return edbInfoId, err
  138. }
  139. //上一年的日期
  140. preDate := currentDate.AddDate(-1, 0, 0)
  141. preDateStr := preDate.Format(utils.FormatDate)
  142. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  143. //dataTime, _ := time.Parse(utils.FormatDate, date)
  144. if _, ok := existMap[edbCode+av]; !ok {
  145. timestamp := currentDate.UnixNano() / 1e6
  146. timestampStr := fmt.Sprintf("%d", timestamp)
  147. val := TbzDiv(currentItem.Value, findItem.Value)
  148. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  149. isAdd = true
  150. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  151. }
  152. existMap[edbCode+av] = av
  153. } else {
  154. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  155. nextDateDay := preDate
  156. preDateDay := preDate
  157. for i := 0; i <= 35; i++ {
  158. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  159. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  160. if _, ok := existMap[edbCode+av]; !ok {
  161. timestamp := currentDate.UnixNano() / 1e6
  162. timestampStr := fmt.Sprintf("%d", timestamp)
  163. val := TbzDiv(currentItem.Value, findItem.Value)
  164. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  165. isAdd = true
  166. }
  167. existMap[edbCode+av] = av
  168. break
  169. } else {
  170. preDateDayStr := preDateDay.Format(utils.FormatDate)
  171. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  172. if _, ok := existMap[edbCode+av]; !ok {
  173. timestamp := currentDate.UnixNano() / 1e6
  174. timestampStr := fmt.Sprintf("%d", timestamp)
  175. val := TbzDiv(currentItem.Value, findItem.Value)
  176. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  177. isAdd = true
  178. }
  179. existMap[edbCode+av] = av
  180. break
  181. }
  182. }
  183. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  184. preDateDay = preDateDay.AddDate(0, 0, -1)
  185. }
  186. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  187. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  188. if _, ok := existMap[edbCode+av]; !ok {
  189. timestamp := currentDate.UnixNano() / 1e6
  190. timestampStr := fmt.Sprintf("%d", timestamp)
  191. val := TbzDiv(currentItem.Value, findItem.Value)
  192. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  193. isAdd = true
  194. }
  195. existMap[edbCode+av] = av
  196. break
  197. }
  198. } else {
  199. nextDateDay := preDate
  200. preDateDay := preDate
  201. for i := 0; i < 35; i++ {
  202. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  203. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  204. if _, ok := existMap[edbCode+av]; !ok {
  205. timestamp := currentDate.UnixNano() / 1e6
  206. timestampStr := fmt.Sprintf("%d", timestamp)
  207. val := TbzDiv(currentItem.Value, findItem.Value)
  208. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  209. isAdd = true
  210. }
  211. existMap[edbCode+av] = av
  212. break
  213. } else {
  214. preDateDayStr := preDateDay.Format(utils.FormatDate)
  215. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  216. if _, ok := existMap[edbCode+av]; !ok {
  217. timestamp := currentDate.UnixNano() / 1e6
  218. timestampStr := fmt.Sprintf("%d", timestamp)
  219. val := TbzDiv(currentItem.Value, findItem.Value)
  220. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  221. isAdd = true
  222. }
  223. existMap[edbCode+av] = av
  224. break
  225. } else {
  226. //fmt.Println("pre not find:", preDateStr, "i:", i)
  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 = to.Raw(addSql).Exec()
  239. if err != nil {
  240. return edbInfoId, err
  241. }
  242. }
  243. return
  244. }
  245. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  246. nowStr := time.Now().Format(utils.FormatDateTime)
  247. addSql += "("
  248. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  249. "," + "'" + nowStr + "'" + "," + "1"
  250. addSql += "," + "'" + timestampStr + "'"
  251. addSql += "),"
  252. return
  253. }
  254. func TbzDiv(a, b float64) string {
  255. var valStr string
  256. if b != 0 {
  257. af := decimal.NewFromFloat(float64(a))
  258. bf := decimal.NewFromFloat(float64(b))
  259. val, _ := af.Div(bf).Float64()
  260. val = val - 1
  261. valStr = utils.SubFloatToString(val, 4)
  262. } else {
  263. valStr = "0"
  264. }
  265. return valStr
  266. }
  267. // 同比值
  268. func EditCalculateTbz(req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo, edbCode string) (edbInfoId int, err error) {
  269. edbInfoId = req.EdbInfoId
  270. o := orm.NewOrmUsingDB("data")
  271. to, err := o.Begin()
  272. if err != nil {
  273. return
  274. }
  275. defer func() {
  276. if err != nil {
  277. _ = to.Rollback()
  278. } else {
  279. _ = to.Commit()
  280. }
  281. }()
  282. //修改指标信息
  283. sql := ` UPDATE edb_info
  284. SET
  285. edb_name =?,
  286. edb_name_source=?,
  287. frequency = ?,
  288. unit = ?,
  289. classify_id = ?,
  290. modify_time = NOW()
  291. WHERE edb_info_id = ? `
  292. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, edbInfoId).Exec()
  293. if err != nil {
  294. return
  295. }
  296. var existCondition string
  297. var existPars []interface{}
  298. existCondition += " AND edb_info_id=? "
  299. existPars = append(existPars, edbInfoId)
  300. existCondition += " AND from_edb_info_id=? "
  301. existPars = append(existPars, req.FromEdbInfoId)
  302. //判断计算指标是否被更换
  303. count, tmpErr := GetEdbInfoCalculateCountByCondition(req.Source, existCondition, existPars)
  304. if tmpErr != nil {
  305. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  306. return
  307. }
  308. if count <= 0 {
  309. //删除指标关联计算指标
  310. //sql := ` DELETE FROM edb_info_calculate_tbz WHERE edb_info_id = ? `
  311. //_, err = to.Raw(sql, edbInfoId).Exec()
  312. //if err != nil {
  313. // return
  314. //}
  315. //
  316. //calculateItem := new(EdbInfoCalculateTbz)
  317. //calculateItem.CreateTime = time.Now()
  318. //calculateItem.ModifyTime = time.Now()
  319. //calculateItem.Sort = 1
  320. //calculateItem.EdbCode = edbCode
  321. //calculateItem.EdbInfoId = edbInfoId
  322. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  323. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  324. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  325. //calculateItem.FromSource = fromEdbInfo.Source
  326. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  327. //
  328. //_, err = to.Insert(calculateItem)
  329. //if err != nil {
  330. // return
  331. //}
  332. //删除,计算指标关联的,基础指标的关联关系
  333. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  334. _, err = to.Raw(sql, edbInfoId).Exec()
  335. if err != nil {
  336. return edbInfoId, err
  337. }
  338. //清空原有数据
  339. sql = ` DELETE FROM edb_data_calculate_tbz WHERE edb_info_id = ? `
  340. _, err = to.Raw(sql, edbInfoId).Exec()
  341. if err != nil {
  342. return edbInfoId, err
  343. }
  344. //关联关系
  345. {
  346. calculateMappingItem := new(EdbInfoCalculateMapping)
  347. calculateMappingItem.CreateTime = time.Now()
  348. calculateMappingItem.ModifyTime = time.Now()
  349. calculateMappingItem.Sort = 1
  350. calculateMappingItem.EdbCode = edbCode
  351. calculateMappingItem.EdbInfoId = edbInfoId
  352. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  353. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  354. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  355. calculateMappingItem.FromSource = fromEdbInfo.Source
  356. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  357. calculateMappingItem.FromTag = ""
  358. calculateMappingItem.Source = utils.DATA_SOURCE_CALCULATE_TBZ
  359. calculateMappingItem.SourceName = "同比值"
  360. _, err = to.Insert(calculateMappingItem)
  361. if err != nil {
  362. return
  363. }
  364. }
  365. edbInfoIdStr := strconv.Itoa(edbInfoId)
  366. //计算数据
  367. var condition string
  368. var pars []interface{}
  369. condition += " AND edb_info_id=? "
  370. pars = append(pars, req.FromEdbInfoId)
  371. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  372. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  373. if err != nil {
  374. return edbInfoId, err
  375. }
  376. var dateArr []string
  377. dataMap := make(map[string]*EdbInfoSearchData)
  378. for _, v := range dataList {
  379. dateArr = append(dateArr, v.DataTime)
  380. dataMap[v.DataTime] = v
  381. }
  382. addSql := ` INSERT INTO edb_data_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  383. var isAdd bool
  384. for _, av := range dateArr {
  385. currentItem := dataMap[av]
  386. if currentItem != nil {
  387. //当前日期
  388. currentDate, err := time.Parse(utils.FormatDate, av)
  389. if err != nil {
  390. return edbInfoId, err
  391. }
  392. //上一年的日期
  393. preDate := currentDate.AddDate(-1, 0, 0)
  394. preDateStr := preDate.Format(utils.FormatDate)
  395. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  396. //dataTime, _ := time.Parse(utils.FormatDate, date)
  397. timestamp := currentDate.UnixNano() / 1e6
  398. timestampStr := fmt.Sprintf("%d", timestamp)
  399. val := TbzDiv(currentItem.Value, findItem.Value)
  400. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  401. isAdd = true
  402. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  403. continue
  404. } else {
  405. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  406. nextDateDay := preDate.AddDate(0, 1, 0)
  407. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  408. preDateDay := preDate.AddDate(0, -1, 0)
  409. preDateDayStr := preDateDay.Format(utils.FormatDate)
  410. for i := 0; i <= 6; i++ {
  411. if i >= 1 {
  412. nextDateDay = nextDateDay.AddDate(0, 0, i)
  413. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  414. }
  415. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  416. timestamp := currentDate.UnixNano() / 1e6
  417. timestampStr := fmt.Sprintf("%d", timestamp)
  418. val := TbzDiv(currentItem.Value, findItem.Value)
  419. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  420. isAdd = true
  421. break
  422. } else {
  423. if i >= 1 {
  424. preDateDay = preDate.AddDate(0, 0, -i)
  425. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  426. }
  427. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  428. timestamp := currentDate.UnixNano() / 1e6
  429. timestampStr := fmt.Sprintf("%d", timestamp)
  430. val := TbzDiv(currentItem.Value, findItem.Value)
  431. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  432. isAdd = true
  433. break
  434. }
  435. }
  436. }
  437. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  438. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  439. timestamp := currentDate.UnixNano() / 1e6
  440. timestampStr := fmt.Sprintf("%d", timestamp)
  441. val := TbzDiv(currentItem.Value, findItem.Value)
  442. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  443. isAdd = true
  444. break
  445. }
  446. } else {
  447. nextDateDay := preDate.AddDate(0, 0, 1)
  448. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  449. preDateDay := preDate.AddDate(0, 0, -1)
  450. preDateDayStr := preDateDay.Format(utils.FormatDate)
  451. for i := 0; i < 35; i++ {
  452. if i >= 1 {
  453. nextDateDay = nextDateDay.AddDate(0, 0, i)
  454. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  455. }
  456. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  457. timestamp := currentDate.UnixNano() / 1e6
  458. timestampStr := fmt.Sprintf("%d", timestamp)
  459. val := TbzDiv(currentItem.Value, findItem.Value)
  460. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  461. isAdd = true
  462. break
  463. } else {
  464. if i >= 1 {
  465. preDateDay = preDate.AddDate(0, 0, -i)
  466. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  467. }
  468. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  469. timestamp := currentDate.UnixNano() / 1e6
  470. timestampStr := fmt.Sprintf("%d", timestamp)
  471. val := TbzDiv(currentItem.Value, findItem.Value)
  472. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  473. isAdd = true
  474. break
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. if isAdd {
  483. addSql = strings.TrimRight(addSql, ",")
  484. _, err = to.Raw(addSql).Exec()
  485. if err != nil {
  486. return edbInfoId, err
  487. }
  488. }
  489. }
  490. return
  491. }
  492. // 刷新同比值数据
  493. func RefreshCalculateTbz(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  494. o := orm.NewOrmUsingDB("data")
  495. to, err := o.Begin()
  496. if err != nil {
  497. return
  498. }
  499. defer func() {
  500. if err != nil {
  501. _ = to.Rollback()
  502. } else {
  503. _ = to.Commit()
  504. }
  505. }()
  506. if err != nil {
  507. return
  508. }
  509. edbInfoIdStr := strconv.Itoa(edbInfoId)
  510. //计算数据
  511. //计算数据
  512. var condition string
  513. var pars []interface{}
  514. condition += " AND edb_info_id=? "
  515. pars = append(pars, fromEdbInfo.EdbInfoId)
  516. if startDate != "" {
  517. condition += " AND data_time>=? "
  518. pars = append(pars, startDate)
  519. }
  520. if endDate != "" {
  521. condition += " AND data_time<=? "
  522. pars = append(pars, endDate)
  523. }
  524. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  525. if err != nil {
  526. return err
  527. }
  528. var dateArr []string
  529. dataMap := make(map[string]*EdbInfoSearchData)
  530. for _, v := range dataList {
  531. dateArr = append(dateArr, v.DataTime)
  532. dataMap[v.DataTime] = v
  533. }
  534. addSql := ` INSERT INTO edb_data_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  535. var isAdd bool
  536. for _, av := range dateArr {
  537. currentItem := dataMap[av]
  538. if currentItem != nil {
  539. //当前日期
  540. currentDate, err := time.Parse(utils.FormatDate, av)
  541. if err != nil {
  542. return err
  543. }
  544. //上一年的日期
  545. preDate := currentDate.AddDate(-1, 0, 0)
  546. preDateStr := preDate.Format(utils.FormatDate)
  547. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  548. //dataTime, _ := time.Parse(utils.FormatDate, date)
  549. timestamp := currentDate.UnixNano() / 1e6
  550. timestampStr := fmt.Sprintf("%d", timestamp)
  551. val := TbzDiv(currentItem.Value, findItem.Value)
  552. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  553. if err != nil && err.Error() != utils.ErrNoRow() {
  554. return err
  555. }
  556. if count <= 0 {
  557. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  558. isAdd = true
  559. } else {
  560. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  561. if err != nil {
  562. return err
  563. }
  564. }
  565. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  566. continue
  567. } else {
  568. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  569. for i := 0; i <= 35; i++ {
  570. nextDateDay := preDate.AddDate(0, 0, i)
  571. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  572. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  573. timestamp := currentDate.UnixNano() / 1e6
  574. timestampStr := fmt.Sprintf("%d", timestamp)
  575. fmt.Println(currentItem.Value, findItem.Value)
  576. val := TbzDiv(currentItem.Value, findItem.Value)
  577. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  578. if err != nil && err.Error() != utils.ErrNoRow() {
  579. return err
  580. }
  581. if count <= 0 {
  582. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  583. isAdd = true
  584. } else {
  585. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  586. if err != nil {
  587. return err
  588. }
  589. }
  590. break
  591. } else {
  592. preDateDay := preDate.AddDate(0, 0, -i)
  593. preDateDayStr := preDateDay.Format(utils.FormatDate)
  594. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  595. timestamp := currentDate.UnixNano() / 1e6
  596. timestampStr := fmt.Sprintf("%d", timestamp)
  597. val := TbzDiv(currentItem.Value, findItem.Value)
  598. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  599. if err != nil && err.Error() != utils.ErrNoRow() {
  600. return err
  601. }
  602. if count <= 0 {
  603. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  604. isAdd = true
  605. } else {
  606. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  607. if err != nil {
  608. return err
  609. }
  610. }
  611. break
  612. }
  613. }
  614. }
  615. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  616. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  617. timestamp := currentDate.UnixNano() / 1e6
  618. timestampStr := fmt.Sprintf("%d", timestamp)
  619. val := TbzDiv(currentItem.Value, findItem.Value)
  620. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  621. if err != nil && err.Error() != utils.ErrNoRow() {
  622. return err
  623. }
  624. if count <= 0 {
  625. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  626. isAdd = true
  627. } else {
  628. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  629. if err != nil {
  630. return err
  631. }
  632. }
  633. break
  634. }
  635. } else {
  636. nextDateDay := preDate.AddDate(0, 0, 1)
  637. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  638. preDateDay := preDate.AddDate(0, 0, -1)
  639. preDateDayStr := preDateDay.Format(utils.FormatDate)
  640. for i := 0; i < 35; i++ {
  641. if i >= 1 {
  642. nextDateDay = nextDateDay.AddDate(0, 0, i)
  643. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  644. }
  645. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  646. timestamp := currentDate.UnixNano() / 1e6
  647. timestampStr := fmt.Sprintf("%d", timestamp)
  648. val := TbzDiv(currentItem.Value, findItem.Value)
  649. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  650. if err != nil && err.Error() != utils.ErrNoRow() {
  651. return err
  652. }
  653. if count <= 0 {
  654. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  655. isAdd = true
  656. } else {
  657. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  658. if err != nil {
  659. return err
  660. }
  661. }
  662. break
  663. } else {
  664. if i >= 1 {
  665. preDateDay = preDate.AddDate(0, 0, -i)
  666. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  667. }
  668. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  669. timestamp := currentDate.UnixNano() / 1e6
  670. timestampStr := fmt.Sprintf("%d", timestamp)
  671. val := TbzDiv(currentItem.Value, findItem.Value)
  672. count, err := GetEdbDataCalculateTbzByCodeAndDate(edbCode, av)
  673. if err != nil && err.Error() != utils.ErrNoRow() {
  674. return err
  675. }
  676. if count <= 0 {
  677. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  678. isAdd = true
  679. } else {
  680. err = ModifyEdbDataCalculateTbz(int64(edbInfoId), av, val)
  681. if err != nil {
  682. return err
  683. }
  684. }
  685. break
  686. }
  687. }
  688. }
  689. }
  690. }
  691. }
  692. }
  693. if isAdd {
  694. addSql = strings.TrimRight(addSql, ",")
  695. _, err = to.Raw(addSql).Exec()
  696. if err != nil {
  697. return err
  698. }
  699. }
  700. return
  701. }
  702. type EdbInfoCalculateTbzDetail struct {
  703. EdbInfoCalculateTbzId int `orm:"column(edb_info_calculate_tbz_id);pk"`
  704. EdbInfoId int `description:"指标id"`
  705. EdbCode string `description:"指标编码"`
  706. FromEdbInfoId int `description:"计算指标id"`
  707. FromEdbCode string `description:"计算指标编码"`
  708. FromEdbName string `description:"计算指标名称"`
  709. FromSource int `description:"计算指标来源"`
  710. FromSourceName string `description:"计算指标来源名称"`
  711. FromTag string `description:"来源指标标签"`
  712. Sort int `description:"计算指标名称排序"`
  713. CreateTime time.Time `description:"创建时间"`
  714. ModifyTime time.Time `description:"修改时间"`
  715. StartDate string `description:"开始日期"`
  716. EndDate string `description:"结束日期"`
  717. }
  718. func GetEdbInfoCalculateTbzDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  719. o := orm.NewOrmUsingDB("data")
  720. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  721. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  722. WHERE a.edb_info_id=? `
  723. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  724. return
  725. }
  726. func GetEdbDataCalculateTbzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  727. o := orm.NewOrmUsingDB("data")
  728. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_tbz WHERE edb_code=? AND data_time=? `
  729. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  730. return
  731. }
  732. func ModifyEdbDataCalculateTbz(edbInfoId int64, dataTime, value string) (err error) {
  733. o := orm.NewOrmUsingDB("data")
  734. sql := ` UPDATE edb_data_calculate_tbz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  735. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  736. return
  737. }
  738. // 刷新所有同比值数据
  739. func RefreshAllCalculateTbz(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  740. o := orm.NewOrmUsingDB("data")
  741. to, err := o.Begin()
  742. if err != nil {
  743. return
  744. }
  745. defer func() {
  746. if err != nil {
  747. _ = to.Rollback()
  748. } else {
  749. _ = to.Commit()
  750. }
  751. }()
  752. if err != nil {
  753. return
  754. }
  755. edbInfoIdStr := strconv.Itoa(edbInfoId)
  756. //计算数据
  757. //计算数据
  758. var condition string
  759. var pars []interface{}
  760. condition += " AND edb_info_id=? "
  761. pars = append(pars, fromEdbInfo.EdbInfoId)
  762. if startDate != "" {
  763. condition += " AND data_time>=? "
  764. pars = append(pars, startDate)
  765. }
  766. if endDate != "" {
  767. condition += " AND data_time<=? "
  768. pars = append(pars, endDate)
  769. }
  770. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  771. if err != nil {
  772. return err
  773. }
  774. var dateArr []string
  775. dataMap := make(map[string]*EdbInfoSearchData)
  776. for _, v := range dataList {
  777. dateArr = append(dateArr, v.DataTime)
  778. dataMap[v.DataTime] = v
  779. }
  780. fmt.Println("source:", source)
  781. //获取指标所有数据
  782. existDataList := make([]*EdbDataBase, 0)
  783. dataTableName := GetEdbDataTableName(source)
  784. fmt.Println("dataTableName:", dataTableName)
  785. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  786. sql = fmt.Sprintf(sql, dataTableName)
  787. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  788. if err != nil {
  789. return err
  790. }
  791. existDataMap := make(map[string]string)
  792. for _, v := range existDataList {
  793. existDataMap[v.DataTime] = v.Value
  794. }
  795. fmt.Println("existDataMap:", existDataMap)
  796. addSql := ` INSERT INTO edb_data_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  797. var isAdd bool
  798. existAddDataMap := make(map[string]string)
  799. for _, av := range dateArr {
  800. currentItem := dataMap[av]
  801. if currentItem != nil {
  802. //当前日期
  803. currentDate, err := time.Parse(utils.FormatDate, av)
  804. if err != nil {
  805. return err
  806. }
  807. //上一年的日期
  808. preDate := currentDate.AddDate(-1, 0, 0)
  809. preDateStr := preDate.Format(utils.FormatDate)
  810. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  811. //dataTime, _ := time.Parse(utils.FormatDate, date)
  812. timestamp := currentDate.UnixNano() / 1e6
  813. timestampStr := fmt.Sprintf("%d", timestamp)
  814. val := TbzDiv(currentItem.Value, findItem.Value)
  815. if existVal, ok := existDataMap[av]; !ok {
  816. if _, existOk := existAddDataMap[av]; !existOk {
  817. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  818. isAdd = true
  819. }
  820. existAddDataMap[av] = av
  821. } else {
  822. if existVal != val {
  823. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  824. sql = fmt.Sprintf(sql, dataTableName)
  825. _, err = to.Raw(sql, val, edbInfoId, av).Exec()
  826. if err != nil {
  827. return err
  828. }
  829. }
  830. }
  831. continue
  832. } else {
  833. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  834. for i := 0; i <= 35; i++ {
  835. nextDateDay := preDate.AddDate(0, 0, i)
  836. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  837. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  838. timestamp := currentDate.UnixNano() / 1e6
  839. timestampStr := fmt.Sprintf("%d", timestamp)
  840. val := TbzDiv(currentItem.Value, findItem.Value)
  841. if existVal, ok := existDataMap[av]; !ok {
  842. if _, existOk := existAddDataMap[av]; !existOk {
  843. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  844. isAdd = true
  845. }
  846. existAddDataMap[av] = av
  847. } else {
  848. if existVal != val {
  849. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  850. sql = fmt.Sprintf(sql, dataTableName)
  851. _, err = to.Raw(sql, val, edbInfoId, av).Exec()
  852. if err != nil {
  853. return err
  854. }
  855. }
  856. }
  857. break
  858. } else {
  859. preDateDay := preDate.AddDate(0, 0, -i)
  860. preDateDayStr := preDateDay.Format(utils.FormatDate)
  861. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  862. timestamp := currentDate.UnixNano() / 1e6
  863. timestampStr := fmt.Sprintf("%d", timestamp)
  864. val := TbzDiv(currentItem.Value, findItem.Value)
  865. if existVal, ok := existDataMap[av]; !ok {
  866. if _, existOk := existAddDataMap[av]; !existOk {
  867. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  868. isAdd = true
  869. }
  870. existAddDataMap[av] = av
  871. } else {
  872. if existVal != val {
  873. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  874. sql = fmt.Sprintf(sql, dataTableName)
  875. _, err = to.Raw(sql, val, edbInfoId, av).Exec()
  876. if err != nil {
  877. return err
  878. }
  879. }
  880. }
  881. break
  882. }
  883. }
  884. }
  885. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  886. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  887. timestamp := currentDate.UnixNano() / 1e6
  888. timestampStr := fmt.Sprintf("%d", timestamp)
  889. val := TbzDiv(currentItem.Value, findItem.Value)
  890. if existVal, ok := existDataMap[av]; !ok {
  891. if _, existOk := existAddDataMap[av]; !existOk {
  892. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  893. isAdd = true
  894. }
  895. existAddDataMap[av] = av
  896. } else {
  897. if existVal != val {
  898. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  899. sql = fmt.Sprintf(sql, dataTableName)
  900. _, err = to.Raw(sql, val, edbInfoId, av).Exec()
  901. if err != nil {
  902. return err
  903. }
  904. }
  905. }
  906. break
  907. }
  908. } else {
  909. nextDateDay := preDate.AddDate(0, 0, 1)
  910. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  911. preDateDay := preDate.AddDate(0, 0, -1)
  912. preDateDayStr := preDateDay.Format(utils.FormatDate)
  913. for i := 0; i < 35; i++ {
  914. if i >= 1 {
  915. nextDateDay = nextDateDay.AddDate(0, 0, i)
  916. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  917. }
  918. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  919. timestamp := currentDate.UnixNano() / 1e6
  920. timestampStr := fmt.Sprintf("%d", timestamp)
  921. val := TbzDiv(currentItem.Value, findItem.Value)
  922. if existVal, ok := existDataMap[av]; !ok {
  923. if _, existOk := existAddDataMap[av]; !existOk {
  924. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  925. isAdd = true
  926. }
  927. existAddDataMap[av] = av
  928. } else {
  929. if existVal != val {
  930. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  931. sql = fmt.Sprintf(sql, dataTableName)
  932. _, err = to.Raw(sql, val, edbInfoId, av).Exec()
  933. if err != nil {
  934. return err
  935. }
  936. }
  937. }
  938. break
  939. } else {
  940. if i >= 1 {
  941. preDateDay = preDate.AddDate(0, 0, -i)
  942. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  943. }
  944. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  945. timestamp := currentDate.UnixNano() / 1e6
  946. timestampStr := fmt.Sprintf("%d", timestamp)
  947. val := TbzDiv(currentItem.Value, findItem.Value)
  948. if existVal, ok := existDataMap[av]; !ok {
  949. if _, existOk := existAddDataMap[av]; !existOk {
  950. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  951. isAdd = true
  952. }
  953. existAddDataMap[av] = av
  954. } else {
  955. if existVal != val {
  956. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  957. sql = fmt.Sprintf(sql, dataTableName)
  958. _, err = to.Raw(sql, val, edbInfoId, av).Exec()
  959. if err != nil {
  960. return err
  961. }
  962. }
  963. }
  964. break
  965. }
  966. }
  967. }
  968. }
  969. }
  970. }
  971. }
  972. if isAdd {
  973. addSql = strings.TrimRight(addSql, ",")
  974. _, err = to.Raw(addSql).Exec()
  975. if err != nil {
  976. return err
  977. }
  978. }
  979. return
  980. }