chart_info.go 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/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. ChartNameEn string `description:"英文图表名称"`
  16. ChartClassifyId int `description:"图表分类id"`
  17. SysUserId int
  18. SysUserRealName string
  19. UniqueCode string `description:"图表唯一编码"`
  20. CreateTime time.Time
  21. ModifyTime time.Time
  22. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  23. StartDate string `description:"自定义开始日期"`
  24. EndDate string `description:"自定义结束日期"`
  25. IsSetName int `description:"设置名称"`
  26. EdbInfoIds string `description:"指标id"`
  27. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图"`
  28. Calendar string `description:"公历/农历"`
  29. SeasonStartDate string `description:"季节性图开始日期"`
  30. SeasonEndDate string `description:"季节性图开始日期"`
  31. ChartImage string `description:"图表图片"`
  32. Sort int `description:"排序字段,数字越小越排前面"`
  33. LeftMin string `description:"图表左侧最小值"`
  34. LeftMax string `description:"图表左侧最大值"`
  35. RightMin string `description:"图表右侧最小值"`
  36. RightMax string `description:"图表右侧最大值"`
  37. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  38. BarConfig string `description:"柱方图的配置,json数据"`
  39. Source int `description:"1:ETA图库;2:商品价格曲线"`
  40. ExtraConfig string `description:"图表额外配置,json数据"`
  41. }
  42. type ChartInfoMore struct {
  43. ChartInfo
  44. IsEnChart bool `description:"是否展示英文标识"`
  45. }
  46. func AddChartInfo(item *ChartInfo) (lastId int64, err error) {
  47. o := orm.NewOrmUsingDB("data")
  48. lastId, err = o.Insert(item)
  49. return
  50. }
  51. type ChartInfoItem struct {
  52. ChartInfoId int `description:"图表id"`
  53. ChartName string `description:"图表名称"`
  54. ChartClassifyId int `description:"图表分类"`
  55. }
  56. // GetChartInfoAll 用于分类展示
  57. // @param source int 1:ETA图库;2:商品价格曲线
  58. func GetChartInfoAll(sourceList []int) (items []*ChartClassifyItems, err error) {
  59. num := len(sourceList)
  60. if num <= 0 {
  61. return
  62. }
  63. o := orm.NewOrmUsingDB("data")
  64. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
  65. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date,source
  66. FROM chart_info WHERE source in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort asc,create_time ASC `
  67. _, err = o.Raw(sql, sourceList).QueryRows(&items)
  68. return
  69. }
  70. // GetChartInfoByAdminId 获取所有我创建的图表,用于分类展示
  71. func GetChartInfoByAdminId(sourceList []int, adminId int) (items []*ChartClassifyItems, err error) {
  72. num := len(sourceList)
  73. if num <= 0 {
  74. return
  75. }
  76. o := orm.NewOrmUsingDB("data")
  77. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
  78. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date
  79. FROM chart_info where source in (` + utils.GetOrmInReplace(num) + `) AND sys_user_id = ? ORDER BY sort asc,create_time ASC `
  80. _, err = o.Raw(sql, sourceList, adminId).QueryRows(&items)
  81. return
  82. }
  83. // 用于分类展示
  84. func GetChartInfoAllList() (items []*ChartInfo, err error) {
  85. o := orm.NewOrmUsingDB("data")
  86. sql := ` SELECT *
  87. FROM chart_info ORDER BY create_time ASC `
  88. _, err = o.Raw(sql).QueryRows(&items)
  89. return
  90. }
  91. // 图表检索数据
  92. type ChartInfoSearch struct {
  93. EdbCode string `description:"图表编码"`
  94. StartDate string `description:"起始日期"`
  95. EndDate string `description:"终止日期"`
  96. DataList []*EdbInfoSearchData
  97. }
  98. type ChartInfoSearchData struct {
  99. DataTime string `description:"数据日期"`
  100. Value float64 `description:"数据"`
  101. }
  102. type ChartInfoSearchResp struct {
  103. SearchItem *EdbInfoSearch `description:"图表分类"`
  104. Status int `description:"1:数据已存在于弘则数据库,2:新数据"`
  105. }
  106. func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  109. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  110. return
  111. }
  112. func GetChartInfoViewById(chartInfoId int) (item *ChartInfoView, err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  115. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  116. return
  117. }
  118. func GetChartInfoViewByIds(chartInfoIds []string) (list []*ChartInfoView, err error) {
  119. o := orm.NewOrmUsingDB("data")
  120. sql := ` SELECT * FROM chart_info WHERE chart_info_id in ("` + strings.Join(chartInfoIds, `","`) + `") `
  121. _, err = o.Raw(sql).QueryRows(&list)
  122. return
  123. }
  124. type SaveChartInfoReq struct {
  125. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  126. ChartInfoId int `description:"图表id,新增时传0"`
  127. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  128. StartDate string `description:"自定义开始日期"`
  129. EndDate string `description:"自定义结束日期"`
  130. Calendar string `description:"公历/农历"`
  131. SeasonStartDate string `description:"季节性图开始日期"`
  132. SeasonEndDate string `description:"季节性图开始日期"`
  133. LeftMin string `description:"图表左侧最小值"`
  134. LeftMax string `description:"图表左侧最大值"`
  135. RightMin string `description:"图表右侧最小值"`
  136. RightMax string `description:"图表右侧最大值"`
  137. ExtraConfig string `description:"图表额外配置,json数据"`
  138. }
  139. type ChartSaveItem struct {
  140. EdbInfoId int `description:"指标id"`
  141. MaxData float64 `description:"上限"`
  142. MinData float64 `description:"下限"`
  143. IsOrder bool `description:"true:正序,false:逆序"`
  144. IsAxis int `description:"1:左轴,0:右轴"`
  145. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  146. LeadValue int `description:"领先值"`
  147. LeadUnit string `description:"领先单位"`
  148. ChartStyle string `description:"图表类型"`
  149. ChartColor string `description:"颜色"`
  150. PredictChartColor string `description:"预测数据的颜色"`
  151. ChartWidth float64 `description:"线条大小"`
  152. Source int `description:"1:ETA图库;2:商品价格曲线"`
  153. }
  154. func DeleteChartInfoAndData(chartInfoId int) (err error) {
  155. o := orm.NewOrmUsingDB("data")
  156. to, err := o.Begin()
  157. if err != nil {
  158. return
  159. }
  160. defer func() {
  161. if err != nil {
  162. _ = to.Rollback()
  163. } else {
  164. _ = to.Commit()
  165. }
  166. }()
  167. sql := ` DELETE FROM chart_info WHERE chart_info_id=? `
  168. _, err = to.Raw(sql, chartInfoId).Exec()
  169. if err != nil {
  170. return
  171. }
  172. sql = ` DELETE FROM chart_edb_mapping WHERE chart_info_id=? `
  173. _, err = to.Raw(sql, chartInfoId).Exec()
  174. return
  175. }
  176. func GetChartInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  177. o := orm.NewOrmUsingDB("data")
  178. sql := ` SELECT COUNT(1) AS count FROM chart_info WHERE 1=1 `
  179. if condition != "" {
  180. sql += condition
  181. }
  182. err = o.Raw(sql, pars).QueryRow(&count)
  183. return
  184. }
  185. type EditChartInfoReq struct {
  186. ChartInfoId int `description:"图表ID"`
  187. ChartName string `description:"图表名称"`
  188. ChartClassifyId int `description:"分类id"`
  189. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  190. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图,10:截面散点图"`
  191. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  192. StartDate string `description:"自定义开始日期"`
  193. EndDate string `description:"自定义结束日期"`
  194. Calendar string `description:"公历/农历"`
  195. SeasonStartDate string `description:"季节性图开始日期"`
  196. SeasonEndDate string `description:"季节性图开始日期"`
  197. LeftMin string `description:"图表左侧最小值"`
  198. LeftMax string `description:"图表左侧最大值"`
  199. RightMin string `description:"图表右侧最小值"`
  200. RightMax string `description:"图表右侧最大值"`
  201. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  202. CorrelationChartInfo CorrelationChartInfoReq `description:"相关性图表配置"`
  203. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  204. }
  205. type EditChartEnInfoReq struct {
  206. ChartInfoId int `description:"图表ID"`
  207. ChartNameEn string `description:"英文图表名称"`
  208. ChartEdbInfoList []*EditChartEnInfoEdbItem `description:"指标及配置信息"`
  209. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  210. }
  211. type EditChartEnInfoEdbItem struct {
  212. EdbInfoId int `description:"指标ID"`
  213. EdbNameEn string `description:"英文指标名称"`
  214. UnitEn string `description:"英文单位"`
  215. }
  216. func ModifyChartInfo(item *EditChartInfoReq) (err error) {
  217. o := orm.NewOrmUsingDB("data")
  218. sql := ` UPDATE chart_info
  219. SET
  220. chart_name =?,
  221. chart_classify_id = ?,
  222. is_set_name=1,
  223. modify_time = NOW()
  224. WHERE chart_info_id = ?`
  225. _, err = o.Raw(sql, item.ChartName, item.ChartClassifyId, item.ChartInfoId).Exec()
  226. return
  227. }
  228. type ChartInfoList struct {
  229. ChartInfoId int `orm:"column(chart_info_id);pk"`
  230. ChartName string `description:"来源名称"`
  231. ChartChartClassifyId int `description:"图表分类id"`
  232. SysUserId int
  233. SysUserRealName string
  234. UniqueCode string `description:"图表唯一编码"`
  235. CreateTime time.Time
  236. ModifyTime time.Time
  237. EdbInfoList []*EdbInfoList
  238. }
  239. type ChartInfoListResp struct {
  240. Paging *paging.PagingItem
  241. Item *ChartInfoList
  242. }
  243. func GetChartInfoByCondition(condition string, pars []interface{}) (item *ChartInfo, err error) {
  244. o := orm.NewOrmUsingDB("data")
  245. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  246. if condition != "" {
  247. sql += condition
  248. }
  249. err = o.Raw(sql, pars).QueryRow(&item)
  250. return
  251. }
  252. func GetChartInfoByNewest(source int) (item *ChartInfoList, err error) {
  253. o := orm.NewOrmUsingDB("data")
  254. sql := ` SELECT * FROM chart_info WHERE 1=1 AND source = ? ORDER BY modify_time DESC LIMIT 1 `
  255. err = o.Raw(sql, source).QueryRow(&item)
  256. return
  257. }
  258. func ModifyChartInfoModifyTime(chartInfoId int64) (err error) {
  259. o := orm.NewOrmUsingDB("data")
  260. sql := ` UPDATE chart_info SET modify_time = NOW() WHERE chart_info_id = ? `
  261. _, err = o.Raw(sql, chartInfoId).Exec()
  262. return
  263. }
  264. type MoveChartInfoReq struct {
  265. ChartInfoId int `description:"图表ID"`
  266. PrevChartInfoId int `description:"上一个图表ID"`
  267. NextChartInfoId int `description:"下一个图表ID"`
  268. ChartClassifyId int `description:"分类id"`
  269. }
  270. func MoveChartInfo(chartInfoId, classifyId int) (err error) {
  271. o := orm.NewOrmUsingDB("data")
  272. sql := ` UPDATE chart_info
  273. SET
  274. chart_classify_id = ?
  275. WHERE chart_info_id = ?`
  276. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  277. return
  278. }
  279. type DeleteChartInfoReq struct {
  280. ChartInfoId int `description:"图表ID"`
  281. }
  282. type EdbDataList struct {
  283. EdbDataId int `description:" 指标数据ID"`
  284. EdbInfoId int `description:"指标ID"`
  285. DataTime string //`json:"-" description:"数据日期"`
  286. DataTimestamp int64 `description:"数据日期"`
  287. Value float64 `description:"数据值"`
  288. }
  289. func GetEdbDataList(source, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  290. tableName := GetEdbDataTableName(source)
  291. if tableName == "" {
  292. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  293. list = make([]*EdbDataList, 0)
  294. return list, err
  295. }
  296. var pars []interface{}
  297. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  298. if startDate != "" {
  299. sql += ` AND data_time>=? `
  300. pars = append(pars, startDate)
  301. }
  302. if endDate != "" {
  303. sql += ` AND data_time<=? `
  304. pars = append(pars, endDate)
  305. }
  306. sql += ` ORDER BY data_time ASC `
  307. sql = fmt.Sprintf(sql, tableName)
  308. o := orm.NewOrmUsingDB("data")
  309. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  310. return
  311. }
  312. type EdbDataListByUniqueCode struct {
  313. EdbDataId int `description:" 指标数据ID" json:"-"`
  314. EdbInfoId int `description:"指标ID" json:"-"`
  315. DataTime string //`json:"-" description:"数据日期"`
  316. DataTimestamp int64 `description:"数据日期"`
  317. Value float64 `description:"数据值"`
  318. }
  319. func GetEdbDataListByByUniqueCode(source, endInfoId int, startDate, endDate string) (list []*EdbDataListByUniqueCode, err error) {
  320. tableName := GetEdbDataTableName(source)
  321. if tableName == "" {
  322. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  323. list = make([]*EdbDataListByUniqueCode, 0)
  324. return list, err
  325. }
  326. var pars []interface{}
  327. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  328. if startDate != "" {
  329. sql += ` AND data_time>=? `
  330. pars = append(pars, startDate)
  331. }
  332. if endDate != "" {
  333. sql += ` AND data_time<=? `
  334. pars = append(pars, endDate)
  335. }
  336. sql += ` ORDER BY data_time ASC `
  337. sql = fmt.Sprintf(sql, tableName)
  338. o := orm.NewOrmUsingDB("data")
  339. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  340. return
  341. }
  342. func GetEdbDataLunarList(endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  343. tableName := "edb_data_quarter"
  344. 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 `
  345. sql = fmt.Sprintf(sql, tableName)
  346. o := orm.NewOrmUsingDB("data")
  347. _, err = o.Raw(sql, endInfoId, startDate, endDate).QueryRows(&list)
  348. return
  349. }
  350. func GetEdbDataListMinAndMax(source, endInfoId int, startDate, endDate string) (min_data, max_data float64, err error) {
  351. tableName := GetEdbDataTableName(source)
  352. sql := `SELECT min(value) AS min_data,max(value) AS max_data FROM %s WHERE edb_info_id=? `
  353. var pars []interface{}
  354. var condition string
  355. if startDate != "" {
  356. condition += ` AND data_time>=? `
  357. pars = append(pars, startDate)
  358. }
  359. if endDate != "" {
  360. condition += ` AND data_time<=? `
  361. pars = append(pars, endDate)
  362. }
  363. sql = fmt.Sprintf(sql, tableName)
  364. o := orm.NewOrmUsingDB("data")
  365. if condition != "" {
  366. sql += condition
  367. }
  368. err = o.Raw(sql, endInfoId, pars).QueryRow(&min_data, &max_data)
  369. return
  370. }
  371. type ChartEdbInfoMapping struct {
  372. EdbInfoId int `description:"指标id"`
  373. SourceName string `description:"来源名称"`
  374. Source int `description:"来源id"`
  375. EdbCode string `description:"指标编码"`
  376. EdbName string `description:"指标名称"`
  377. EdbAliasName string `description:"指标名称(别名)"`
  378. EdbNameEn string `description:"英文指标名称"`
  379. EdbAliasNameEn string `description:"英文指标名称(别名)"`
  380. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  381. Frequency string `description:"频率"`
  382. FrequencyEn string `description:"英文频率"`
  383. Unit string `description:"单位"`
  384. UnitEn string `description:"英文单位"`
  385. StartDate string `description:"起始日期"`
  386. EndDate string `description:"终止日期"`
  387. ModifyTime string `description:"指标最后更新时间"`
  388. ChartEdbMappingId int `description:"图表指标id"`
  389. ChartInfoId int `description:"图表id"`
  390. MaxData float64 `description:"上限"`
  391. MinData float64 `description:"下限"`
  392. IsOrder bool `description:"true:正序,false:逆序"`
  393. IsAxis int `description:"1:左轴,0:右轴"`
  394. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  395. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  396. LeadValue int `description:"领先值"`
  397. LeadUnit string `description:"领先单位"`
  398. LeadUnitEn string `description:"领先英文单位"`
  399. ChartStyle string `description:"图表类型"`
  400. ChartColor string `description:"颜色"`
  401. PredictChartColor string `description:"预测数据的颜色"`
  402. ChartWidth float64 `description:"线条大小"`
  403. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图"`
  404. LatestDate string `description:"数据最新日期"`
  405. LatestValue float64 `description:"数据最新值"`
  406. MoveLatestDate string `description:"移动后的数据最新日期"`
  407. UniqueCode string `description:"指标唯一编码"`
  408. MinValue float64 `json:"-" description:"最小值"`
  409. MaxValue float64 `json:"-" description:"最大值"`
  410. DataList interface{}
  411. IsNullData bool `json:"-" description:"是否空数据"`
  412. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  413. RegionType string `description:"交易所来源,海外还是国内" json:"-"`
  414. ClassifyId int `description:"分类id"`
  415. }
  416. type QuarterData struct {
  417. Year int
  418. DataList []*EdbDataList
  419. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  420. }
  421. type QuarterDataByUniqueCode struct {
  422. Year int
  423. DataList []*EdbDataListByUniqueCode
  424. }
  425. type ChartEdbInfoDetailResp struct {
  426. EdbInfo *ChartEdbInfoMapping
  427. }
  428. type ChartInfoDetailResp struct {
  429. ChartInfo *ChartInfoView
  430. EdbInfoList []*ChartEdbInfoMapping
  431. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  432. YDataList []YData `description:"柱方图的y轴数据"`
  433. XDataList []XData `description:"商品价格曲线的X轴数据"`
  434. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  435. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  436. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  437. }
  438. // XData 商品价格曲线的的x轴数据
  439. type XData struct {
  440. Name string `description:"别名"`
  441. NameEn string `description:"英文别名"`
  442. }
  443. // YData 柱方图的y轴数据
  444. type YData struct {
  445. Date string `description:"数据日期"`
  446. ConfigDate time.Time `description:"配置的日期" json:"-"`
  447. Color string `description:"数据颜色"`
  448. Name string `description:"别名"`
  449. NameEn string `description:"英文别名"`
  450. Value []float64 `description:"每个指标的值"`
  451. NoDataEdbList []int `description:"没有数据的指标列表"`
  452. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  453. NameList []string `description:"每个值对应的名称"`
  454. EnNameList []string `description:"每个值对应的英文名称"`
  455. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  456. M []int `description:"对应开始日期的间隔值" json:"-"`
  457. Unit string `description:"中文单位名称"`
  458. UnitEn string `description:"英文单位名称"`
  459. }
  460. func ModifyChartInfoAndMapping(edbInfoIdStr string, req *SaveChartInfoReq, chartType int) (err error) {
  461. o := orm.NewOrmUsingDB("data")
  462. to, err := o.Begin()
  463. if err != nil {
  464. return
  465. }
  466. defer func() {
  467. if err != nil {
  468. _ = to.Rollback()
  469. } else {
  470. _ = to.Commit()
  471. }
  472. }()
  473. //非季节图
  474. if chartType != 2 {
  475. updateStr := `edb_info_ids=?,
  476. modify_time = NOW(),
  477. date_type=?,
  478. start_date=?,
  479. end_date=?,
  480. left_min=?,
  481. left_max=?,
  482. right_min=?,
  483. right_max=?`
  484. pars := []interface{}{edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax}
  485. if req.ExtraConfig != `` {
  486. updateStr += `,extra_config=? `
  487. pars = append(pars, req.ExtraConfig)
  488. }
  489. pars = append(pars, req.ChartInfoId)
  490. sql := ` UPDATE chart_info SET ` + updateStr + ` WHERE chart_info_id = ?`
  491. _, err = to.Raw(sql, pars).Exec()
  492. if err != nil {
  493. fmt.Println("UPDATE chart_info Err:", err.Error())
  494. return err
  495. }
  496. } else {
  497. sql := ` UPDATE chart_info
  498. SET
  499. edb_info_ids=?,
  500. modify_time = NOW(),
  501. calendar=?,
  502. season_start_date=?,
  503. season_end_date=?,
  504. left_min=?,
  505. left_max=?,
  506. right_min=?,
  507. right_max=?
  508. WHERE chart_info_id = ?`
  509. _, err = to.Raw(sql, edbInfoIdStr, req.Calendar, req.SeasonStartDate, req.SeasonEndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Exec()
  510. if err != nil {
  511. fmt.Println("UPDATE chart_info Err:", err.Error())
  512. return err
  513. }
  514. }
  515. var edbInfoIdArr []string
  516. mapList := make([]*ChartEdbMapping, 0)
  517. for _, v := range req.ChartEdbInfoList {
  518. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  519. var count int
  520. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  521. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  522. if err != nil {
  523. fmt.Println("QueryRow Err:", err.Error())
  524. return err
  525. }
  526. if count > 0 {
  527. msql := `UPDATE chart_edb_mapping
  528. SET
  529. modify_time = NOW(),
  530. max_data = ?,
  531. min_data = ?,
  532. is_order = ?,
  533. is_axis = ?,
  534. edb_info_type = ?,
  535. lead_value = ?,
  536. lead_unit = ?,
  537. chart_style = ?,
  538. chart_color = ?,
  539. predict_chart_color = ?,
  540. chart_width = ?
  541. WHERE chart_info_id =? AND edb_info_id=? `
  542. _, err = to.Raw(msql, v.MaxData, v.MinData, v.IsOrder, v.IsAxis, v.EdbInfoType, v.LeadValue, v.LeadUnit, v.ChartStyle, v.ChartColor, v.PredictChartColor, v.ChartWidth, req.ChartInfoId, v.EdbInfoId).Exec()
  543. if err != nil {
  544. fmt.Println("chart_edb_mapping Err:" + err.Error())
  545. return err
  546. }
  547. } else {
  548. mapItem := new(ChartEdbMapping)
  549. mapItem.ChartInfoId = req.ChartInfoId
  550. mapItem.EdbInfoId = v.EdbInfoId
  551. mapItem.CreateTime = time.Now()
  552. mapItem.ModifyTime = time.Now()
  553. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  554. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  555. mapItem.MaxData = v.MaxData
  556. mapItem.MinData = v.MinData
  557. mapItem.IsOrder = v.IsOrder
  558. mapItem.IsAxis = v.IsAxis
  559. mapItem.EdbInfoType = v.EdbInfoType
  560. mapItem.LeadValue = v.LeadValue
  561. mapItem.LeadUnit = v.LeadUnit
  562. mapItem.ChartStyle = v.ChartStyle
  563. mapItem.ChartColor = v.ChartColor
  564. mapItem.PredictChartColor = v.PredictChartColor
  565. mapItem.ChartWidth = v.ChartWidth
  566. mapList = append(mapList, mapItem)
  567. }
  568. }
  569. if len(mapList) > 0 {
  570. _, err = to.InsertMulti(1, mapList)
  571. if err != nil {
  572. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  573. return err
  574. }
  575. }
  576. if len(edbInfoIdArr) > 0 {
  577. edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
  578. if edbInfoIdStr != "" {
  579. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id NOT IN(` + edbInfoIdStr + `)`
  580. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  581. if err != nil {
  582. fmt.Println("delete err:" + err.Error())
  583. return err
  584. }
  585. }
  586. }
  587. return
  588. }
  589. // EditChartInfoAndMapping 修改 ETA图库 的 图表与指标 的关系
  590. func EditChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, barChartConf string, chartEdbInfoList []*ChartSaveItem) (err error) {
  591. o := orm.NewOrmUsingDB("data")
  592. to, err := o.Begin()
  593. if err != nil {
  594. return
  595. }
  596. defer func() {
  597. if err != nil {
  598. _ = to.Rollback()
  599. } else {
  600. _ = to.Commit()
  601. }
  602. }()
  603. var pars []interface{}
  604. pars = append(pars, req.ChartName)
  605. pars = append(pars, edbInfoIdStr)
  606. pars = append(pars, req.ChartType)
  607. pars = append(pars, req.ChartClassifyId)
  608. pars = append(pars, disabled)
  609. pars = append(pars, barChartConf)
  610. pars = append(pars, req.ExtraConfig)
  611. sql := ` UPDATE chart_info
  612. SET
  613. chart_name =?,
  614. edb_info_ids=?,
  615. chart_type=?,
  616. chart_classify_id = ?,
  617. modify_time = NOW(),
  618. disabled = ?,
  619. bar_config = ?,extra_config = ?
  620. `
  621. if calendar != "" {
  622. sql += `,calendar = ? `
  623. pars = append(pars, calendar)
  624. }
  625. if dateType > 0 {
  626. sql += `,date_type = ? `
  627. pars = append(pars, dateType)
  628. }
  629. sql += `,start_date = ? `
  630. pars = append(pars, req.StartDate)
  631. sql += `,end_date = ? `
  632. pars = append(pars, req.EndDate)
  633. sql += `,season_start_date = ? `
  634. pars = append(pars, req.SeasonStartDate)
  635. sql += `,season_end_date = ? `
  636. pars = append(pars, req.SeasonEndDate)
  637. sql += `,left_min = ? `
  638. pars = append(pars, req.LeftMin)
  639. sql += `,left_max = ? `
  640. pars = append(pars, req.LeftMax)
  641. sql += `,right_min = ? `
  642. pars = append(pars, req.RightMin)
  643. sql += `,right_max = ? `
  644. pars = append(pars, req.RightMax)
  645. sql += `WHERE chart_info_id = ?`
  646. pars = append(pars, req.ChartInfoId)
  647. _, err = to.Raw(sql, pars).Exec()
  648. if err != nil {
  649. fmt.Println("UPDATE chart_info Err:", err.Error())
  650. return err
  651. }
  652. chartEdbMappingIdList := make([]string, 0)
  653. for _, v := range chartEdbInfoList {
  654. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  655. var tmpChartEdbMapping *ChartEdbMapping
  656. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  657. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_DEFAULT).QueryRow(&tmpChartEdbMapping)
  658. if err != nil && err.Error() != utils.ErrNoRow() {
  659. fmt.Println("QueryRow Err:", err.Error())
  660. return err
  661. }
  662. if tmpChartEdbMapping != nil {
  663. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  664. tmpChartEdbMapping.ModifyTime = time.Now()
  665. tmpChartEdbMapping.MaxData = v.MaxData
  666. tmpChartEdbMapping.MinData = v.MinData
  667. tmpChartEdbMapping.IsOrder = v.IsOrder
  668. tmpChartEdbMapping.IsAxis = v.IsAxis
  669. tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  670. tmpChartEdbMapping.LeadValue = v.LeadValue
  671. tmpChartEdbMapping.LeadUnit = v.LeadUnit
  672. tmpChartEdbMapping.ChartStyle = v.ChartStyle
  673. tmpChartEdbMapping.ChartColor = v.ChartColor
  674. tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  675. tmpChartEdbMapping.ChartWidth = v.ChartWidth
  676. _, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  677. if err != nil {
  678. fmt.Println("chart_edb_mapping Err:" + err.Error())
  679. return err
  680. }
  681. } else {
  682. mapItem := new(ChartEdbMapping)
  683. mapItem.ChartInfoId = req.ChartInfoId
  684. mapItem.EdbInfoId = v.EdbInfoId
  685. mapItem.CreateTime = time.Now()
  686. mapItem.ModifyTime = time.Now()
  687. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  688. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  689. mapItem.MaxData = v.MaxData
  690. mapItem.MinData = v.MinData
  691. mapItem.IsOrder = v.IsOrder
  692. mapItem.IsAxis = v.IsAxis
  693. mapItem.EdbInfoType = v.EdbInfoType
  694. mapItem.LeadValue = v.LeadValue
  695. mapItem.LeadUnit = v.LeadUnit
  696. mapItem.ChartStyle = v.ChartStyle
  697. mapItem.ChartColor = v.ChartColor
  698. mapItem.PredictChartColor = v.PredictChartColor
  699. mapItem.ChartWidth = v.ChartWidth
  700. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  701. tmpId, err := to.Insert(mapItem)
  702. if err != nil {
  703. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  704. return err
  705. }
  706. mapItem.ChartEdbMappingId = int(tmpId)
  707. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  708. }
  709. }
  710. if len(chartEdbMappingIdList) > 0 {
  711. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  712. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  713. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  714. if err != nil {
  715. fmt.Println("delete err:" + err.Error())
  716. return err
  717. }
  718. }
  719. return
  720. }
  721. // EditFutureGoodChartInfoAndMapping 修改商品价格曲线的 图表与指标 的关系
  722. func EditFutureGoodChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, barChartConf string) (err error) {
  723. o := orm.NewOrmUsingDB("data")
  724. to, err := o.Begin()
  725. if err != nil {
  726. return
  727. }
  728. defer func() {
  729. if err != nil {
  730. _ = to.Rollback()
  731. } else {
  732. _ = to.Commit()
  733. }
  734. }()
  735. var pars []interface{}
  736. pars = append(pars, req.ChartName)
  737. pars = append(pars, edbInfoIdStr)
  738. pars = append(pars, req.ChartType)
  739. pars = append(pars, req.ChartClassifyId)
  740. pars = append(pars, disabled)
  741. pars = append(pars, barChartConf)
  742. sql := ` UPDATE chart_info
  743. SET
  744. chart_name =?,
  745. edb_info_ids=?,
  746. chart_type=?,
  747. chart_classify_id = ?,
  748. modify_time = NOW(),
  749. disabled = ?,
  750. bar_config = ?
  751. `
  752. if calendar != "" {
  753. sql += `,calendar = ? `
  754. pars = append(pars, calendar)
  755. }
  756. if dateType > 0 {
  757. sql += `,date_type = ? `
  758. pars = append(pars, dateType)
  759. }
  760. sql += `,start_date = ? `
  761. pars = append(pars, req.StartDate)
  762. sql += `,end_date = ? `
  763. pars = append(pars, req.EndDate)
  764. sql += `,season_start_date = ? `
  765. pars = append(pars, req.SeasonStartDate)
  766. sql += `,season_end_date = ? `
  767. pars = append(pars, req.SeasonEndDate)
  768. sql += `,left_min = ? `
  769. pars = append(pars, req.LeftMin)
  770. sql += `,left_max = ? `
  771. pars = append(pars, req.LeftMax)
  772. sql += `,right_min = ? `
  773. pars = append(pars, req.RightMin)
  774. sql += `,right_max = ? `
  775. pars = append(pars, req.RightMax)
  776. sql += `WHERE chart_info_id = ?`
  777. pars = append(pars, req.ChartInfoId)
  778. _, err = to.Raw(sql, pars).Exec()
  779. if err != nil {
  780. fmt.Println("UPDATE chart_info Err:", err.Error())
  781. return err
  782. }
  783. chartEdbMappingIdList := make([]string, 0)
  784. for _, v := range req.BarChartInfo.EdbInfoIdList {
  785. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  786. var tmpChartEdbMapping *ChartEdbMapping
  787. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  788. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, v.Source).QueryRow(&tmpChartEdbMapping)
  789. if err != nil && err.Error() != utils.ErrNoRow() {
  790. fmt.Println("QueryRow Err:", err.Error())
  791. return err
  792. }
  793. if tmpChartEdbMapping != nil {
  794. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  795. //tmpChartEdbMapping.ModifyTime = time.Now()
  796. //tmpChartEdbMapping.MaxData = v.MaxData
  797. //tmpChartEdbMapping.MinData = v.MinData
  798. //tmpChartEdbMapping.IsOrder = v.IsOrder
  799. //tmpChartEdbMapping.IsAxis = v.IsAxis
  800. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  801. //tmpChartEdbMapping.LeadValue = v.LeadValue
  802. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  803. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  804. //tmpChartEdbMapping.ChartColor = v.ChartColor
  805. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  806. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  807. //_, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  808. //if err != nil {
  809. // fmt.Println("chart_edb_mapping Err:" + err.Error())
  810. // return err
  811. //}
  812. } else {
  813. mapItem := new(ChartEdbMapping)
  814. mapItem.ChartInfoId = req.ChartInfoId
  815. mapItem.EdbInfoId = v.EdbInfoId
  816. mapItem.CreateTime = time.Now()
  817. mapItem.ModifyTime = time.Now()
  818. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  819. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  820. //mapItem.MaxData = v.MaxData
  821. //mapItem.MinData = v.MinData
  822. mapItem.IsOrder = true
  823. mapItem.IsAxis = 1
  824. mapItem.EdbInfoType = 1
  825. //mapItem.LeadValue = v.LeadValue
  826. //mapItem.LeadUnit = v.LeadUnit
  827. //mapItem.ChartStyle = v.ChartStyle
  828. //mapItem.ChartColor = v.ChartColor
  829. //mapItem.PredictChartColor = v.PredictChartColor
  830. //mapItem.ChartWidth = v.ChartWidth
  831. mapItem.Source = v.Source
  832. tmpId, err := to.Insert(mapItem)
  833. if err != nil {
  834. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  835. return err
  836. }
  837. mapItem.ChartEdbMappingId = int(tmpId)
  838. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  839. }
  840. }
  841. if len(chartEdbMappingIdList) > 0 {
  842. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  843. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  844. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  845. if err != nil {
  846. fmt.Println("delete err:" + err.Error())
  847. return err
  848. }
  849. }
  850. return
  851. }
  852. func EditChartEnInfoAndEdbEnInfo(req *EditChartEnInfoReq) (err error) {
  853. o := orm.NewOrmUsingDB("data")
  854. to, err := o.Begin()
  855. if err != nil {
  856. return
  857. }
  858. defer func() {
  859. if err != nil {
  860. _ = to.Rollback()
  861. } else {
  862. _ = to.Commit()
  863. }
  864. }()
  865. var pars []interface{}
  866. updateField := ` chart_name_en =?, `
  867. pars = append(pars, req.ChartNameEn)
  868. if req.ExtraConfig != `` {
  869. updateField += ` extra_config =?, `
  870. pars = append(pars, req.ExtraConfig)
  871. }
  872. sql := ` UPDATE chart_info
  873. SET ` + updateField + ` modify_time = NOW()
  874. WHERE chart_info_id = ?`
  875. pars = append(pars, req.ChartInfoId)
  876. _, err = to.Raw(sql, pars).Exec()
  877. if err != nil {
  878. fmt.Println("UPDATE chart_info Err:", err.Error())
  879. return err
  880. }
  881. var edbInfoIdArr []string
  882. for _, v := range req.ChartEdbInfoList {
  883. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  884. var count int
  885. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  886. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  887. if err != nil {
  888. fmt.Println("QueryRow Err:", err.Error())
  889. return err
  890. }
  891. if count > 0 {
  892. msql := ` UPDATE edb_info
  893. SET
  894. edb_name_en =?,
  895. unit_en = ?,
  896. modify_time = NOW()
  897. WHERE edb_info_id = ? `
  898. _, err = to.Raw(msql, v.EdbNameEn, v.UnitEn, v.EdbInfoId).Exec()
  899. if err != nil {
  900. fmt.Println("edb_info Err:" + err.Error())
  901. return err
  902. }
  903. }
  904. }
  905. return
  906. }
  907. func ModifyEdbDatadTimestamp(source, edbDataId int, dataTimestamp int64) (err error) {
  908. tableName := GetEdbDataTableName(source)
  909. sql := `UPDATE %s SET data_timestamp=? WHERE edb_data_id=? `
  910. sql = fmt.Sprintf(sql, tableName)
  911. o := orm.NewOrmUsingDB("data")
  912. _, err = o.Raw(sql, dataTimestamp, edbDataId).Exec()
  913. return
  914. }
  915. type AddChartInfoReq struct {
  916. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  917. ChartClassifyId int `description:"分类id"`
  918. ChartName string `description:"图表名称"`
  919. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图"`
  920. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  921. StartDate string `description:"自定义开始日期"`
  922. EndDate string `description:"自定义结束日期"`
  923. Calendar string `description:"公历/农历"`
  924. SeasonStartDate string `description:"季节性图开始日期"`
  925. SeasonEndDate string `description:"季节性图开始日期"`
  926. LeftMin string `description:"图表左侧最小值"`
  927. LeftMax string `description:"图表左侧最大值"`
  928. RightMin string `description:"图表右侧最小值"`
  929. RightMax string `description:"图表右侧最大值"`
  930. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  931. CorrelationChartInfo CorrelationChartInfoReq `description:"相关性图表配置"`
  932. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  933. ChartImage string `description:"封面图" json:"-"`
  934. }
  935. type AddChartInfoResp struct {
  936. ChartInfoId int `description:"图表id"`
  937. UniqueCode string `description:"图表唯一编码"`
  938. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  939. }
  940. // 判断图表指标是否已经存在
  941. func ChartInfoExist(condition, edbInfoIdStr string) (count int, err error) {
  942. sql := `SELECT COUNT(1) AS count FROM (
  943. SELECT GROUP_CONCAT(edb_info_id ORDER BY edb_info_id ASC SEPARATOR ',') AS edb_info_id_str
  944. FROM chart_edb_mapping AS a
  945. WHERE 1=1`
  946. if condition != "" {
  947. sql += condition
  948. }
  949. sql += ` GROUP BY chart_info_id
  950. )AS t
  951. WHERE t.edb_info_id_str=?
  952. GROUP BY t.edb_info_id_str `
  953. o := orm.NewOrmUsingDB("data")
  954. err = o.Raw(sql, edbInfoIdStr).QueryRow(&count)
  955. return
  956. }
  957. func ChartInfoSearchByKeyWord(KeyWord string, showSysId int) (searchList []*ChartInfo, err error) {
  958. o := orm.NewOrmUsingDB("data")
  959. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  960. var pars []interface{}
  961. if KeyWord != "" {
  962. sql += `AND chart_name LIKE ? `
  963. pars = append(pars, "%"+KeyWord+"%")
  964. }
  965. if showSysId > 0 {
  966. sql += ` AND sys_user_id = ? `
  967. pars = append(pars, showSysId)
  968. }
  969. sql += ` ORDER BY create_time DESC `
  970. if KeyWord == "" {
  971. sql += ` LIMIT 100 `
  972. }
  973. _, err = o.Raw(sql, pars).QueryRows(&searchList)
  974. return
  975. }
  976. // ChartInfoSearchByEmptyKeyWord 没有关键字的时候获取默认100条数据
  977. func ChartInfoSearchByEmptyKeyWord(showSysId int, sourceList []int, noPermissionChartIdList []int) (total int64, searchList []*ChartInfo, err error) {
  978. num := len(sourceList)
  979. o := orm.NewOrmUsingDB("data")
  980. sql := ` SELECT * FROM chart_info WHERE 1=1 AND source in (` + utils.GetOrmInReplace(num) + `)`
  981. var pars []interface{}
  982. pars = append(pars, sourceList)
  983. if showSysId > 0 {
  984. sql += ` AND sys_user_id = ? `
  985. pars = append(pars, showSysId)
  986. }
  987. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  988. if lenNoPermissionChartIdList > 0 {
  989. sql += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  990. pars = append(pars, noPermissionChartIdList)
  991. }
  992. sql += ` ORDER BY create_time DESC LIMIT 100 `
  993. total, err = o.Raw(sql, pars).QueryRows(&searchList)
  994. return
  995. }
  996. func GetNextChartInfo(classifyId int) (item *ChartInfo, err error) {
  997. o := orm.NewOrmUsingDB("data")
  998. sql := ` SELECT b.* FROM chart_classify AS a
  999. INNER JOIN chart_info AS b ON a.chart_classify_id=b.chart_classify_id
  1000. WHERE a.chart_classify_id>?
  1001. ORDER BY a.chart_classify_id ASC
  1002. LIMIT 1 `
  1003. err = o.Raw(sql, classifyId).QueryRow(&item)
  1004. return
  1005. }
  1006. // GetChartInfoRefreshData 获取图表关联的指标信息
  1007. func GetChartInfoRefreshData(chartInfoId int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  1008. o := orm.NewOrmUsingDB("data")
  1009. sql := ` SELECT b.* FROM chart_edb_mapping AS a
  1010. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  1011. WHERE a.chart_info_id=? `
  1012. edbInfoList := make([]*EdbInfo, 0)
  1013. _, err = o.Raw(sql, chartInfoId).QueryRows(&edbInfoList)
  1014. if err != nil {
  1015. return
  1016. }
  1017. for _, v := range edbInfoList {
  1018. fmt.Println(v.EdbInfoId, v.EdbType)
  1019. if v.EdbType == 1 {
  1020. baseEdbInfoArr = append(baseEdbInfoArr, v)
  1021. } else {
  1022. calculateInfoArr = append(calculateInfoArr, v)
  1023. newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
  1024. if err != nil {
  1025. return baseEdbInfoArr, calculateInfoArr, err
  1026. }
  1027. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  1028. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  1029. }
  1030. }
  1031. return
  1032. }
  1033. func GetChartRefreshEdbInfo(edbInfoId, source, n int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  1034. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  1035. if err != nil && err.Error() != utils.ErrNoRow() {
  1036. return
  1037. }
  1038. n++
  1039. for _, item := range calculateList {
  1040. fmt.Println(item.EdbInfoId)
  1041. if item.EdbType == 1 {
  1042. baseEdbInfoArr = append(baseEdbInfoArr, item)
  1043. } else {
  1044. calculateInfoArr = append(calculateInfoArr, item)
  1045. if n > 10 {
  1046. return
  1047. }
  1048. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetChartRefreshEdbInfo(item.EdbInfoId, item.Source, n)
  1049. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  1050. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  1051. }
  1052. }
  1053. return
  1054. }
  1055. type SetChartInfoImageReq struct {
  1056. ChartInfoId int `description:"图表ID"`
  1057. ImageUrl string `description:"图表图片地址"`
  1058. }
  1059. func EditChartInfoImage(req *SetChartInfoImageReq) (err error) {
  1060. o := orm.NewOrmUsingDB("data")
  1061. sql := ` UPDATE chart_info SET chart_image=?, modify_time = NOW() WHERE chart_info_id = ? `
  1062. _, err = o.Raw(sql, req.ImageUrl, req.ChartInfoId).Exec()
  1063. if err != nil {
  1064. fmt.Println("EditChartInfoImage Err:", err.Error())
  1065. return err
  1066. }
  1067. return
  1068. }
  1069. type ChartInfoDetailFromUniqueCodeResp struct {
  1070. ChartInfo *ChartInfoView
  1071. Status bool `description:"true:图表存在,false:图表不存在"`
  1072. EdbInfoList []*ChartEdbInfoMapping
  1073. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  1074. YDataList []YData `description:"柱方图的y轴数据"`
  1075. XDataList []XData `description:"商品价格曲线的X轴数据"`
  1076. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  1077. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  1078. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  1079. }
  1080. func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfo, err error) {
  1081. o := orm.NewOrmUsingDB("data")
  1082. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  1083. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  1084. return
  1085. }
  1086. func GetChartInfoViewByUniqueCode(uniqueCode string) (item *ChartInfoView, err error) {
  1087. o := orm.NewOrmUsingDB("data")
  1088. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  1089. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  1090. return
  1091. }
  1092. // GetFirstChartInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  1093. func GetFirstChartInfoByClassifyId(classifyId int) (item *ChartInfo, err error) {
  1094. o := orm.NewOrmUsingDB("data")
  1095. sql := ` SELECT * FROM chart_info WHERE chart_classify_id=? order by sort asc,chart_info_id asc limit 1`
  1096. err = o.Raw(sql, classifyId).QueryRow(&item)
  1097. return
  1098. }
  1099. // UpdateChartInfoSortByClassifyId 根据图表id更新排序
  1100. func UpdateChartInfoSortByClassifyId(classifyId, nowSort, prevChartInfoId int, sourceList []int, updateSort string) (err error) {
  1101. o := orm.NewOrmUsingDB("data")
  1102. num := len(sourceList)
  1103. if num <= 0 {
  1104. return
  1105. }
  1106. sql := ` update chart_info set sort = ` + updateSort + ` WHERE chart_classify_id=? AND source in (` + utils.GetOrmInReplace(num) + `) AND `
  1107. if prevChartInfoId > 0 {
  1108. sql += ` (sort > ? or (chart_info_id > ` + fmt.Sprint(prevChartInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  1109. }
  1110. _, err = o.Raw(sql, classifyId, sourceList, nowSort).Exec()
  1111. return
  1112. }
  1113. // Update 更新图表基础信息
  1114. func (chartInfo *ChartInfo) Update(cols []string) (err error) {
  1115. o := orm.NewOrmUsingDB("data")
  1116. _, err = o.Update(chartInfo, cols...)
  1117. return
  1118. }
  1119. type ChartInfoView struct {
  1120. ChartInfoId int `orm:"column(chart_info_id);pk"`
  1121. ChartName string `description:"来源名称"`
  1122. ChartNameEn string `description:"英文图表名称"`
  1123. UnitEn string `description:"英文单位名称"`
  1124. Unit string `description:"图表单位"`
  1125. ChartClassifyId int `description:"图表分类id"`
  1126. ChartClassifyName string `description:"图表名称"`
  1127. SysUserId int
  1128. SysUserRealName string
  1129. UniqueCode string `description:"图表唯一编码"`
  1130. CreateTime time.Time
  1131. ModifyTime time.Time
  1132. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  1133. StartDate string `description:"自定义开始日期"`
  1134. EndDate string `description:"自定义结束日期"`
  1135. IsSetName int `description:"设置名称"`
  1136. EdbInfoIds string `description:"指标id"`
  1137. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  1138. Calendar string `description:"公历/农历"`
  1139. SeasonStartDate string `description:"季节性图开始日期"`
  1140. SeasonEndDate string `description:"季节性图开始日期"`
  1141. ChartImage string `description:"图表图片"`
  1142. Sort int `description:"排序字段,数字越小越排前面"`
  1143. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  1144. MyChartId int
  1145. MyChartClassifyId string `description:"我的图表分类,多个用逗号隔开"`
  1146. ChartClassify []*ChartClassifyView
  1147. EdbEndDate string `description:"指标最新更新日期"`
  1148. LeftMin string `description:"图表左侧最小值"`
  1149. LeftMax string `description:"图表左侧最大值"`
  1150. RightMin string `description:"图表右侧最小值"`
  1151. RightMax string `description:"图表右侧最大值"`
  1152. IsEdit bool `description:"是否有编辑权限"`
  1153. IsEnChart bool `description:"是否展示英文标识"`
  1154. WarnMsg string `description:"错误信息"`
  1155. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  1156. BarConfig string `description:"柱方图的配置,json数据" json:"-"`
  1157. Source int `description:"1:ETA图库;2:商品价格曲线;3:相关性图表"`
  1158. //CorrelationLeadUnit string `description:"相关性图表-领先单位"`
  1159. ExtraConfig string `description:"图表额外配置,json数据"`
  1160. Button ChartViewButton `description:"操作按钮"`
  1161. }
  1162. type ChartViewButton struct {
  1163. IsEdit bool `description:"是否有编辑权限"`
  1164. IsEnChart bool `description:"是否展示英文标识"`
  1165. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  1166. IsCopy bool `description:"是否有另存为按钮"`
  1167. IsSetName int `description:"设置名称"`
  1168. }
  1169. type ImageSvgToPngResp struct {
  1170. Data struct {
  1171. ResourceURL string `json:"ResourceUrl"`
  1172. } `json:"Data"`
  1173. ErrCode string `json:"ErrCode"`
  1174. ErrMsg string `json:"ErrMsg"`
  1175. IsSendEmail bool `json:"IsSendEmail"`
  1176. Msg string `json:"Msg"`
  1177. Ret int64 `json:"Ret"`
  1178. Success bool `json:"Success"`
  1179. }
  1180. // GetChartInfoByClassifyIdAndName 根据分类id和图表名获取图表信息
  1181. func GetChartInfoByClassifyIdAndName(classifyId int, chartName string) (item *ChartInfo, err error) {
  1182. o := orm.NewOrmUsingDB("data")
  1183. sql := ` SELECT * FROM chart_info WHERE chart_classify_id = ? and chart_name=? `
  1184. err = o.Raw(sql, classifyId, chartName).QueryRow(&item)
  1185. return
  1186. }
  1187. // BatchChartRefreshReq 批量刷新图表请求
  1188. type BatchChartRefreshReq struct {
  1189. ChartInfoCode []string `description:"图表编码数组"`
  1190. }
  1191. // GetChartInfoListByUniqueCodeSlice 根据图表编码获取图表列表数据
  1192. func GetChartInfoListByUniqueCodeSlice(uniqueCodeSlice []string) (total int64, items []*ChartInfo, err error) {
  1193. if len(uniqueCodeSlice) <= 0 {
  1194. return
  1195. }
  1196. o := orm.NewOrmUsingDB("data")
  1197. sql := ` SELECT * FROM chart_info WHERE unique_code in ("` + strings.Join(uniqueCodeSlice, `","`) + `") `
  1198. total, err = o.Raw(sql).QueryRows(&items)
  1199. return
  1200. }
  1201. // GetChartInfoRefreshDataByChartInfoIdSlice 根据图表id切片获取对应的指标数据
  1202. func GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice []string) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  1203. o := orm.NewOrmUsingDB("data")
  1204. if len(chartInfoIdSlice) <= 0 {
  1205. return
  1206. }
  1207. sql := ` SELECT b.* FROM chart_edb_mapping AS a
  1208. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  1209. WHERE a.chart_info_id in (` + strings.Join(chartInfoIdSlice, ",") + `) `
  1210. edbInfoList := make([]*EdbInfo, 0)
  1211. _, err = o.Raw(sql).QueryRows(&edbInfoList)
  1212. if err != nil {
  1213. return
  1214. }
  1215. for _, v := range edbInfoList {
  1216. if v.EdbType == 1 {
  1217. baseEdbInfoArr = append(baseEdbInfoArr, v)
  1218. } else {
  1219. calculateInfoArr = append(calculateInfoArr, v)
  1220. newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
  1221. if err != nil {
  1222. return baseEdbInfoArr, calculateInfoArr, err
  1223. }
  1224. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  1225. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  1226. }
  1227. }
  1228. return
  1229. }
  1230. // CopyAddChartInfoReq 复制并新增图表
  1231. type CopyAddChartInfoReq struct {
  1232. ChartInfoId int `description:"待复制的图表id"`
  1233. ChartClassifyId int `description:"分类id"`
  1234. ChartName string `description:"图表名称"`
  1235. }
  1236. // ChartInfoListByEsResp 图表数据Es搜索返回
  1237. type ChartInfoListByEsResp struct {
  1238. Paging *paging.PagingItem
  1239. List []*ChartInfoMore
  1240. }
  1241. // GetChartInfoListByCondition 根据条件获取图表数据了列表
  1242. func GetChartInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*ChartInfo, err error) {
  1243. o := orm.NewOrmUsingDB("data")
  1244. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  1245. if condition != "" {
  1246. sql += condition
  1247. }
  1248. sql += ` ORDER BY chart_info_id DESC LIMIT ?,? `
  1249. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1250. return
  1251. }
  1252. // GetChartInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1253. func GetChartInfoListGroupByUserId(edbIdList []string) (items []*ChartInfo, err error) {
  1254. num := len(edbIdList)
  1255. if num <= 0 {
  1256. return
  1257. }
  1258. o := orm.NewOrmUsingDB("data")
  1259. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  1260. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1261. return
  1262. }
  1263. // ModifyChartInfoUserIdByCodeList 根据指标code列表修改创建人
  1264. func ModifyChartInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1265. num := len(edbIdList)
  1266. if num <= 0 {
  1267. return
  1268. }
  1269. o := orm.NewOrmUsingDB("data")
  1270. sql := `UPDATE chart_info SET sys_user_id=?,sys_user_real_name=? WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1271. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1272. return
  1273. }
  1274. // BarChartInfoReq 柱方图预览请求数据
  1275. type BarChartInfoReq struct {
  1276. EdbInfoIdList []BarChartInfoEdbItemReq `description:"指标信息"`
  1277. DateList []BarChartInfoDateReq `description:"日期配置"`
  1278. Sort BarChartInfoSortReq `description:"排序"`
  1279. XEdbList []BarChartInfoEdbItemReq `description:"X轴选择的指标列表"`
  1280. YEdbList []BarChartInfoEdbItemReq `description:"Y轴选择的指标列表"`
  1281. Unit string `description:"中文单位名称"`
  1282. UnitEn string `description:"英文单位名称"`
  1283. }
  1284. // BarChartInfoEdbItemReq 柱方图预览请求数据(指标相关)
  1285. type BarChartInfoEdbItemReq struct {
  1286. EdbInfoId int `description:"指标ID"`
  1287. Name string `description:"别名"`
  1288. NameEn string `description:"英文别名"`
  1289. Source int `description:"1:ETA图库;2:商品价格"`
  1290. }
  1291. // BarChartInfoDateReq 柱方图预览请求数据(日期相关)
  1292. type BarChartInfoDateReq struct {
  1293. Type int `description:"配置类型"`
  1294. Date string `description:"固定日期"`
  1295. Value int `description:"N天的值"`
  1296. Color string `description:"颜色"`
  1297. Name string `description:"别名"`
  1298. }
  1299. // BarChartInfoSortReq 柱方图预览请求数据(排序相关)
  1300. type BarChartInfoSortReq struct {
  1301. Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1302. DateIndex int `description:"日期数据的下标,从0开始"`
  1303. }
  1304. // CorrelationChartInfoReq 相关性图表请求体
  1305. type CorrelationChartInfoReq struct {
  1306. LeadValue int `description:"领先期数"`
  1307. LeadUnit string `description:"频度"`
  1308. CalculateValue int `description:"计算窗口"`
  1309. CalculateUnit string `description:"计算频度"`
  1310. BaseCalculateValue int `description:"基础计算窗口(滚动相关性的时候用到)"`
  1311. BaseCalculateUnit string `description:"基础计算频度(滚动相关性的时候用到)"`
  1312. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  1313. StartDate string `description:"开始日期"`
  1314. EndDate string `description:"结束日期"`
  1315. EdbInfoIdList []CorrelationChartInfoEdbItemReq `description:"指标信息"`
  1316. }
  1317. // CorrelationChartInfoEdbItemReq 相关性图表请求指标
  1318. type CorrelationChartInfoEdbItemReq struct {
  1319. EdbInfoId int `description:"指标ID"`
  1320. Name string `description:"别名"`
  1321. NameEn string `description:"英文别名"`
  1322. //Source int `description:"1:ETA图库;2:商品价格"`
  1323. }
  1324. // EditCorrelationChartInfoAndMapping 修改相关性图表的 图表与指标 的关系
  1325. func EditCorrelationChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, barChartConf string, correlationChart *ChartInfoCorrelation, correlationUpdateCols []string) (err error) {
  1326. o := orm.NewOrmUsingDB("data")
  1327. to, err := o.Begin()
  1328. if err != nil {
  1329. return
  1330. }
  1331. defer func() {
  1332. if err != nil {
  1333. _ = to.Rollback()
  1334. } else {
  1335. _ = to.Commit()
  1336. }
  1337. }()
  1338. var pars []interface{}
  1339. pars = append(pars, req.ChartName)
  1340. pars = append(pars, edbInfoIdStr)
  1341. pars = append(pars, req.ChartType)
  1342. pars = append(pars, req.ChartClassifyId)
  1343. pars = append(pars, disabled)
  1344. pars = append(pars, barChartConf)
  1345. sql := ` UPDATE chart_info
  1346. SET
  1347. chart_name =?,
  1348. edb_info_ids=?,
  1349. chart_type=?,
  1350. chart_classify_id = ?,
  1351. modify_time = NOW(),
  1352. disabled = ?,
  1353. bar_config = ?
  1354. `
  1355. if calendar != "" {
  1356. sql += `,calendar = ? `
  1357. pars = append(pars, calendar)
  1358. }
  1359. if dateType > 0 {
  1360. sql += `,date_type = ? `
  1361. pars = append(pars, dateType)
  1362. }
  1363. sql += `,start_date = ? `
  1364. pars = append(pars, req.StartDate)
  1365. sql += `,end_date = ? `
  1366. pars = append(pars, req.EndDate)
  1367. sql += `,season_start_date = ? `
  1368. pars = append(pars, req.SeasonStartDate)
  1369. sql += `,season_end_date = ? `
  1370. pars = append(pars, req.SeasonEndDate)
  1371. sql += `,left_min = ? `
  1372. pars = append(pars, req.LeftMin)
  1373. sql += `,left_max = ? `
  1374. pars = append(pars, req.LeftMax)
  1375. sql += `,right_min = ? `
  1376. pars = append(pars, req.RightMin)
  1377. sql += `,right_max = ? `
  1378. pars = append(pars, req.RightMax)
  1379. sql += `WHERE chart_info_id = ?`
  1380. pars = append(pars, req.ChartInfoId)
  1381. _, err = to.Raw(sql, pars).Exec()
  1382. if err != nil {
  1383. fmt.Println("UPDATE chart_info Err:", err.Error())
  1384. return err
  1385. }
  1386. chartEdbMappingIdList := make([]string, 0)
  1387. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  1388. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  1389. var tmpChartEdbMapping *ChartEdbMapping
  1390. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  1391. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_CORRELATION).QueryRow(&tmpChartEdbMapping)
  1392. if err != nil && err.Error() != utils.ErrNoRow() {
  1393. fmt.Println("QueryRow Err:", err.Error())
  1394. return err
  1395. }
  1396. if tmpChartEdbMapping != nil {
  1397. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  1398. //tmpChartEdbMapping.ModifyTime = time.Now()
  1399. //tmpChartEdbMapping.MaxData = v.MaxData
  1400. //tmpChartEdbMapping.MinData = v.MinData
  1401. //tmpChartEdbMapping.IsOrder = v.IsOrder
  1402. //tmpChartEdbMapping.IsAxis = v.IsAxis
  1403. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  1404. //tmpChartEdbMapping.LeadValue = v.LeadValue
  1405. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  1406. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  1407. //tmpChartEdbMapping.ChartColor = v.ChartColor
  1408. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  1409. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  1410. //_, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  1411. //if err != nil {
  1412. // fmt.Println("chart_edb_mapping Err:" + err.Error())
  1413. // return err
  1414. //}
  1415. } else {
  1416. mapItem := new(ChartEdbMapping)
  1417. mapItem.ChartInfoId = req.ChartInfoId
  1418. mapItem.EdbInfoId = v.EdbInfoId
  1419. mapItem.CreateTime = time.Now()
  1420. mapItem.ModifyTime = time.Now()
  1421. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1422. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  1423. //mapItem.MaxData = v.MaxData
  1424. //mapItem.MinData = v.MinData
  1425. mapItem.IsOrder = true
  1426. mapItem.IsAxis = 1
  1427. mapItem.EdbInfoType = 1
  1428. //mapItem.LeadValue = v.LeadValue
  1429. //mapItem.LeadUnit = v.LeadUnit
  1430. //mapItem.ChartStyle = v.ChartStyle
  1431. //mapItem.ChartColor = v.ChartColor
  1432. //mapItem.PredictChartColor = v.PredictChartColor
  1433. //mapItem.ChartWidth = v.ChartWidth
  1434. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  1435. tmpId, err := to.Insert(mapItem)
  1436. if err != nil {
  1437. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  1438. return err
  1439. }
  1440. mapItem.ChartEdbMappingId = int(tmpId)
  1441. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  1442. }
  1443. }
  1444. if len(chartEdbMappingIdList) > 0 {
  1445. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  1446. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  1447. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  1448. if err != nil {
  1449. fmt.Println("delete err:" + err.Error())
  1450. return err
  1451. }
  1452. }
  1453. // 相关性图表
  1454. _, err = to.Update(correlationChart, correlationUpdateCols...)
  1455. return
  1456. }
  1457. // PreviewSectionScatterChartReq 预览截面散点图的请求
  1458. type PreviewSectionScatterChartReq struct {
  1459. ChartName string `description:"图表名称"`
  1460. ChartClassifyId int `description:"分类id"`
  1461. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  1462. }
  1463. // SectionScatterReq 截面散点请求
  1464. type SectionScatterReq struct {
  1465. XName string `description:"x轴名称"`
  1466. XNameEn string `description:"x轴名称(英文)"`
  1467. XUnitName string `description:"x轴单位名称"`
  1468. XUnitNameEn string `description:"x轴单位名称(英文)"`
  1469. YName string `description:"y轴名称"`
  1470. YNameEn string `description:"y轴名称(英文)"`
  1471. YUnitName string `description:"y轴单位名称"`
  1472. YUnitNameEn string `description:"y轴单位名称(英文)"`
  1473. XMinValue string `description:"X轴的最小值"`
  1474. XMaxValue string `description:"X轴的最大值"`
  1475. YMinValue string `description:"Y轴的最小值"`
  1476. YMaxValue string `description:"Y轴的最大值"`
  1477. //EdbList []SectionScatterEdbItemReq `description:"指标数据"`
  1478. SeriesList []SectionScatterSeriesItemReq `description:"系列数据"`
  1479. }
  1480. // SectionScatterSeriesItemReq 系列的请求
  1481. type SectionScatterSeriesItemReq struct {
  1482. Name string `description:"系列名"`
  1483. NameEn string `description:"系列名(英文名)"`
  1484. Color string `description:"颜色"`
  1485. EdbInfoList []SectionScatterEdbItemReq
  1486. ShowTrendLine bool `description:"是否展示趋势线"`
  1487. ShowFitEquation bool `description:"是否展示方程式"`
  1488. ShowRSquare bool `description:"是否展示R平方"`
  1489. }
  1490. // SectionScatterEdbItemReq 截面散点请求的指标
  1491. type SectionScatterEdbItemReq struct {
  1492. XEdbInfoId int `description:"X轴的指标ID"`
  1493. YEdbInfoId int `description:"Y轴的指标ID"`
  1494. Name string `description:"别名"`
  1495. NameEn string `description:"英文别名"`
  1496. XDateType int `description:"X轴的日期配置类型"`
  1497. XDate string `description:"X轴的日期固定日期"`
  1498. XDateValue int `description:"X轴的日期N天的值"`
  1499. YDateType int `description:"Y轴的日期配置类型"`
  1500. YDate string `description:"Y轴的日期固定日期"`
  1501. YDateValue int `description:"Y轴的日期N天的值"`
  1502. IsShow bool `description:"是否展示"`
  1503. }
  1504. // SectionScatterInfoResp 截面散点图数据
  1505. type SectionScatterInfoResp struct {
  1506. XName string `description:"x轴名称"`
  1507. XNameEn string `description:"x轴名称(英文)"`
  1508. XUnitName string `description:"x轴单位名称"`
  1509. XUnitNameEn string `description:"x轴单位名称(英文)"`
  1510. YName string `description:"y轴名称"`
  1511. YNameEn string `description:"y轴名称(英文)"`
  1512. YUnitName string `description:"y轴单位名称"`
  1513. YUnitNameEn string `description:"y轴单位名称(英文)"`
  1514. XMinValue string `description:"X轴的最小值"`
  1515. XMaxValue string `description:"X轴的最大值"`
  1516. YMinValue string `description:"Y轴的最小值"`
  1517. YMaxValue string `description:"Y轴的最大值"`
  1518. DataList []SectionScatterSeriesItemResp `description:"数据列"`
  1519. }
  1520. // SectionScatterSeriesItemResp 系列的返回
  1521. type SectionScatterSeriesItemResp struct {
  1522. Name string `description:"系列名"`
  1523. NameEn string `description:"系列名(英文)"`
  1524. Color string `description:"颜色"`
  1525. EdbInfoList []SectionScatterEdbItemResp
  1526. ShowTrendLine bool `description:"是否展示趋势线"`
  1527. ShowFitEquation bool `description:"是否展示方程式"`
  1528. ShowRSquare bool `description:"是否展示R平方"`
  1529. TrendLine string `description:"方程式"`
  1530. RSquare string `description:"R平方的值(决定系数R2)"`
  1531. TrendLimitData []CoordinatePoint `description:"趋势线的前后坐标点"`
  1532. }
  1533. // SectionScatterEdbItemResp 截面散点的返回参数
  1534. type SectionScatterEdbItemResp struct {
  1535. XEdbInfoId int `description:"X轴指标id"`
  1536. XDate string `description:"X轴指标实际日期"`
  1537. XName string `description:"X轴指标名称"`
  1538. XNameEn string `description:"X轴指标英文名称"`
  1539. XValue float64 `description:"X轴实际值"`
  1540. YEdbInfoId int `description:"Y轴指标id"`
  1541. YDate string `description:"Y轴指标实际日期"`
  1542. YName string `description:"Y轴指标名称"`
  1543. YNameEn string `description:"Y轴指标英文名称"`
  1544. YValue float64 `description:"Y轴实际值"`
  1545. IsShow bool `description:"是否展示"`
  1546. Name string `description:"标签名称"`
  1547. NameEn string `description:"英文标签名称"`
  1548. }
  1549. // CoordinatePoint 坐标点
  1550. type CoordinatePoint struct {
  1551. X float64
  1552. Y float64
  1553. }
  1554. // RollingCorrelationChartDataResp 滚动相关性图
  1555. type RollingCorrelationChartDataResp struct {
  1556. XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
  1557. YDataList []YData `description:"滚动相关性图的y轴数据"`
  1558. }
  1559. // GetChartInfoAll 用于分类展示
  1560. // @param source int 1:ETA图库;2:商品价格曲线
  1561. func GetChartInfoAllByClassifyId(source, classifyId int) (items []*ChartClassifyItems, err error) {
  1562. o := orm.NewOrmUsingDB("data")
  1563. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,chart_name_en AS chart_classify_name_en,
  1564. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date
  1565. FROM chart_info WHERE chart_classify_id = ? AND source = ? ORDER BY sort asc,create_time DESC `
  1566. _, err = o.Raw(sql, classifyId, source).QueryRows(&items)
  1567. return
  1568. }
  1569. // ProfitFutureGoodChartResp 商品利润图
  1570. type ProfitFutureGoodChartResp struct {
  1571. XDataList []XData
  1572. YDataList []YData
  1573. ProfitName string `description:"利润的名称"`
  1574. ProfitNameEn string `description:"利润的英文名称"`
  1575. }
  1576. // ChartInfoLocate 图表位置响应体
  1577. type ChartInfoLocate struct {
  1578. ChartInfoId int `description:"图表ID"`
  1579. ChartName string `description:"图表名称"`
  1580. ChartType int `description:"图表类型"`
  1581. ChartClassifyId int `description:"图表分类id"`
  1582. ChartClassifyName string `description:"图表分类名称"`
  1583. UniqueCode string `description:"图表唯一编码"`
  1584. Source int `description:"1:ETA图库;2:商品价格曲线;3:相关性图表"`
  1585. StartDate string `description:""`
  1586. EndDate string `description:""`
  1587. SeasonStartDate string `description:""`
  1588. SeasonEndDate string `description:""`
  1589. DateType int `description:""`
  1590. Calendar string `description:""`
  1591. }
  1592. // GetChartInfoByAdminIdAndClassify 获取所有我创建的图表,用于分类展示
  1593. func GetChartInfoByAdminIdAndClassify(sourceList []int, adminId, classifyId int) (items []*ChartClassifyItems, err error) {
  1594. num := len(sourceList)
  1595. if num <= 0 {
  1596. return
  1597. }
  1598. o := orm.NewOrmUsingDB("data")
  1599. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
  1600. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date
  1601. FROM chart_info where source in (` + utils.GetOrmInReplace(num) + `) AND sys_user_id = ? AND chart_classify_id = ? ORDER BY sort asc,create_time ASC `
  1602. _, err = o.Raw(sql, sourceList, adminId, classifyId).QueryRows(&items)
  1603. return
  1604. }