chart_info.go 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_api/models/mgo"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type ChartInfo struct {
  15. ChartInfoId int `orm:"column(chart_info_id);pk"`
  16. ChartName string `description:"图表名称"`
  17. ChartNameEn string `description:"英文图表名称"`
  18. ChartClassifyId int `description:"图表分类id"`
  19. SysUserId int
  20. SysUserRealName string
  21. UniqueCode string `description:"图表唯一编码"`
  22. CreateTime time.Time
  23. ModifyTime time.Time
  24. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  25. StartDate string `description:"自定义开始日期"`
  26. EndDate string `description:"自定义结束日期"`
  27. IsSetName int `description:"设置名称"`
  28. EdbInfoIds string `description:"指标id"`
  29. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图,10:截面散点图, 11:雷达图"`
  30. Calendar string `description:"公历/农历"`
  31. SeasonStartDate string `description:"季节性图开始日期"`
  32. SeasonEndDate string `description:"季节性图开始日期"`
  33. ChartImage string `description:"图表图片"`
  34. Sort int `description:"排序字段,数字越小越排前面"`
  35. XMin string `description:"图表X轴最小值"`
  36. XMax string `description:"图表X轴最大值"`
  37. LeftMin string `description:"图表左侧最小值"`
  38. LeftMax string `description:"图表左侧最大值"`
  39. RightMin string `description:"图表右侧最小值"`
  40. RightMax string `description:"图表右侧最大值"`
  41. Right2Min string `description:"图表右侧2最小值"`
  42. Right2Max string `description:"图表右侧2最大值"`
  43. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  44. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  45. BarConfig string `description:"柱方图的配置,json数据"`
  46. Source int `description:"1:ETA图库;2:商品价格曲线"`
  47. ExtraConfig string `description:"图表额外配置,json数据"`
  48. SeasonExtraConfig string `description:"季节性图表中的配置,json数据"`
  49. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  50. ChartThemeId int `description:"图表应用主题ID"`
  51. SourcesFrom string `description:"图表来源"`
  52. Instructions string `description:"图表说明"`
  53. MarkersLines string `description:"标识线"`
  54. MarkersAreas string `description:"标识区"`
  55. Unit string `description:"中文单位名称"`
  56. UnitEn string `description:"英文单位名称"`
  57. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  58. ForumChartInfoId int `description:"社区的图表ID"`
  59. }
  60. type ChartInfoMore struct {
  61. ChartInfo
  62. IsEnChart bool `description:"是否展示英文标识"`
  63. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  64. }
  65. func AddChartInfo(item *ChartInfo) (lastId int64, err error) {
  66. o := orm.NewOrmUsingDB("data")
  67. lastId, err = o.Insert(item)
  68. return
  69. }
  70. type ChartInfoItem struct {
  71. ChartInfoId int `description:"图表id"`
  72. ChartName string `description:"图表名称"`
  73. ChartClassifyId int `description:"图表分类"`
  74. }
  75. // GetChartInfoAll 用于分类展示
  76. // @param source int 1:ETA图库;2:商品价格曲线
  77. func GetChartInfoAll(sourceList []int) (items []*ChartClassifyItems, err error) {
  78. num := len(sourceList)
  79. if num <= 0 {
  80. return
  81. }
  82. o := orm.NewOrmUsingDB("data")
  83. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,chart_name_en AS chart_classify_name_en,
  84. 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
  85. FROM chart_info WHERE source in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort asc,chart_info_id ASC `
  86. _, err = o.Raw(sql, sourceList).QueryRows(&items)
  87. return
  88. }
  89. // GetChartInfoByAdminId 获取所有我创建的图表,用于分类展示
  90. func GetChartInfoByAdminId(sourceList []int, adminId int) (items []*ChartClassifyItems, err error) {
  91. num := len(sourceList)
  92. if num <= 0 {
  93. return
  94. }
  95. o := orm.NewOrmUsingDB("data")
  96. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
  97. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date
  98. FROM chart_info where source in (` + utils.GetOrmInReplace(num) + `) AND sys_user_id = ? ORDER BY sort asc,create_time ASC `
  99. _, err = o.Raw(sql, sourceList, adminId).QueryRows(&items)
  100. return
  101. }
  102. // 用于分类展示
  103. func GetChartInfoAllList() (items []*ChartInfo, err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. sql := ` SELECT *
  106. FROM chart_info ORDER BY create_time ASC `
  107. _, err = o.Raw(sql).QueryRows(&items)
  108. return
  109. }
  110. // 图表检索数据
  111. type ChartInfoSearch struct {
  112. EdbCode string `description:"图表编码"`
  113. StartDate string `description:"起始日期"`
  114. EndDate string `description:"终止日期"`
  115. DataList []*EdbInfoSearchData
  116. }
  117. type ChartInfoSearchData struct {
  118. DataTime string `description:"数据日期"`
  119. Value float64 `description:"数据"`
  120. }
  121. type ChartInfoSearchResp struct {
  122. SearchItem *EdbInfoSearch `description:"图表分类"`
  123. Status int `description:"1:数据已存在于弘则数据库,2:新数据"`
  124. }
  125. func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
  126. o := orm.NewOrmUsingDB("data")
  127. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  128. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  129. return
  130. }
  131. func GetChartInfoViewById(chartInfoId int) (item *ChartInfoView, err error) {
  132. o := orm.NewOrmUsingDB("data")
  133. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  134. err = o.Raw(sql, chartInfoId).QueryRow(&item)
  135. return
  136. }
  137. func GetChartInfoViewByIds(chartInfoIds []string) (list []*ChartInfoView, err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. sql := ` SELECT * FROM chart_info WHERE chart_info_id in ("` + strings.Join(chartInfoIds, `","`) + `") `
  140. _, err = o.Raw(sql).QueryRows(&list)
  141. return
  142. }
  143. type SaveChartInfoReq struct {
  144. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  145. ChartInfoId int `description:"图表id,新增时传0"`
  146. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  147. StartDate string `description:"自定义开始日期"`
  148. EndDate string `description:"自定义结束日期"`
  149. Calendar string `description:"公历/农历"`
  150. LeftMin string `description:"图表左侧最小值"`
  151. LeftMax string `description:"图表左侧最大值"`
  152. RightMin string `description:"图表右侧最小值"`
  153. RightMax string `description:"图表右侧最大值"`
  154. Right2Min string `description:"图表右侧最小值"`
  155. Right2Max string `description:"图表右侧最大值"`
  156. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  157. ExtraConfig string `description:"图表额外配置,json数据"`
  158. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  159. }
  160. type ChartSaveItem struct {
  161. EdbInfoId int `description:"指标id"`
  162. MaxData float64 `description:"上限"`
  163. MinData float64 `description:"下限"`
  164. IsOrder bool `description:"true:正序,false:逆序"`
  165. IsAxis int `description:"1:左轴,0:右轴"`
  166. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  167. LeadValue int `description:"领先值"`
  168. LeadUnit string `description:"领先单位"`
  169. ChartStyle string `description:"图表类型"`
  170. ChartColor string `description:"颜色"`
  171. PredictChartColor string `description:"预测数据的颜色"`
  172. ChartWidth float64 `description:"线条大小"`
  173. Source int `description:"1:ETA图库;2:商品价格曲线"`
  174. EdbAliasName string `description:"中文别名"`
  175. IsConvert int `description:"是否数据转换 0不转 1转"`
  176. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  177. ConvertValue float64 `description:"数据转换值"`
  178. ConvertUnit string `description:"数据转换单位"`
  179. ConvertEnUnit string `description:"数据转换单位"`
  180. }
  181. func DeleteChartInfoAndData(chartInfoId int) (err error) {
  182. o := orm.NewOrmUsingDB("data")
  183. to, err := o.Begin()
  184. if err != nil {
  185. return
  186. }
  187. defer func() {
  188. if err != nil {
  189. _ = to.Rollback()
  190. } else {
  191. _ = to.Commit()
  192. }
  193. }()
  194. sql := ` DELETE FROM chart_info WHERE chart_info_id=? `
  195. _, err = to.Raw(sql, chartInfoId).Exec()
  196. if err != nil {
  197. return
  198. }
  199. sql = ` DELETE FROM chart_edb_mapping WHERE chart_info_id=? `
  200. _, err = to.Raw(sql, chartInfoId).Exec()
  201. return
  202. }
  203. func GetChartInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  204. o := orm.NewOrmUsingDB("data")
  205. sql := ` SELECT COUNT(1) AS count FROM chart_info WHERE 1=1 `
  206. if condition != "" {
  207. sql += condition
  208. }
  209. err = o.Raw(sql, pars).QueryRow(&count)
  210. return
  211. }
  212. type EditChartInfoReq struct {
  213. ChartInfoId int `description:"图表ID"`
  214. ChartName string `description:"图表名称"`
  215. ChartNameEn string `description:"英文图表名称"`
  216. ChartClassifyId int `description:"分类id"`
  217. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  218. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图,10:截面散点图"`
  219. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今,20:N年至今"`
  220. StartDate string `description:"自定义开始日期"`
  221. EndDate string `description:"自定义结束日期"`
  222. Calendar string `description:"公历/农历"`
  223. LeftMin string `description:"图表左侧最小值"`
  224. LeftMax string `description:"图表左侧最大值"`
  225. RightMin string `description:"图表右侧最小值"`
  226. RightMax string `description:"图表右侧最大值"`
  227. Right2Min string `description:"图表右侧最小值"`
  228. Right2Max string `description:"图表右侧最大值"`
  229. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  230. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  231. CorrelationChartInfo CorrelationChartInfoReq `description:"相关性图表配置"`
  232. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  233. SeasonExtraConfig SeasonExtraItem `description:"季节性图表中的配置,json数据"`
  234. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  235. ChartThemeId int `description:"图表应用主题ID"`
  236. SourcesFrom string `description:"图表来源"`
  237. Instructions string `description:"图表说明"`
  238. MarkersLines string `description:"标识线"`
  239. MarkersAreas string `description:"标识区"`
  240. Unit string `description:"中文单位名称"`
  241. UnitEn string `description:"英文单位名称"`
  242. }
  243. type EditChartEnInfoReq struct {
  244. ChartInfoId int `description:"图表ID"`
  245. ChartNameEn string `description:"英文图表名称"`
  246. ChartEdbInfoList []*EditChartEnInfoEdbItem `description:"指标及配置信息"`
  247. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  248. }
  249. type EditChartEnInfoEdbItem struct {
  250. EdbInfoId int `description:"指标ID"`
  251. EdbNameEn string `description:"英文指标名称"`
  252. UnitEn string `description:"英文单位"`
  253. }
  254. // EditChartInfoBaseReq
  255. // @Description: 图表基础信息修改
  256. type EditChartInfoBaseReq struct {
  257. ChartInfoId int `description:"图表ID"`
  258. ChartName string `description:"图表名称(根据语言版本区分)"`
  259. ChartEdbInfoList []*EditChartInfoEdbBaseItem `description:"指标及配置信息"`
  260. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  261. }
  262. type EditChartInfoEdbBaseItem struct {
  263. EdbInfoId int `description:"指标ID"`
  264. EdbName string `description:"指标名称(根据语言版本区分)"`
  265. Unit string `description:"单位(根据语言版本区分)"`
  266. }
  267. func ModifyChartInfo(item *EditChartInfoReq) (err error) {
  268. o := orm.NewOrmUsingDB("data")
  269. sql := ` UPDATE chart_info
  270. SET
  271. chart_name =?,
  272. chart_classify_id = ?,
  273. is_set_name=1,
  274. modify_time = NOW()
  275. WHERE chart_info_id = ?`
  276. _, err = o.Raw(sql, item.ChartName, item.ChartClassifyId, item.ChartInfoId).Exec()
  277. return
  278. }
  279. func SetForumChartInfoId(chartInfoId, forumChartInfoId int) (err error) {
  280. o := orm.NewOrmUsingDB("data")
  281. sql := ` UPDATE chart_info
  282. SET
  283. forum_chart_info_id=?,
  284. modify_time = NOW()
  285. WHERE chart_info_id = ?`
  286. _, err = o.Raw(sql, forumChartInfoId, chartInfoId).Exec()
  287. return
  288. }
  289. type ChartInfoList struct {
  290. ChartInfoId int `orm:"column(chart_info_id);pk"`
  291. ChartName string `description:"来源名称"`
  292. ChartChartClassifyId int `description:"图表分类id"`
  293. SysUserId int
  294. SysUserRealName string
  295. UniqueCode string `description:"图表唯一编码"`
  296. CreateTime time.Time
  297. ModifyTime time.Time
  298. EdbInfoList []*EdbInfoList
  299. }
  300. type ChartInfoListResp struct {
  301. Paging *paging.PagingItem
  302. Item *ChartInfoList
  303. }
  304. func GetChartInfoByCondition(condition string, pars []interface{}) (item *ChartInfo, err error) {
  305. o := orm.NewOrmUsingDB("data")
  306. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  307. if condition != "" {
  308. sql += condition
  309. }
  310. err = o.Raw(sql, pars).QueryRow(&item)
  311. return
  312. }
  313. func GetChartInfoByNewest(source int) (item *ChartInfoList, err error) {
  314. o := orm.NewOrmUsingDB("data")
  315. sql := ` SELECT * FROM chart_info WHERE 1=1 AND source = ? ORDER BY modify_time DESC LIMIT 1 `
  316. err = o.Raw(sql, source).QueryRow(&item)
  317. return
  318. }
  319. func ModifyChartInfoModifyTime(chartInfoId int64) (err error) {
  320. o := orm.NewOrmUsingDB("data")
  321. sql := ` UPDATE chart_info SET modify_time = NOW() WHERE chart_info_id = ? `
  322. _, err = o.Raw(sql, chartInfoId).Exec()
  323. return
  324. }
  325. type MoveChartInfoReq struct {
  326. ChartInfoId int `description:"图表ID"`
  327. PrevChartInfoId int `description:"上一个图表ID"`
  328. NextChartInfoId int `description:"下一个图表ID"`
  329. ChartClassifyId int `description:"分类id"`
  330. }
  331. func MoveChartInfo(chartInfoId, classifyId int) (err error) {
  332. o := orm.NewOrmUsingDB("data")
  333. sql := ` UPDATE chart_info
  334. SET
  335. chart_classify_id = ?
  336. WHERE chart_info_id = ?`
  337. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  338. return
  339. }
  340. type DeleteChartInfoReq struct {
  341. ChartInfoId int `description:"图表ID"`
  342. }
  343. type EdbDataList struct {
  344. EdbDataId int `description:" 指标数据ID"`
  345. EdbInfoId int `description:"指标ID"`
  346. DataTime string //`json:"-" description:"数据日期"`
  347. DataTimestamp int64 `description:"数据日期"`
  348. Value float64 `description:"数据值"`
  349. }
  350. // GetEdbDataList
  351. // @Description: 获取指标的数据(日期正序返回)
  352. // @author: Roc
  353. // @datetime 2024-05-07 10:48:43
  354. // @param source int
  355. // @param subSource int
  356. // @param edbInfoId int
  357. // @param startDate string
  358. // @param endDate string
  359. // @return list []*EdbDataList
  360. // @return err error
  361. func GetEdbDataList(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  362. // 自有数据需要额外处理(从mongo获取)
  363. if source == utils.DATA_SOURCE_BUSINESS {
  364. return getEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  365. }
  366. return getEdbDataListByMysql(source, subSource, edbInfoId, startDate, endDate)
  367. }
  368. // getEdbDataListByMysql
  369. // @Description: 通过mysql查询指标数据
  370. // @author: Roc
  371. // @datetime 2024-05-07 10:40:43
  372. // @param source int
  373. // @param subSource int
  374. // @param edbInfoId int
  375. // @param startDate string
  376. // @param endDate string
  377. // @return list []*EdbDataList
  378. // @return err error
  379. func getEdbDataListByMysql(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  380. tableName := GetEdbDataTableName(source, subSource)
  381. if tableName == "" {
  382. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  383. list = make([]*EdbDataList, 0)
  384. return list, err
  385. }
  386. var pars []interface{}
  387. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  388. if startDate != "" {
  389. sql += ` AND data_time>=? `
  390. pars = append(pars, startDate)
  391. }
  392. if endDate != "" {
  393. sql += ` AND data_time<=? `
  394. pars = append(pars, endDate)
  395. }
  396. sql += ` ORDER BY data_time ASC `
  397. sql = fmt.Sprintf(sql, tableName)
  398. o := orm.NewOrmUsingDB("data")
  399. _, err = o.Raw(sql, edbInfoId, pars).QueryRows(&list)
  400. return
  401. }
  402. // getEdbDataListByMongo
  403. // @Description: 通过mongo查询指标数据
  404. // @author: Roc
  405. // @datetime 2024-05-07 10:40:43
  406. // @param source int
  407. // @param subSource int
  408. // @param endInfoId int
  409. // @param startDate string
  410. // @param endDate string
  411. // @return list []*EdbDataList
  412. // @return err error
  413. func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  414. list = make([]*EdbDataList, 0)
  415. mogDataObj := mgo.EdbDataBusiness{}
  416. // 构建查询条件
  417. queryConditions := bson.M{
  418. "edb_info_id": edbInfoId,
  419. }
  420. // 数据日期
  421. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  422. if err != nil {
  423. return
  424. }
  425. if len(dateCondition) > 0 {
  426. queryConditions["data_time"] = dateCondition
  427. }
  428. // 获取列表数据
  429. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  430. if tmpErr != nil {
  431. err = tmpErr
  432. return
  433. }
  434. for k, v := range tmpDataList {
  435. list = append(list, &EdbDataList{
  436. EdbDataId: k + 1,
  437. EdbInfoId: v.EdbInfoId,
  438. DataTime: v.DataTime.Format(utils.FormatDate),
  439. DataTimestamp: v.DataTimestamp,
  440. Value: v.Value,
  441. })
  442. }
  443. return
  444. }
  445. type EdbDataListByUniqueCode struct {
  446. EdbDataId int `description:" 指标数据ID" json:"-"`
  447. EdbInfoId int `description:"指标ID" json:"-"`
  448. DataTime string //`json:"-" description:"数据日期"`
  449. DataTimestamp int64 `description:"数据日期"`
  450. Value float64 `description:"数据值"`
  451. }
  452. func GetEdbDataLunarList(endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  453. tableName := "edb_data_quarter"
  454. 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 `
  455. sql = fmt.Sprintf(sql, tableName)
  456. o := orm.NewOrmUsingDB("data")
  457. _, err = o.Raw(sql, endInfoId, startDate, endDate).QueryRows(&list)
  458. return
  459. }
  460. // GetEdbDataListMinAndMax
  461. // @Description: 根据条件数据库中获取指标数据最小值和最大值
  462. // @author: Roc
  463. // @datetime 2024-05-07 13:11:32
  464. // @param source int
  465. // @param subSource int
  466. // @param edbInfoId int
  467. // @param startDate string
  468. // @param endDate string
  469. // @return min_data float64
  470. // @return max_data float64
  471. // @return err error
  472. func GetEdbDataListMinAndMax(source, subSource, edbInfoId int, startDate, endDate string) (min_data, max_data float64, err error) {
  473. // 自有数据需要额外处理(从mongo获取)
  474. if source == utils.DATA_SOURCE_BUSINESS {
  475. return getEdbDataListMinAndMaxByMongo(source, subSource, edbInfoId, startDate, endDate)
  476. }
  477. return getEdbDataListMinAndMaxByMysql(source, subSource, edbInfoId, startDate, endDate)
  478. }
  479. // getEdbDataListMinAndMaxByMysql
  480. // @Description: 根据条件从mysql数据库中获取指标数据最小值和最大值
  481. // @author: Roc
  482. // @datetime 2024-05-07 13:11:22
  483. // @param source int
  484. // @param subSource int
  485. // @param edbInfoId int
  486. // @param startDate string
  487. // @param endDate string
  488. // @return minData float64
  489. // @return maxData float64
  490. // @return err error
  491. func getEdbDataListMinAndMaxByMysql(source, subSource, edbInfoId int, startDate, endDate string) (minData, maxData float64, err error) {
  492. tableName := GetEdbDataTableName(source, subSource)
  493. sql := `SELECT min(value) AS min_data,max(value) AS max_data FROM %s WHERE edb_info_id=? `
  494. var pars []interface{}
  495. var condition string
  496. if startDate != "" {
  497. condition += ` AND data_time>=? `
  498. pars = append(pars, startDate)
  499. }
  500. if endDate != "" {
  501. condition += ` AND data_time<=? `
  502. pars = append(pars, endDate)
  503. }
  504. sql = fmt.Sprintf(sql, tableName)
  505. o := orm.NewOrmUsingDB("data")
  506. if condition != "" {
  507. sql += condition
  508. }
  509. err = o.Raw(sql, edbInfoId, pars).QueryRow(&minData, &maxData)
  510. return
  511. }
  512. // getEdbDataListMinAndMaxByMongo
  513. // @Description: 根据条件从mongo数据库中获取指标数据最小值和最大值
  514. // @author: Roc
  515. // @datetime 2024-05-07 13:10:26
  516. // @param source int
  517. // @param subSource int
  518. // @param edbInfoId int
  519. // @param startDate string
  520. // @param endDate string
  521. // @return minData float64
  522. // @return maxData float64
  523. // @return err error
  524. func getEdbDataListMinAndMaxByMongo(source, subSource, edbInfoId int, startDate, endDate string) (minData, maxData float64, err error) {
  525. mogDataObj := mgo.EdbDataBusiness{}
  526. // 构建查询条件
  527. queryConditions := bson.M{
  528. "edb_info_id": edbInfoId,
  529. }
  530. // 日期
  531. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  532. if err != nil {
  533. return
  534. }
  535. if len(dateCondition) > 0 {
  536. queryConditions["data_time"] = dateCondition
  537. }
  538. pipeline := []bson.M{
  539. {"$match": queryConditions},
  540. {"$group": bson.M{
  541. "_id": nil,
  542. "min_value": bson.M{"$min": "$value"},
  543. "max_value": bson.M{"$max": "$value"},
  544. }},
  545. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  546. }
  547. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  548. if err != nil {
  549. return
  550. }
  551. minData = result.MinValue
  552. maxData = result.MaxValue
  553. return
  554. }
  555. type ChartEdbInfoMapping struct {
  556. EdbInfoId int `description:"指标id"`
  557. SourceName string `description:"来源名称"`
  558. Source int `description:"来源id"`
  559. SubSource int `description:"来源id"`
  560. EdbCode string `description:"指标编码"`
  561. EdbName string `description:"指标名称"`
  562. EdbAliasName string `description:"指标名称(别名)"`
  563. EdbNameEn string `description:"英文指标名称"`
  564. EdbAliasNameEn string `description:"英文指标名称(别名)"`
  565. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  566. Frequency string `description:"频率"`
  567. FrequencyEn string `description:"英文频率"`
  568. Unit string `description:"单位"`
  569. UnitEn string `description:"英文单位"`
  570. StartDate string `description:"起始日期"`
  571. EndDate string `description:"终止日期"`
  572. ModifyTime string `description:"指标最后更新时间"`
  573. ChartEdbMappingId int `description:"图表指标id"`
  574. ChartInfoId int `description:"图表id"`
  575. MaxData float64 `description:"上限"`
  576. MinData float64 `description:"下限"`
  577. IsOrder bool `description:"true:正序,false:逆序"`
  578. IsAxis int `description:"1:左轴,0:右轴"`
  579. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  580. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  581. LeadValue int `description:"领先值"`
  582. LeadUnit string `description:"领先单位"`
  583. LeadUnitEn string `description:"领先英文单位"`
  584. ChartStyle string `description:"图表类型"`
  585. ChartColor string `description:"颜色"`
  586. PredictChartColor string `description:"预测数据的颜色"`
  587. ChartWidth float64 `description:"线条大小"`
  588. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图"`
  589. LatestDate string `description:"数据最新日期"`
  590. LatestValue float64 `description:"数据最新值"`
  591. MoveLatestDate string `description:"移动后的数据最新日期"`
  592. UniqueCode string `description:"指标唯一编码"`
  593. MinValue float64 `json:"-" description:"最小值"`
  594. MaxValue float64 `json:"-" description:"最大值"`
  595. DataList interface{}
  596. IsNullData bool `json:"-" description:"是否空数据"`
  597. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  598. RegionType string `description:"交易所来源,海外还是国内" json:"-"`
  599. ClassifyId int `description:"分类id"`
  600. SubSourceName string `description:"子数据来源名称"`
  601. IndicatorCode string `description:"指标代码"`
  602. IsConvert int `description:"是否数据转换 0不转 1转"`
  603. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  604. ConvertValue float64 `description:"数据转换值"`
  605. ConvertUnit string `description:"数据转换单位"`
  606. ConvertEnUnit string `description:"数据转换单位"`
  607. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  608. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  609. }
  610. type QuarterData struct {
  611. Year int
  612. DataList []*EdbDataList
  613. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  614. ChartLegend string
  615. Years string
  616. }
  617. type QuarterXDateItem struct {
  618. StartDate time.Time
  619. EndDate time.Time
  620. ShowName string
  621. ChartLegend string
  622. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  623. }
  624. type QuarterDataByUniqueCode struct {
  625. Year int
  626. DataList []*EdbDataListByUniqueCode
  627. }
  628. type ChartEdbInfoDetailResp struct {
  629. EdbInfo *ChartEdbInfoMapping
  630. }
  631. type ChartInfoDetailResp struct {
  632. ChartInfo *ChartInfoView
  633. EdbInfoList []*ChartEdbInfoMapping
  634. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  635. YDataList []YData `description:"柱方图的y轴数据"`
  636. XDataList []XData `description:"商品价格曲线的X轴数据"`
  637. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  638. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  639. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  640. }
  641. // XData 商品价格曲线的的x轴数据
  642. type XData struct {
  643. Name string `description:"别名"`
  644. NameEn string `description:"英文别名"`
  645. }
  646. // YData 柱方图的y轴数据
  647. type YData struct {
  648. Date string `description:"数据日期"`
  649. ConfigDate time.Time `description:"配置的日期" json:"-"`
  650. Color string `description:"数据颜色"`
  651. Name string `description:"别名"`
  652. NameEn string `description:"英文别名"`
  653. Value []float64 `description:"每个指标的值"`
  654. NoDataEdbList []int `description:"没有数据的指标列表"`
  655. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  656. NameList []string `description:"每个值对应的名称"`
  657. EnNameList []string `description:"每个值对应的英文名称"`
  658. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  659. M []int `description:"对应开始日期的间隔值" json:"-"`
  660. Unit string `description:"中文单位名称"`
  661. UnitEn string `description:"英文单位名称"`
  662. }
  663. func ModifyChartInfoAndMapping(edbInfoIdStr string, req *SaveChartInfoReq, chartType int) (err error) {
  664. o := orm.NewOrmUsingDB("data")
  665. to, err := o.Begin()
  666. if err != nil {
  667. return
  668. }
  669. defer func() {
  670. if err != nil {
  671. _ = to.Rollback()
  672. } else {
  673. _ = to.Commit()
  674. }
  675. }()
  676. //非季节图
  677. if chartType != 2 {
  678. updateStr := `edb_info_ids=?,
  679. modify_time = NOW(),
  680. date_type=?,
  681. start_date=?,
  682. end_date=?,
  683. left_min=?,
  684. left_max=?,
  685. right_min=?,
  686. right_max=?,
  687. right2_min=?,
  688. right2_max=?,
  689. min_max_save=?,
  690. start_year=?`
  691. pars := []interface{}{edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.Right2Min, req.Right2Max, req.MinMaxSave, req.StartYear}
  692. if req.ExtraConfig != `` {
  693. updateStr += `,extra_config=? `
  694. pars = append(pars, req.ExtraConfig)
  695. }
  696. pars = append(pars, req.ChartInfoId)
  697. sql := ` UPDATE chart_info SET ` + updateStr + ` WHERE chart_info_id = ?`
  698. _, err = to.Raw(sql, pars).Exec()
  699. if err != nil {
  700. fmt.Println("UPDATE chart_info Err:", err.Error())
  701. return err
  702. }
  703. } else {
  704. sql := ` UPDATE chart_info
  705. SET
  706. edb_info_ids=?,
  707. modify_time = NOW(),
  708. calendar=?,
  709. date_type=?,
  710. start_date=?,
  711. end_date=?,
  712. season_start_date=?,
  713. season_end_date=?,
  714. left_min=?,
  715. left_max=?,
  716. right_min=?,
  717. right_max=?,
  718. right2_min=?,
  719. right2_max=?,
  720. min_max_save=?,
  721. start_year=?
  722. WHERE chart_info_id = ?`
  723. _, err = to.Raw(sql, edbInfoIdStr, req.Calendar, req.DateType, req.StartDate, req.EndDate, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.Right2Min, req.Right2Max, req.MinMaxSave, req.StartYear, req.ChartInfoId).Exec()
  724. if err != nil {
  725. fmt.Println("UPDATE chart_info Err:", err.Error())
  726. return err
  727. }
  728. }
  729. var edbInfoIdArr []string
  730. mapList := make([]*ChartEdbMapping, 0)
  731. for _, v := range req.ChartEdbInfoList {
  732. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  733. var count int
  734. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  735. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  736. if err != nil {
  737. fmt.Println("QueryRow Err:", err.Error())
  738. return err
  739. }
  740. if count > 0 {
  741. msql := `UPDATE chart_edb_mapping
  742. SET
  743. modify_time = NOW(),
  744. max_data = ?,
  745. min_data = ?,
  746. is_order = ?,
  747. is_axis = ?,
  748. edb_info_type = ?,
  749. lead_value = ?,
  750. lead_unit = ?,
  751. chart_style = ?,
  752. chart_color = ?,
  753. predict_chart_color = ?,
  754. chart_width = ?
  755. WHERE chart_info_id =? AND edb_info_id=? `
  756. _, 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()
  757. if err != nil {
  758. fmt.Println("chart_edb_mapping Err:" + err.Error())
  759. return err
  760. }
  761. } else {
  762. mapItem := new(ChartEdbMapping)
  763. mapItem.ChartInfoId = req.ChartInfoId
  764. mapItem.EdbInfoId = v.EdbInfoId
  765. mapItem.CreateTime = time.Now()
  766. mapItem.ModifyTime = time.Now()
  767. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  768. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  769. mapItem.MaxData = v.MaxData
  770. mapItem.MinData = v.MinData
  771. mapItem.IsOrder = v.IsOrder
  772. mapItem.IsAxis = v.IsAxis
  773. mapItem.EdbInfoType = v.EdbInfoType
  774. mapItem.LeadValue = v.LeadValue
  775. mapItem.LeadUnit = v.LeadUnit
  776. mapItem.ChartStyle = v.ChartStyle
  777. mapItem.ChartColor = v.ChartColor
  778. mapItem.PredictChartColor = v.PredictChartColor
  779. mapItem.ChartWidth = v.ChartWidth
  780. mapList = append(mapList, mapItem)
  781. }
  782. }
  783. if len(mapList) > 0 {
  784. _, err = to.InsertMulti(1, mapList)
  785. if err != nil {
  786. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  787. return err
  788. }
  789. }
  790. if len(edbInfoIdArr) > 0 {
  791. edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
  792. if edbInfoIdStr != "" {
  793. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id NOT IN(` + edbInfoIdStr + `)`
  794. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  795. if err != nil {
  796. fmt.Println("delete err:" + err.Error())
  797. return err
  798. }
  799. }
  800. }
  801. return
  802. }
  803. // EditChartInfoAndMapping 修改 ETA图库 的 图表与指标 的关系
  804. func EditChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, barChartConf string, chartEdbInfoList []*ChartSaveItem, seasonExtra string) (err error) {
  805. o := orm.NewOrmUsingDB("data")
  806. to, err := o.Begin()
  807. if err != nil {
  808. return
  809. }
  810. defer func() {
  811. if err != nil {
  812. _ = to.Rollback()
  813. } else {
  814. _ = to.Commit()
  815. }
  816. }()
  817. var pars []interface{}
  818. pars = append(pars, req.ChartName)
  819. pars = append(pars, req.ChartNameEn)
  820. pars = append(pars, edbInfoIdStr)
  821. pars = append(pars, req.ChartType)
  822. pars = append(pars, req.ChartClassifyId)
  823. pars = append(pars, disabled)
  824. pars = append(pars, barChartConf)
  825. pars = append(pars, req.ExtraConfig)
  826. pars = append(pars, seasonExtra)
  827. pars = append(pars, req.StartYear)
  828. pars = append(pars, req.ChartThemeId)
  829. pars = append(pars, req.SourcesFrom)
  830. pars = append(pars, req.Instructions)
  831. pars = append(pars, req.MarkersLines)
  832. pars = append(pars, req.MarkersAreas)
  833. pars = append(pars, req.Unit)
  834. pars = append(pars, req.UnitEn)
  835. sql := ` UPDATE chart_info
  836. SET
  837. chart_name =?,
  838. chart_name_en =?,
  839. edb_info_ids=?,
  840. chart_type=?,
  841. chart_classify_id = ?,
  842. modify_time = NOW(),
  843. disabled = ?,
  844. bar_config = ?,
  845. extra_config = ?,
  846. season_extra_config = ?,
  847. start_year = ?,
  848. chart_theme_id = ?,
  849. sources_from = ?,
  850. instructions = ?,
  851. markers_lines = ?,
  852. markers_areas = ?,
  853. unit = ?,
  854. unit_en = ?
  855. `
  856. if calendar != "" {
  857. sql += `,calendar = ? `
  858. pars = append(pars, calendar)
  859. }
  860. if dateType > 0 {
  861. sql += `,date_type = ? `
  862. pars = append(pars, dateType)
  863. }
  864. sql += `,start_date = ? `
  865. pars = append(pars, req.StartDate)
  866. sql += `,end_date = ? `
  867. pars = append(pars, req.EndDate)
  868. sql += `,season_start_date = ? `
  869. pars = append(pars, req.StartDate)
  870. sql += `,season_end_date = ? `
  871. pars = append(pars, req.EndDate)
  872. sql += `,left_min = ? `
  873. pars = append(pars, req.LeftMin)
  874. sql += `,left_max = ? `
  875. pars = append(pars, req.LeftMax)
  876. sql += `,right_min = ? `
  877. pars = append(pars, req.RightMin)
  878. sql += `,right_max = ? `
  879. pars = append(pars, req.RightMax)
  880. sql += `,right2_min = ? `
  881. pars = append(pars, req.Right2Min)
  882. sql += `,right2_max = ? `
  883. pars = append(pars, req.Right2Max)
  884. sql += `,min_max_save = ? `
  885. pars = append(pars, req.MinMaxSave)
  886. sql += `WHERE chart_info_id = ?`
  887. pars = append(pars, req.ChartInfoId)
  888. _, err = to.Raw(sql, pars).Exec()
  889. if err != nil {
  890. fmt.Println("UPDATE chart_info Err:", err.Error())
  891. return err
  892. }
  893. chartEdbMappingIdList := make([]string, 0)
  894. for _, v := range chartEdbInfoList {
  895. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  896. var tmpChartEdbMapping *ChartEdbMapping
  897. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  898. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_DEFAULT).QueryRow(&tmpChartEdbMapping)
  899. if err != nil && err.Error() != utils.ErrNoRow() {
  900. fmt.Println("QueryRow Err:", err.Error())
  901. return err
  902. }
  903. if tmpChartEdbMapping != nil {
  904. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  905. tmpChartEdbMapping.ModifyTime = time.Now()
  906. tmpChartEdbMapping.MaxData = v.MaxData
  907. tmpChartEdbMapping.MinData = v.MinData
  908. tmpChartEdbMapping.IsOrder = v.IsOrder
  909. tmpChartEdbMapping.IsAxis = v.IsAxis
  910. tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  911. tmpChartEdbMapping.LeadValue = v.LeadValue
  912. tmpChartEdbMapping.LeadUnit = v.LeadUnit
  913. tmpChartEdbMapping.ChartStyle = v.ChartStyle
  914. tmpChartEdbMapping.ChartColor = v.ChartColor
  915. tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  916. tmpChartEdbMapping.ChartWidth = v.ChartWidth
  917. tmpChartEdbMapping.EdbAliasName = v.EdbAliasName
  918. tmpChartEdbMapping.IsConvert = v.IsConvert
  919. tmpChartEdbMapping.ConvertType = v.ConvertType
  920. tmpChartEdbMapping.ConvertValue = v.ConvertValue
  921. tmpChartEdbMapping.ConvertUnit = v.ConvertUnit
  922. tmpChartEdbMapping.ConvertEnUnit = v.ConvertEnUnit
  923. _, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth", "EdbAliasName",
  924. "IsConvert", "ConvertType", "ConvertValue", "ConvertUnit", "ConvertEnUnit")
  925. if err != nil {
  926. fmt.Println("chart_edb_mapping Err:" + err.Error())
  927. return err
  928. }
  929. } else {
  930. mapItem := new(ChartEdbMapping)
  931. mapItem.ChartInfoId = req.ChartInfoId
  932. mapItem.EdbInfoId = v.EdbInfoId
  933. mapItem.CreateTime = time.Now()
  934. mapItem.ModifyTime = time.Now()
  935. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  936. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  937. mapItem.MaxData = v.MaxData
  938. mapItem.MinData = v.MinData
  939. mapItem.IsOrder = v.IsOrder
  940. mapItem.IsAxis = v.IsAxis
  941. mapItem.EdbInfoType = v.EdbInfoType
  942. mapItem.LeadValue = v.LeadValue
  943. mapItem.LeadUnit = v.LeadUnit
  944. mapItem.ChartStyle = v.ChartStyle
  945. mapItem.ChartColor = v.ChartColor
  946. mapItem.PredictChartColor = v.PredictChartColor
  947. mapItem.ChartWidth = v.ChartWidth
  948. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  949. mapItem.EdbAliasName = v.EdbAliasName
  950. mapItem.IsConvert = v.IsConvert
  951. mapItem.ConvertType = v.ConvertType
  952. mapItem.ConvertValue = v.ConvertValue
  953. mapItem.ConvertUnit = v.ConvertUnit
  954. mapItem.ConvertEnUnit = v.ConvertEnUnit
  955. tmpId, err := to.Insert(mapItem)
  956. if err != nil {
  957. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  958. return err
  959. }
  960. mapItem.ChartEdbMappingId = int(tmpId)
  961. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  962. }
  963. }
  964. if len(chartEdbMappingIdList) > 0 {
  965. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  966. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  967. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  968. if err != nil {
  969. fmt.Println("delete err:" + err.Error())
  970. return err
  971. }
  972. }
  973. return
  974. }
  975. // EditFutureGoodChartInfoAndMapping 修改商品价格曲线的 图表与指标 的关系
  976. func EditFutureGoodChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, barChartConf string) (err error) {
  977. o := orm.NewOrmUsingDB("data")
  978. to, err := o.Begin()
  979. if err != nil {
  980. return
  981. }
  982. defer func() {
  983. if err != nil {
  984. _ = to.Rollback()
  985. } else {
  986. _ = to.Commit()
  987. }
  988. }()
  989. var pars []interface{}
  990. pars = append(pars, req.ChartName)
  991. pars = append(pars, edbInfoIdStr)
  992. pars = append(pars, req.ChartType)
  993. pars = append(pars, req.ChartClassifyId)
  994. pars = append(pars, disabled)
  995. pars = append(pars, barChartConf)
  996. sql := ` UPDATE chart_info
  997. SET
  998. chart_name =?,
  999. edb_info_ids=?,
  1000. chart_type=?,
  1001. chart_classify_id = ?,
  1002. modify_time = NOW(),
  1003. disabled = ?,
  1004. bar_config = ?
  1005. `
  1006. if calendar != "" {
  1007. sql += `,calendar = ? `
  1008. pars = append(pars, calendar)
  1009. }
  1010. if dateType > 0 {
  1011. sql += `,date_type = ? `
  1012. pars = append(pars, dateType)
  1013. }
  1014. sql += `,start_date = ? `
  1015. pars = append(pars, req.StartDate)
  1016. sql += `,end_date = ? `
  1017. pars = append(pars, req.EndDate)
  1018. sql += `,season_start_date = ? `
  1019. pars = append(pars, req.StartDate)
  1020. sql += `,season_end_date = ? `
  1021. pars = append(pars, req.EndDate)
  1022. sql += `,left_min = ? `
  1023. pars = append(pars, req.LeftMin)
  1024. sql += `,left_max = ? `
  1025. pars = append(pars, req.LeftMax)
  1026. sql += `,right_min = ? `
  1027. pars = append(pars, req.RightMin)
  1028. sql += `,right_max = ? `
  1029. pars = append(pars, req.RightMax)
  1030. sql += `,right2_min = ? `
  1031. pars = append(pars, req.Right2Min)
  1032. sql += `,right2_max = ? `
  1033. pars = append(pars, req.Right2Max)
  1034. sql += `WHERE chart_info_id = ?`
  1035. pars = append(pars, req.ChartInfoId)
  1036. _, err = to.Raw(sql, pars).Exec()
  1037. if err != nil {
  1038. fmt.Println("UPDATE chart_info Err:", err.Error())
  1039. return err
  1040. }
  1041. chartEdbMappingIdList := make([]string, 0)
  1042. for _, v := range req.BarChartInfo.EdbInfoIdList {
  1043. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  1044. var tmpChartEdbMapping *ChartEdbMapping
  1045. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  1046. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, v.Source).QueryRow(&tmpChartEdbMapping)
  1047. if err != nil && err.Error() != utils.ErrNoRow() {
  1048. fmt.Println("QueryRow Err:", err.Error())
  1049. return err
  1050. }
  1051. if tmpChartEdbMapping != nil {
  1052. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  1053. //tmpChartEdbMapping.ModifyTime = time.Now()
  1054. //tmpChartEdbMapping.MaxData = v.MaxData
  1055. //tmpChartEdbMapping.MinData = v.MinData
  1056. //tmpChartEdbMapping.IsOrder = v.IsOrder
  1057. //tmpChartEdbMapping.IsAxis = v.IsAxis
  1058. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  1059. //tmpChartEdbMapping.LeadValue = v.LeadValue
  1060. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  1061. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  1062. //tmpChartEdbMapping.ChartColor = v.ChartColor
  1063. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  1064. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  1065. //_, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  1066. //if err != nil {
  1067. // fmt.Println("chart_edb_mapping Err:" + err.Error())
  1068. // return err
  1069. //}
  1070. } else {
  1071. mapItem := new(ChartEdbMapping)
  1072. mapItem.ChartInfoId = req.ChartInfoId
  1073. mapItem.EdbInfoId = v.EdbInfoId
  1074. mapItem.CreateTime = time.Now()
  1075. mapItem.ModifyTime = time.Now()
  1076. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1077. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  1078. //mapItem.MaxData = v.MaxData
  1079. //mapItem.MinData = v.MinData
  1080. mapItem.IsOrder = true
  1081. mapItem.IsAxis = 1
  1082. mapItem.EdbInfoType = 1
  1083. //mapItem.LeadValue = v.LeadValue
  1084. //mapItem.LeadUnit = v.LeadUnit
  1085. //mapItem.ChartStyle = v.ChartStyle
  1086. //mapItem.ChartColor = v.ChartColor
  1087. //mapItem.PredictChartColor = v.PredictChartColor
  1088. //mapItem.ChartWidth = v.ChartWidth
  1089. mapItem.Source = v.Source
  1090. tmpId, err := to.Insert(mapItem)
  1091. if err != nil {
  1092. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  1093. return err
  1094. }
  1095. mapItem.ChartEdbMappingId = int(tmpId)
  1096. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  1097. }
  1098. }
  1099. if len(chartEdbMappingIdList) > 0 {
  1100. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  1101. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  1102. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  1103. if err != nil {
  1104. fmt.Println("delete err:" + err.Error())
  1105. return err
  1106. }
  1107. }
  1108. return
  1109. }
  1110. // EditChartEnInfoAndEdbEnInfo
  1111. // @Description: 修改图表和指标的英文信息
  1112. // @author: Roc
  1113. // @datetime 2024-04-18 13:58:13
  1114. // @param req *EditChartEnInfoReq
  1115. // @return err error
  1116. func EditChartEnInfoAndEdbEnInfo(req *EditChartEnInfoReq) (err error) {
  1117. o := orm.NewOrmUsingDB("data")
  1118. to, err := o.Begin()
  1119. if err != nil {
  1120. return
  1121. }
  1122. defer func() {
  1123. if err != nil {
  1124. _ = to.Rollback()
  1125. } else {
  1126. _ = to.Commit()
  1127. }
  1128. }()
  1129. var pars []interface{}
  1130. updateField := ` chart_name_en =?, `
  1131. pars = append(pars, req.ChartNameEn)
  1132. if req.ExtraConfig != `` {
  1133. updateField += ` extra_config =?, `
  1134. pars = append(pars, req.ExtraConfig)
  1135. }
  1136. sql := ` UPDATE chart_info
  1137. SET ` + updateField + ` modify_time = NOW()
  1138. WHERE chart_info_id = ?`
  1139. pars = append(pars, req.ChartInfoId)
  1140. _, err = to.Raw(sql, pars).Exec()
  1141. if err != nil {
  1142. fmt.Println("UPDATE chart_info Err:", err.Error())
  1143. return err
  1144. }
  1145. var edbInfoIdArr []string
  1146. for _, v := range req.ChartEdbInfoList {
  1147. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  1148. var count int
  1149. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  1150. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  1151. if err != nil {
  1152. fmt.Println("QueryRow Err:", err.Error())
  1153. return err
  1154. }
  1155. if count > 0 {
  1156. msql := ` UPDATE edb_info
  1157. SET
  1158. edb_name_en =?,
  1159. unit_en = ?,
  1160. modify_time = NOW()
  1161. WHERE edb_info_id = ? `
  1162. _, err = to.Raw(msql, v.EdbNameEn, v.UnitEn, v.EdbInfoId).Exec()
  1163. if err != nil {
  1164. fmt.Println("edb_info Err:" + err.Error())
  1165. return err
  1166. }
  1167. }
  1168. }
  1169. return
  1170. }
  1171. // EditChartBaseInfoAndEdbEnInfo
  1172. // @Description: 修改图表和指标的基础信息
  1173. // @author: Roc
  1174. // @datetime 2024-04-18 13:28:16
  1175. // @param req *EditChartInfoBaseReq
  1176. // @param chartItem *ChartInfo
  1177. // @param lang string
  1178. // @return err error
  1179. func EditChartBaseInfoAndEdbEnInfo(req *EditChartInfoBaseReq, chartItem *ChartInfo, lang string) (err error) {
  1180. o := orm.NewOrmUsingDB("data")
  1181. to, err := o.Begin()
  1182. if err != nil {
  1183. return
  1184. }
  1185. defer func() {
  1186. if err != nil {
  1187. _ = to.Rollback()
  1188. } else {
  1189. _ = to.Commit()
  1190. }
  1191. }()
  1192. updateChartCols := make([]string, 0)
  1193. switch lang {
  1194. case utils.EnLangVersion:
  1195. chartItem.ChartNameEn = req.ChartName
  1196. updateChartCols = append(updateChartCols, "ChartNameEn")
  1197. default:
  1198. chartItem.ChartName = req.ChartName
  1199. updateChartCols = append(updateChartCols, "ChartName")
  1200. }
  1201. if req.ExtraConfig != `` {
  1202. chartItem.ExtraConfig = req.ExtraConfig
  1203. updateChartCols = append(updateChartCols, "ExtraConfig")
  1204. }
  1205. chartItem.ModifyTime = time.Now()
  1206. updateChartCols = append(updateChartCols, "ModifyTime")
  1207. _, err = to.Update(chartItem, updateChartCols...)
  1208. if err != nil {
  1209. fmt.Println("UPDATE chart_info Err:", err.Error())
  1210. return err
  1211. }
  1212. var edbInfoIdArr []string
  1213. for _, v := range req.ChartEdbInfoList {
  1214. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  1215. var count int
  1216. csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? `
  1217. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId).QueryRow(&count)
  1218. if err != nil {
  1219. fmt.Println("QueryRow Err:", err.Error())
  1220. return err
  1221. }
  1222. if count > 0 {
  1223. msql := ` UPDATE edb_info SET modify_time = NOW() `
  1224. pars := make([]interface{}, 0)
  1225. switch lang {
  1226. case utils.EnLangVersion:
  1227. msql += ` ,edb_name_en = ? `
  1228. pars = append(pars, v.EdbName)
  1229. // 如果有传单位
  1230. if v.Unit != `` {
  1231. msql += ` ,unit_en = ? `
  1232. pars = append(pars, v.Unit)
  1233. }
  1234. default:
  1235. msql += ` ,edb_name = ? `
  1236. pars = append(pars, v.EdbName)
  1237. // 如果有传单位
  1238. if v.Unit != `` {
  1239. msql += ` ,unit = ? `
  1240. pars = append(pars, v.Unit)
  1241. }
  1242. }
  1243. msql += ` WHERE edb_info_id = ? `
  1244. pars = append(pars, v.EdbInfoId)
  1245. _, err = to.Raw(msql, pars...).Exec()
  1246. if err != nil {
  1247. fmt.Println("edb_info Err:" + err.Error())
  1248. return err
  1249. }
  1250. }
  1251. }
  1252. return
  1253. }
  1254. type AddChartInfoReq struct {
  1255. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  1256. ChartClassifyId int `description:"分类id"`
  1257. ChartName string `description:"图表名称"`
  1258. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图"`
  1259. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  1260. StartDate string `description:"自定义开始日期"`
  1261. EndDate string `description:"自定义结束日期"`
  1262. Calendar string `description:"公历/农历"`
  1263. LeftMin string `description:"图表左侧最小值"`
  1264. LeftMax string `description:"图表左侧最大值"`
  1265. RightMin string `description:"图表右侧最小值"`
  1266. RightMax string `description:"图表右侧最大值"`
  1267. Right2Min string `description:"图表右侧2最小值"`
  1268. Right2Max string `description:"图表右侧2最大值"`
  1269. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  1270. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  1271. CorrelationChartInfo CorrelationChartInfoReq `description:"相关性图表配置"`
  1272. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  1273. ChartImage string `description:"封面图" json:"-"`
  1274. SeasonExtraConfig SeasonExtraItem `description:"季节性图表中的配置,json数据"`
  1275. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  1276. ChartThemeId int `description:"图表应用主题ID"`
  1277. SourcesFrom string `description:"图表来源"`
  1278. Instructions string `description:"图表说明"`
  1279. MarkersLines string `description:"标识线"`
  1280. MarkersAreas string `description:"标识区"`
  1281. Unit string `description:"中文单位名称"`
  1282. UnitEn string `description:"英文单位名称"`
  1283. }
  1284. type PreviewChartInfoReq struct {
  1285. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  1286. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今 20:最近N年"`
  1287. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图"`
  1288. StartDate string `description:"自定义开始日期"`
  1289. EndDate string `description:"自定义结束日期"`
  1290. Calendar string `description:"公历/农历"`
  1291. SeasonExtraConfig SeasonExtraItem `description:"季节性图表中的配置,json数据"`
  1292. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  1293. }
  1294. type SeasonExtraItem struct {
  1295. ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
  1296. XStartDate string `description:"横坐标显示的起始日"`
  1297. XEndDate string `description:"横坐标显示的截止日"`
  1298. JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
  1299. }
  1300. type SeasonChartLegend struct {
  1301. Name string
  1302. Value string
  1303. }
  1304. type AddChartInfoResp struct {
  1305. ChartInfoId int `description:"图表id"`
  1306. UniqueCode string `description:"图表唯一编码"`
  1307. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  1308. }
  1309. type QuarterDataList []*QuarterData
  1310. func (m QuarterDataList) Len() int {
  1311. return len(m)
  1312. }
  1313. func (m QuarterDataList) Less(i, j int) bool {
  1314. return m[i].Years < m[j].Years
  1315. }
  1316. func (m QuarterDataList) Swap(i, j int) {
  1317. m[i], m[j] = m[j], m[i]
  1318. }
  1319. // 判断图表指标是否已经存在
  1320. func ChartInfoExist(condition, edbInfoIdStr string) (count int, err error) {
  1321. sql := `SELECT COUNT(1) AS count FROM (
  1322. SELECT GROUP_CONCAT(edb_info_id ORDER BY edb_info_id ASC SEPARATOR ',') AS edb_info_id_str
  1323. FROM chart_edb_mapping AS a
  1324. WHERE 1=1`
  1325. if condition != "" {
  1326. sql += condition
  1327. }
  1328. sql += ` GROUP BY chart_info_id
  1329. )AS t
  1330. WHERE t.edb_info_id_str=?
  1331. GROUP BY t.edb_info_id_str `
  1332. o := orm.NewOrmUsingDB("data")
  1333. err = o.Raw(sql, edbInfoIdStr).QueryRow(&count)
  1334. return
  1335. }
  1336. func ChartInfoSearchByKeyWord(keyword string, showSysId int) (searchList []*ChartInfo, err error) {
  1337. o := orm.NewOrmUsingDB("data")
  1338. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  1339. var pars []interface{}
  1340. if keyword != "" {
  1341. sql += `AND chart_name LIKE ? `
  1342. pars = append(pars, utils.GetLikeKeyword(keyword))
  1343. }
  1344. if showSysId > 0 {
  1345. sql += ` AND sys_user_id = ? `
  1346. pars = append(pars, showSysId)
  1347. }
  1348. sql += ` ORDER BY create_time DESC `
  1349. if keyword == "" {
  1350. sql += ` LIMIT 100 `
  1351. }
  1352. _, err = o.Raw(sql, pars).QueryRows(&searchList)
  1353. return
  1354. }
  1355. // ChartInfoSearchByEmptyKeyWord 没有关键字的时候获取默认100条数据
  1356. func ChartInfoSearchByEmptyKeyWord(showSysId int, sourceList []int, noPermissionChartIdList []int, startSize, pageSize int) (total int64, searchList []*ChartInfo, err error) {
  1357. num := len(sourceList)
  1358. o := orm.NewOrmUsingDB("data")
  1359. baseSql := ` FROM chart_info WHERE 1=1 AND source in (` + utils.GetOrmInReplace(num) + `)`
  1360. var basePars []interface{}
  1361. basePars = append(basePars, sourceList)
  1362. if showSysId > 0 {
  1363. baseSql += ` AND sys_user_id = ? `
  1364. basePars = append(basePars, showSysId)
  1365. }
  1366. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  1367. if lenNoPermissionChartIdList > 0 {
  1368. baseSql += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  1369. basePars = append(basePars, noPermissionChartIdList)
  1370. }
  1371. // 查找数量
  1372. totalSql := " SELECT count(1) as total " + baseSql
  1373. err = o.Raw(totalSql, basePars).QueryRow(&total)
  1374. if err != nil {
  1375. return
  1376. }
  1377. // 查找列表数据
  1378. sql := " SELECT * " + baseSql + ` ORDER BY create_time DESC LIMIT ?,? `
  1379. basePars = append(basePars, startSize, pageSize)
  1380. _, err = o.Raw(sql, basePars).QueryRows(&searchList)
  1381. return
  1382. }
  1383. func GetNextChartInfo(classifyId int) (item *ChartInfo, err error) {
  1384. o := orm.NewOrmUsingDB("data")
  1385. sql := ` SELECT b.* FROM chart_classify AS a
  1386. INNER JOIN chart_info AS b ON a.chart_classify_id=b.chart_classify_id
  1387. WHERE a.chart_classify_id>?
  1388. ORDER BY a.chart_classify_id ASC
  1389. LIMIT 1 `
  1390. err = o.Raw(sql, classifyId).QueryRow(&item)
  1391. return
  1392. }
  1393. // GetChartInfoRefreshData 获取图表关联的指标信息
  1394. func GetChartInfoRefreshData(chartInfoId int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  1395. o := orm.NewOrmUsingDB("data")
  1396. sql := ` SELECT b.* FROM chart_edb_mapping AS a
  1397. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  1398. WHERE a.chart_info_id=? `
  1399. edbInfoList := make([]*EdbInfo, 0)
  1400. _, err = o.Raw(sql, chartInfoId).QueryRows(&edbInfoList)
  1401. if err != nil {
  1402. return
  1403. }
  1404. for _, v := range edbInfoList {
  1405. fmt.Println(v.EdbInfoId, v.EdbType)
  1406. if v.EdbType == 1 {
  1407. baseEdbInfoArr = append(baseEdbInfoArr, v)
  1408. } else {
  1409. calculateInfoArr = append(calculateInfoArr, v)
  1410. newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
  1411. if err != nil {
  1412. return baseEdbInfoArr, calculateInfoArr, err
  1413. }
  1414. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  1415. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  1416. }
  1417. }
  1418. return
  1419. }
  1420. func GetChartRefreshEdbInfo(edbInfoId, source, n int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  1421. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  1422. if err != nil && err.Error() != utils.ErrNoRow() {
  1423. return
  1424. }
  1425. n++
  1426. for _, item := range calculateList {
  1427. fmt.Println(item.EdbInfoId)
  1428. if item.EdbType == 1 {
  1429. baseEdbInfoArr = append(baseEdbInfoArr, item)
  1430. } else {
  1431. calculateInfoArr = append(calculateInfoArr, item)
  1432. if n > 10 {
  1433. return
  1434. }
  1435. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetChartRefreshEdbInfo(item.EdbInfoId, item.Source, n)
  1436. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  1437. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  1438. }
  1439. }
  1440. return
  1441. }
  1442. type SetChartInfoImageReq struct {
  1443. ChartInfoId int `description:"图表ID"`
  1444. ImageUrl string `description:"图表图片地址"`
  1445. }
  1446. func EditChartInfoImage(req *SetChartInfoImageReq) (err error) {
  1447. o := orm.NewOrmUsingDB("data")
  1448. sql := ` UPDATE chart_info SET chart_image=?, modify_time = NOW() WHERE chart_info_id = ? `
  1449. _, err = o.Raw(sql, req.ImageUrl, req.ChartInfoId).Exec()
  1450. if err != nil {
  1451. fmt.Println("EditChartInfoImage Err:", err.Error())
  1452. return err
  1453. }
  1454. return
  1455. }
  1456. type ChartInfoDetailFromUniqueCodeResp struct {
  1457. ChartInfo *ChartInfoView
  1458. Status bool `description:"true:图表存在,false:图表不存在"`
  1459. EdbInfoList []*ChartEdbInfoMapping
  1460. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  1461. YDataList []YData `description:"柱方图的y轴数据"`
  1462. XDataList []XData `description:"商品价格曲线的X轴数据"`
  1463. BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
  1464. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  1465. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  1466. }
  1467. func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfo, err error) {
  1468. o := orm.NewOrmUsingDB("data")
  1469. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  1470. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  1471. return
  1472. }
  1473. func GetChartInfoViewByUniqueCode(uniqueCode string) (item *ChartInfoView, err error) {
  1474. o := orm.NewOrmUsingDB("data")
  1475. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  1476. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  1477. return
  1478. }
  1479. // GetFirstChartInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  1480. func GetFirstChartInfoByClassifyId(classifyId int) (item *ChartInfo, err error) {
  1481. o := orm.NewOrmUsingDB("data")
  1482. sql := ` SELECT * FROM chart_info WHERE chart_classify_id=? order by sort asc,chart_info_id asc limit 1`
  1483. err = o.Raw(sql, classifyId).QueryRow(&item)
  1484. return
  1485. }
  1486. // UpdateChartInfoSortByClassifyId 根据图表id更新排序
  1487. func UpdateChartInfoSortByClassifyId(classifyId, nowSort, prevChartInfoId int, sourceList []int, updateSort string) (err error) {
  1488. o := orm.NewOrmUsingDB("data")
  1489. num := len(sourceList)
  1490. if num <= 0 {
  1491. return
  1492. }
  1493. sql := ` update chart_info set sort = ` + updateSort + ` WHERE chart_classify_id=? AND source in (` + utils.GetOrmInReplace(num) + `) AND `
  1494. if prevChartInfoId > 0 {
  1495. sql += ` (sort > ? or (chart_info_id > ` + fmt.Sprint(prevChartInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  1496. }
  1497. _, err = o.Raw(sql, classifyId, sourceList, nowSort).Exec()
  1498. return
  1499. }
  1500. // Update 更新图表基础信息
  1501. func (chartInfo *ChartInfo) Update(cols []string) (err error) {
  1502. o := orm.NewOrmUsingDB("data")
  1503. _, err = o.Update(chartInfo, cols...)
  1504. return
  1505. }
  1506. type ChartInfoView struct {
  1507. ChartInfoId int `orm:"column(chart_info_id);pk"`
  1508. ChartName string `description:"来源名称"`
  1509. ChartNameEn string `description:"英文图表名称"`
  1510. Unit string `description:"中文单位名称"`
  1511. UnitEn string `description:"英文单位名称"`
  1512. ChartClassifyId int `description:"图表分类id"`
  1513. ChartClassifyName string `description:"图表名称"`
  1514. SysUserId int
  1515. SysUserRealName string
  1516. UniqueCode string `description:"图表唯一编码"`
  1517. CreateTime time.Time
  1518. ModifyTime time.Time
  1519. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  1520. StartDate string `description:"自定义开始日期"`
  1521. EndDate string `description:"自定义结束日期"`
  1522. IsSetName int `description:"设置名称"`
  1523. EdbInfoIds string `description:"指标id"`
  1524. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  1525. Calendar string `description:"公历/农历"`
  1526. SeasonStartDate string `description:"季节性图开始日期"`
  1527. SeasonEndDate string `description:"季节性图开始日期"`
  1528. ChartImage string `description:"图表图片"`
  1529. Sort int `description:"排序字段,数字越小越排前面"`
  1530. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  1531. MyChartId int
  1532. MyChartClassifyId string `description:"我的图表分类,多个用逗号隔开"`
  1533. ChartClassify []*ChartClassifyView
  1534. EdbEndDate string `description:"指标最新更新日期"`
  1535. XMin string `description:"图表X轴最小值"`
  1536. XMax string `description:"图表X轴最大值"`
  1537. LeftMin string `description:"图表左侧最小值"`
  1538. LeftMax string `description:"图表左侧最大值"`
  1539. RightMin string `description:"图表右侧最小值"`
  1540. RightMax string `description:"图表右侧最大值"`
  1541. Right2Min string `description:"图表右侧最小值"`
  1542. Right2Max string `description:"图表右侧最大值"`
  1543. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  1544. IsEdit bool `description:"是否有编辑权限"`
  1545. IsEnChart bool `description:"是否展示英文标识"`
  1546. WarnMsg string `description:"错误信息"`
  1547. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  1548. BarConfig string `description:"柱方图的配置,json数据" json:"-"`
  1549. Source int `description:"1:ETA图库;2:商品价格曲线;3:相关性图表"`
  1550. //CorrelationLeadUnit string `description:"相关性图表-领先单位"`
  1551. ExtraConfig string `description:"图表额外配置,json数据"`
  1552. ChartSource string `description:"图表来源str"`
  1553. ChartSourceEn string `description:"图表来源(英文)"`
  1554. Button ChartViewButton `description:"操作按钮"`
  1555. SeasonExtraConfig string `description:"季节性图表中的配置,json数据"`
  1556. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  1557. ChartThemeId int `description:"图表应用主题ID"`
  1558. ChartThemeStyle string `description:"图表应用主题样式"`
  1559. SourcesFrom string `description:"图表来源"`
  1560. Instructions string `description:"图表说明"`
  1561. MarkersLines string `description:"标识线"`
  1562. MarkersAreas string `description:"标识区"`
  1563. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  1564. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  1565. ForumChartInfoId int `description:"社区的图表ID"`
  1566. }
  1567. type ChartViewButton struct {
  1568. IsEdit bool `description:"是否有编辑权限"`
  1569. IsEnChart bool `description:"是否展示英文标识"`
  1570. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  1571. IsCopy bool `description:"是否有另存为按钮"`
  1572. IsSetName int `description:"设置名称"`
  1573. }
  1574. type ImageSvgToPngResp struct {
  1575. Data struct {
  1576. ResourceURL string `json:"ResourceUrl"`
  1577. } `json:"Data"`
  1578. ErrCode string `json:"ErrCode"`
  1579. ErrMsg string `json:"ErrMsg"`
  1580. IsSendEmail bool `json:"IsSendEmail"`
  1581. Msg string `json:"Msg"`
  1582. Ret int64 `json:"Ret"`
  1583. Success bool `json:"Success"`
  1584. }
  1585. // GetChartInfoByClassifyIdAndName 根据分类id和图表名获取图表信息
  1586. func GetChartInfoByClassifyIdAndName(classifyId int, chartName string) (item *ChartInfo, err error) {
  1587. o := orm.NewOrmUsingDB("data")
  1588. sql := ` SELECT * FROM chart_info WHERE chart_classify_id = ? and chart_name=? `
  1589. err = o.Raw(sql, classifyId, chartName).QueryRow(&item)
  1590. return
  1591. }
  1592. // BatchChartRefreshReq 批量刷新图表请求
  1593. type BatchChartRefreshReq struct {
  1594. ChartInfoCode []string `description:"图表编码数组"`
  1595. ReportId int `description:"报告id"`
  1596. ReportChapterId int `description:"报告章节id"`
  1597. Source string `description:"来源,枚举值:report、english_report、smart_report"`
  1598. }
  1599. // GetChartInfoListByUniqueCodeSlice 根据图表编码获取图表列表数据
  1600. func GetChartInfoListByUniqueCodeSlice(uniqueCodeSlice []string) (total int64, items []*ChartInfo, err error) {
  1601. if len(uniqueCodeSlice) <= 0 {
  1602. return
  1603. }
  1604. o := orm.NewOrmUsingDB("data")
  1605. sql := ` SELECT * FROM chart_info WHERE unique_code in ("` + strings.Join(uniqueCodeSlice, `","`) + `") `
  1606. total, err = o.Raw(sql).QueryRows(&items)
  1607. return
  1608. }
  1609. // GetChartInfoRefreshDataByChartInfoIdSlice 根据图表id切片获取对应的指标数据
  1610. func GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice []string) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  1611. o := orm.NewOrmUsingDB("data")
  1612. if len(chartInfoIdSlice) <= 0 {
  1613. return
  1614. }
  1615. sql := ` SELECT b.* FROM chart_edb_mapping AS a
  1616. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  1617. WHERE a.chart_info_id in (` + strings.Join(chartInfoIdSlice, ",") + `) `
  1618. edbInfoList := make([]*EdbInfo, 0)
  1619. _, err = o.Raw(sql).QueryRows(&edbInfoList)
  1620. if err != nil {
  1621. return
  1622. }
  1623. for _, v := range edbInfoList {
  1624. if v.EdbType == 1 {
  1625. baseEdbInfoArr = append(baseEdbInfoArr, v)
  1626. } else {
  1627. calculateInfoArr = append(calculateInfoArr, v)
  1628. newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
  1629. if err != nil {
  1630. return baseEdbInfoArr, calculateInfoArr, err
  1631. }
  1632. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  1633. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  1634. }
  1635. }
  1636. return
  1637. }
  1638. // CopyAddChartInfoReq 复制并新增图表
  1639. type CopyAddChartInfoReq struct {
  1640. ChartInfoId int `description:"待复制的图表id"`
  1641. ChartClassifyId int `description:"分类id"`
  1642. ChartName string `description:"图表名称"`
  1643. }
  1644. // ChartInfoListByEsResp 图表数据Es搜索返回
  1645. type ChartInfoListByEsResp struct {
  1646. Paging *paging.PagingItem
  1647. List []*ChartInfoMore
  1648. }
  1649. // GetChartInfoListByCondition 根据条件获取图表数据了列表
  1650. func GetChartInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*ChartInfo, err error) {
  1651. o := orm.NewOrmUsingDB("data")
  1652. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  1653. if condition != "" {
  1654. sql += condition
  1655. }
  1656. sql += ` ORDER BY chart_info_id DESC LIMIT ?,? `
  1657. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1658. return
  1659. }
  1660. // GetChartInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1661. func GetChartInfoListGroupByUserId(edbIdList []string) (items []*ChartInfo, err error) {
  1662. num := len(edbIdList)
  1663. if num <= 0 {
  1664. return
  1665. }
  1666. o := orm.NewOrmUsingDB("data")
  1667. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY chart_info_id desc `
  1668. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1669. return
  1670. }
  1671. // GetChartInfoListByChartIdList
  1672. // @Description: 根据图表id列表获取列表信息
  1673. // @param edbIdList
  1674. // @return items
  1675. // @return err
  1676. func GetChartInfoListByChartIdList(charIdList []string) (items []*ChartInfo, err error) {
  1677. num := len(charIdList)
  1678. if num <= 0 {
  1679. return
  1680. }
  1681. o := orm.NewOrmUsingDB("data")
  1682. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY chart_info_id desc `
  1683. _, err = o.Raw(sql, charIdList).QueryRows(&items)
  1684. return
  1685. }
  1686. // GetChartInfoListByUserId
  1687. // @Description: 根据图表id列表获取列表信息
  1688. // @param userIdList []int
  1689. // @param source int
  1690. // @return items
  1691. // @return err
  1692. func GetChartInfoListByUserId(userIdList []int, source int) (items []*ChartInfo, err error) {
  1693. num := len(userIdList)
  1694. if num <= 0 {
  1695. return
  1696. }
  1697. o := orm.NewOrmUsingDB("data")
  1698. sql := ` SELECT * FROM chart_info WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY chart_info_id desc `
  1699. _, err = o.Raw(sql, source, userIdList).QueryRows(&items)
  1700. return
  1701. }
  1702. // ModifyChartInfoUserIdByCodeList 根据指标code列表修改创建人
  1703. func ModifyChartInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1704. num := len(edbIdList)
  1705. if num <= 0 {
  1706. return
  1707. }
  1708. o := orm.NewOrmUsingDB("data")
  1709. sql := `UPDATE chart_info SET sys_user_id=?,sys_user_real_name=? WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1710. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1711. return
  1712. }
  1713. // ModifyChartInfoUserIdByOldUserId
  1714. // @Description: 根据旧的用户id修改创建人
  1715. // @author: Roc
  1716. // @datetime 2024-03-25 19:14:59
  1717. // @param oldUserId int
  1718. // @param userId int
  1719. // @param userName string
  1720. // @return err error
  1721. func ModifyChartInfoUserIdByOldUserId(oldUserIdList []int, userId int, userName string) (err error) {
  1722. num := len(oldUserIdList)
  1723. if num <= 0 {
  1724. return
  1725. }
  1726. o := orm.NewOrmUsingDB("data")
  1727. sql := `UPDATE chart_info SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  1728. _, err = o.Raw(sql, userId, userName, oldUserIdList).Exec()
  1729. return
  1730. }
  1731. // BarChartInfoReq 柱方图预览请求数据
  1732. type BarChartInfoReq struct {
  1733. EdbInfoIdList []BarChartInfoEdbItemReq `description:"指标信息"`
  1734. DateList []BarChartInfoDateReq `description:"日期配置"`
  1735. Sort BarChartInfoSortReq `description:"排序"`
  1736. XEdbList []BarChartInfoEdbItemReq `description:"X轴选择的指标列表"`
  1737. YEdbList []BarChartInfoEdbItemReq `description:"Y轴选择的指标列表"`
  1738. Unit string `description:"中文单位"`
  1739. UnitEn string `description:"英文单位"`
  1740. }
  1741. // BarChartInfoEdbItemReq 柱方图预览请求数据(指标相关)
  1742. type BarChartInfoEdbItemReq struct {
  1743. EdbInfoId int `description:"指标ID"`
  1744. Name string `description:"别名"`
  1745. NameEn string `description:"英文别名"`
  1746. Source int `description:"1:ETA图库;2:商品价格"`
  1747. IsConvert int `description:"是否数据转换 0不转 1转"`
  1748. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  1749. ConvertValue float64 `description:"数据转换值"`
  1750. ConvertUnit string `description:"数据转换单位"`
  1751. ConvertEnUnit string `description:"数据转换单位"`
  1752. }
  1753. // BarChartInfoDateReq 柱方图预览请求数据(日期相关)
  1754. type BarChartInfoDateReq struct {
  1755. Type int `description:"配置类型"`
  1756. Date string `description:"固定日期"`
  1757. Value int `description:"N天的值"`
  1758. Color string `description:"颜色"`
  1759. Name string `description:"别名"`
  1760. }
  1761. // BarChartInfoSortReq 柱方图预览请求数据(排序相关)
  1762. type BarChartInfoSortReq struct {
  1763. Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1764. DateIndex int `description:"日期数据的下标,从0开始"`
  1765. }
  1766. // CorrelationChartInfoReq 相关性图表请求体
  1767. type CorrelationChartInfoReq struct {
  1768. LeadValue int `description:"领先期数"`
  1769. LeadUnit string `description:"频度"`
  1770. CalculateValue int `description:"计算窗口"`
  1771. CalculateUnit string `description:"计算频度"`
  1772. BaseCalculateValue int `description:"基础计算窗口(滚动相关性的时候用到)"`
  1773. BaseCalculateUnit string `description:"基础计算频度(滚动相关性的时候用到)"`
  1774. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
  1775. StartDate string `description:"开始日期"`
  1776. EndDate string `description:"结束日期"`
  1777. EdbInfoIdList []CorrelationChartInfoEdbItemReq `description:"指标信息"`
  1778. }
  1779. // CorrelationChartInfoEdbItemReq 相关性图表请求指标
  1780. type CorrelationChartInfoEdbItemReq struct {
  1781. EdbInfoId int `description:"指标ID"`
  1782. Name string `description:"别名"`
  1783. NameEn string `description:"英文别名"`
  1784. //Source int `description:"1:ETA图库;2:商品价格"`
  1785. }
  1786. // EditCorrelationChartInfoAndMapping 修改相关性图表的 图表与指标 的关系
  1787. func EditCorrelationChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, barChartConf string, correlationChart *ChartInfoCorrelation, correlationUpdateCols []string) (err error) {
  1788. o := orm.NewOrmUsingDB("data")
  1789. to, err := o.Begin()
  1790. if err != nil {
  1791. return
  1792. }
  1793. defer func() {
  1794. if err != nil {
  1795. _ = to.Rollback()
  1796. } else {
  1797. _ = to.Commit()
  1798. }
  1799. }()
  1800. var pars []interface{}
  1801. pars = append(pars, req.ChartName)
  1802. pars = append(pars, edbInfoIdStr)
  1803. pars = append(pars, req.ChartType)
  1804. pars = append(pars, req.ChartClassifyId)
  1805. pars = append(pars, disabled)
  1806. pars = append(pars, barChartConf)
  1807. sql := ` UPDATE chart_info
  1808. SET
  1809. chart_name =?,
  1810. edb_info_ids=?,
  1811. chart_type=?,
  1812. chart_classify_id = ?,
  1813. modify_time = NOW(),
  1814. disabled = ?,
  1815. bar_config = ?
  1816. `
  1817. if calendar != "" {
  1818. sql += `,calendar = ? `
  1819. pars = append(pars, calendar)
  1820. }
  1821. if dateType > 0 {
  1822. sql += `,date_type = ? `
  1823. pars = append(pars, dateType)
  1824. }
  1825. sql += `,start_date = ? `
  1826. pars = append(pars, req.StartDate)
  1827. sql += `,end_date = ? `
  1828. pars = append(pars, req.EndDate)
  1829. sql += `,season_start_date = ? `
  1830. pars = append(pars, req.StartDate)
  1831. sql += `,season_end_date = ? `
  1832. pars = append(pars, req.EndDate)
  1833. sql += `,left_min = ? `
  1834. pars = append(pars, req.LeftMin)
  1835. sql += `,left_max = ? `
  1836. pars = append(pars, req.LeftMax)
  1837. sql += `,right_min = ? `
  1838. pars = append(pars, req.RightMin)
  1839. sql += `,right_max = ? `
  1840. pars = append(pars, req.RightMax)
  1841. sql += `WHERE chart_info_id = ?`
  1842. pars = append(pars, req.ChartInfoId)
  1843. _, err = to.Raw(sql, pars).Exec()
  1844. if err != nil {
  1845. fmt.Println("UPDATE chart_info Err:", err.Error())
  1846. return err
  1847. }
  1848. chartEdbMappingIdList := make([]string, 0)
  1849. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  1850. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  1851. var tmpChartEdbMapping *ChartEdbMapping
  1852. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  1853. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_CORRELATION).QueryRow(&tmpChartEdbMapping)
  1854. if err != nil && err.Error() != utils.ErrNoRow() {
  1855. fmt.Println("QueryRow Err:", err.Error())
  1856. return err
  1857. }
  1858. if tmpChartEdbMapping != nil {
  1859. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  1860. //tmpChartEdbMapping.ModifyTime = time.Now()
  1861. //tmpChartEdbMapping.MaxData = v.MaxData
  1862. //tmpChartEdbMapping.MinData = v.MinData
  1863. //tmpChartEdbMapping.IsOrder = v.IsOrder
  1864. //tmpChartEdbMapping.IsAxis = v.IsAxis
  1865. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  1866. //tmpChartEdbMapping.LeadValue = v.LeadValue
  1867. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  1868. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  1869. //tmpChartEdbMapping.ChartColor = v.ChartColor
  1870. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  1871. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  1872. //_, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  1873. //if err != nil {
  1874. // fmt.Println("chart_edb_mapping Err:" + err.Error())
  1875. // return err
  1876. //}
  1877. } else {
  1878. mapItem := new(ChartEdbMapping)
  1879. mapItem.ChartInfoId = req.ChartInfoId
  1880. mapItem.EdbInfoId = v.EdbInfoId
  1881. mapItem.CreateTime = time.Now()
  1882. mapItem.ModifyTime = time.Now()
  1883. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1884. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  1885. //mapItem.MaxData = v.MaxData
  1886. //mapItem.MinData = v.MinData
  1887. mapItem.IsOrder = true
  1888. mapItem.IsAxis = 1
  1889. mapItem.EdbInfoType = 1
  1890. //mapItem.LeadValue = v.LeadValue
  1891. //mapItem.LeadUnit = v.LeadUnit
  1892. //mapItem.ChartStyle = v.ChartStyle
  1893. //mapItem.ChartColor = v.ChartColor
  1894. //mapItem.PredictChartColor = v.PredictChartColor
  1895. //mapItem.ChartWidth = v.ChartWidth
  1896. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  1897. tmpId, err := to.Insert(mapItem)
  1898. if err != nil {
  1899. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  1900. return err
  1901. }
  1902. mapItem.ChartEdbMappingId = int(tmpId)
  1903. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  1904. }
  1905. }
  1906. if len(chartEdbMappingIdList) > 0 {
  1907. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  1908. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  1909. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  1910. if err != nil {
  1911. fmt.Println("delete err:" + err.Error())
  1912. return err
  1913. }
  1914. }
  1915. // 相关性图表
  1916. _, err = to.Update(correlationChart, correlationUpdateCols...)
  1917. return
  1918. }
  1919. // PreviewSectionScatterChartReq 预览截面散点图的请求
  1920. type PreviewSectionScatterChartReq struct {
  1921. ChartName string `description:"图表名称"`
  1922. ChartClassifyId int `description:"分类id"`
  1923. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  1924. }
  1925. // SectionScatterReq 截面散点请求
  1926. type SectionScatterReq struct {
  1927. XName string `description:"x轴名称"`
  1928. XNameEn string `description:"x轴名称(英文)"`
  1929. XUnitName string `description:"x轴单位名称"`
  1930. XUnitNameEn string `description:"x轴单位名称(英文)"`
  1931. YName string `description:"y轴名称"`
  1932. YNameEn string `description:"y轴名称(英文)"`
  1933. YUnitName string `description:"y轴单位名称"`
  1934. YUnitNameEn string `description:"y轴单位名称(英文)"`
  1935. XMinValue string `description:"X轴的最小值"`
  1936. XMaxValue string `description:"X轴的最大值"`
  1937. YMinValue string `description:"Y轴的最小值"`
  1938. YMaxValue string `description:"Y轴的最大值"`
  1939. //EdbList []SectionScatterEdbItemReq `description:"指标数据"`
  1940. SeriesList []SectionScatterSeriesItemReq `description:"系列数据"`
  1941. }
  1942. // SectionScatterSeriesItemReq 系列的请求
  1943. type SectionScatterSeriesItemReq struct {
  1944. Name string `description:"系列名"`
  1945. NameEn string `description:"系列名(英文名)"`
  1946. IsNameDefault bool `description:"是否使用默认的系列名 false修改过 true默认 影响自动更新"`
  1947. Color string `description:"颜色"`
  1948. EdbInfoList []SectionScatterEdbItemReq
  1949. ShowTrendLine bool `description:"是否展示趋势线"`
  1950. ShowFitEquation bool `description:"是否展示方程式"`
  1951. ShowRSquare bool `description:"是否展示R平方"`
  1952. }
  1953. // SectionScatterEdbItemReq 截面散点请求的指标
  1954. type SectionScatterEdbItemReq struct {
  1955. XEdbInfoId int `description:"X轴的指标ID"`
  1956. YEdbInfoId int `description:"Y轴的指标ID"`
  1957. Name string `description:"别名"`
  1958. NameEn string `description:"英文别名"`
  1959. XDateType int `description:"X轴的日期配置类型"`
  1960. XDate string `description:"X轴的日期固定日期"`
  1961. XDateValue int `description:"X轴的日期N天的值"`
  1962. YDateType int `description:"Y轴的日期配置类型"`
  1963. YDate string `description:"Y轴的日期固定日期"`
  1964. YDateValue int `description:"Y轴的日期N天的值"`
  1965. IsShow bool `description:"是否展示"`
  1966. }
  1967. // SectionScatterInfoResp 截面散点图数据
  1968. type SectionScatterInfoResp struct {
  1969. XName string `description:"x轴名称"`
  1970. XNameEn string `description:"x轴名称(英文)"`
  1971. XUnitName string `description:"x轴单位名称"`
  1972. XUnitNameEn string `description:"x轴单位名称(英文)"`
  1973. YName string `description:"y轴名称"`
  1974. YNameEn string `description:"y轴名称(英文)"`
  1975. YUnitName string `description:"y轴单位名称"`
  1976. YUnitNameEn string `description:"y轴单位名称(英文)"`
  1977. XMinValue string `description:"X轴的最小值"`
  1978. XMaxValue string `description:"X轴的最大值"`
  1979. YMinValue string `description:"Y轴的最小值"`
  1980. YMaxValue string `description:"Y轴的最大值"`
  1981. DataList []SectionScatterSeriesItemResp `description:"数据列"`
  1982. }
  1983. // SectionScatterSeriesItemResp 系列的返回
  1984. type SectionScatterSeriesItemResp struct {
  1985. Name string `description:"系列名"`
  1986. NameEn string `description:"系列名(英文)"`
  1987. IsNameDefault bool `description:"是否使用默认的系列名 false修改过 true默认 影响自动更新"`
  1988. Color string `description:"颜色"`
  1989. EdbInfoList []SectionScatterEdbItemResp
  1990. ShowTrendLine bool `description:"是否展示趋势线"`
  1991. ShowFitEquation bool `description:"是否展示方程式"`
  1992. ShowRSquare bool `description:"是否展示R平方"`
  1993. TrendLine string `description:"方程式"`
  1994. RSquare string `description:"R平方的值(决定系数R2)"`
  1995. TrendLimitData []CoordinatePoint `description:"趋势线的前后坐标点"`
  1996. }
  1997. // SectionScatterEdbItemResp 截面散点的返回参数
  1998. type SectionScatterEdbItemResp struct {
  1999. XEdbInfoId int `description:"X轴指标id"`
  2000. XDate string `description:"X轴指标实际日期"`
  2001. XName string `description:"X轴指标名称"`
  2002. XNameEn string `description:"X轴指标英文名称"`
  2003. XValue float64 `description:"X轴实际值"`
  2004. YEdbInfoId int `description:"Y轴指标id"`
  2005. YDate string `description:"Y轴指标实际日期"`
  2006. YName string `description:"Y轴指标名称"`
  2007. YNameEn string `description:"Y轴指标英文名称"`
  2008. YValue float64 `description:"Y轴实际值"`
  2009. IsShow bool `description:"是否展示"`
  2010. Name string `description:"标签名称"`
  2011. NameEn string `description:"英文标签名称"`
  2012. }
  2013. // CoordinatePoint 坐标点
  2014. type CoordinatePoint struct {
  2015. X float64
  2016. Y float64
  2017. }
  2018. // RollingCorrelationChartDataResp 滚动相关性图
  2019. type RollingCorrelationChartDataResp struct {
  2020. XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
  2021. YDataList []YData `description:"滚动相关性图的y轴数据"`
  2022. }
  2023. // GetChartInfoAll 用于分类展示
  2024. // @param source int 1:ETA图库;2:商品价格曲线
  2025. func GetChartInfoAllByClassifyId(source, classifyId int) (items []*ChartClassifyItems, err error) {
  2026. o := orm.NewOrmUsingDB("data")
  2027. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,chart_name_en AS chart_classify_name_en,
  2028. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date,is_join_permission
  2029. FROM chart_info WHERE chart_classify_id = ? AND source = ? ORDER BY sort asc,create_time DESC `
  2030. _, err = o.Raw(sql, classifyId, source).QueryRows(&items)
  2031. return
  2032. }
  2033. // ProfitFutureGoodChartResp 商品利润图
  2034. type ProfitFutureGoodChartResp struct {
  2035. XDataList []XData
  2036. YDataList []YData
  2037. ProfitName string `description:"利润的名称"`
  2038. ProfitNameEn string `description:"利润的英文名称"`
  2039. }
  2040. func FIXChartClassifyId(newId, oldId int) (err error) {
  2041. o := orm.NewOrmUsingDB("data")
  2042. sql := ` UPDATE chart_info SET chart_classify_id=? WHERE chart_classify_id=? `
  2043. _, err = o.Raw(sql, newId, oldId).Exec()
  2044. return
  2045. }
  2046. // GetChartInfoByAdminIdAndClassify 获取所有我创建的图表,用于分类展示
  2047. func GetChartInfoByAdminIdAndClassify(sourceList []int, adminId, classifyId int) (items []*ChartClassifyItems, err error) {
  2048. num := len(sourceList)
  2049. if num <= 0 {
  2050. return
  2051. }
  2052. o := orm.NewOrmUsingDB("data")
  2053. sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
  2054. unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date,is_join_permission
  2055. FROM chart_info where source in (` + utils.GetOrmInReplace(num) + `) AND sys_user_id = ? AND chart_classify_id = ? ORDER BY sort asc,create_time ASC `
  2056. _, err = o.Raw(sql, sourceList, adminId, classifyId).QueryRows(&items)
  2057. return
  2058. }
  2059. // GetChartInfoAdminList 根据条件获取图表数据了列表
  2060. func GetChartInfoAdminList() (items []int, err error) {
  2061. o := orm.NewOrmUsingDB("data")
  2062. sql := ` SELECT sys_user_id FROM chart_info GROUP BY sys_user_id `
  2063. _, err = o.Raw(sql).QueryRows(&items)
  2064. return
  2065. }
  2066. // EditChartInfoExtraConfig 修改 ETA图库 的 图表额外配置
  2067. func EditChartInfoExtraConfig(chartId int, extraConfig string) (err error) {
  2068. o := orm.NewOrmUsingDB("data")
  2069. var pars []interface{}
  2070. pars = append(pars, extraConfig)
  2071. sql := ` UPDATE chart_info
  2072. SET
  2073. modify_time = NOW(),
  2074. extra_config = ?
  2075. `
  2076. sql += `WHERE chart_info_id = ?`
  2077. pars = append(pars, chartId)
  2078. _, err = o.Raw(sql, pars).Exec()
  2079. if err != nil {
  2080. fmt.Println("UPDATE chart_info Err:", err.Error())
  2081. return err
  2082. }
  2083. return
  2084. }
  2085. // PreviewRadarChartReq 预览雷达图的请求入参
  2086. type PreviewRadarChartReq struct {
  2087. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  2088. ExtraConfig string `description:"图表额外配置信息,json字符串"`
  2089. }
  2090. // RadarChartInfoReq 雷达图预览请求数据
  2091. type RadarChartInfoReq struct {
  2092. DateList []RadarChartInfoDateReq `description:"日期配置"`
  2093. }
  2094. // RadarChartInfoEdbItemReq 雷达图预览请求数据(指标相关)
  2095. type RadarChartInfoEdbItemReq struct {
  2096. EdbInfoId int `description:"指标ID"`
  2097. Name string `description:"别名"`
  2098. //NameEn string `description:"英文别名"`
  2099. //Source int `description:"1:ETA图库;2:商品价格"`
  2100. }
  2101. // RadarChartInfoDateReq 雷达图预览请求数据(日期相关)
  2102. type RadarChartInfoDateReq struct {
  2103. Type int `description:"配置类型"`
  2104. Date string `description:"固定日期"`
  2105. Value int `description:"N天的值"`
  2106. Color string `description:"颜色"`
  2107. Name string `description:"别名"`
  2108. }
  2109. // RadarChartInfoResp 雷达图数据
  2110. type RadarChartInfoResp struct {
  2111. YDataList []RadarYData `description:"数据列"`
  2112. XEdbIdValue []int
  2113. }
  2114. // RadarYData 雷达图的y轴数据
  2115. type RadarYData struct {
  2116. Date string `description:"数据日期"`
  2117. Color string `description:"数据颜色"`
  2118. Name string `description:"别名"`
  2119. Value []float64 `description:"每个指标的值"`
  2120. }