chart_info.go 84 KB

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