chart_info.go 88 KB

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