chart_info.go 64 KB

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