my_chart.go 34 KB

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