chart_info.go 68 KB

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