chart_info.go 89 KB

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