chart_info.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_chart_lib/utils"
  6. "github.com/rdlucklib/rdluck_tools/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type ChartInfo struct {
  13. ChartInfoId int `orm:"column(chart_info_id);pk"`
  14. ChartName string `description:"来源名称"`
  15. ChartClassifyId int `description:"图表分类id"`
  16. SysUserId int
  17. SysUserRealName string
  18. UniqueCode string `description:"图表唯一编码"`
  19. CreateTime time.Time
  20. ModifyTime time.Time
  21. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  22. StartDate string `description:"自定义开始日期"`
  23. EndDate string `description:"自定义结束日期"`
  24. IsSetName int `description:"设置名称"`
  25. EdbInfoIds string `description:"指标id"`
  26. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  27. Calendar string `description:"公历/农历"`
  28. SeasonStartDate string `description:"季节性图开始日期"`
  29. SeasonEndDate string `description:"季节性图开始日期"`
  30. ChartImage string `description:"图表图片"`
  31. Sort int `description:"排序字段,数字越小越排前面"`
  32. LeftMin string `description:"图表左侧最小值"`
  33. LeftMax string `description:"图表左侧最大值"`
  34. RightMin string `description:"图表右侧最小值"`
  35. RightMax string `description:"图表右侧最大值"`
  36. }
  37. func AddChartInfo(item *ChartInfo) (lastId int64, err error) {
  38. o := orm.NewOrm()
  39. o.Using("data")
  40. lastId, err = o.Insert(item)
  41. return
  42. }
  43. type ChartInfoItem struct {
  44. ChartInfoId int `description:"图表id"`
  45. ChartName string `description:"图表名称"`
  46. ChartClassifyId int `description:"图表分类"`
  47. }
  48. //用于分类展示
  49. func GetChartInfoAll() (items []*ChartClassifyItems, err error) {
  50. o := orm.NewOrm()
  51. o.Using("data")
  52. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
  53. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date
  54. FROM chart_info ORDER BY sort asc,create_time ASC `
  55. _, err = o.Raw(sql).QueryRows(&items)
  56. return
  57. }
  58. //用于分类展示
  59. func GetChartInfoAllList() (items []*ChartInfo, err error) {
  60. o := orm.NewOrm()
  61. o.Using("data")
  62. sql := ` SELECT *
  63. FROM chart_info ORDER BY create_time ASC `
  64. _, err = o.Raw(sql).QueryRows(&items)
  65. return
  66. }
  67. //图表检索数据
  68. type ChartInfoSearch struct {
  69. EdbCode string `description:"图表编码"`
  70. StartDate string `description:"起始日期"`
  71. EndDate string `description:"终止日期"`
  72. DataList []*EdbInfoSearchData
  73. }
  74. type ChartInfoSearchData struct {
  75. DataTime string `description:"数据日期"`
  76. Value float64 `description:"数据"`
  77. }
  78. type ChartInfoSearchResp struct {
  79. SearchItem *EdbInfoSearch `description:"图表分类"`
  80. Status int `description:"1:数据已存在于弘则数据库,2:新数据"`
  81. }
  82. func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
  83. o := orm.NewOrm()
  84. o.Using("data")
  85. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  86. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  87. return
  88. }
  89. func GetChartInfoViewById(chartInfoId int) (item *ChartInfoView, err error) {
  90. o := orm.NewOrm()
  91. o.Using("data")
  92. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  93. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  94. return
  95. }
  96. type SaveChartInfoReq struct {
  97. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  98. ChartInfoId int `description:"图表id,新增时传0"`
  99. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  100. StartDate string `description:"自定义开始日期"`
  101. EndDate string `description:"自定义结束日期"`
  102. Calendar string `description:"公历/农历"`
  103. SeasonStartDate string `description:"季节性图开始日期"`
  104. SeasonEndDate string `description:"季节性图开始日期"`
  105. LeftMin string `description:"图表左侧最小值"`
  106. LeftMax string `description:"图表左侧最大值"`
  107. RightMin string `description:"图表右侧最小值"`
  108. RightMax string `description:"图表右侧最大值"`
  109. }
  110. type ChartSaveItem struct {
  111. EdbInfoId int `description:"指标id"`
  112. MaxData float64 `description:"上限"`
  113. MinData float64 `description:"下限"`
  114. IsOrder bool `description:"true:正序,false:逆序"`
  115. IsAxis int `description:"1:左轴,0:右轴"`
  116. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  117. LeadValue int `description:"领先值"`
  118. LeadUnit string `description:"领先单位"`
  119. ChartStyle string `description:"图表类型"`
  120. ChartColor string `description:"颜色"`
  121. ChartWidth float64 `description:"线条大小"`
  122. }
  123. func DeleteChartInfoAndData(chartInfoId int) (err error) {
  124. o := orm.NewOrm()
  125. o.Using("data")
  126. defer func() {
  127. if err != nil {
  128. o.Rollback()
  129. } else {
  130. o.Commit()
  131. }
  132. }()
  133. sql := ` DELETE FROM chart_info WHERE chart_info_id=? `
  134. _, err = o.Raw(sql, chartInfoId).Exec()
  135. if err != nil {
  136. return
  137. }
  138. sql = ` DELETE FROM chart_edb_mapping WHERE chart_info_id=? `
  139. _, err = o.Raw(sql, chartInfoId).Exec()
  140. return
  141. }
  142. func GetChartInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  143. o := orm.NewOrm()
  144. o.Using("data")
  145. sql := ` SELECT COUNT(1) AS count FROM chart_info WHERE 1=1 `
  146. if condition != "" {
  147. sql += condition
  148. }
  149. err = o.Raw(sql, pars).QueryRow(&count)
  150. return
  151. }
  152. type EditChartInfoReq struct {
  153. ChartInfoId int `description:"图表ID"`
  154. ChartName string `description:"图表名称"`
  155. ChartClassifyId int `description:"分类id"`
  156. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  157. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  158. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  159. StartDate string `description:"自定义开始日期"`
  160. EndDate string `description:"自定义结束日期"`
  161. Calendar string `description:"公历/农历"`
  162. SeasonStartDate string `description:"季节性图开始日期"`
  163. SeasonEndDate string `description:"季节性图开始日期"`
  164. LeftMin string `description:"图表左侧最小值"`
  165. LeftMax string `description:"图表左侧最大值"`
  166. RightMin string `description:"图表右侧最小值"`
  167. RightMax string `description:"图表右侧最大值"`
  168. }
  169. func ModifyChartInfo(item *EditChartInfoReq) (err error) {
  170. o := orm.NewOrm()
  171. o.Using("data")
  172. sql := ` UPDATE chart_info
  173. SET
  174. chart_name =?,
  175. chart_classify_id = ?,
  176. is_set_name=1,
  177. modify_time = NOW()
  178. WHERE chart_info_id = ?`
  179. _, err = o.Raw(sql, item.ChartName, item.ChartClassifyId, item.ChartInfoId).Exec()
  180. return
  181. }
  182. type ChartInfoList struct {
  183. ChartInfoId int `orm:"column(chart_info_id);pk"`
  184. ChartName string `description:"来源名称"`
  185. ChartChartClassifyId int `description:"图表分类id"`
  186. SysUserId int
  187. SysUserRealName string
  188. UniqueCode string `description:"图表唯一编码"`
  189. CreateTime time.Time
  190. ModifyTime time.Time
  191. EdbInfoList []*EdbInfoList
  192. }
  193. type ChartInfoListResp struct {
  194. Paging *paging.PagingItem
  195. Item *ChartInfoList
  196. }
  197. func GetChartInfoByCondition(condition string, pars []interface{}) (item *ChartInfo, err error) {
  198. o := orm.NewOrm()
  199. o.Using("data")
  200. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  201. if condition != "" {
  202. sql += condition
  203. }
  204. err = o.Raw(sql, pars).QueryRow(&item)
  205. return
  206. }
  207. func GetChartInfoByNewest() (item *EdbInfoList, err error) {
  208. o := orm.NewOrm()
  209. o.Using("data")
  210. sql := ` SELECT * FROM chart_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  211. err = o.Raw(sql).QueryRow(&item)
  212. return
  213. }
  214. func ModifyChartInfoModifyTime(chartInfoId int64) (err error) {
  215. o := orm.NewOrm()
  216. o.Using("data")
  217. sql := ` UPDATE chart_info SET modify_time = NOW() WHERE chart_info_id = ? `
  218. _, err = o.Raw(sql, chartInfoId).Exec()
  219. return
  220. }
  221. type MoveChartInfoReq struct {
  222. ChartInfoId int `description:"图表ID"`
  223. PrevChartInfoId int `description:"上一个图表ID"`
  224. NextChartInfoId int `description:"下一个图表ID"`
  225. ChartClassifyId int `description:"分类id"`
  226. }
  227. func MoveChartInfo(chartInfoId, classifyId int) (err error) {
  228. o := orm.NewOrm()
  229. o.Using("data")
  230. sql := ` UPDATE chart_info
  231. SET
  232. chart_classify_id = ?
  233. WHERE chart_info_id = ?`
  234. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  235. return
  236. }
  237. type DeleteChartInfoReq struct {
  238. ChartInfoId int `description:"图表ID"`
  239. }
  240. type EdbDataList struct {
  241. EdbDataId int `description:" 指标数据ID"`
  242. EdbInfoId int `description:"指标ID"`
  243. DataTime string //`json:"-" description:"数据日期"`
  244. DataTimestamp int64 `description:"数据日期"`
  245. Value float64 `description:"数据值"`
  246. }
  247. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  248. tableName := GetEdbDataTableName(source)
  249. if tableName == "" {
  250. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  251. list = make([]*EdbDataList, 0)
  252. return list, err
  253. }
  254. var pars []interface{}
  255. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  256. if startDate != "" {
  257. sql += ` AND data_time>=? `
  258. pars = append(pars, startDate)
  259. }
  260. if endDate != "" {
  261. sql += ` AND data_time<=? `
  262. pars = append(pars, endDate)
  263. }
  264. sql += ` ORDER BY data_time ASC `
  265. sql = fmt.Sprintf(sql, tableName)
  266. o := orm.NewOrm()
  267. o.Using("data")
  268. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  269. return
  270. }
  271. func GetEdbDataLunarList(endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  272. tableName := "edb_data_quarter"
  273. sql := `SELECT edb_data_id,edb_info_id,data_time,TRUNCATE(value,2) AS value,data_timestamp FROM %s WHERE edb_info_id=? AND data_time>=? AND data_time<=? ORDER BY data_time ASC `
  274. sql = fmt.Sprintf(sql, tableName)
  275. o := orm.NewOrm()
  276. o.Using("data")
  277. _, err = o.Raw(sql, endInfoId, startDate, endDate).QueryRows(&list)
  278. return
  279. }
  280. func GetEdbDataListMinAndMax(source, endInfoId int, startDate, endDate string) (min_data, max_data float64, err error) {
  281. tableName := GetEdbDataTableName(source)
  282. sql := `SELECT min(value) AS min_data,max(value) AS max_data FROM %s WHERE edb_info_id=? `
  283. var pars []interface{}
  284. var condition string
  285. if startDate != "" {
  286. condition += ` AND data_time>=? `
  287. pars = append(pars, startDate)
  288. }
  289. if endDate != "" {
  290. condition += ` AND data_time<=? `
  291. pars = append(pars, endDate)
  292. }
  293. sql = fmt.Sprintf(sql, tableName)
  294. o := orm.NewOrm()
  295. o.Using("data")
  296. if condition != "" {
  297. sql += condition
  298. }
  299. err = o.Raw(sql, endInfoId, pars).QueryRow(&min_data, &max_data)
  300. return
  301. }
  302. type ChartEdbInfoMapping struct {
  303. EdbInfoId int `description:"指标id"`
  304. SourceName string `description:"来源名称"`
  305. Source int `description:"来源id"`
  306. EdbCode string `description:"指标编码"`
  307. EdbName string `description:"指标名称"`
  308. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  309. Frequency string `description:"频率"`
  310. Unit string `description:"单位"`
  311. StartDate string `description:"起始日期"`
  312. EndDate string `description:"终止日期"`
  313. ModifyTime string `description:"指标最后更新时间"`
  314. ChartEdbMappingId int `description:"图表指标id"`
  315. ChartInfoId int `description:"图表id"`
  316. MaxData float64 `description:"上限"`
  317. MinData float64 `description:"下限"`
  318. IsOrder bool `description:"true:正序,false:逆序"`
  319. IsAxis int `description:"1:左轴,0:右轴"`
  320. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  321. LeadValue int `description:"领先值"`
  322. LeadUnit string `description:"领先单位"`
  323. ChartStyle string `description:"图表类型"`
  324. ChartColor string `description:"颜色"`
  325. ChartWidth float64 `description:"线条大小"`
  326. LatestDate string `description:"数据最新日期"`
  327. LatestValue float64 `description:"数据最新值"`
  328. UniqueCode string `description:"指标唯一编码"`
  329. MinValue float64 `json:"-" description:"最小值"`
  330. MaxValue float64 `json:"-" description:"最大值"`
  331. DataList interface{}
  332. }
  333. type QuarterData struct {
  334. Year int
  335. DataList []*EdbDataList
  336. }
  337. type ChartEdbInfoDetailResp struct {
  338. EdbInfo *ChartEdbInfoMapping
  339. }
  340. type ChartInfoDetailResp struct {
  341. ChartInfo *ChartInfoView
  342. EdbInfoList []*ChartEdbInfoMapping
  343. }
  344. func ModifyChartInfoAndMapping(edbInfoIdStr string, req *SaveChartInfoReq, chartType int) (err error) {
  345. o := orm.NewOrm()
  346. o.Using("data")
  347. o.Begin()
  348. defer func() {
  349. if err != nil {
  350. o.Rollback()
  351. } else {
  352. o.Commit()
  353. }
  354. }()
  355. if chartType == 1 {
  356. sql := ` UPDATE chart_info
  357. SET
  358. edb_info_ids=?,
  359. modify_time = NOW(),
  360. date_type=?,
  361. start_date=?,
  362. end_date=?,
  363. left_min=?,
  364. left_max=?,
  365. right_min=?,
  366. right_max=?
  367. WHERE chart_info_id = ?`
  368. _, err = o.Raw(sql, edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Exec()
  369. if err != nil {
  370. fmt.Println("UPDATE chart_info Err:", err.Error())
  371. return err
  372. }
  373. } else {
  374. sql := ` UPDATE chart_info
  375. SET
  376. edb_info_ids=?,
  377. modify_time = NOW(),
  378. calendar=?,
  379. season_start_date=?,
  380. season_end_date=?,
  381. left_min=?,
  382. left_max=?,
  383. right_min=?,
  384. right_max=?
  385. WHERE chart_info_id = ?`
  386. _, err = o.Raw(sql, edbInfoIdStr, req.Calendar, req.SeasonStartDate, req.SeasonEndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Exec()
  387. if err != nil {
  388. fmt.Println("UPDATE chart_info Err:", err.Error())
  389. return err
  390. }
  391. }
  392. var edbInfoIdArr []string
  393. mapList := make([]*ChartEdbMapping, 0)
  394. for _, v := range req.ChartEdbInfoList {
  395. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  396. var count int
  397. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  398. err = o.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  399. if err != nil {
  400. fmt.Println("QueryRow Err:", err.Error())
  401. return err
  402. }
  403. if count > 0 {
  404. msql := `UPDATE chart_edb_mapping
  405. SET
  406. modify_time = NOW(),
  407. max_data = ?,
  408. min_data = ?,
  409. is_order = ?,
  410. is_axis = ?,
  411. edb_info_type = ?,
  412. lead_value = ?,
  413. lead_unit = ?,
  414. chart_style = ?,
  415. chart_color = ?,
  416. chart_width = ?
  417. WHERE chart_info_id =? AND edb_info_id=? `
  418. _, err = o.Raw(msql, v.MaxData, v.MinData, v.IsOrder, v.IsAxis, v.EdbInfoType, v.LeadValue, v.LeadUnit, v.ChartStyle, v.ChartColor, v.ChartWidth, req.ChartInfoId, v.EdbInfoId).Exec()
  419. if err != nil {
  420. fmt.Println("chart_edb_mapping Err:" + err.Error())
  421. return err
  422. }
  423. } else {
  424. mapItem := new(ChartEdbMapping)
  425. mapItem.ChartInfoId = req.ChartInfoId
  426. mapItem.EdbInfoId = v.EdbInfoId
  427. mapItem.CreateTime = time.Now()
  428. mapItem.ModifyTime = time.Now()
  429. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  430. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  431. mapItem.MaxData = v.MaxData
  432. mapItem.MinData = v.MinData
  433. mapItem.IsOrder = v.IsOrder
  434. mapItem.IsAxis = v.IsAxis
  435. mapItem.EdbInfoType = v.EdbInfoType
  436. mapItem.LeadValue = v.LeadValue
  437. mapItem.LeadUnit = v.LeadUnit
  438. mapItem.ChartStyle = v.ChartStyle
  439. mapItem.ChartColor = v.ChartColor
  440. mapItem.ChartWidth = v.ChartWidth
  441. mapList = append(mapList, mapItem)
  442. }
  443. }
  444. if len(mapList) > 0 {
  445. _, err = o.InsertMulti(1, mapList)
  446. if err != nil {
  447. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  448. return err
  449. }
  450. }
  451. if len(edbInfoIdArr) > 0 {
  452. edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
  453. if edbInfoIdStr != "" {
  454. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id NOT IN(` + edbInfoIdStr + `)`
  455. _, err = o.Raw(dsql, req.ChartInfoId).Exec()
  456. if err != nil {
  457. fmt.Println("delete err:" + err.Error())
  458. return err
  459. }
  460. }
  461. }
  462. return
  463. }
  464. func EditChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType int) (err error) {
  465. o := orm.NewOrm()
  466. o.Using("data")
  467. o.Begin()
  468. defer func() {
  469. if err != nil {
  470. o.Rollback()
  471. } else {
  472. o.Commit()
  473. }
  474. }()
  475. var pars []interface{}
  476. pars = append(pars, req.ChartName)
  477. pars = append(pars, edbInfoIdStr)
  478. pars = append(pars, req.ChartType)
  479. pars = append(pars, req.ChartClassifyId)
  480. sql := ` UPDATE chart_info
  481. SET
  482. chart_name =?,
  483. edb_info_ids=?,
  484. chart_type=?,
  485. chart_classify_id = ?,
  486. modify_time = NOW()
  487. `
  488. if calendar != "" {
  489. sql += `,calendar = ? `
  490. pars = append(pars, calendar)
  491. }
  492. if dateType > 0 {
  493. sql += `,date_type = ? `
  494. pars = append(pars, dateType)
  495. }
  496. sql += `,start_date = ? `
  497. pars = append(pars, req.StartDate)
  498. sql += `,end_date = ? `
  499. pars = append(pars, req.EndDate)
  500. sql += `,season_start_date = ? `
  501. pars = append(pars, req.SeasonStartDate)
  502. sql += `,season_end_date = ? `
  503. pars = append(pars, req.SeasonEndDate)
  504. sql += `,left_min = ? `
  505. pars = append(pars, req.LeftMin)
  506. sql += `,left_max = ? `
  507. pars = append(pars, req.LeftMax)
  508. sql += `,right_min = ? `
  509. pars = append(pars, req.RightMin)
  510. sql += `,right_max = ? `
  511. pars = append(pars, req.RightMax)
  512. sql += `WHERE chart_info_id = ?`
  513. pars = append(pars, req.ChartInfoId)
  514. _, err = o.Raw(sql, pars).Exec()
  515. if err != nil {
  516. fmt.Println("UPDATE chart_info Err:", err.Error())
  517. return err
  518. }
  519. var edbInfoIdArr []string
  520. mapList := make([]*ChartEdbMapping, 0)
  521. for _, v := range req.ChartEdbInfoList {
  522. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  523. var count int
  524. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  525. err = o.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  526. if err != nil {
  527. fmt.Println("QueryRow Err:", err.Error())
  528. return err
  529. }
  530. if count > 0 {
  531. msql := `UPDATE chart_edb_mapping
  532. SET
  533. modify_time = NOW(),
  534. max_data = ?,
  535. min_data = ?,
  536. is_order = ?,
  537. is_axis = ?,
  538. edb_info_type = ?,
  539. lead_value = ?,
  540. lead_unit = ?,
  541. chart_style = ?,
  542. chart_color = ?,
  543. chart_width = ?
  544. WHERE chart_info_id =? AND edb_info_id=? `
  545. _, err = o.Raw(msql, v.MaxData, v.MinData, v.IsOrder, v.IsAxis, v.EdbInfoType, v.LeadValue, v.LeadUnit, v.ChartStyle, v.ChartColor, v.ChartWidth, req.ChartInfoId, v.EdbInfoId).Exec()
  546. if err != nil {
  547. fmt.Println("chart_edb_mapping Err:" + err.Error())
  548. return err
  549. }
  550. } else {
  551. mapItem := new(ChartEdbMapping)
  552. mapItem.ChartInfoId = req.ChartInfoId
  553. mapItem.EdbInfoId = v.EdbInfoId
  554. mapItem.CreateTime = time.Now()
  555. mapItem.ModifyTime = time.Now()
  556. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  557. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  558. mapItem.MaxData = v.MaxData
  559. mapItem.MinData = v.MinData
  560. mapItem.IsOrder = v.IsOrder
  561. mapItem.IsAxis = v.IsAxis
  562. mapItem.EdbInfoType = v.EdbInfoType
  563. mapItem.LeadValue = v.LeadValue
  564. mapItem.LeadUnit = v.LeadUnit
  565. mapItem.ChartStyle = v.ChartStyle
  566. mapItem.ChartColor = v.ChartColor
  567. mapItem.ChartWidth = v.ChartWidth
  568. mapList = append(mapList, mapItem)
  569. }
  570. }
  571. if len(mapList) > 0 {
  572. _, err = o.InsertMulti(1, mapList)
  573. if err != nil {
  574. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  575. return err
  576. }
  577. }
  578. if len(edbInfoIdArr) > 0 {
  579. edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
  580. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id NOT IN(` + edbInfoIdStr + `)`
  581. _, err = o.Raw(dsql, req.ChartInfoId).Exec()
  582. if err != nil {
  583. fmt.Println("delete err:" + err.Error())
  584. return err
  585. }
  586. }
  587. return
  588. }
  589. func ModifyEdbDatadTimestamp(source, edbDataId int, dataTimestamp int64) (err error) {
  590. tableName := GetEdbDataTableName(source)
  591. sql := `UPDATE %s SET data_timestamp=? WHERE edb_data_id=? `
  592. sql = fmt.Sprintf(sql, tableName)
  593. o := orm.NewOrm()
  594. o.Using("data")
  595. _, err = o.Raw(sql, dataTimestamp, edbDataId).Exec()
  596. return
  597. }
  598. type AddChartInfoReq struct {
  599. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  600. ChartClassifyId int `description:"分类id"`
  601. ChartName string `description:"图表名称"`
  602. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  603. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  604. StartDate string `description:"自定义开始日期"`
  605. EndDate string `description:"自定义结束日期"`
  606. Calendar string `description:"公历/农历"`
  607. SeasonStartDate string `description:"季节性图开始日期"`
  608. SeasonEndDate string `description:"季节性图开始日期"`
  609. LeftMin string `description:"图表左侧最小值"`
  610. LeftMax string `description:"图表左侧最大值"`
  611. RightMin string `description:"图表右侧最小值"`
  612. RightMax string `description:"图表右侧最大值"`
  613. }
  614. type AddChartInfoResp struct {
  615. ChartInfoId int `description:"图表id"`
  616. UniqueCode string `description:"图表唯一编码"`
  617. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  618. }
  619. //判断图表指标是否已经存在
  620. func ChartInfoExist(condition, edbInfoIdStr string) (count int, err error) {
  621. sql := `SELECT COUNT(1) AS count FROM (
  622. SELECT GROUP_CONCAT(edb_info_id ORDER BY edb_info_id ASC SEPARATOR ',') AS edb_info_id_str
  623. FROM chart_edb_mapping AS a
  624. WHERE 1=1`
  625. if condition != "" {
  626. sql += condition
  627. }
  628. sql += ` GROUP BY chart_info_id
  629. )AS t
  630. WHERE t.edb_info_id_str=?
  631. GROUP BY t.edb_info_id_str `
  632. o := orm.NewOrm()
  633. o.Using("data")
  634. err = o.Raw(sql, edbInfoIdStr).QueryRow(&count)
  635. return
  636. }
  637. func ChartInfoSearchByKeyWord(KeyWord string) (searchList []*ChartInfo, err error) {
  638. o := orm.NewOrm()
  639. o.Using("data")
  640. sql := ` SELECT * FROM chart_info `
  641. if KeyWord != "" {
  642. sql += `WHERE chart_name LIKE '%` + KeyWord + `%' `
  643. }
  644. sql += ` ORDER BY create_time DESC `
  645. if KeyWord == "" {
  646. sql += ` LIMIT 100 `
  647. }
  648. _, err = o.Raw(sql).QueryRows(&searchList)
  649. return
  650. }
  651. func GetNextChartInfo(classifyId int) (item *ChartInfo, err error) {
  652. o := orm.NewOrm()
  653. o.Using("data")
  654. sql := ` SELECT b.* FROM chart_classify AS a
  655. INNER JOIN chart_info AS b ON a.chart_classify_id=b.chart_classify_id
  656. WHERE a.chart_classify_id>?
  657. ORDER BY a.chart_classify_id ASC
  658. LIMIT 1 `
  659. err = o.Raw(sql, classifyId).QueryRow(&item)
  660. return
  661. }
  662. func GetChartInfoRefreshData(chartInfoId int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  663. o := orm.NewOrm()
  664. o.Using("data")
  665. sql := ` SELECT b.* FROM chart_edb_mapping AS a
  666. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  667. WHERE a.chart_info_id=? `
  668. edbInfoList := make([]*EdbInfo, 0)
  669. _, err = o.Raw(sql, chartInfoId).QueryRows(&edbInfoList)
  670. if err != nil {
  671. return
  672. }
  673. for _, v := range edbInfoList {
  674. fmt.Println(v.EdbInfoId, v.EdbType)
  675. if v.EdbType == 1 {
  676. baseEdbInfoArr = append(baseEdbInfoArr, v)
  677. } else {
  678. calculateInfoArr = append(calculateInfoArr, v)
  679. newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
  680. if err != nil {
  681. return baseEdbInfoArr, calculateInfoArr, err
  682. }
  683. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  684. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  685. }
  686. }
  687. return
  688. }
  689. func GetChartRefreshEdbInfo(edbInfoId, source, n int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  690. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  691. if err != nil && err.Error() != utils.ErrNoRow() {
  692. return
  693. }
  694. n++
  695. for _, item := range calculateList {
  696. fmt.Println(item.EdbInfoId)
  697. if item.EdbType == 1 {
  698. baseEdbInfoArr = append(baseEdbInfoArr, item)
  699. } else {
  700. calculateInfoArr = append(calculateInfoArr, item)
  701. if n > 10 {
  702. return
  703. }
  704. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetChartRefreshEdbInfo(item.EdbInfoId, item.Source, n)
  705. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  706. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  707. }
  708. }
  709. return
  710. }
  711. type SetChartInfoImageReq struct {
  712. ChartInfoId int `description:"图表ID"`
  713. ImageUrl string `description:"图表图片地址"`
  714. }
  715. func EditChartInfoImage(req *SetChartInfoImageReq) (err error) {
  716. o := orm.NewOrm()
  717. o.Using("data")
  718. sql := ` UPDATE chart_info SET chart_image=?, modify_time = NOW() WHERE chart_info_id = ? `
  719. _, err = o.Raw(sql, req.ImageUrl, req.ChartInfoId).Exec()
  720. if err != nil {
  721. fmt.Println("EditChartInfoImage Err:", err.Error())
  722. return err
  723. }
  724. return
  725. }
  726. type ChartInfoDetailFromUniqueCodeResp struct {
  727. ChartInfo *ChartInfoView
  728. Status bool `description:"true:图表存在,false:图表不存在"`
  729. EdbInfoList []*ChartEdbInfoMapping
  730. }
  731. func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfoView, err error) {
  732. o := orm.NewOrm()
  733. o.Using("data")
  734. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  735. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  736. return
  737. }
  738. // GetFirstChartInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  739. func GetFirstChartInfoByClassifyId(classifyId int) (item *ChartInfo, err error) {
  740. o := orm.NewOrm()
  741. o.Using("data")
  742. sql := ` SELECT * FROM chart_info WHERE chart_classify_id=? order by sort asc,chart_info_id asc limit 1`
  743. err = o.Raw(sql, classifyId).QueryRow(&item)
  744. return
  745. }
  746. // UpdateChartInfoSortByClassifyId 根据图表id更新排序
  747. func UpdateChartInfoSortByClassifyId(classifyId, nowSort, prevChartInfoId int, updateSort string) (err error) {
  748. o := orm.NewOrm()
  749. o.Using("data")
  750. sql := ` update chart_info set sort = ` + updateSort + ` WHERE chart_classify_id=? and sort > ? `
  751. if prevChartInfoId > 0 {
  752. sql += ` or (chart_info_id > ` + fmt.Sprint(prevChartInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  753. }
  754. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  755. return
  756. }
  757. // Update 更新图表基础信息
  758. func (chartInfo *ChartInfo) Update(cols []string) (err error) {
  759. o := orm.NewOrm()
  760. o.Using("data")
  761. _, err = o.Update(chartInfo, cols...)
  762. return
  763. }
  764. type ChartInfoView struct {
  765. ChartInfoId int `orm:"column(chart_info_id);pk"`
  766. ChartName string `description:"来源名称"`
  767. ChartClassifyId int `description:"图表分类id"`
  768. ChartClassifyName string `description:"图表名称"`
  769. SysUserId int
  770. SysUserRealName string
  771. UniqueCode string `description:"图表唯一编码"`
  772. CreateTime time.Time
  773. ModifyTime time.Time
  774. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  775. StartDate string `description:"自定义开始日期"`
  776. EndDate string `description:"自定义结束日期"`
  777. IsSetName int `description:"设置名称"`
  778. EdbInfoIds string `description:"指标id"`
  779. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  780. Calendar string `description:"公历/农历"`
  781. SeasonStartDate string `description:"季节性图开始日期"`
  782. SeasonEndDate string `description:"季节性图开始日期"`
  783. ChartImage string `description:"图表图片"`
  784. Sort int `description:"排序字段,数字越小越排前面"`
  785. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  786. MyChartId int
  787. MyChartClassifyId string `description:"我的图表分类,多个用逗号隔开"`
  788. ChartClassify []*ChartClassifyView
  789. EdbEndDate string `description:"指标最新更新日期"`
  790. LeftMin string `description:"图表左侧最小值"`
  791. LeftMax string `description:"图表左侧最大值"`
  792. RightMin string `description:"图表右侧最小值"`
  793. RightMax string `description:"图表右侧最大值"`
  794. }
  795. type ImageSvgToPngResp struct {
  796. Data struct {
  797. ResourceURL string `json:"ResourceUrl"`
  798. } `json:"Data"`
  799. ErrCode string `json:"ErrCode"`
  800. ErrMsg string `json:"ErrMsg"`
  801. IsSendEmail bool `json:"IsSendEmail"`
  802. Msg string `json:"Msg"`
  803. Ret int64 `json:"Ret"`
  804. Success bool `json:"Success"`
  805. }
  806. // GetChartInfoByClassifyIdAndName 根据分类id和图表名获取图表信息
  807. func GetChartInfoByClassifyIdAndName(classifyId int, chartName string) (item *ChartInfo, err error) {
  808. o := orm.NewOrm()
  809. o.Using("data")
  810. sql := ` SELECT * FROM chart_info WHERE chart_classify_id = ? and chart_name=? `
  811. err = o.Raw(sql, classifyId, chartName).QueryRow(&item)
  812. return
  813. }