chart_info.go 74 KB

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