chart_info.go 88 KB

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