edb_info_calculate_tcz.go 33 KB

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