my_chart.go 35 KB

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