edb_data_calculate_ljzzy.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "hongze/hongze_edb_lib/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfoCalculateLjzzyDetail struct {
  13. EdbInfoCalculateLjzzyId int `orm:"column(edb_info_calculate_ljzzy_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. StartDate string `description:"开始日期"`
  26. EndDate string `description:"结束日期"`
  27. }
  28. func GetEdbInfoCalculateLjzzyDetail(edbInfoId int) (item *EdbInfoCalculateLjzzyDetail, err error) {
  29. o := orm.NewOrm()
  30. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  31. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  32. WHERE a.edb_info_id=? `
  33. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  34. return
  35. }
  36. // AddCalculateLjzzy 累计值转月
  37. func AddCalculateLjzzy(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  38. o := orm.NewOrm()
  39. to, err := o.Begin()
  40. if err != nil {
  41. return
  42. }
  43. defer func() {
  44. if err != nil {
  45. fmt.Println("AddCalculateLjzzy,Err:" + err.Error())
  46. _ = to.Rollback()
  47. } else {
  48. _ = to.Commit()
  49. }
  50. }()
  51. if req.EdbInfoId <= 0 {
  52. edbInfo := new(EdbInfo)
  53. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_LJZZY
  54. edbInfo.SourceName = "累计值转月值"
  55. edbInfo.EdbCode = edbCode
  56. edbInfo.EdbName = req.EdbName
  57. edbInfo.EdbNameSource = req.EdbName
  58. edbInfo.Frequency = req.Frequency
  59. edbInfo.Unit = req.Unit
  60. edbInfo.ClassifyId = req.ClassifyId
  61. edbInfo.SysUserId = sysUserId
  62. edbInfo.SysUserRealName = sysUserRealName
  63. edbInfo.CreateTime = time.Now()
  64. edbInfo.ModifyTime = time.Now()
  65. edbInfo.UniqueCode = uniqueCode
  66. edbInfo.CalculateFormula = req.Formula
  67. edbInfo.EdbType = 2
  68. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  69. if tmpErr != nil {
  70. return int(newEdbInfoId), tmpErr
  71. }
  72. edbInfoId = int(newEdbInfoId)
  73. //关联关系
  74. {
  75. calculateMappingItem := new(EdbInfoCalculateMapping)
  76. calculateMappingItem.CreateTime = time.Now()
  77. calculateMappingItem.ModifyTime = time.Now()
  78. calculateMappingItem.Sort = 1
  79. calculateMappingItem.EdbCode = edbCode
  80. calculateMappingItem.EdbInfoId = edbInfoId
  81. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  82. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  83. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  84. calculateMappingItem.FromSource = fromEdbInfo.Source
  85. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  86. calculateMappingItem.FromTag = ""
  87. calculateMappingItem.Source = edbInfo.Source
  88. calculateMappingItem.SourceName = edbInfo.SourceName
  89. _, err = to.Insert(calculateMappingItem)
  90. if err != nil {
  91. return
  92. }
  93. }
  94. } else {
  95. edbInfoId = req.EdbInfoId
  96. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_LJZZY)
  97. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  98. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  99. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  100. if err != nil {
  101. return
  102. }
  103. }
  104. edbInfoIdStr := strconv.Itoa(edbInfoId)
  105. //计算数据
  106. var condition string
  107. var pars []interface{}
  108. condition += " AND edb_info_id=? "
  109. if req.EdbInfoId <= 0 {
  110. pars = append(pars, req.FromEdbInfoId)
  111. } else {
  112. pars = append(pars, fromEdbInfo.EdbInfoId)
  113. }
  114. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  115. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  116. if err != nil {
  117. return edbInfoId, err
  118. }
  119. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  120. dataLen := len(dataList)
  121. for i := 0; i < dataLen; i++ {
  122. item := dataList[i]
  123. //日其中获取年
  124. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  125. if err != nil {
  126. return edbInfoId, err
  127. }
  128. year := itemDate.Year()
  129. month := int(itemDate.Month())
  130. if monthMap, yok := yearMap[year]; yok {
  131. monthMap[month] = item
  132. yearMap[year] = monthMap
  133. } else {
  134. monthMap = make(map[int]*EdbInfoSearchData)
  135. monthMap[month] = item
  136. yearMap[year] = monthMap
  137. }
  138. }
  139. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  140. var isAdd bool
  141. existMap := make(map[string]string)
  142. for yk, yv := range yearMap {
  143. _, oneMonthOk := yv[1]
  144. _, twoMonthOk := yv[2]
  145. if !oneMonthOk && !twoMonthOk {
  146. continue
  147. }
  148. for i := 1; i <= 12; i++ {
  149. fmt.Println(yk, i, yv[i])
  150. dataCurrentItem := yv[i]
  151. fmt.Println("i:", i, yk, dataCurrentItem)
  152. var date string
  153. var val float64
  154. if i == 1 || i == 2 {
  155. if _, mok := yv[1]; mok { //1月有值
  156. if i == 1 {
  157. date = dataCurrentItem.DataTime
  158. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  159. }
  160. if i == 2 {
  161. dataOneItem := yv[1]
  162. if dataCurrentItem != nil {
  163. date = dataCurrentItem.DataTime
  164. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  165. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  166. val, _ = twoMonth.Sub(oneMonth).Float64()
  167. } else {
  168. continue
  169. }
  170. }
  171. } else { //1月无值
  172. dataTwoItem := yv[2]
  173. if i == 1 {
  174. date = strconv.Itoa(yk) + "-01-31"
  175. a := decimal.NewFromFloat(dataTwoItem.Value)
  176. b := decimal.NewFromFloat(2.0)
  177. val, _ = a.Div(b).Float64()
  178. }
  179. if i == 2 {
  180. date = dataCurrentItem.DataTime
  181. a := decimal.NewFromFloat(dataTwoItem.Value)
  182. b := decimal.NewFromFloat(2.0)
  183. val, _ = a.Div(b).Float64()
  184. }
  185. }
  186. } else {
  187. dataPreItem := yv[i-1]
  188. if dataCurrentItem != nil && dataPreItem != nil {
  189. date = dataCurrentItem.DataTime
  190. //val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Sub(decimal.NewFromFloat(dataPreItem.Value)).Float64()
  191. a := decimal.NewFromFloat(dataCurrentItem.Value)
  192. b := decimal.NewFromFloat(dataPreItem.Value)
  193. val, _ = a.Sub(b).Float64()
  194. }
  195. }
  196. if date != "" {
  197. dataTime, _ := time.Parse(utils.FormatDate, date)
  198. timestamp := dataTime.UnixNano() / 1e6
  199. timeStr := fmt.Sprintf("%d", timestamp)
  200. if _, ok := existMap[edbCode+date]; !ok {
  201. addSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, utils.SubFloatToString(val, 4))
  202. isAdd = true
  203. }
  204. existMap[edbCode+date] = date
  205. }
  206. }
  207. }
  208. if isAdd {
  209. addSql = strings.TrimRight(addSql, ",")
  210. _, err = to.Raw(addSql).Exec()
  211. if err != nil {
  212. return edbInfoId, err
  213. }
  214. }
  215. return
  216. }
  217. // EditCalculateLjzzy 编辑累计值转月数据
  218. func EditCalculateLjzzy(req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo, edbCode string) (edbInfoId int, err error) {
  219. edbInfoId = req.EdbInfoId
  220. o := orm.NewOrm()
  221. to, err := o.Begin()
  222. if err != nil {
  223. return
  224. }
  225. defer func() {
  226. if err != nil {
  227. fmt.Println("EditCalculateLjzzy,Err:" + err.Error())
  228. _ = to.Rollback()
  229. } else {
  230. _ = to.Commit()
  231. }
  232. }()
  233. //修改指标信息
  234. sql := ` UPDATE edb_info
  235. SET
  236. edb_name =?,
  237. edb_name_source=?,
  238. frequency = ?,
  239. unit = ?,
  240. classify_id = ?,
  241. modify_time = NOW()
  242. WHERE edb_info_id = ? `
  243. _, err = o.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, edbInfoId).Exec()
  244. if err != nil {
  245. return
  246. }
  247. var existCondition string
  248. var existPars []interface{}
  249. existCondition += " AND edb_info_id=? "
  250. existPars = append(existPars, edbInfoId)
  251. existCondition += " AND from_edb_info_id=? "
  252. existPars = append(existPars, req.FromEdbInfoId)
  253. //判断计算指标是否被更换
  254. count, err := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  255. if err != nil {
  256. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  257. return
  258. }
  259. if count <= 0 {
  260. //删除指标关联计算指标
  261. //sql := ` DELETE FROM edb_info_calculate_ljzzy WHERE edb_info_id = ? `
  262. //_, err = o.Raw(sql, edbInfoId).Exec()
  263. //if err != nil {
  264. // return
  265. //}
  266. //calculateItem := new(EdbInfoCalculateLjzzy)
  267. //calculateItem.CreateTime = time.Now()
  268. //calculateItem.ModifyTime = time.Now()
  269. //calculateItem.Sort = 1
  270. //calculateItem.EdbCode = edbCode
  271. //calculateItem.EdbInfoId = edbInfoId
  272. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  273. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  274. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  275. //calculateItem.FromSource = fromEdbInfo.Source
  276. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  277. //_, err = o.Insert(calculateItem)
  278. //if err != nil {
  279. // return
  280. //}
  281. //删除,计算指标关联的,基础指标的关联关系
  282. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  283. _, err = o.Raw(sql, edbInfoId).Exec()
  284. if err != nil {
  285. return
  286. }
  287. //清空原有数据
  288. sql = ` DELETE FROM edb_data_calculate_ljzzy WHERE edb_info_id = ? `
  289. _, err = o.Raw(sql, edbInfoId).Exec()
  290. if err != nil {
  291. return edbInfoId, err
  292. }
  293. //关联关系
  294. {
  295. calculateMappingItem := new(EdbInfoCalculateMapping)
  296. calculateMappingItem.CreateTime = time.Now()
  297. calculateMappingItem.ModifyTime = time.Now()
  298. calculateMappingItem.Sort = 1
  299. calculateMappingItem.EdbCode = edbCode
  300. calculateMappingItem.EdbInfoId = edbInfoId
  301. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  302. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  303. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  304. calculateMappingItem.FromSource = fromEdbInfo.Source
  305. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  306. calculateMappingItem.FromTag = ""
  307. calculateMappingItem.Source = utils.DATA_SOURCE_CALCULATE_LJZZY
  308. calculateMappingItem.SourceName = "累计值转月"
  309. _, err = o.Insert(calculateMappingItem)
  310. if err != nil {
  311. return
  312. }
  313. }
  314. //计算数据
  315. var condition string
  316. var pars []interface{}
  317. condition += " AND edb_info_id=? "
  318. pars = append(pars, req.FromEdbInfoId)
  319. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  320. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  321. if err != nil {
  322. return edbInfoId, err
  323. }
  324. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  325. dataLen := len(dataList)
  326. for i := 0; i < dataLen; i++ {
  327. item := dataList[i]
  328. //日其中获取年
  329. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  330. if err != nil {
  331. return edbInfoId, err
  332. }
  333. year := itemDate.Year()
  334. month := int(itemDate.Month())
  335. if monthMap, yok := yearMap[year]; yok {
  336. monthMap[month] = item
  337. yearMap[year] = monthMap
  338. } else {
  339. monthMap = make(map[int]*EdbInfoSearchData)
  340. monthMap[month] = item
  341. yearMap[year] = monthMap
  342. }
  343. }
  344. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  345. nowStr := time.Now().Format(utils.FormatDateTime)
  346. var isAdd bool
  347. for yk, yv := range yearMap {
  348. _, oneMonthOk := yv[1]
  349. _, twoMonthOk := yv[2]
  350. if !oneMonthOk && !twoMonthOk {
  351. continue
  352. }
  353. for i := 1; i <= 12; i++ {
  354. fmt.Println(yk, i, yv[i])
  355. dataCurrentItem := yv[i]
  356. var date string
  357. var val float64
  358. if i == 1 || i == 2 {
  359. if _, mok := yv[1]; mok { //1月有值
  360. if i == 1 {
  361. date = dataCurrentItem.DataTime
  362. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  363. }
  364. if i == 2 {
  365. dataOneItem := yv[1]
  366. date = dataCurrentItem.DataTime
  367. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  368. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  369. val, _ = twoMonth.Sub(oneMonth).Float64()
  370. }
  371. } else { //1月无值
  372. dataTwoItem := yv[2]
  373. if i == 1 {
  374. date = strconv.Itoa(yk) + "-01-31"
  375. a := decimal.NewFromFloat(dataTwoItem.Value)
  376. b := decimal.NewFromFloat(2.0)
  377. val, _ = a.Div(b).Float64()
  378. }
  379. if i == 2 {
  380. date = dataCurrentItem.DataTime
  381. a := decimal.NewFromFloat(dataTwoItem.Value)
  382. b := decimal.NewFromFloat(2.0)
  383. val, _ = a.Div(b).Float64()
  384. }
  385. }
  386. } else {
  387. dataPreItem := yv[i-1]
  388. if dataCurrentItem != nil && dataPreItem != nil {
  389. date = dataCurrentItem.DataTime
  390. //val = dataCurrentItem.Value - dataPreItem.Value
  391. a := decimal.NewFromFloat(dataCurrentItem.Value)
  392. b := decimal.NewFromFloat(dataPreItem.Value)
  393. val, _ = a.Sub(b).Float64()
  394. }
  395. }
  396. if date != "" {
  397. dataTime, _ := time.Parse(utils.FormatDate, date)
  398. timestamp := dataTime.UnixNano() / 1e6
  399. timeStr := fmt.Sprintf("%d", timestamp)
  400. addSql += "("
  401. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + date + "'" + "," + utils.SubFloatToString(val, 4) + "," + "'" + nowStr + "'" +
  402. "," + "'" + nowStr + "'" + "," + "1"
  403. addSql += "," + "'" + timeStr + "'"
  404. addSql += "),"
  405. isAdd = true
  406. }
  407. }
  408. }
  409. if isAdd {
  410. addSql = strings.TrimRight(addSql, ",")
  411. _, err = o.Raw(addSql).Exec()
  412. if err != nil {
  413. return edbInfoId, err
  414. }
  415. }
  416. }
  417. return
  418. }
  419. // RefreshAllCalculateLjzzy 刷新全部累计值转月数据
  420. func RefreshAllCalculateLjzzy(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  421. o := orm.NewOrm()
  422. to, err := o.Begin()
  423. if err != nil {
  424. return
  425. }
  426. defer func() {
  427. if err != nil {
  428. fmt.Println("RefreshAllCalculateLjzzy,Err:" + err.Error())
  429. _ = to.Rollback()
  430. } else {
  431. _ = to.Commit()
  432. }
  433. }()
  434. if err != nil {
  435. return
  436. }
  437. edbInfoIdStr := strconv.Itoa(edbInfoId)
  438. //计算数据
  439. var condition string
  440. var pars []interface{}
  441. condition += " AND edb_info_id=? "
  442. pars = append(pars, fromEdbInfo.EdbInfoId)
  443. if startDate != "" {
  444. condition += " AND data_time>=? "
  445. pars = append(pars, startDate)
  446. }
  447. if endDate != "" {
  448. condition += " AND data_time<=? "
  449. pars = append(pars, endDate)
  450. }
  451. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  452. if err != nil {
  453. return err
  454. }
  455. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  456. dataLen := len(dataList)
  457. for i := 0; i < dataLen; i++ {
  458. item := dataList[i]
  459. //日其中获取年
  460. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  461. if err != nil {
  462. return err
  463. }
  464. year := itemDate.Year()
  465. month := int(itemDate.Month())
  466. if monthMap, yok := yearMap[year]; yok {
  467. monthMap[month] = item
  468. yearMap[year] = monthMap
  469. } else {
  470. monthMap = make(map[int]*EdbInfoSearchData)
  471. monthMap[month] = item
  472. yearMap[year] = monthMap
  473. }
  474. }
  475. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  476. var isAdd bool
  477. //获取指标所有数据
  478. existDataList := make([]*EdbData, 0)
  479. dataTableName := GetEdbDataTableName(source)
  480. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  481. sql = fmt.Sprintf(sql, dataTableName)
  482. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  483. if err != nil {
  484. return err
  485. }
  486. dataMap := make(map[string]string)
  487. for _, v := range existDataList {
  488. dataMap[v.DataTime] = v.Value
  489. }
  490. existDataMap := make(map[string]string)
  491. for yk, yv := range yearMap {
  492. _, oneMonthOk := yv[1]
  493. _, twoMonthOk := yv[2]
  494. if !oneMonthOk && !twoMonthOk {
  495. continue
  496. }
  497. for i := 1; i <= 12; i++ {
  498. fmt.Println(yk, i, yv[i])
  499. dataCurrentItem := yv[i]
  500. var date string
  501. var val float64
  502. if i == 1 || i == 2 {
  503. if _, mok := yv[1]; mok { //1月有值
  504. if i == 1 {
  505. date = dataCurrentItem.DataTime
  506. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  507. }
  508. if i == 2 {
  509. dataOneItem := yv[1]
  510. date = dataCurrentItem.DataTime
  511. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  512. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  513. val, _ = twoMonth.Sub(oneMonth).Float64()
  514. }
  515. } else { //1月无值
  516. dataTwoItem := yv[2]
  517. if i == 1 {
  518. date = strconv.Itoa(yk) + "-01-31"
  519. a := decimal.NewFromFloat(dataTwoItem.Value)
  520. b := decimal.NewFromFloat(2.0)
  521. val, _ = a.Div(b).Float64()
  522. }
  523. if i == 2 {
  524. date = dataCurrentItem.DataTime
  525. a := decimal.NewFromFloat(dataTwoItem.Value)
  526. b := decimal.NewFromFloat(2.0)
  527. val, _ = a.Div(b).Float64()
  528. }
  529. }
  530. } else {
  531. dataPreItem := yv[i-1]
  532. if dataCurrentItem != nil && dataPreItem != nil {
  533. date = dataCurrentItem.DataTime
  534. //val = dataCurrentItem.Value - dataPreItem.Value
  535. a := decimal.NewFromFloat(dataCurrentItem.Value)
  536. b := decimal.NewFromFloat(dataPreItem.Value)
  537. val, _ = a.Sub(b).Float64()
  538. }
  539. }
  540. if date != "" {
  541. saveValue := utils.SubFloatToString(val, 4)
  542. //判断数据是否存在
  543. if existVal, ok := dataMap[date]; !ok {
  544. dataTime, _ := time.Parse(utils.FormatDate, date)
  545. timestamp := dataTime.UnixNano() / 1e6
  546. timeStr := fmt.Sprintf("%d", timestamp)
  547. if _, existOk := existDataMap[date]; !existOk {
  548. addSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, saveValue)
  549. isAdd = true
  550. }
  551. existDataMap[date] = date
  552. } else {
  553. if existVal != saveValue {
  554. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  555. sql = fmt.Sprintf(sql, dataTableName)
  556. _, err = o.Raw(sql, saveValue, edbInfoId, date).Exec()
  557. if err != nil {
  558. return err
  559. }
  560. }
  561. }
  562. }
  563. }
  564. }
  565. if isAdd {
  566. addSql = strings.TrimRight(addSql, ",")
  567. _, err = o.Raw(addSql).Exec()
  568. if err != nil {
  569. fmt.Println("RefreshAllCalculateLjzzy add Err", err.Error())
  570. return
  571. }
  572. }
  573. return
  574. }