my_chart.go 29 KB

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