my_chart.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type ChartListResp struct {
  10. Paging *paging.PagingItem
  11. List []*ChartInfoView
  12. }
  13. func GetChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  14. o := orm.NewOrmUsingDB("data")
  15. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  16. if condition != "" {
  17. sql += condition
  18. }
  19. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  20. sql += " ORDER BY create_time DESC LIMIT ?,? "
  21. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  22. return
  23. }
  24. func GetChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  25. o := orm.NewOrmUsingDB("data")
  26. sql := ` SELECT COUNT(1) AS count FROM chart_info WHERE 1=1 `
  27. if condition != "" {
  28. sql += condition
  29. }
  30. err = o.Raw(sql, pars).QueryRow(&count)
  31. return
  32. }
  33. type MyChartClassify struct {
  34. MyChartClassifyId int `orm:"column(my_chart_classify_id);pk"`
  35. MyChartClassifyName string
  36. Sort int `json:"-" description:"排序字段,值越小,排的越靠前"`
  37. AdminId int `description:"创建人id"`
  38. IsPublic int `description:"是否公共分类"`
  39. IsCompanyPublic int `description:"是否为用户公共分类"`
  40. CreateTime time.Time `json:"-"`
  41. ModifyTime time.Time `json:"-"`
  42. PublicTime time.Time `json:"-" description:"公开(分享)时间"`
  43. CompanyPublicTime time.Time `json:"-" description:"用户可见公开时间"`
  44. }
  45. func AddMyChartClassify(item *MyChartClassify) (err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. _, err = o.Insert(item)
  48. return
  49. }
  50. func GetMyChartClassifyAll(adminId int) (item []*MyChartClassify, err error) {
  51. o := orm.NewOrmUsingDB("data")
  52. sql := ` SELECT * FROM my_chart_classify WHERE 1=1 AND admin_id=? `
  53. sql += " ORDER BY sort asc, my_chart_classify_id asc "
  54. _, err = o.Raw(sql, adminId).QueryRows(&item)
  55. return
  56. }
  57. func GetPublicChartClassifyAllExceptMy(adminId int) (item []*MyChartClassify, err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := ` SELECT * FROM my_chart_classify WHERE 1=1 AND is_public=1 AND admin_id !=?`
  60. sql += " ORDER BY public_time asc"
  61. _, err = o.Raw(sql, adminId).QueryRows(&item)
  62. return
  63. }
  64. type MyChartClassifyResp struct {
  65. List []*MyChartClassifyItem
  66. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  67. }
  68. // PublicChartClassifyResp 公共分类返回数据结构体
  69. type PublicChartClassifyResp struct {
  70. List []PublicChartClassifyList
  71. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  72. }
  73. // PublicChartClassifyItem 公共分类结构体
  74. type PublicChartClassifyItem struct {
  75. MyChartClassifyId int `description:"分类id"`
  76. MyChartClassifyName string `description:"分类名称"`
  77. AdminId int `description:"创建人id"`
  78. RealName string `description:"创建人名称"`
  79. IsPublic int `description:"是否公共分类"`
  80. IsCompanyPublic int `description:"是否为客户可见"`
  81. }
  82. // PublicChartClassifyList 公共分类结构体
  83. type PublicChartClassifyList struct {
  84. MenuAdminId int `description:"目录创建人ID"`
  85. MenuName string `description:"目录名称"`
  86. Items []PublicChartClassifyItem `description:"分类数据"`
  87. }
  88. type MyChartClassifyAddReq struct {
  89. MyChartClassifyName string `description:"分类名称"`
  90. }
  91. func GetMyChartClassifyCountByCondition(condition string, pars []interface{}) (count int, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify WHERE 1=1 `
  94. if condition != "" {
  95. sql += condition
  96. }
  97. err = o.Raw(sql, pars).QueryRow(&count)
  98. return
  99. }
  100. func GetMyChartClassifyById(adminId, myChartClassifyId int) (item *MyChartClassify, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? AND my_chart_classify_id=? `
  103. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&item)
  104. return
  105. }
  106. // GetMyChartClassifyMastSort 获取MY ETA 分类中排序最大的值
  107. func GetMyChartClassifyMastSort(adminId int) (sort int, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ` SELECT MAX(sort) sort FROM my_chart_classify WHERE admin_id=? `
  110. err = o.Raw(sql, adminId).QueryRow(&sort)
  111. return
  112. }
  113. // GetMyChartClassifyMinSort 获取MY ETA 分类中排序最小的值
  114. func GetMyChartClassifyMinSort(adminId int) (sort int, err error) {
  115. o := orm.NewOrmUsingDB("data")
  116. sql := ` SELECT MIN(sort) sort FROM my_chart_classify WHERE admin_id=? `
  117. err = o.Raw(sql, adminId).QueryRow(&sort)
  118. return
  119. }
  120. type MyChartClassifyEditReq struct {
  121. MyChartClassifyId int `description:"分类ID"`
  122. MyChartClassifyName string `description:"分类名称"`
  123. }
  124. // MyChartClassifyMoveReq 移动分类请求参数
  125. type MyChartClassifyMoveReq struct {
  126. MyChartClassifyId int `description:"分类id"`
  127. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  128. NextClassifyId int `description:"下一个兄弟节点分类id"`
  129. }
  130. func ModifyMyChartClassify(myChartClassifyId int, myChartClassifyName string) (err error) {
  131. o := orm.NewOrmUsingDB("data")
  132. sql := `UPDATE my_chart_classify SET my_chart_classify_name=?,modify_time=NOW() WHERE my_chart_classify_id=?`
  133. _, err = o.Raw(sql, myChartClassifyName, myChartClassifyId).Exec()
  134. return
  135. }
  136. type MyChartClassifyDeleteReq struct {
  137. MyChartClassifyId int `description:"分类ID"`
  138. }
  139. func DeleteMyChartClassify(myChartClassifyId int) (err error) {
  140. o := orm.NewOrmUsingDB("data")
  141. //o.Begin()
  142. //
  143. //defer func() {
  144. // if err != nil {
  145. // o.Rollback()
  146. // } else {
  147. // o.Commit()
  148. // }
  149. //}()
  150. sql := `DELETE FROM my_chart_classify WHERE my_chart_classify_id=?`
  151. _, err = o.Raw(sql, myChartClassifyId).Exec()
  152. if err != nil {
  153. return err
  154. }
  155. mappingList := make([]*MyChartClassifyMapping, 0)
  156. sql = `SELECT * FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  157. _, err = o.Raw(sql, myChartClassifyId).QueryRows(&mappingList)
  158. sql = `DELETE FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  159. _, err = o.Raw(sql, myChartClassifyId).Exec()
  160. if err != nil {
  161. return err
  162. }
  163. for _, v := range mappingList {
  164. var count int
  165. sql = ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=?`
  166. err = o.Raw(sql, v.MyChartId).QueryRow(&count)
  167. if err != nil {
  168. return err
  169. }
  170. if count <= 0 {
  171. sql = `DELETE FROM my_chart WHERE my_chart_id=?`
  172. _, err = o.Raw(sql, v.MyChartId).Exec()
  173. if err != nil {
  174. return err
  175. }
  176. }
  177. }
  178. return
  179. }
  180. type MyChartAddReq struct {
  181. ChartInfoId int `description:"图表id"`
  182. MyChartClassifyId []int `description:"分类id,数组形式"`
  183. }
  184. type MyChartAddResp struct {
  185. MyChartInfoId int `description:"图表id"`
  186. MyChartClassifyId string `description:"分类id,多个用英文逗号隔开"`
  187. }
  188. type MyChart struct {
  189. MyChartId int `orm:"column(my_chart_id);pk"`
  190. ChartInfoId int `description:"图表id"`
  191. Source int `description:"1:ETA图库;2:商品价格"`
  192. AdminId int `description:"用户id"`
  193. CreateTime time.Time `description:"创建时间"`
  194. ModifyTime time.Time `description:"修改时间"`
  195. }
  196. type MyChartView struct {
  197. MyChartId int `orm:"column(my_chart_id);pk"`
  198. ChartInfoId int `description:"图表id"`
  199. AdminId int `description:"用户id"`
  200. CreateTime time.Time `description:"创建时间"`
  201. ModifyTime time.Time `description:"修改时间"`
  202. MyChartClassifyId string
  203. }
  204. func AddMyChart(item *MyChart) (lastId int64, err error) {
  205. o := orm.NewOrmUsingDB("data")
  206. lastId, err = o.Insert(item)
  207. return
  208. }
  209. type MyChartClassifyMapping struct {
  210. MyChartClassifyMappingId int `orm:"column(my_chart_classify_mapping_id);pk"`
  211. AdminId int
  212. MyChartId int
  213. MyChartClassifyId int
  214. CreateTime time.Time
  215. ModifyTime time.Time
  216. Sort float64
  217. }
  218. func AddMyChartClassifyMapping(item *MyChartClassifyMapping) (lastId int64, err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. lastId, err = o.Insert(item)
  221. return
  222. }
  223. func GetMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (item *MyChartClassifyMapping, err error) {
  224. o := orm.NewOrmUsingDB("data")
  225. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  226. err = o.Raw(sql, adminId, myChartId, myChartClassifyId).QueryRow(&item)
  227. return
  228. }
  229. type MyChartDeleteReq struct {
  230. MyChartId int `description:"我的图表id"`
  231. MyChartClassifyId int `description:"我的图表分类id"`
  232. }
  233. func DeleteMyChart(myChartId, myChartClassifyId int) (isDelMyChart bool, err error) {
  234. o := orm.NewOrmUsingDB("data")
  235. if myChartClassifyId > 0 {
  236. sql := `DELETE FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  237. _, err = o.Raw(sql, myChartId, myChartClassifyId).Exec()
  238. if err != nil {
  239. return
  240. }
  241. var count int
  242. sql = ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? `
  243. err = o.Raw(sql, myChartId).QueryRow(&count)
  244. if err != nil {
  245. return
  246. }
  247. if count <= 0 {
  248. sql = `DELETE FROM my_chart WHERE my_chart_id=? `
  249. _, err = o.Raw(sql, myChartId).Exec()
  250. if err != nil {
  251. return
  252. }
  253. isDelMyChart = true
  254. }
  255. } else {
  256. sql := `DELETE FROM my_chart WHERE my_chart_id=? `
  257. _, err = o.Raw(sql, myChartId).Exec()
  258. if err != nil {
  259. return
  260. }
  261. isDelMyChart = true
  262. }
  263. return
  264. }
  265. type MyChartList struct {
  266. AdminId int `description:"关联人id"`
  267. MyChartId int `description:"我的图表主键"`
  268. ChartInfoId int
  269. ChartName string `description:"来源名称"`
  270. ChartNameEn string `description:"英文图表名称"`
  271. ChartClassifyId int `description:"图表分类id"`
  272. SysUserId int
  273. SysUserRealName string
  274. UniqueCode string `description:"图表唯一编码"`
  275. CreateTime time.Time
  276. ModifyTime time.Time
  277. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  278. StartDate string `description:"自定义开始日期"`
  279. EndDate string `description:"自定义结束日期"`
  280. IsSetName int `description:"设置名称"`
  281. EdbInfoIds string `description:"指标id"`
  282. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  283. Calendar string `description:"公历/农历"`
  284. SeasonStartDate string `description:"季节性图开始日期"`
  285. SeasonEndDate string `description:"季节性图开始日期"`
  286. ChartImage string `description:"图表图片"`
  287. Sort int `description:"排序字段,数字越小越排前面"`
  288. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  289. MyChartClassifyName string `description:"分类名称,多个用英文逗号分割"`
  290. MyChartClassifyId string `description:"分类id,多个用英文逗号分割"`
  291. EdbEndDate string `description:"指标最新更新日期"`
  292. IsEnChart bool `description:"是否展示英文标识"`
  293. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  294. Source int `description:"1:ETA图库;2:商品价格曲线"`
  295. }
  296. type MyChartListResp struct {
  297. Paging *paging.PagingItem
  298. List []*MyChartList
  299. }
  300. func GetMyChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*MyChartList, err error) {
  301. o := orm.NewOrmUsingDB("data")
  302. field := `a.my_chart_id,c.admin_id,c.sort,c.create_time as c_ctime,b.chart_info_id,b.chart_name,b.chart_name_en,b.chart_classify_id,b.sys_user_id,b.sys_user_real_name,b.unique_code,b.create_time,b.modify_time,b.date_type,b.start_date,b.end_date,b.is_set_name,b.edb_info_ids,b.chart_type,b.calendar,b.season_start_date,b.season_end_date,b.chart_image,b.edb_end_date,b.disabled,b.source `
  303. //MyChartClassifyName string `description:"分类名称,多个用英文逗号分割"`
  304. //MyChartClassifyId string `description:"分类id,多个用英文逗号分割"`
  305. sql := ` SELECT * FROM ( SELECT ` + field + ` FROM my_chart AS a
  306. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  307. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  308. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  309. WHERE 1=1 `
  310. if condition != "" {
  311. sql += condition
  312. }
  313. sql += " GROUP BY a.chart_info_id) AS f ORDER BY sort ASC,c_ctime desc LIMIT ?,? "
  314. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  315. return
  316. }
  317. func GetMyChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  318. o := orm.NewOrmUsingDB("data")
  319. sql := ` select a.* from my_chart AS a
  320. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  321. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  322. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  323. WHERE 1=1`
  324. if condition != "" {
  325. sql += condition
  326. }
  327. sql1 := ` SELECT COUNT(1) AS count FROM (` + sql + ` GROUP BY a.chart_info_id ) d`
  328. err = o.Raw(sql1, pars).QueryRow(&count)
  329. return
  330. }
  331. func GetMyChartMaxSort(adminId, myChartClassifyId int) (count int, err error) {
  332. o := orm.NewOrmUsingDB("data")
  333. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  334. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&count)
  335. return
  336. }
  337. type MoveMyChartReq struct {
  338. MyChartId int `description:"移动的,我的图表图表ID"`
  339. PrevMyChartId int `description:"移动到的位置,上一级的,我的图表ID,如果没有上一级,则传0"`
  340. NextMyChartId int `description:"移动到的位置,下一级的,我的图表ID,如果没有下一级,则传0"`
  341. MyChartClassifyId int `description:"当前分类id"`
  342. }
  343. func MyChartMove(sort float64, adminId int, myChartId, myChartClassifyId int) (err error) {
  344. o := orm.NewOrmUsingDB("data")
  345. sql := ` UPDATE my_chart_classify_mapping SET sort = ?,modify_time=NOW() WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  346. _, err = o.Raw(sql, sort, adminId, myChartId, myChartClassifyId).Exec()
  347. return
  348. }
  349. // UpdateMyChartSortByClassifyId 根据分类id更新排序
  350. func UpdateMyChartSortByClassifyId(myChartClassifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  351. o := orm.NewOrmUsingDB("data")
  352. sql := ` update my_chart_classify_mapping set sort = ` + updateSort + ` WHERE my_chart_classify_id=? AND `
  353. if prevMyChartClassifyMappingId > 0 {
  354. sql += ` ( sort > ? or ( my_chart_classify_mapping_id < ? and sort=? )) `
  355. }
  356. _, err = o.Raw(sql, myChartClassifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Exec()
  357. return
  358. }
  359. func GetMyChartMinSortByAdminId(adminId int) (item *MyChart, err error) {
  360. o := orm.NewOrmUsingDB("data")
  361. sql := ` SELECT * FROM my_chart WHERE admin_id=? ORDER BY sort ASC LIMIT 1`
  362. err = o.Raw(sql, adminId).QueryRow(&item)
  363. return
  364. }
  365. type ChartRecommendListResp struct {
  366. List []*ChartInfoView
  367. }
  368. func GetChartClassify(chartClassifyId int) (chart_classify_id string, err error) {
  369. o := orm.NewOrmUsingDB("data")
  370. sql := `SELECT GROUP_CONCAT(t.chart_classify_id) AS chart_classify_id FROM (
  371. SELECT a.chart_classify_id FROM chart_classify AS a
  372. WHERE a.chart_classify_id=?
  373. UNION ALL
  374. SELECT a.chart_classify_id FROM chart_classify AS a
  375. WHERE a.parent_id=? UNION ALL
  376. SELECT
  377. chart_classify_id
  378. FROM
  379. chart_classify
  380. WHERE
  381. parent_id IN ( SELECT chart_classify_id FROM chart_classify WHERE parent_id = ? )
  382. )AS t`
  383. err = o.Raw(sql, chartClassifyId, chartClassifyId, chartClassifyId).QueryRow(&chart_classify_id)
  384. return
  385. }
  386. func GetRecommendChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  387. o := orm.NewOrmUsingDB("data")
  388. sql := ` SELECT b.* FROM my_chart AS a
  389. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id `
  390. if condition != "" {
  391. sql += condition
  392. }
  393. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  394. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  395. return
  396. }
  397. func GetMyChartListByAdminId(adminId int) (item []*MyChartView, err error) {
  398. o := orm.NewOrmUsingDB("data")
  399. //sql := ` SELECT * FROM my_chart WHERE 1=1 AND admin_id=? `
  400. sql := ` SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  401. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  402. LEFT JOIN my_chart_classify AS c ON b.my_chart_classify_id=c.my_chart_classify_id AND b.admin_id=c.admin_id
  403. WHERE 1=1 AND a.admin_id=?
  404. GROUP BY a.chart_info_id `
  405. _, err = o.Raw(sql, adminId).QueryRows(&item)
  406. return
  407. }
  408. func GetMyChartSearchByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  409. o := orm.NewOrmUsingDB("data")
  410. sql := ` SELECT a.my_chart_id,c.sort,b.*,GROUP_CONCAT(DISTINCT d.my_chart_classify_name) AS my_chart_classify_name,GROUP_CONCAT(DISTINCT d.my_chart_classify_id) AS my_chart_classify_id
  411. FROM my_chart AS a
  412. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  413. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  414. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  415. WHERE 1=1 `
  416. if condition != "" {
  417. sql += condition
  418. }
  419. sql += " GROUP BY a.chart_info_id ORDER BY c.sort DESC "
  420. _, err = o.Raw(sql, pars).QueryRows(&item)
  421. return
  422. }
  423. type MyChartSearchListResp struct {
  424. List []*MyChartList
  425. Paging *paging.PagingItem
  426. }
  427. type MyChartEditReq struct {
  428. MyChartId int `description:"我的图表主键"`
  429. CurrentMyChartClassifyId int `description:"当前分类ID"`
  430. MyChartClassifyId []int `description:"分类id,数组形式"`
  431. }
  432. func GetMyChartClassifyMappingCount(adminId, myChartId, myChartClassifyId int) (count int, err error) {
  433. o := orm.NewOrmUsingDB("data")
  434. sql := ` SELECT COUNT(1) AS count
  435. FROM my_chart_classify_mapping
  436. WHERE 1=1 AND admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  437. err = o.Raw(sql, adminId, myChartId, myChartClassifyId).QueryRow(&count)
  438. return
  439. }
  440. func DeleteMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (err error) {
  441. o := orm.NewOrmUsingDB("data")
  442. sql := ` DELETE FROM my_chart_classify_mapping
  443. WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  444. _, err = o.Raw(sql, adminId, myChartId, myChartClassifyId).Exec()
  445. return
  446. }
  447. func GetMyChartSort(adminId, myChartClassifyId, sort int) (item *MyChartClassifyMapping, err error) {
  448. o := orm.NewOrmUsingDB("data")
  449. sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  450. if sort == 1 {
  451. sql += ` ORDER BY sort DESC,my_chart_classify_mapping_id ASC LIMIT 1 `
  452. } else {
  453. sql += ` ORDER BY sort ASC,my_chart_classify_mapping_id DESC LIMIT 1 `
  454. }
  455. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&item)
  456. return
  457. }
  458. func GetMyChartByCondition(condition string, pars []interface{}) (item []*MyChartView, err error) {
  459. o := orm.NewOrmUsingDB("data")
  460. //sql := ` SELECT * FROM my_chart WHERE 1=1 `
  461. sql := `SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  462. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  463. LEFT JOIN my_chart_classify AS c ON b.my_chart_classify_id=c.my_chart_classify_id AND b.admin_id=c.admin_id
  464. WHERE 1=1
  465. `
  466. if condition != "" {
  467. sql += condition
  468. }
  469. sql += ` GROUP BY a.chart_info_id `
  470. _, err = o.Raw(sql, pars).QueryRows(&item)
  471. return
  472. }
  473. func GetMyChartDetailByCondition(condition string, pars []interface{}) (item *MyChartView, err error) {
  474. o := orm.NewOrmUsingDB("data")
  475. sql := ` SELECT * FROM my_chart WHERE 1=1 `
  476. if condition != "" {
  477. sql += condition
  478. }
  479. err = o.Raw(sql, pars).QueryRow(&item)
  480. return
  481. }
  482. func MoveMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  483. o := orm.NewOrmUsingDB("data")
  484. to, err := o.Begin()
  485. if err != nil {
  486. return
  487. }
  488. defer func() {
  489. if err != nil {
  490. _ = to.Rollback()
  491. } else {
  492. _ = to.Commit()
  493. }
  494. }()
  495. classifyIdMap := make(map[int]int)
  496. for _, v := range myChartClassifyId {
  497. classifyIdMap[v] = v
  498. }
  499. mappingList := make([]*MyChartClassifyMapping, 0)
  500. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  501. _, err = to.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  502. if err != nil {
  503. return
  504. }
  505. for _, v := range mappingList {
  506. if _, ok := classifyIdMap[v.MyChartClassifyId]; !ok {
  507. sql := ` DELETE FROM my_chart_classify_mapping WHERE my_chart_classify_mapping_id=? `
  508. _, err = to.Raw(sql, v.MyChartClassifyMappingId).Exec()
  509. if err != nil {
  510. return
  511. }
  512. }
  513. }
  514. //sql := `DELETE FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? `
  515. //_, err = to.Raw(sql, adminId, myChartId).Exec()
  516. //if err != nil {
  517. // return err
  518. //}
  519. //step := float64(65536)
  520. for _, v := range myChartClassifyId {
  521. var count int
  522. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  523. err = to.Raw(sql, myChartId, v).QueryRow(&count)
  524. if err != nil {
  525. return
  526. }
  527. if count <= 0 {
  528. //total, err := GetMyChartMaxSort(adminId, v)
  529. if err != nil {
  530. return err
  531. }
  532. //sort := (float64(total) + 1) * step
  533. sort := 0
  534. sql = ` INSERT INTO my_chart_classify_mapping (
  535. admin_id,
  536. my_chart_id,
  537. my_chart_classify_id,
  538. create_time,
  539. modify_time,
  540. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  541. _, err = to.Raw(sql, adminId, myChartId, v, sort).Exec()
  542. if err != nil {
  543. return err
  544. }
  545. }
  546. }
  547. return
  548. }
  549. // Update 更新分类基础信息
  550. func (myChartClassify *MyChartClassify) Update(cols []string) (err error) {
  551. o := orm.NewOrmUsingDB("data")
  552. _, err = o.Update(myChartClassify, cols...)
  553. return
  554. }
  555. // GetFirstMyChartClassifyByAdminId 获取当前账号下,排序第一条的分类数据
  556. func GetFirstMyChartClassifyByAdminId(adminId int) (item *MyChartClassify, err error) {
  557. o := orm.NewOrmUsingDB("data")
  558. sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? order by sort asc,my_chart_classify_id asc limit 1`
  559. err = o.Raw(sql, adminId).QueryRow(&item)
  560. return
  561. }
  562. // UpdateMyChartClassifySortByClassifyId 根据分类id更新排序
  563. func UpdateMyChartClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) {
  564. o := orm.NewOrmUsingDB("data")
  565. sql := ` update my_chart_classify set sort = ` + updateSort + ` WHERE admin_id = ? and sort > ? `
  566. if classifyId > 0 {
  567. sql += ` or ( my_chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  568. }
  569. _, err = o.Raw(sql, adminId, nowSort).Exec()
  570. return
  571. }
  572. // ModifyChartClassifyPublicReq 修改我的图库分类是否可见
  573. type ModifyChartClassifyPublicReq struct {
  574. MyChartClassifyId int `description:"分类id"`
  575. IsPublic int `description:"是否所有人可见,0:仅自己可见,1:所有人可见"`
  576. }
  577. // CopyChartClassifyReq 复制我的图库分类
  578. type CopyChartClassifyReq struct {
  579. MyChartClassifyId int `description:"分类id"`
  580. }
  581. // GetPublicMyChartClassifyById 根据分类ID获取公开分类
  582. func GetPublicMyChartClassifyById(myChartClassifyId int) (item *MyChartClassify, err error) {
  583. o := orm.NewOrmUsingDB("data")
  584. sql := ` SELECT * FROM my_chart_classify WHERE is_public=1 AND my_chart_classify_id=? `
  585. err = o.Raw(sql, myChartClassifyId).QueryRow(&item)
  586. return
  587. }
  588. // CopyPublicMyChartClassifyById 根据分类ID获取公开分类
  589. func CopyPublicMyChartClassifyById(myChartClassifyId, adminId int, newMyChartClassifyName string) (newMyChartClassify *MyChartClassify, err error) {
  590. o := orm.NewOrmUsingDB("data")
  591. to, err := o.Begin()
  592. if err != nil {
  593. return
  594. }
  595. defer func() {
  596. if err != nil {
  597. _ = to.Rollback()
  598. } else {
  599. _ = to.Commit()
  600. }
  601. }()
  602. //获取分类里面所有的图表信息
  603. fromMappingList := make([]*MyChartClassifyMapping, 0)
  604. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  605. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMappingList)
  606. if err != nil {
  607. return
  608. }
  609. fromMyChartSortMap := make(map[int]float64)
  610. for _, fromMapping := range fromMappingList {
  611. fromMyChartSortMap[fromMapping.MyChartId] = fromMapping.Sort
  612. }
  613. var minSort int
  614. sql = ` SELECT MIN(sort) sort FROM my_chart_classify WHERE admin_id=? `
  615. err = o.Raw(sql, adminId).QueryRow(&minSort)
  616. if err != nil {
  617. return
  618. }
  619. // 我的图表
  620. fromMyChartList := make([]*MyChart, 0)
  621. sql = `SELECT a.* FROM my_chart a join my_chart_classify_mapping b on a.my_chart_id=b.my_chart_id
  622. WHERE b.my_chart_classify_id=?`
  623. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMyChartList)
  624. if err != nil {
  625. return
  626. }
  627. //新增我的图库分类
  628. newMyChartClassify = &MyChartClassify{
  629. MyChartClassifyName: newMyChartClassifyName,
  630. AdminId: adminId,
  631. CreateTime: time.Now(),
  632. ModifyTime: time.Now(),
  633. Sort: minSort - 1,
  634. }
  635. newMyChartClassifyId, err := to.Insert(newMyChartClassify)
  636. if err != nil {
  637. return
  638. }
  639. newMappingList := make([]*MyChartClassifyMapping, 0)
  640. //将图表加入我的图库
  641. for _, v := range fromMyChartList {
  642. tmpMyChart := &MyChart{
  643. ChartInfoId: v.ChartInfoId,
  644. AdminId: adminId,
  645. Source: v.Source,
  646. CreateTime: time.Now(),
  647. ModifyTime: time.Now(),
  648. }
  649. tmpMyChartId, tmpErr := to.Insert(tmpMyChart)
  650. if tmpErr != nil {
  651. err = tmpErr
  652. }
  653. tmpMyChart.MyChartId = int(tmpMyChartId)
  654. // 往自己的分类里面新增图表关系
  655. //原始图表的排序
  656. tmpSort, ok := fromMyChartSortMap[v.MyChartId]
  657. if !ok {
  658. tmpSort = 0
  659. }
  660. tmp := &MyChartClassifyMapping{
  661. AdminId: adminId,
  662. MyChartId: int(tmpMyChartId),
  663. MyChartClassifyId: int(newMyChartClassifyId),
  664. CreateTime: time.Now(),
  665. ModifyTime: time.Now(),
  666. Sort: tmpSort,
  667. }
  668. newMappingList = append(newMappingList, tmp)
  669. }
  670. if len(newMappingList) > 0 {
  671. _, err = to.InsertMulti(len(newMappingList), newMappingList)
  672. }
  673. return
  674. }
  675. // ModifyChartClassifyCompanyPublicReq 修改公共图库分类是否用户可见
  676. type ModifyChartClassifyCompanyPublicReq struct {
  677. MyChartClassifyId int `description:"分类id"`
  678. IsCompanyPublic int `description:"是否用户可见 0-否 1-是"`
  679. }
  680. // GetCompanyPublicClassifyList 获取用户可见的分类列表
  681. func GetCompanyPublicClassifyList(condition string, pars []interface{}) (item []*MyChartClassify, err error) {
  682. o := orm.NewOrmUsingDB("data")
  683. sql := ` SELECT * FROM my_chart_classify WHERE 1 = 1 AND is_public = 1 AND is_company_public = 1 `
  684. sql += condition
  685. sql += " ORDER BY public_time asc"
  686. _, err = o.Raw(sql, pars).QueryRows(&item)
  687. return
  688. }
  689. // GetMyChartClassifyList 获取我的图表关联的分类列表数据
  690. func GetMyChartClassifyList(myChartClassifyId int) (mappingList []*MyChartClassifyMapping, err error) {
  691. o := orm.NewOrmUsingDB("data")
  692. //o.Begin()
  693. //
  694. //defer func() {
  695. // if err != nil {
  696. // o.Rollback()
  697. // } else {
  698. // o.Commit()
  699. // }
  700. //}()
  701. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_id=?`
  702. _, err = o.Raw(sql, myChartClassifyId).QueryRows(&mappingList)
  703. return
  704. }
  705. // AddMyChartToMyChartClassify 添加我的图表至我的分类
  706. func AddMyChartToMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  707. o := orm.NewOrmUsingDB("data")
  708. //o.Begin()
  709. //defer func() {
  710. // if err != nil {
  711. // o.Rollback()
  712. // } else {
  713. // o.Commit()
  714. // }
  715. //}()
  716. //需要添加的分类id 集合map
  717. classifyIdMap := make(map[int]int)
  718. for _, v := range myChartClassifyId {
  719. classifyIdMap[v] = v
  720. }
  721. //查询已存在系统中的图表分类集合
  722. mappingList := make([]*MyChartClassifyMapping, 0)
  723. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  724. _, err = o.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  725. if err != nil {
  726. return
  727. }
  728. //遍历已存在系统中的图表分类集合,如果和需要添加的分类重复了,那么移除需要添加分类集合map里面的数据
  729. for _, v := range mappingList {
  730. if _, ok := classifyIdMap[v.MyChartClassifyId]; ok {
  731. delete(classifyIdMap, v.MyChartClassifyId)
  732. }
  733. }
  734. step := float64(65536)
  735. for _, v := range myChartClassifyId {
  736. var count int
  737. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  738. err = o.Raw(sql, myChartId, v).QueryRow(&count)
  739. if err != nil {
  740. return
  741. }
  742. if count <= 0 {
  743. total, err := GetMyChartMaxSort(adminId, v)
  744. if err != nil {
  745. return err
  746. }
  747. sort := (float64(total) + 1) * step
  748. sql = ` INSERT INTO my_chart_classify_mapping (
  749. admin_id,
  750. my_chart_id,
  751. my_chart_classify_id,
  752. create_time,
  753. modify_time,
  754. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  755. _, err = o.Raw(sql, adminId, myChartId, v, sort).Exec()
  756. if err != nil {
  757. return err
  758. }
  759. }
  760. }
  761. return
  762. }
  763. // GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition 获取MY ETA 图表数据,根据图表id和操作员id
  764. func GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  765. o := orm.NewOrmUsingDB("data")
  766. sql := ` SELECT a.admin_id,a.my_chart_id,c.sort,b.*,GROUP_CONCAT(DISTINCT d.my_chart_classify_name) AS my_chart_classify_name,GROUP_CONCAT(DISTINCT d.my_chart_classify_id) AS my_chart_classify_id
  767. FROM my_chart AS a
  768. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  769. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  770. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  771. WHERE 1=1 `
  772. if condition != "" {
  773. sql += condition
  774. }
  775. sql += " GROUP BY a.chart_info_id,a.admin_id ORDER BY c.sort DESC "
  776. _, err = o.Raw(sql, pars).QueryRows(&item)
  777. return
  778. }
  779. func GetRelationChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  780. o := orm.NewOrmUsingDB("data")
  781. sql := ` SELECT a.* FROM chart_info AS a
  782. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  783. if condition != "" {
  784. sql += condition
  785. }
  786. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  787. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  788. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  789. return
  790. }
  791. func GetRelationChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  792. o := orm.NewOrmUsingDB("data")
  793. sql := ` SELECT COUNT(1) AS count FROM chart_info AS a
  794. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  795. if condition != "" {
  796. sql += condition
  797. }
  798. err = o.Raw(sql, pars).QueryRow(&count)
  799. return
  800. }
  801. // GetChartInfoByIdList 根据图表id集合 获取 图表列表
  802. func GetChartInfoByIdList(chartInfoIdList []int) (items []*ChartInfo, err error) {
  803. num := len(chartInfoIdList)
  804. if num <= 0 {
  805. return
  806. }
  807. o := orm.NewOrmUsingDB("data")
  808. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
  809. _, err = o.Raw(sql, chartInfoIdList).QueryRows(&items)
  810. return
  811. }
  812. // GetMyChartClassifyByClassifyId 主键获取分类
  813. func GetMyChartClassifyByClassifyId(classifyId int) (item *MyChartClassify, err error) {
  814. o := orm.NewOrmUsingDB("data")
  815. sql := ` SELECT * FROM my_chart_classify WHERE my_chart_classify_id = ? `
  816. err = o.Raw(sql, classifyId).QueryRow(&item)
  817. return
  818. }
  819. // MyChartClassifyItem 我的图表分类信息
  820. type MyChartClassifyItem struct {
  821. MyChartClassifyId int `description:"分类ID"`
  822. MyChartClassifyName string `description:"分类名称"`
  823. AdminId int `description:"创建人id"`
  824. IsPublic int `description:"是否公共分类"`
  825. IsCompanyPublic int `description:"是否为用户公共分类"`
  826. ChartNum int `description:"分类下的图表数量"`
  827. }
  828. // FormatMyChartClassify2Item 格式化我的图表信息
  829. func FormatMyChartClassify2Item(origin *MyChartClassify, chartNum int) (item *MyChartClassifyItem) {
  830. if origin == nil {
  831. return
  832. }
  833. item = new(MyChartClassifyItem)
  834. item.MyChartClassifyId = origin.MyChartClassifyId
  835. item.MyChartClassifyName = origin.MyChartClassifyName
  836. item.AdminId = origin.AdminId
  837. item.IsPublic = origin.IsPublic
  838. item.IsCompanyPublic = origin.IsCompanyPublic
  839. item.ChartNum = chartNum
  840. return
  841. }
  842. // MyChartClassifyIdAndNum 我的图表-分类ID及图表数
  843. type MyChartClassifyIdAndNum struct {
  844. MyChartClassifyId int `description:"分类ID"`
  845. ChartNum int `description:"分类下的图表数量"`
  846. }
  847. // GetMyChartClassifyIdAndNum 我的图表-获取分类ID及图表数
  848. func GetMyChartClassifyIdAndNum(cond string, pars []interface{}) (items []*MyChartClassifyIdAndNum, err error) {
  849. o := orm.NewOrmUsingDB("data")
  850. sql := fmt.Sprintf(`SELECT
  851. a.my_chart_classify_id,
  852. COUNT(1) AS chart_num
  853. FROM
  854. my_chart_classify AS a
  855. INNER JOIN my_chart_classify_mapping AS b ON a.my_chart_classify_id = b.my_chart_classify_id
  856. INNER JOIN my_chart AS c ON b.my_chart_id = c.my_chart_id
  857. INNER JOIN chart_info AS d ON c.chart_info_id = d.chart_info_id
  858. WHERE
  859. 1 = 1 %s
  860. GROUP BY
  861. a.my_chart_classify_id`, cond)
  862. _, err = o.Raw(sql, pars).QueryRows(&items)
  863. return
  864. }