my_chart.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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=?
  350. )AS t`
  351. err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&chart_classify_id)
  352. return
  353. }
  354. func GetRecommendChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  355. o := orm.NewOrmUsingDB("data")
  356. sql := ` SELECT b.* FROM my_chart AS a
  357. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id `
  358. if condition != "" {
  359. sql += condition
  360. }
  361. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  362. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  363. return
  364. }
  365. func GetMyChartListByAdminId(adminId int) (item []*MyChartView, err error) {
  366. o := orm.NewOrmUsingDB("data")
  367. //sql := ` SELECT * FROM my_chart WHERE 1=1 AND admin_id=? `
  368. sql := ` SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  369. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  370. 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
  371. WHERE 1=1 AND a.admin_id=?
  372. GROUP BY a.chart_info_id `
  373. _, err = o.Raw(sql, adminId).QueryRows(&item)
  374. return
  375. }
  376. func GetMyChartSearchByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  377. o := orm.NewOrmUsingDB("data")
  378. 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
  379. FROM my_chart AS a
  380. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  381. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  382. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  383. WHERE 1=1 `
  384. if condition != "" {
  385. sql += condition
  386. }
  387. sql += " GROUP BY a.chart_info_id ORDER BY c.sort DESC "
  388. _, err = o.Raw(sql, pars).QueryRows(&item)
  389. return
  390. }
  391. type MyChartSearchListResp struct {
  392. List []*MyChartList
  393. Paging *paging.PagingItem
  394. }
  395. type MyChartEditReq struct {
  396. MyChartId int `description:"我的图表主键"`
  397. CurrentMyChartClassifyId int `description:"当前分类ID"`
  398. MyChartClassifyId []int `description:"分类id,数组形式"`
  399. }
  400. func GetMyChartClassifyMappingCount(adminId, myChartId, myChartClassifyId int) (count int, err error) {
  401. o := orm.NewOrmUsingDB("data")
  402. sql := ` SELECT COUNT(1) AS count
  403. FROM my_chart_classify_mapping
  404. WHERE 1=1 AND admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  405. err = o.Raw(sql, adminId, myChartId, myChartClassifyId).QueryRow(&count)
  406. return
  407. }
  408. func DeleteMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (err error) {
  409. o := orm.NewOrmUsingDB("data")
  410. sql := ` DELETE FROM my_chart_classify_mapping
  411. WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  412. _, err = o.Raw(sql, adminId, myChartId, myChartClassifyId).Exec()
  413. return
  414. }
  415. func GetMyChartSort(adminId, myChartClassifyId, sort int) (item *MyChartClassifyMapping, err error) {
  416. o := orm.NewOrmUsingDB("data")
  417. sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  418. if sort == 1 {
  419. sql += ` ORDER BY sort DESC LIMIT 1 `
  420. } else {
  421. sql += ` ORDER BY sort ASC LIMIT 1 `
  422. }
  423. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&item)
  424. return
  425. }
  426. func GetMyChartByCondition(condition string, pars []interface{}) (item []*MyChartView, err error) {
  427. o := orm.NewOrmUsingDB("data")
  428. //sql := ` SELECT * FROM my_chart WHERE 1=1 `
  429. sql := `SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  430. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  431. 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
  432. WHERE 1=1
  433. `
  434. if condition != "" {
  435. sql += condition
  436. }
  437. sql += ` GROUP BY a.chart_info_id `
  438. _, err = o.Raw(sql, pars).QueryRows(&item)
  439. return
  440. }
  441. func GetMyChartDetailByCondition(condition string, pars []interface{}) (item *MyChartView, err error) {
  442. o := orm.NewOrmUsingDB("data")
  443. sql := ` SELECT * FROM my_chart WHERE 1=1 `
  444. if condition != "" {
  445. sql += condition
  446. }
  447. err = o.Raw(sql, pars).QueryRow(&item)
  448. return
  449. }
  450. func MoveMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  451. o := orm.NewOrmUsingDB("data")
  452. to, err := o.Begin()
  453. if err != nil {
  454. return
  455. }
  456. defer func() {
  457. if err != nil {
  458. _ = to.Rollback()
  459. } else {
  460. _ = to.Commit()
  461. }
  462. }()
  463. classifyIdMap := make(map[int]int)
  464. for _, v := range myChartClassifyId {
  465. classifyIdMap[v] = v
  466. }
  467. mappingList := make([]*MyChartClassifyMapping, 0)
  468. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  469. _, err = to.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  470. if err != nil {
  471. return
  472. }
  473. for _, v := range mappingList {
  474. if _, ok := classifyIdMap[v.MyChartClassifyId]; !ok {
  475. sql := ` DELETE FROM my_chart_classify_mapping WHERE my_chart_classify_mapping_id=? `
  476. _, err = to.Raw(sql, v.MyChartClassifyMappingId).Exec()
  477. if err != nil {
  478. return
  479. }
  480. }
  481. }
  482. //sql := `DELETE FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? `
  483. //_, err = to.Raw(sql, adminId, myChartId).Exec()
  484. //if err != nil {
  485. // return err
  486. //}
  487. //step := float64(65536)
  488. for _, v := range myChartClassifyId {
  489. var count int
  490. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  491. err = to.Raw(sql, myChartId, v).QueryRow(&count)
  492. if err != nil {
  493. return
  494. }
  495. if count <= 0 {
  496. //total, err := GetMyChartMaxSort(adminId, v)
  497. if err != nil {
  498. return err
  499. }
  500. //sort := (float64(total) + 1) * step
  501. sort := 0
  502. sql = ` INSERT INTO my_chart_classify_mapping (
  503. admin_id,
  504. my_chart_id,
  505. my_chart_classify_id,
  506. create_time,
  507. modify_time,
  508. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  509. _, err = to.Raw(sql, adminId, myChartId, v, sort).Exec()
  510. if err != nil {
  511. return err
  512. }
  513. }
  514. }
  515. return
  516. }
  517. // Update 更新分类基础信息
  518. func (myChartClassify *MyChartClassify) Update(cols []string) (err error) {
  519. o := orm.NewOrmUsingDB("data")
  520. _, err = o.Update(myChartClassify, cols...)
  521. return
  522. }
  523. // GetFirstMyChartClassifyByAdminId 获取当前账号下,排序第一条的分类数据
  524. func GetFirstMyChartClassifyByAdminId(adminId int) (item *MyChartClassify, err error) {
  525. o := orm.NewOrmUsingDB("data")
  526. sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? order by sort asc,my_chart_classify_id asc limit 1`
  527. err = o.Raw(sql, adminId).QueryRow(&item)
  528. return
  529. }
  530. // UpdateMyChartClassifySortByClassifyId 根据分类id更新排序
  531. func UpdateMyChartClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) {
  532. o := orm.NewOrmUsingDB("data")
  533. sql := ` update my_chart_classify set sort = ` + updateSort + ` WHERE admin_id = ? and sort > ? `
  534. if classifyId > 0 {
  535. sql += ` or ( my_chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  536. }
  537. _, err = o.Raw(sql, adminId, nowSort).Exec()
  538. return
  539. }
  540. // ModifyChartClassifyPublicReq 修改我的图库分类是否可见
  541. type ModifyChartClassifyPublicReq struct {
  542. MyChartClassifyId int `description:"分类id"`
  543. IsPublic int `description:"是否所有人可见,0:仅自己可见,1:所有人可见"`
  544. }
  545. // CopyChartClassifyReq 复制我的图库分类
  546. type CopyChartClassifyReq struct {
  547. MyChartClassifyId int `description:"分类id"`
  548. }
  549. // GetPublicMyChartClassifyById 根据分类ID获取公开分类
  550. func GetPublicMyChartClassifyById(myChartClassifyId int) (item *MyChartClassify, err error) {
  551. o := orm.NewOrmUsingDB("data")
  552. sql := ` SELECT * FROM my_chart_classify WHERE is_public=1 AND my_chart_classify_id=? `
  553. err = o.Raw(sql, myChartClassifyId).QueryRow(&item)
  554. return
  555. }
  556. // CopyPublicMyChartClassifyById 根据分类ID获取公开分类
  557. func CopyPublicMyChartClassifyById(myChartClassifyId, adminId int, newMyChartClassifyName string) (newMyChartClassify *MyChartClassify, err error) {
  558. o := orm.NewOrmUsingDB("data")
  559. to, err := o.Begin()
  560. if err != nil {
  561. return
  562. }
  563. defer func() {
  564. if err != nil {
  565. _ = to.Rollback()
  566. } else {
  567. _ = to.Commit()
  568. }
  569. }()
  570. //获取分类里面所有的图表信息
  571. fromMappingList := make([]*MyChartClassifyMapping, 0)
  572. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  573. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMappingList)
  574. if err != nil {
  575. return
  576. }
  577. fromMyChartSortMap := make(map[int]float64)
  578. for _, fromMapping := range fromMappingList {
  579. fromMyChartSortMap[fromMapping.MyChartId] = fromMapping.Sort
  580. }
  581. // 我的图表
  582. fromMyChartList := make([]*MyChart, 0)
  583. sql = `SELECT a.* FROM my_chart a join my_chart_classify_mapping b on a.my_chart_id=b.my_chart_id
  584. WHERE b.my_chart_classify_id=?`
  585. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMyChartList)
  586. if err != nil {
  587. return
  588. }
  589. //新增我的图库分类
  590. newMyChartClassify = &MyChartClassify{
  591. MyChartClassifyName: newMyChartClassifyName,
  592. AdminId: adminId,
  593. CreateTime: time.Now(),
  594. ModifyTime: time.Now(),
  595. }
  596. newMyChartClassifyId, err := to.Insert(newMyChartClassify)
  597. if err != nil {
  598. return
  599. }
  600. newMappingList := make([]*MyChartClassifyMapping, 0)
  601. //将图表加入我的图库
  602. for _, v := range fromMyChartList {
  603. tmpMyChart := &MyChart{
  604. ChartInfoId: v.ChartInfoId,
  605. AdminId: adminId,
  606. Source: v.Source,
  607. CreateTime: time.Now(),
  608. ModifyTime: time.Now(),
  609. }
  610. tmpMyChartId, tmpErr := to.Insert(tmpMyChart)
  611. if tmpErr != nil {
  612. err = tmpErr
  613. }
  614. tmpMyChart.MyChartId = int(tmpMyChartId)
  615. // 往自己的分类里面新增图表关系
  616. //原始图表的排序
  617. tmpSort, ok := fromMyChartSortMap[v.MyChartId]
  618. if !ok {
  619. tmpSort = 0
  620. }
  621. tmp := &MyChartClassifyMapping{
  622. AdminId: adminId,
  623. MyChartId: int(tmpMyChartId),
  624. MyChartClassifyId: int(newMyChartClassifyId),
  625. CreateTime: time.Now(),
  626. ModifyTime: time.Now(),
  627. Sort: tmpSort,
  628. }
  629. newMappingList = append(newMappingList, tmp)
  630. }
  631. if len(newMappingList) > 0 {
  632. _, err = to.InsertMulti(len(newMappingList), newMappingList)
  633. }
  634. return
  635. }
  636. // ModifyChartClassifyCompanyPublicReq 修改公共图库分类是否用户可见
  637. type ModifyChartClassifyCompanyPublicReq struct {
  638. MyChartClassifyId int `description:"分类id"`
  639. IsCompanyPublic int `description:"是否用户可见 0-否 1-是"`
  640. }
  641. // GetCompanyPublicClassifyList 获取用户可见的分类列表
  642. func GetCompanyPublicClassifyList(condition string, pars []interface{}) (item []*MyChartClassify, err error) {
  643. o := orm.NewOrmUsingDB("data")
  644. sql := ` SELECT * FROM my_chart_classify WHERE 1 = 1 AND is_public = 1 AND is_company_public = 1 `
  645. sql += condition
  646. sql += " ORDER BY public_time asc"
  647. _, err = o.Raw(sql, pars).QueryRows(&item)
  648. return
  649. }
  650. // GetMyChartClassifyList 获取我的图表关联的分类列表数据
  651. func GetMyChartClassifyList(myChartClassifyId int) (mappingList []*MyChartClassifyMapping, err error) {
  652. o := orm.NewOrmUsingDB("data")
  653. //o.Begin()
  654. //
  655. //defer func() {
  656. // if err != nil {
  657. // o.Rollback()
  658. // } else {
  659. // o.Commit()
  660. // }
  661. //}()
  662. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_id=?`
  663. _, err = o.Raw(sql, myChartClassifyId).QueryRows(&mappingList)
  664. return
  665. }
  666. // AddMyChartToMyChartClassify 添加我的图表至我的分类
  667. func AddMyChartToMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  668. o := orm.NewOrmUsingDB("data")
  669. //o.Begin()
  670. //defer func() {
  671. // if err != nil {
  672. // o.Rollback()
  673. // } else {
  674. // o.Commit()
  675. // }
  676. //}()
  677. //需要添加的分类id 集合map
  678. classifyIdMap := make(map[int]int)
  679. for _, v := range myChartClassifyId {
  680. classifyIdMap[v] = v
  681. }
  682. //查询已存在系统中的图表分类集合
  683. mappingList := make([]*MyChartClassifyMapping, 0)
  684. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  685. _, err = o.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  686. if err != nil {
  687. return
  688. }
  689. //遍历已存在系统中的图表分类集合,如果和需要添加的分类重复了,那么移除需要添加分类集合map里面的数据
  690. for _, v := range mappingList {
  691. if _, ok := classifyIdMap[v.MyChartClassifyId]; ok {
  692. delete(classifyIdMap, v.MyChartClassifyId)
  693. }
  694. }
  695. step := float64(65536)
  696. for _, v := range myChartClassifyId {
  697. var count int
  698. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  699. err = o.Raw(sql, myChartId, v).QueryRow(&count)
  700. if err != nil {
  701. return
  702. }
  703. if count <= 0 {
  704. total, err := GetMyChartMaxSort(adminId, v)
  705. if err != nil {
  706. return err
  707. }
  708. sort := (float64(total) + 1) * step
  709. sql = ` INSERT INTO my_chart_classify_mapping (
  710. admin_id,
  711. my_chart_id,
  712. my_chart_classify_id,
  713. create_time,
  714. modify_time,
  715. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  716. _, err = o.Raw(sql, adminId, myChartId, v, sort).Exec()
  717. if err != nil {
  718. return err
  719. }
  720. }
  721. }
  722. return
  723. }
  724. // GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition 获取MY ETA 图表数据,根据图表id和操作员id
  725. func GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  726. o := orm.NewOrmUsingDB("data")
  727. 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
  728. FROM my_chart AS a
  729. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  730. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  731. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  732. WHERE 1=1 `
  733. if condition != "" {
  734. sql += condition
  735. }
  736. sql += " GROUP BY a.chart_info_id,a.admin_id ORDER BY c.sort DESC "
  737. _, err = o.Raw(sql, pars).QueryRows(&item)
  738. return
  739. }
  740. func GetRelationChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  741. o := orm.NewOrmUsingDB("data")
  742. sql := ` SELECT a.* FROM chart_info AS a
  743. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  744. if condition != "" {
  745. sql += condition
  746. }
  747. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  748. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  749. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  750. return
  751. }
  752. func GetRelationChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  753. o := orm.NewOrmUsingDB("data")
  754. sql := ` SELECT COUNT(1) AS count FROM chart_info AS a
  755. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  756. if condition != "" {
  757. sql += condition
  758. }
  759. err = o.Raw(sql, pars).QueryRow(&count)
  760. return
  761. }