my_chart.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/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. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  296. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  297. SearchText string `description:"搜索结果(含高亮)"`
  298. }
  299. type MyChartListResp struct {
  300. Paging *paging.PagingItem
  301. List []*MyChartList
  302. }
  303. func GetMyChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*MyChartList, err error) {
  304. o := orm.NewOrmUsingDB("data")
  305. 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,b.is_join_permission `
  306. //MyChartClassifyName string `description:"分类名称,多个用英文逗号分割"`
  307. //MyChartClassifyId string `description:"分类id,多个用英文逗号分割"`
  308. sql := ` SELECT * FROM ( SELECT ` + field + ` FROM my_chart AS a
  309. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  310. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  311. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  312. WHERE 1=1 `
  313. if condition != "" {
  314. sql += condition
  315. }
  316. sql += " GROUP BY a.chart_info_id) AS f ORDER BY sort ASC,c_ctime desc LIMIT ?,? "
  317. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  318. return
  319. }
  320. func GetMyChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  321. o := orm.NewOrmUsingDB("data")
  322. sql := ` select a.* from my_chart AS a
  323. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  324. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  325. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  326. WHERE 1=1`
  327. if condition != "" {
  328. sql += condition
  329. }
  330. sql1 := ` SELECT COUNT(1) AS count FROM (` + sql + ` GROUP BY a.chart_info_id ) d`
  331. err = o.Raw(sql1, pars).QueryRow(&count)
  332. return
  333. }
  334. func GetMyChartMaxSort(adminId, myChartClassifyId int) (count int, err error) {
  335. o := orm.NewOrmUsingDB("data")
  336. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  337. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&count)
  338. return
  339. }
  340. type MoveMyChartReq struct {
  341. MyChartId int `description:"移动的,我的图表图表ID"`
  342. PrevMyChartId int `description:"移动到的位置,上一级的,我的图表ID,如果没有上一级,则传0"`
  343. NextMyChartId int `description:"移动到的位置,下一级的,我的图表ID,如果没有下一级,则传0"`
  344. MyChartClassifyId int `description:"当前分类id"`
  345. }
  346. func MyChartMove(sort float64, adminId int, myChartId, myChartClassifyId int) (err error) {
  347. o := orm.NewOrmUsingDB("data")
  348. sql := ` UPDATE my_chart_classify_mapping SET sort = ?,modify_time=NOW() WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  349. _, err = o.Raw(sql, sort, adminId, myChartId, myChartClassifyId).Exec()
  350. return
  351. }
  352. // UpdateMyChartSortByClassifyId 根据分类id更新排序
  353. func UpdateMyChartSortByClassifyId(myChartClassifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  354. o := orm.NewOrmUsingDB("data")
  355. sql := ` update my_chart_classify_mapping set sort = ` + updateSort + ` WHERE my_chart_classify_id=? AND `
  356. if prevMyChartClassifyMappingId > 0 {
  357. sql += ` ( sort > ? or ( my_chart_classify_mapping_id < ? and sort=? )) `
  358. }
  359. _, err = o.Raw(sql, myChartClassifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Exec()
  360. return
  361. }
  362. func GetMyChartMinSortByAdminId(adminId int) (item *MyChart, err error) {
  363. o := orm.NewOrmUsingDB("data")
  364. sql := ` SELECT * FROM my_chart WHERE admin_id=? ORDER BY sort ASC LIMIT 1`
  365. err = o.Raw(sql, adminId).QueryRow(&item)
  366. return
  367. }
  368. type ChartRecommendListResp struct {
  369. List []*ChartInfoView
  370. }
  371. func GetChartClassify(chartClassifyId int) (chart_classify_id string, err error) {
  372. o := orm.NewOrmUsingDB("data")
  373. sql := `SELECT GROUP_CONCAT(t.chart_classify_id) AS chart_classify_id FROM (
  374. SELECT a.chart_classify_id FROM chart_classify AS a
  375. WHERE a.chart_classify_id=?
  376. UNION ALL
  377. SELECT a.chart_classify_id FROM chart_classify AS a
  378. WHERE a.parent_id=? UNION ALL
  379. SELECT
  380. chart_classify_id
  381. FROM
  382. chart_classify
  383. WHERE
  384. parent_id IN ( SELECT chart_classify_id FROM chart_classify WHERE parent_id = ? )
  385. )AS t`
  386. err = o.Raw(sql, chartClassifyId, chartClassifyId, chartClassifyId).QueryRow(&chart_classify_id)
  387. return
  388. }
  389. func GetRecommendChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  390. o := orm.NewOrmUsingDB("data")
  391. sql := ` SELECT b.* FROM my_chart AS a
  392. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id `
  393. if condition != "" {
  394. sql += condition
  395. }
  396. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  397. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  398. return
  399. }
  400. func GetMyChartListByAdminId(adminId int) (item []*MyChartView, err error) {
  401. o := orm.NewOrmUsingDB("data")
  402. //sql := ` SELECT * FROM my_chart WHERE 1=1 AND admin_id=? `
  403. sql := ` SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  404. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  405. 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
  406. WHERE 1=1 AND a.admin_id=?
  407. GROUP BY a.chart_info_id `
  408. _, err = o.Raw(sql, adminId).QueryRows(&item)
  409. return
  410. }
  411. func GetMyChartSearchByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  412. o := orm.NewOrmUsingDB("data")
  413. 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
  414. FROM my_chart AS a
  415. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  416. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  417. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  418. WHERE 1=1 `
  419. if condition != "" {
  420. sql += condition
  421. }
  422. sql += " GROUP BY a.chart_info_id ORDER BY c.sort DESC "
  423. _, err = o.Raw(sql, pars).QueryRows(&item)
  424. return
  425. }
  426. type MyChartSearchListResp struct {
  427. List []*MyChartList
  428. Paging *paging.PagingItem
  429. }
  430. type MyChartEditReq struct {
  431. MyChartId int `description:"我的图表主键"`
  432. CurrentMyChartClassifyId int `description:"当前分类ID"`
  433. MyChartClassifyId []int `description:"分类id,数组形式"`
  434. }
  435. func GetMyChartClassifyMappingCount(adminId, myChartId, myChartClassifyId int) (count int, err error) {
  436. o := orm.NewOrmUsingDB("data")
  437. sql := ` SELECT COUNT(1) AS count
  438. FROM my_chart_classify_mapping
  439. WHERE 1=1 AND admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  440. err = o.Raw(sql, adminId, myChartId, myChartClassifyId).QueryRow(&count)
  441. return
  442. }
  443. func DeleteMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (err error) {
  444. o := orm.NewOrmUsingDB("data")
  445. sql := ` DELETE FROM my_chart_classify_mapping
  446. WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  447. _, err = o.Raw(sql, adminId, myChartId, myChartClassifyId).Exec()
  448. return
  449. }
  450. func GetMyChartSort(adminId, myChartClassifyId, sort int) (item *MyChartClassifyMapping, err error) {
  451. o := orm.NewOrmUsingDB("data")
  452. sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  453. if sort == 1 {
  454. sql += ` ORDER BY sort DESC,my_chart_classify_mapping_id ASC LIMIT 1 `
  455. } else {
  456. sql += ` ORDER BY sort ASC,my_chart_classify_mapping_id DESC LIMIT 1 `
  457. }
  458. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&item)
  459. return
  460. }
  461. func GetMyChartByCondition(condition string, pars []interface{}) (item []*MyChartView, err error) {
  462. o := orm.NewOrmUsingDB("data")
  463. //sql := ` SELECT * FROM my_chart WHERE 1=1 `
  464. sql := `SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  465. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  466. 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
  467. WHERE 1=1
  468. `
  469. if condition != "" {
  470. sql += condition
  471. }
  472. sql += ` GROUP BY a.chart_info_id `
  473. _, err = o.Raw(sql, pars).QueryRows(&item)
  474. return
  475. }
  476. func GetMyChartDetailByCondition(condition string, pars []interface{}) (item *MyChartView, err error) {
  477. o := orm.NewOrmUsingDB("data")
  478. sql := ` SELECT * FROM my_chart WHERE 1=1 `
  479. if condition != "" {
  480. sql += condition
  481. }
  482. err = o.Raw(sql, pars).QueryRow(&item)
  483. return
  484. }
  485. func MoveMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  486. o := orm.NewOrmUsingDB("data")
  487. to, err := o.Begin()
  488. if err != nil {
  489. return
  490. }
  491. defer func() {
  492. if err != nil {
  493. _ = to.Rollback()
  494. } else {
  495. _ = to.Commit()
  496. }
  497. }()
  498. classifyIdMap := make(map[int]int)
  499. for _, v := range myChartClassifyId {
  500. classifyIdMap[v] = v
  501. }
  502. mappingList := make([]*MyChartClassifyMapping, 0)
  503. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  504. _, err = to.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  505. if err != nil {
  506. return
  507. }
  508. for _, v := range mappingList {
  509. if _, ok := classifyIdMap[v.MyChartClassifyId]; !ok {
  510. sql := ` DELETE FROM my_chart_classify_mapping WHERE my_chart_classify_mapping_id=? `
  511. _, err = to.Raw(sql, v.MyChartClassifyMappingId).Exec()
  512. if err != nil {
  513. return
  514. }
  515. }
  516. }
  517. //sql := `DELETE FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? `
  518. //_, err = to.Raw(sql, adminId, myChartId).Exec()
  519. //if err != nil {
  520. // return err
  521. //}
  522. //step := float64(65536)
  523. for _, v := range myChartClassifyId {
  524. var count int
  525. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  526. err = to.Raw(sql, myChartId, v).QueryRow(&count)
  527. if err != nil {
  528. return
  529. }
  530. if count <= 0 {
  531. //total, err := GetMyChartMaxSort(adminId, v)
  532. if err != nil {
  533. return err
  534. }
  535. //sort := (float64(total) + 1) * step
  536. sort := 0
  537. sql = ` INSERT INTO my_chart_classify_mapping (
  538. admin_id,
  539. my_chart_id,
  540. my_chart_classify_id,
  541. create_time,
  542. modify_time,
  543. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  544. _, err = to.Raw(sql, adminId, myChartId, v, sort).Exec()
  545. if err != nil {
  546. return err
  547. }
  548. }
  549. }
  550. return
  551. }
  552. // Update 更新分类基础信息
  553. func (myChartClassify *MyChartClassify) Update(cols []string) (err error) {
  554. o := orm.NewOrmUsingDB("data")
  555. _, err = o.Update(myChartClassify, cols...)
  556. return
  557. }
  558. // GetFirstMyChartClassifyByAdminId 获取当前账号下,排序第一条的分类数据
  559. func GetFirstMyChartClassifyByAdminId(adminId int) (item *MyChartClassify, err error) {
  560. o := orm.NewOrmUsingDB("data")
  561. sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? order by sort asc,my_chart_classify_id asc limit 1`
  562. err = o.Raw(sql, adminId).QueryRow(&item)
  563. return
  564. }
  565. // UpdateMyChartClassifySortByClassifyId 根据分类id更新排序
  566. func UpdateMyChartClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) {
  567. o := orm.NewOrmUsingDB("data")
  568. sql := ` update my_chart_classify set sort = ` + updateSort + ` WHERE admin_id = ? and sort > ? `
  569. if classifyId > 0 {
  570. sql += ` or ( my_chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  571. }
  572. _, err = o.Raw(sql, adminId, nowSort).Exec()
  573. return
  574. }
  575. // ModifyChartClassifyPublicReq 修改我的图库分类是否可见
  576. type ModifyChartClassifyPublicReq struct {
  577. MyChartClassifyId int `description:"分类id"`
  578. IsPublic int `description:"是否所有人可见,0:仅自己可见,1:所有人可见"`
  579. }
  580. // CopyChartClassifyReq 复制我的图库分类
  581. type CopyChartClassifyReq struct {
  582. MyChartClassifyId int `description:"分类id"`
  583. }
  584. // GetPublicMyChartClassifyById 根据分类ID获取公开分类
  585. func GetPublicMyChartClassifyById(myChartClassifyId int) (item *MyChartClassify, err error) {
  586. o := orm.NewOrmUsingDB("data")
  587. sql := ` SELECT * FROM my_chart_classify WHERE is_public=1 AND my_chart_classify_id=? `
  588. err = o.Raw(sql, myChartClassifyId).QueryRow(&item)
  589. return
  590. }
  591. // CopyPublicMyChartClassifyById 根据分类ID获取公开分类
  592. func CopyPublicMyChartClassifyById(myChartClassifyId, adminId int, newMyChartClassifyName string) (newMyChartClassify *MyChartClassify, err error) {
  593. o := orm.NewOrmUsingDB("data")
  594. to, err := o.Begin()
  595. if err != nil {
  596. return
  597. }
  598. defer func() {
  599. if err != nil {
  600. _ = to.Rollback()
  601. } else {
  602. _ = to.Commit()
  603. }
  604. }()
  605. //获取分类里面所有的图表信息
  606. fromMappingList := make([]*MyChartClassifyMapping, 0)
  607. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  608. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMappingList)
  609. if err != nil {
  610. return
  611. }
  612. fromMyChartSortMap := make(map[int]float64)
  613. for _, fromMapping := range fromMappingList {
  614. fromMyChartSortMap[fromMapping.MyChartId] = fromMapping.Sort
  615. }
  616. var minSort int
  617. sql = ` SELECT MIN(sort) sort FROM my_chart_classify WHERE admin_id=? `
  618. err = o.Raw(sql, adminId).QueryRow(&minSort)
  619. if err != nil {
  620. return
  621. }
  622. // 我的图表
  623. fromMyChartList := make([]*MyChart, 0)
  624. sql = `SELECT a.* FROM my_chart a join my_chart_classify_mapping b on a.my_chart_id=b.my_chart_id
  625. WHERE b.my_chart_classify_id=?`
  626. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMyChartList)
  627. if err != nil {
  628. return
  629. }
  630. //新增我的图库分类
  631. newMyChartClassify = &MyChartClassify{
  632. MyChartClassifyName: newMyChartClassifyName,
  633. AdminId: adminId,
  634. CreateTime: time.Now(),
  635. ModifyTime: time.Now(),
  636. Sort: minSort - 1,
  637. }
  638. newMyChartClassifyId, err := to.Insert(newMyChartClassify)
  639. if err != nil {
  640. return
  641. }
  642. newMappingList := make([]*MyChartClassifyMapping, 0)
  643. //将图表加入我的图库
  644. for _, v := range fromMyChartList {
  645. tmpMyChart := &MyChart{
  646. ChartInfoId: v.ChartInfoId,
  647. AdminId: adminId,
  648. Source: v.Source,
  649. CreateTime: time.Now(),
  650. ModifyTime: time.Now(),
  651. }
  652. tmpMyChartId, tmpErr := to.Insert(tmpMyChart)
  653. if tmpErr != nil {
  654. err = tmpErr
  655. }
  656. tmpMyChart.MyChartId = int(tmpMyChartId)
  657. // 往自己的分类里面新增图表关系
  658. //原始图表的排序
  659. tmpSort, ok := fromMyChartSortMap[v.MyChartId]
  660. if !ok {
  661. tmpSort = 0
  662. }
  663. tmp := &MyChartClassifyMapping{
  664. AdminId: adminId,
  665. MyChartId: int(tmpMyChartId),
  666. MyChartClassifyId: int(newMyChartClassifyId),
  667. CreateTime: time.Now(),
  668. ModifyTime: time.Now(),
  669. Sort: tmpSort,
  670. }
  671. newMappingList = append(newMappingList, tmp)
  672. }
  673. if len(newMappingList) > 0 {
  674. _, err = to.InsertMulti(len(newMappingList), newMappingList)
  675. }
  676. return
  677. }
  678. // ModifyChartClassifyCompanyPublicReq 修改公共图库分类是否用户可见
  679. type ModifyChartClassifyCompanyPublicReq struct {
  680. MyChartClassifyId int `description:"分类id"`
  681. IsCompanyPublic int `description:"是否用户可见 0-否 1-是"`
  682. }
  683. // GetCompanyPublicClassifyList 获取用户可见的分类列表
  684. func GetCompanyPublicClassifyList(condition string, pars []interface{}) (item []*MyChartClassify, err error) {
  685. o := orm.NewOrmUsingDB("data")
  686. sql := ` SELECT * FROM my_chart_classify WHERE 1 = 1 AND is_public = 1 AND is_company_public = 1 `
  687. sql += condition
  688. sql += " ORDER BY public_time asc"
  689. _, err = o.Raw(sql, pars).QueryRows(&item)
  690. return
  691. }
  692. // GetMyChartClassifyList 获取我的图表关联的分类列表数据
  693. func GetMyChartClassifyList(myChartClassifyId int) (mappingList []*MyChartClassifyMapping, err error) {
  694. o := orm.NewOrmUsingDB("data")
  695. //o.Begin()
  696. //
  697. //defer func() {
  698. // if err != nil {
  699. // o.Rollback()
  700. // } else {
  701. // o.Commit()
  702. // }
  703. //}()
  704. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_id=?`
  705. _, err = o.Raw(sql, myChartClassifyId).QueryRows(&mappingList)
  706. return
  707. }
  708. // AddMyChartToMyChartClassify 添加我的图表至我的分类
  709. func AddMyChartToMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  710. o := orm.NewOrmUsingDB("data")
  711. //o.Begin()
  712. //defer func() {
  713. // if err != nil {
  714. // o.Rollback()
  715. // } else {
  716. // o.Commit()
  717. // }
  718. //}()
  719. //需要添加的分类id 集合map
  720. classifyIdMap := make(map[int]int)
  721. for _, v := range myChartClassifyId {
  722. classifyIdMap[v] = v
  723. }
  724. //查询已存在系统中的图表分类集合
  725. mappingList := make([]*MyChartClassifyMapping, 0)
  726. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  727. _, err = o.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  728. if err != nil {
  729. return
  730. }
  731. //遍历已存在系统中的图表分类集合,如果和需要添加的分类重复了,那么移除需要添加分类集合map里面的数据
  732. for _, v := range mappingList {
  733. if _, ok := classifyIdMap[v.MyChartClassifyId]; ok {
  734. delete(classifyIdMap, v.MyChartClassifyId)
  735. }
  736. }
  737. step := float64(65536)
  738. for _, v := range myChartClassifyId {
  739. var count int
  740. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  741. err = o.Raw(sql, myChartId, v).QueryRow(&count)
  742. if err != nil {
  743. return
  744. }
  745. if count <= 0 {
  746. total, err := GetMyChartMaxSort(adminId, v)
  747. if err != nil {
  748. return err
  749. }
  750. sort := (float64(total) + 1) * step
  751. sql = ` INSERT INTO my_chart_classify_mapping (
  752. admin_id,
  753. my_chart_id,
  754. my_chart_classify_id,
  755. create_time,
  756. modify_time,
  757. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  758. _, err = o.Raw(sql, adminId, myChartId, v, sort).Exec()
  759. if err != nil {
  760. return err
  761. }
  762. }
  763. }
  764. return
  765. }
  766. // GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition 获取MY ETA 图表数据,根据图表id和操作员id
  767. func GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  768. o := orm.NewOrmUsingDB("data")
  769. 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
  770. FROM my_chart AS a
  771. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  772. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  773. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  774. WHERE 1=1 `
  775. if condition != "" {
  776. sql += condition
  777. }
  778. sql += " GROUP BY a.chart_info_id,a.admin_id ORDER BY c.sort DESC "
  779. _, err = o.Raw(sql, pars).QueryRows(&item)
  780. return
  781. }
  782. func GetRelationChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  783. o := orm.NewOrmUsingDB("data")
  784. sql := ` SELECT a.* FROM chart_info AS a
  785. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  786. if condition != "" {
  787. sql += condition
  788. }
  789. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  790. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  791. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  792. return
  793. }
  794. func GetRelationChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  795. o := orm.NewOrmUsingDB("data")
  796. sql := ` SELECT COUNT(1) AS count FROM chart_info AS a
  797. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  798. if condition != "" {
  799. sql += condition
  800. }
  801. err = o.Raw(sql, pars).QueryRow(&count)
  802. return
  803. }
  804. // GetChartInfoByIdList 根据图表id集合 获取 图表列表
  805. func GetChartInfoByIdList(chartInfoIdList []int) (items []*ChartInfo, err error) {
  806. num := len(chartInfoIdList)
  807. if num <= 0 {
  808. return
  809. }
  810. o := orm.NewOrmUsingDB("data")
  811. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
  812. _, err = o.Raw(sql, chartInfoIdList).QueryRows(&items)
  813. return
  814. }
  815. // GetMyChartClassifyByClassifyId 主键获取分类
  816. func GetMyChartClassifyByClassifyId(classifyId int) (item *MyChartClassify, err error) {
  817. o := orm.NewOrmUsingDB("data")
  818. sql := ` SELECT * FROM my_chart_classify WHERE my_chart_classify_id = ? `
  819. err = o.Raw(sql, classifyId).QueryRow(&item)
  820. return
  821. }
  822. // MyChartClassifyItem 我的图表分类信息
  823. type MyChartClassifyItem struct {
  824. MyChartClassifyId int `description:"分类ID"`
  825. MyChartClassifyName string `description:"分类名称"`
  826. AdminId int `description:"创建人id"`
  827. IsPublic int `description:"是否公共分类"`
  828. IsCompanyPublic int `description:"是否为用户公共分类"`
  829. ChartNum int `description:"分类下的图表数量"`
  830. }
  831. // FormatMyChartClassify2Item 格式化我的图表信息
  832. func FormatMyChartClassify2Item(origin *MyChartClassify, chartNum int) (item *MyChartClassifyItem) {
  833. if origin == nil {
  834. return
  835. }
  836. item = new(MyChartClassifyItem)
  837. item.MyChartClassifyId = origin.MyChartClassifyId
  838. item.MyChartClassifyName = origin.MyChartClassifyName
  839. item.AdminId = origin.AdminId
  840. item.IsPublic = origin.IsPublic
  841. item.IsCompanyPublic = origin.IsCompanyPublic
  842. item.ChartNum = chartNum
  843. return
  844. }
  845. // MyChartClassifyIdAndNum 我的图表-分类ID及图表数
  846. type MyChartClassifyIdAndNum struct {
  847. MyChartClassifyId int `description:"分类ID"`
  848. ChartNum int `description:"分类下的图表数量"`
  849. }
  850. // GetMyChartClassifyIdAndNum 我的图表-获取分类ID及图表数
  851. func GetMyChartClassifyIdAndNum(cond string, pars []interface{}) (items []*MyChartClassifyIdAndNum, err error) {
  852. o := orm.NewOrmUsingDB("data")
  853. sql := fmt.Sprintf(`SELECT
  854. a.my_chart_classify_id,
  855. COUNT(1) AS chart_num
  856. FROM
  857. my_chart_classify AS a
  858. INNER JOIN my_chart_classify_mapping AS b ON a.my_chart_classify_id = b.my_chart_classify_id
  859. INNER JOIN my_chart AS c ON b.my_chart_id = c.my_chart_id
  860. INNER JOIN chart_info AS d ON c.chart_info_id = d.chart_info_id
  861. WHERE
  862. 1 = 1 %s
  863. GROUP BY
  864. a.my_chart_classify_id`, cond)
  865. _, err = o.Raw(sql, pars).QueryRows(&items)
  866. return
  867. }
  868. func GetChartItems(cond string, pars []interface{}, orderRule string) (item []*ChartInfoView, err error) {
  869. o := orm.NewOrmUsingDB("data")
  870. sql := `SELECT * FROM chart_info WHERE 1=1 `
  871. if cond != "" {
  872. sql += cond
  873. }
  874. order := ` ORDER BY create_time DESC`
  875. if orderRule != "" {
  876. order = fmt.Sprintf(` ORDER BY %s`, orderRule)
  877. }
  878. sql += order
  879. _, err = o.Raw(sql, pars).QueryRows(&item)
  880. return
  881. }
  882. func GetChartInfoViewByIdList(chartInfoIdList []int) (items []*ChartInfoView, err error) {
  883. num := len(chartInfoIdList)
  884. if num <= 0 {
  885. return
  886. }
  887. o := orm.NewOrmUsingDB("data")
  888. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
  889. _, err = o.Raw(sql, chartInfoIdList).QueryRows(&items)
  890. return
  891. }