my_chart.go 31 KB

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