edb_info_calculate_ljzzy.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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 EdbInfoCalculateLjzzy 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. }
  26. func AddEdbInfoCalculateLjzzyMulti(items []*EdbInfoCalculateLjzzy) (err error) {
  27. o := orm.NewOrmUsingDB("data")
  28. _, err = o.InsertMulti(1, items)
  29. return
  30. }
  31. // 累计值转月
  32. func AddCalculateLjzzy(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  33. o := orm.NewOrmUsingDB("data")
  34. to, err := o.Begin()
  35. if err != nil {
  36. return
  37. }
  38. defer func() {
  39. if err != nil {
  40. _ = to.Rollback()
  41. } else {
  42. _ = to.Commit()
  43. }
  44. }()
  45. if req.EdbInfoId <= 0 {
  46. edbInfo := new(EdbInfo)
  47. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_LJZZY
  48. edbInfo.SourceName = "累计值转月值"
  49. edbInfo.EdbCode = edbCode
  50. edbInfo.EdbName = req.EdbName
  51. edbInfo.EdbNameSource = req.EdbName
  52. edbInfo.Frequency = req.Frequency
  53. edbInfo.Unit = req.Unit
  54. edbInfo.ClassifyId = req.ClassifyId
  55. edbInfo.SysUserId = sysUserId
  56. edbInfo.SysUserRealName = sysUserRealName
  57. edbInfo.CreateTime = time.Now()
  58. edbInfo.ModifyTime = time.Now()
  59. edbInfo.UniqueCode = uniqueCode
  60. edbInfo.CalculateFormula = req.Formula
  61. edbInfo.EdbType = 2
  62. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  63. if tmpErr != nil {
  64. return int(newEdbInfoId), tmpErr
  65. }
  66. edbInfoId = int(newEdbInfoId)
  67. //calculateItem := new(EdbInfoCalculateLjzzy)
  68. //calculateItem.CreateTime = time.Now()
  69. //calculateItem.ModifyTime = time.Now()
  70. //calculateItem.Sort = 1
  71. //calculateItem.EdbCode = edbCode
  72. //calculateItem.EdbInfoId = edbInfoId
  73. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  74. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  75. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  76. //calculateItem.FromSource = fromEdbInfo.Source
  77. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  78. //_, err = to.Insert(calculateItem)
  79. //if err != nil {
  80. // return edbInfoId, err
  81. //}
  82. //关联关系
  83. {
  84. calculateMappingItem := new(EdbInfoCalculateMapping)
  85. calculateMappingItem.CreateTime = time.Now()
  86. calculateMappingItem.ModifyTime = time.Now()
  87. calculateMappingItem.Sort = 1
  88. calculateMappingItem.EdbCode = edbCode
  89. calculateMappingItem.EdbInfoId = edbInfoId
  90. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  91. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  92. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  93. calculateMappingItem.FromSource = fromEdbInfo.Source
  94. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  95. calculateMappingItem.FromTag = ""
  96. calculateMappingItem.Source = edbInfo.Source
  97. calculateMappingItem.SourceName = edbInfo.SourceName
  98. _, err = to.Insert(calculateMappingItem)
  99. if err != nil {
  100. return
  101. }
  102. }
  103. } else {
  104. edbInfoId = req.EdbInfoId
  105. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_LJZZY)
  106. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  107. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  108. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  109. }
  110. edbInfoIdStr := strconv.Itoa(edbInfoId)
  111. //计算数据
  112. var condition string
  113. var pars []interface{}
  114. condition += " AND edb_info_id=? "
  115. if req.EdbInfoId <= 0 {
  116. pars = append(pars, req.FromEdbInfoId)
  117. } else {
  118. pars = append(pars, fromEdbInfo.EdbInfoId)
  119. }
  120. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  121. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  122. if err != nil {
  123. return edbInfoId, err
  124. }
  125. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  126. dataLen := len(dataList)
  127. for i := 0; i < dataLen; i++ {
  128. item := dataList[i]
  129. //日其中获取年
  130. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  131. if err != nil {
  132. return edbInfoId, err
  133. }
  134. year := itemDate.Year()
  135. month := int(itemDate.Month())
  136. if monthMap, yok := yearMap[year]; yok {
  137. monthMap[month] = item
  138. yearMap[year] = monthMap
  139. } else {
  140. monthMap = make(map[int]*EdbInfoSearchData)
  141. monthMap[month] = item
  142. yearMap[year] = monthMap
  143. }
  144. }
  145. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  146. var isAdd bool
  147. existMap := make(map[string]string)
  148. for yk, yv := range yearMap {
  149. _, oneMonthOk := yv[1]
  150. _, twoMonthOk := yv[2]
  151. if !oneMonthOk && !twoMonthOk {
  152. continue
  153. }
  154. for i := 1; i <= 12; i++ {
  155. fmt.Println(yk, i, yv[i])
  156. dataCurrentItem := yv[i]
  157. fmt.Println("i:", i, yk, dataCurrentItem)
  158. var date string
  159. var val float64
  160. if i == 1 || i == 2 {
  161. if _, mok := yv[1]; mok { //1月有值
  162. if i == 1 {
  163. date = dataCurrentItem.DataTime
  164. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  165. }
  166. if i == 2 {
  167. dataOneItem := yv[1]
  168. if dataCurrentItem != nil {
  169. date = dataCurrentItem.DataTime
  170. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  171. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  172. val, _ = twoMonth.Sub(oneMonth).Float64()
  173. } else {
  174. continue
  175. }
  176. }
  177. } else { //1月无值
  178. dataTwoItem := yv[2]
  179. if i == 1 {
  180. date = strconv.Itoa(yk) + "-01-31"
  181. a := decimal.NewFromFloat(dataTwoItem.Value)
  182. b := decimal.NewFromFloat(2.0)
  183. val, _ = a.Div(b).Float64()
  184. }
  185. if i == 2 {
  186. date = dataCurrentItem.DataTime
  187. a := decimal.NewFromFloat(dataTwoItem.Value)
  188. b := decimal.NewFromFloat(2.0)
  189. val, _ = a.Div(b).Float64()
  190. }
  191. }
  192. } else {
  193. dataPreItem := yv[i-1]
  194. if dataCurrentItem != nil && dataPreItem != nil {
  195. date = dataCurrentItem.DataTime
  196. //val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Sub(decimal.NewFromFloat(dataPreItem.Value)).Float64()
  197. a := decimal.NewFromFloat(dataCurrentItem.Value)
  198. b := decimal.NewFromFloat(dataPreItem.Value)
  199. val, _ = a.Sub(b).Float64()
  200. }
  201. }
  202. if date != "" {
  203. dataTime, _ := time.Parse(utils.FormatDate, date)
  204. timestamp := dataTime.UnixNano() / 1e6
  205. timeStr := fmt.Sprintf("%d", timestamp)
  206. if _, ok := existMap[edbCode+date]; !ok {
  207. addSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, utils.SubFloatToString(val, 4))
  208. isAdd = true
  209. }
  210. existMap[edbCode+date] = date
  211. }
  212. }
  213. }
  214. if isAdd {
  215. addSql = strings.TrimRight(addSql, ",")
  216. _, err = to.Raw(addSql).Exec()
  217. if err != nil {
  218. return edbInfoId, err
  219. }
  220. }
  221. return
  222. }
  223. // 修改累计值转月
  224. func EditCalculateLjzzy(req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo, edbCode string) (edbInfoId int, err error) {
  225. edbInfoId = req.EdbInfoId
  226. o := orm.NewOrmUsingDB("data")
  227. to, err := o.Begin()
  228. if err != nil {
  229. return
  230. }
  231. defer func() {
  232. if err != nil {
  233. _ = to.Rollback()
  234. } else {
  235. _ = to.Commit()
  236. }
  237. }()
  238. //修改指标信息
  239. sql := ` UPDATE edb_info
  240. SET
  241. edb_name =?,
  242. edb_name_source=?,
  243. frequency = ?,
  244. unit = ?,
  245. classify_id = ?,
  246. modify_time = NOW()
  247. WHERE edb_info_id = ? `
  248. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, edbInfoId).Exec()
  249. if err != nil {
  250. return
  251. }
  252. var existCondition string
  253. var existPars []interface{}
  254. existCondition += " AND edb_info_id=? "
  255. existPars = append(existPars, edbInfoId)
  256. existCondition += " AND from_edb_info_id=? "
  257. existPars = append(existPars, req.FromEdbInfoId)
  258. //判断计算指标是否被更换
  259. count, err := GetEdbInfoCalculateCountByCondition(req.Source, existCondition, existPars)
  260. if err != nil {
  261. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  262. return
  263. }
  264. if count <= 0 {
  265. //删除指标关联计算指标
  266. //sql := ` DELETE FROM edb_info_calculate_ljzzy WHERE edb_info_id = ? `
  267. //_, err = to.Raw(sql, edbInfoId).Exec()
  268. //if err != nil {
  269. // return
  270. //}
  271. //calculateItem := new(EdbInfoCalculateLjzzy)
  272. //calculateItem.CreateTime = time.Now()
  273. //calculateItem.ModifyTime = time.Now()
  274. //calculateItem.Sort = 1
  275. //calculateItem.EdbCode = edbCode
  276. //calculateItem.EdbInfoId = edbInfoId
  277. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  278. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  279. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  280. //calculateItem.FromSource = fromEdbInfo.Source
  281. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  282. //_, err = to.Insert(calculateItem)
  283. //if err != nil {
  284. // return
  285. //}
  286. //删除,计算指标关联的,基础指标的关联关系
  287. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  288. _, err = to.Raw(sql, edbInfoId).Exec()
  289. if err != nil {
  290. return
  291. }
  292. //清空原有数据
  293. sql = ` DELETE FROM edb_data_calculate_ljzzy WHERE edb_info_id = ? `
  294. _, err = to.Raw(sql, edbInfoId).Exec()
  295. if err != nil {
  296. return edbInfoId, err
  297. }
  298. //关联关系
  299. {
  300. calculateMappingItem := new(EdbInfoCalculateMapping)
  301. calculateMappingItem.CreateTime = time.Now()
  302. calculateMappingItem.ModifyTime = time.Now()
  303. calculateMappingItem.Sort = 1
  304. calculateMappingItem.EdbCode = edbCode
  305. calculateMappingItem.EdbInfoId = edbInfoId
  306. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  307. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  308. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  309. calculateMappingItem.FromSource = fromEdbInfo.Source
  310. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  311. calculateMappingItem.FromTag = ""
  312. calculateMappingItem.Source = utils.DATA_SOURCE_CALCULATE_LJZZY
  313. calculateMappingItem.SourceName = "累计值转月"
  314. _, err = to.Insert(calculateMappingItem)
  315. if err != nil {
  316. return
  317. }
  318. }
  319. //计算数据
  320. var condition string
  321. var pars []interface{}
  322. condition += " AND edb_info_id=? "
  323. pars = append(pars, req.FromEdbInfoId)
  324. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  325. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  326. if err != nil {
  327. return edbInfoId, err
  328. }
  329. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  330. dataLen := len(dataList)
  331. for i := 0; i < dataLen; i++ {
  332. item := dataList[i]
  333. //日其中获取年
  334. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  335. if err != nil {
  336. return edbInfoId, err
  337. }
  338. year := itemDate.Year()
  339. month := int(itemDate.Month())
  340. if monthMap, yok := yearMap[year]; yok {
  341. monthMap[month] = item
  342. yearMap[year] = monthMap
  343. } else {
  344. monthMap = make(map[int]*EdbInfoSearchData)
  345. monthMap[month] = item
  346. yearMap[year] = monthMap
  347. }
  348. }
  349. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  350. nowStr := time.Now().Format(utils.FormatDateTime)
  351. var isAdd bool
  352. for yk, yv := range yearMap {
  353. _, oneMonthOk := yv[1]
  354. _, twoMonthOk := yv[2]
  355. if !oneMonthOk && !twoMonthOk {
  356. continue
  357. }
  358. for i := 1; i <= 12; i++ {
  359. fmt.Println(yk, i, yv[i])
  360. dataCurrentItem := yv[i]
  361. var date string
  362. var val float64
  363. if i == 1 || i == 2 {
  364. if _, mok := yv[1]; mok { //1月有值
  365. if i == 1 {
  366. date = dataCurrentItem.DataTime
  367. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  368. }
  369. if i == 2 {
  370. dataOneItem := yv[1]
  371. date = dataCurrentItem.DataTime
  372. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  373. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  374. val, _ = twoMonth.Sub(oneMonth).Float64()
  375. }
  376. } else { //1月无值
  377. dataTwoItem := yv[2]
  378. if i == 1 {
  379. date = strconv.Itoa(yk) + "-01-31"
  380. a := decimal.NewFromFloat(dataTwoItem.Value)
  381. b := decimal.NewFromFloat(2.0)
  382. val, _ = a.Div(b).Float64()
  383. }
  384. if i == 2 {
  385. date = dataCurrentItem.DataTime
  386. a := decimal.NewFromFloat(dataTwoItem.Value)
  387. b := decimal.NewFromFloat(2.0)
  388. val, _ = a.Div(b).Float64()
  389. }
  390. }
  391. } else {
  392. dataPreItem := yv[i-1]
  393. if dataCurrentItem != nil && dataPreItem != nil {
  394. date = dataCurrentItem.DataTime
  395. //val = dataCurrentItem.Value - dataPreItem.Value
  396. a := decimal.NewFromFloat(dataCurrentItem.Value)
  397. b := decimal.NewFromFloat(dataPreItem.Value)
  398. val, _ = a.Sub(b).Float64()
  399. }
  400. }
  401. if date != "" {
  402. dataTime, _ := time.Parse(utils.FormatDate, date)
  403. timestamp := dataTime.UnixNano() / 1e6
  404. timeStr := fmt.Sprintf("%d", timestamp)
  405. addSql += "("
  406. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + date + "'" + "," + utils.SubFloatToString(val, 4) + "," + "'" + nowStr + "'" +
  407. "," + "'" + nowStr + "'" + "," + "1"
  408. addSql += "," + "'" + timeStr + "'"
  409. addSql += "),"
  410. isAdd = true
  411. }
  412. }
  413. }
  414. if isAdd {
  415. addSql = strings.TrimRight(addSql, ",")
  416. _, err = to.Raw(addSql).Exec()
  417. if err != nil {
  418. return edbInfoId, err
  419. }
  420. }
  421. }
  422. return
  423. }
  424. // 刷新累计值转月值数据
  425. func RefreshCalculateLjzzy(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  426. o := orm.NewOrmUsingDB("data")
  427. to, err := o.Begin()
  428. if err != nil {
  429. return
  430. }
  431. defer func() {
  432. if err != nil {
  433. _ = to.Rollback()
  434. } else {
  435. _ = to.Commit()
  436. }
  437. }()
  438. if err != nil {
  439. return
  440. }
  441. edbInfoIdStr := strconv.Itoa(edbInfoId)
  442. //计算数据
  443. var condition string
  444. var pars []interface{}
  445. condition += " AND edb_info_id=? "
  446. pars = append(pars, fromEdbInfo.EdbInfoId)
  447. if startDate != "" {
  448. condition += " AND data_time>=? "
  449. pars = append(pars, startDate)
  450. }
  451. if endDate != "" {
  452. condition += " AND data_time<=? "
  453. pars = append(pars, endDate)
  454. }
  455. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  456. if err != nil {
  457. return err
  458. }
  459. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  460. dataLen := len(dataList)
  461. for i := 0; i < dataLen; i++ {
  462. item := dataList[i]
  463. //日其中获取年
  464. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  465. if err != nil {
  466. return err
  467. }
  468. year := itemDate.Year()
  469. month := int(itemDate.Month())
  470. if monthMap, yok := yearMap[year]; yok {
  471. monthMap[month] = item
  472. yearMap[year] = monthMap
  473. } else {
  474. monthMap = make(map[int]*EdbInfoSearchData)
  475. monthMap[month] = item
  476. yearMap[year] = monthMap
  477. }
  478. }
  479. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  480. nowStr := time.Now().Format(utils.FormatDateTime)
  481. var isAdd bool
  482. for yk, yv := range yearMap {
  483. _, oneMonthOk := yv[1]
  484. _, twoMonthOk := yv[2]
  485. if !oneMonthOk && !twoMonthOk {
  486. continue
  487. }
  488. for i := 1; i <= 12; i++ {
  489. fmt.Println(yk, i, yv[i])
  490. dataCurrentItem := yv[i]
  491. var date string
  492. var val float64
  493. if i == 1 || i == 2 {
  494. if _, mok := yv[1]; mok { //1月有值
  495. if i == 1 {
  496. date = dataCurrentItem.DataTime
  497. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  498. }
  499. if i == 2 {
  500. dataOneItem := yv[1]
  501. date = dataCurrentItem.DataTime
  502. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  503. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  504. val, _ = twoMonth.Sub(oneMonth).Float64()
  505. }
  506. } else { //1月无值
  507. dataTwoItem := yv[2]
  508. if i == 1 {
  509. date = strconv.Itoa(yk) + "-01-31"
  510. a := decimal.NewFromFloat(dataTwoItem.Value)
  511. b := decimal.NewFromFloat(2.0)
  512. val, _ = a.Div(b).Float64()
  513. }
  514. if i == 2 {
  515. date = dataCurrentItem.DataTime
  516. a := decimal.NewFromFloat(dataTwoItem.Value)
  517. b := decimal.NewFromFloat(2.0)
  518. val, _ = a.Div(b).Float64()
  519. }
  520. }
  521. } else {
  522. dataPreItem := yv[i-1]
  523. if dataCurrentItem != nil && dataPreItem != nil {
  524. date = dataCurrentItem.DataTime
  525. //val = dataCurrentItem.Value - dataPreItem.Value
  526. a := decimal.NewFromFloat(dataCurrentItem.Value)
  527. b := decimal.NewFromFloat(dataPreItem.Value)
  528. val, _ = a.Sub(b).Float64()
  529. }
  530. }
  531. if date != "" {
  532. //判断数据是否存在
  533. count, err := GetEdbDataCalculateLjzzyByCodeAndDate(edbCode, date)
  534. if err != nil && err.Error() != utils.ErrNoRow() {
  535. return err
  536. }
  537. if count <= 0 {
  538. dataTime, _ := time.Parse(utils.FormatDate, date)
  539. timestamp := dataTime.UnixNano() / 1e6
  540. timeStr := fmt.Sprintf("%d", timestamp)
  541. addSql += "("
  542. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + date + "'" + "," + utils.SubFloatToString(val, 4) + "," + "'" + nowStr + "'" +
  543. "," + "'" + nowStr + "'" + "," + "1"
  544. addSql += "," + "'" + timeStr + "'"
  545. addSql += "),"
  546. isAdd = true
  547. } else {
  548. valStr := utils.SubFloatToString(val, 4)
  549. err = ModifyEdbDataCalculateLjzzy(int64(edbInfoId), date, valStr)
  550. if err != nil {
  551. return err
  552. }
  553. }
  554. }
  555. }
  556. }
  557. if isAdd {
  558. addSql = strings.TrimRight(addSql, ",")
  559. _, err = to.Raw(addSql).Exec()
  560. if err != nil {
  561. return err
  562. }
  563. }
  564. return
  565. }
  566. type EdbInfoCalculateLjzzyDetail struct {
  567. EdbInfoCalculateLjzzyId int `orm:"column(edb_info_calculate_ljzzy_id);pk"`
  568. EdbInfoId int `description:"指标id"`
  569. EdbCode string `description:"指标编码"`
  570. FromEdbInfoId int `description:"计算指标id"`
  571. FromEdbCode string `description:"计算指标编码"`
  572. FromEdbName string `description:"计算指标名称"`
  573. FromSource int `description:"计算指标来源"`
  574. FromSourceName string `description:"计算指标来源名称"`
  575. FromTag string `description:"来源指标标签"`
  576. Sort int `description:"计算指标名称排序"`
  577. CreateTime time.Time `description:"创建时间"`
  578. ModifyTime time.Time `description:"修改时间"`
  579. StartDate string `description:"开始日期"`
  580. EndDate string `description:"结束日期"`
  581. }
  582. func GetEdbInfoCalculateLjzzyDetail(edbInfoId int) (item *EdbInfoCalculateLjzzyDetail, err error) {
  583. o := orm.NewOrmUsingDB("data")
  584. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  585. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  586. WHERE a.edb_info_id=? `
  587. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  588. return
  589. }
  590. func GetEdbDataCalculateLjzzyByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  591. o := orm.NewOrmUsingDB("data")
  592. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_ljzzy WHERE edb_code=? AND data_time=? `
  593. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  594. return
  595. }
  596. func ModifyEdbDataCalculateLjzzy(edbInfoId int64, dataTime, value string) (err error) {
  597. o := orm.NewOrmUsingDB("data")
  598. sql := ` UPDATE edb_data_calculate_ljzzy SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  599. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  600. return
  601. }
  602. // 刷新全部数据
  603. func RefreshAllCalculateLjzzy(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  604. o := orm.NewOrmUsingDB("data")
  605. to, err := o.Begin()
  606. if err != nil {
  607. return
  608. }
  609. defer func() {
  610. if err != nil {
  611. _ = to.Rollback()
  612. } else {
  613. _ = to.Commit()
  614. }
  615. }()
  616. if err != nil {
  617. return
  618. }
  619. edbInfoIdStr := strconv.Itoa(edbInfoId)
  620. //计算数据
  621. var condition string
  622. var pars []interface{}
  623. condition += " AND edb_info_id=? "
  624. pars = append(pars, fromEdbInfo.EdbInfoId)
  625. if startDate != "" {
  626. condition += " AND data_time>=? "
  627. pars = append(pars, startDate)
  628. }
  629. if endDate != "" {
  630. condition += " AND data_time<=? "
  631. pars = append(pars, endDate)
  632. }
  633. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  634. if err != nil {
  635. return err
  636. }
  637. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  638. dataLen := len(dataList)
  639. for i := 0; i < dataLen; i++ {
  640. item := dataList[i]
  641. //日其中获取年
  642. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  643. if err != nil {
  644. return err
  645. }
  646. year := itemDate.Year()
  647. month := int(itemDate.Month())
  648. if monthMap, yok := yearMap[year]; yok {
  649. monthMap[month] = item
  650. yearMap[year] = monthMap
  651. } else {
  652. monthMap = make(map[int]*EdbInfoSearchData)
  653. monthMap[month] = item
  654. yearMap[year] = monthMap
  655. }
  656. }
  657. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  658. var isAdd bool
  659. //获取指标所有数据
  660. existDataList := make([]*EdbDataBase, 0)
  661. dataTableName := GetEdbDataTableName(source)
  662. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  663. sql = fmt.Sprintf(sql, dataTableName)
  664. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  665. if err != nil {
  666. return err
  667. }
  668. dataMap := make(map[string]string)
  669. for _, v := range existDataList {
  670. dataMap[v.DataTime] = v.Value
  671. }
  672. existDataMap := make(map[string]string)
  673. for yk, yv := range yearMap {
  674. _, oneMonthOk := yv[1]
  675. _, twoMonthOk := yv[2]
  676. if !oneMonthOk && !twoMonthOk {
  677. continue
  678. }
  679. for i := 1; i <= 12; i++ {
  680. fmt.Println(yk, i, yv[i])
  681. dataCurrentItem := yv[i]
  682. var date string
  683. var val float64
  684. if i == 1 || i == 2 {
  685. if _, mok := yv[1]; mok { //1月有值
  686. if i == 1 {
  687. date = dataCurrentItem.DataTime
  688. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  689. }
  690. if i == 2 {
  691. dataOneItem := yv[1]
  692. date = dataCurrentItem.DataTime
  693. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  694. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  695. val, _ = twoMonth.Sub(oneMonth).Float64()
  696. }
  697. } else { //1月无值
  698. dataTwoItem := yv[2]
  699. if i == 1 {
  700. date = strconv.Itoa(yk) + "-01-31"
  701. a := decimal.NewFromFloat(dataTwoItem.Value)
  702. b := decimal.NewFromFloat(2.0)
  703. val, _ = a.Div(b).Float64()
  704. }
  705. if i == 2 {
  706. date = dataCurrentItem.DataTime
  707. a := decimal.NewFromFloat(dataTwoItem.Value)
  708. b := decimal.NewFromFloat(2.0)
  709. val, _ = a.Div(b).Float64()
  710. }
  711. }
  712. } else {
  713. dataPreItem := yv[i-1]
  714. if dataCurrentItem != nil && dataPreItem != nil {
  715. date = dataCurrentItem.DataTime
  716. //val = dataCurrentItem.Value - dataPreItem.Value
  717. a := decimal.NewFromFloat(dataCurrentItem.Value)
  718. b := decimal.NewFromFloat(dataPreItem.Value)
  719. val, _ = a.Sub(b).Float64()
  720. }
  721. }
  722. if date != "" {
  723. saveValue := utils.SubFloatToString(val, 4)
  724. //判断数据是否存在
  725. if existVal, ok := dataMap[date]; !ok {
  726. dataTime, _ := time.Parse(utils.FormatDate, date)
  727. timestamp := dataTime.UnixNano() / 1e6
  728. timeStr := fmt.Sprintf("%d", timestamp)
  729. if _, existOk := existDataMap[date]; !existOk {
  730. addSql += GetAddSql(edbInfoIdStr, edbCode, date, timeStr, saveValue)
  731. isAdd = true
  732. }
  733. existDataMap[date] = date
  734. } else {
  735. if existVal != saveValue {
  736. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  737. sql = fmt.Sprintf(sql, dataTableName)
  738. _, err = to.Raw(sql, saveValue, edbInfoId, date).Exec()
  739. if err != nil {
  740. return err
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. if isAdd {
  748. addSql = strings.TrimRight(addSql, ",")
  749. _, err = to.Raw(addSql).Exec()
  750. if err != nil {
  751. fmt.Println("RefreshAllCalculateLjzzy add Err", err.Error())
  752. return
  753. }
  754. }
  755. return
  756. }