classify.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. package models
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "time"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. )
  9. type Classify struct {
  10. Id int `orm:"column(id);pk"`
  11. ClassifyName string `description:"分类名称"`
  12. Sort int `json:"-"`
  13. ParentId int `description:"父级分类id"`
  14. CreateTime time.Time `description:"创建时间"`
  15. ModifyTime time.Time `description:"修改时间"`
  16. Abstract string `description:"栏目简介"`
  17. Descript string `description:"分享描述"`
  18. ReportAuthor string `description:"栏目作者"`
  19. AuthorDescript string `description:"作者简介"`
  20. ColumnImgUrl string `description:"栏目配图"`
  21. HeadImgUrl string `description:"头部banner"`
  22. AvatarImgUrl string `description:"头像"`
  23. ReportImgUrl string `description:"报告配图"`
  24. HomeImgUrl string `description:"首页配图"`
  25. ClassifyLabel string `description:"分类标签"`
  26. ShowType int `description:"展示类型:1-列表 2-专栏"`
  27. HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  28. VipTitle string `description:"研究员头衔"`
  29. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  30. YbFiccSort int `description:"小程序FICC页排序"`
  31. YbFiccIcon string `description:"小程序FICC页icon"`
  32. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  33. YbIconUrl string `description:"小程序已购页icon"`
  34. YbBgUrl string `description:"小程序已购详情背景图"`
  35. YbListImg string `description:"小程序研报列表封面图"`
  36. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  37. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  38. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  39. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`
  40. IsMassSend int `description:"1:群发,0:非群发"`
  41. Enabled int `description:"是否可用,1可用,0禁用"`
  42. Level int `description:"层级"`
  43. HasChild int `description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  44. ReportDetailShowType int `description:"报告详情的展示类型:1-拼接;2:目录"`
  45. }
  46. type ClassifyVO struct {
  47. Id int `orm:"column(id);pk"`
  48. ClassifyName string `description:"分类名称"`
  49. Sort int `json:"-"`
  50. ParentId int `description:"父级分类id"`
  51. ClassifyLabel string `description:"分类标签"`
  52. Enabled int `description:"是否可用,1可用,0禁用"`
  53. Level int `description:"层级"`
  54. Children *[]ClassifyVO
  55. }
  56. type ClassifyAddReq struct {
  57. ClassifyName string `description:"分类名称"`
  58. ParentId int `description:"父级分类id,没有父级分类传0"`
  59. ChartPermissionIdList []int `description:"权限id数组"`
  60. /*Abstract string `description:"栏目简介"`
  61. Descript string `description:"分享描述"`
  62. ReportAuthor string `description:"栏目作者"`
  63. AuthorDescript string `description:"作者简介"`
  64. ColumnImgUrl string `description:"栏目配图"`
  65. ReportImgUrl string `description:"报告配图"`
  66. HeadImgUrl string `description:"头部banner"`
  67. AvatarImgUrl string `description:"头像"`
  68. HomeImgUrl string `description:"首页配图"`
  69. ClassifyLabel string `description:"分类标签"`
  70. ShowType int `description:"展示类型:1-列表 2-专栏"`
  71. HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  72. VipTitle string `description:"研究员头衔"`
  73. Sort int `description:"后台排序"`
  74. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  75. YbFiccSort int `description:"小程序FICC页排序"`
  76. YbFiccIcon string `description:"小程序FICC页icon"`
  77. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  78. YbIconUrl string `description:"小程序已购页icon"`
  79. YbBgUrl string `description:"小程序已购详情背景图"`
  80. YbListImg string `description:"小程序研报列表封面图"`
  81. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  82. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  83. MenuList []*ClassifyMenuSaveReq `description:"子目录列表"`
  84. ClassifyMenuId int `description:"二级分类-子目录ID"`
  85. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  86. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`*/
  87. }
  88. func (c *Classify) Delete(childIds []int) (err error) {
  89. o := orm.NewOrmUsingDB("rddp")
  90. tx, err := o.Begin()
  91. if err != nil {
  92. return
  93. }
  94. defer func() {
  95. if err != nil {
  96. tx.Rollback()
  97. } else {
  98. tx.Commit()
  99. }
  100. }()
  101. // 删除自身
  102. _, err = tx.Delete(c)
  103. if err != nil {
  104. return
  105. }
  106. // 删除子分类
  107. if len(childIds) > 0 {
  108. sql := `DELETE FROM classify WHERE id IN (` + utils.GetOrmInReplace(len(childIds)) + `)`
  109. _, err = tx.Raw(sql, childIds).Exec()
  110. if err != nil {
  111. return
  112. }
  113. }
  114. // 检查是否需要更新父类
  115. if c.ParentId > 0 {
  116. var count int
  117. sql := `SELECT COUNT(*) FROM classify WHERE parent_id=? `
  118. err = tx.Raw(sql, c.ParentId).QueryRow(&count)
  119. if err != nil {
  120. return
  121. }
  122. if count == 0 {
  123. sql = `UPDATE classify SET has_child=0 WHERE id=? `
  124. _, err = tx.Raw(sql, c.ParentId).Exec()
  125. if err != nil {
  126. return
  127. }
  128. }
  129. }
  130. deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  131. _, err = tx.Raw(deleteImgSql, c.Id).Exec()
  132. return
  133. }
  134. func GetClassifyByName(classifyName string, parentId int) (item *Classify, err error) {
  135. sql := `SELECT * FROM classify WHERE classify_name=? AND parent_id=? `
  136. o := orm.NewOrmUsingDB("rddp")
  137. err = o.Raw(sql, classifyName, parentId).QueryRow(&item)
  138. return
  139. }
  140. func GetClassifyById(classifyId int) (item *Classify, err error) {
  141. sql := `SELECT * FROM classify WHERE id=? `
  142. o := orm.NewOrmUsingDB("rddp")
  143. err = o.Raw(sql, classifyId).QueryRow(&item)
  144. return
  145. }
  146. // 添加分类
  147. func AddClassify(item *Classify) (err error) {
  148. o := orm.NewOrmUsingDB("rddp")
  149. id, err := o.Insert(item)
  150. if err != nil {
  151. return
  152. }
  153. item.Id = int(id)
  154. return
  155. }
  156. func GetReportCountByClassifyId(classifyId int) (count int, err error) {
  157. o := orm.NewOrmUsingDB("rddp")
  158. sql := `SELECT COUNT(1) AS count FROM report WHERE classify_id_second=? `
  159. err = o.Raw(sql, classifyId).QueryRow(&count)
  160. return
  161. }
  162. func GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  163. o := orm.NewOrmUsingDB("rddp")
  164. sql := `SELECT COUNT(1) as num FROM classify AS a
  165. INNER JOIN report AS b ON a.id=b.classify_id_second
  166. WHERE a.parent_id=? `
  167. err = o.Raw(sql, classifyId).QueryRow(&count)
  168. return
  169. }
  170. func GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  171. sqlCount := `
  172. SELECT COUNT(1) as num FROM classify AS a
  173. WHERE a.parent_id=? `
  174. o := orm.NewOrmUsingDB("rddp")
  175. err = o.Raw(sqlCount, classifyId).QueryRow(&count)
  176. return
  177. }
  178. // 删除分类
  179. func DeleteClassify(classifyId int) (err error) {
  180. sql := `DELETE FROM classify WHERE id=? `
  181. o := orm.NewOrmUsingDB("rddp")
  182. _, err = o.Raw(sql, classifyId).Exec()
  183. if err != nil {
  184. return
  185. }
  186. deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  187. _, err = o.Raw(deleteImgSql, classifyId).Exec()
  188. return
  189. }
  190. // classifyName, abstract, descript string, parentId, classifyId int
  191. // 修改分类
  192. func EditClassify(req *EditClassifyReq) (err error) {
  193. o := orm.NewOrmUsingDB("rddp")
  194. //sql := `UPDATE classify SET classify_name = ?,abstract=?, parent_id= ?,descript=?,report_author=?,author_descript=?,column_img_url=?,head_img_url=?,avatar_img_url=?,report_img_url=?,home_img_url=?,classify_label=?,show_type=?,has_teleconference=?,vip_title=?,modify_time= NOW() WHERE id = ? `
  195. //_, err = o.Raw(sql, req.ClassifyName, req.Abstract, req.ParentId, req.Descript, req.ReportAuthor, req.AuthorDescript, req.ColumnImgUrl, req.HeadImgUrl, req.AvatarImgUrl, req.ReportImgUrl, req.HomeImgUrl, req.ClassifyLabel, req.ShowType, req.HasTeleconference, req.VipTitle, req.ClassifyId).Exec()
  196. sql := `UPDATE classify SET classify_name = ?,parent_id= ?,modify_time= NOW() WHERE id = ? `
  197. _, err = o.Raw(sql, req.ClassifyName, req.ParentId, req.ClassifyId).Exec()
  198. return
  199. }
  200. // ParentClassify
  201. // @Description: 获取父级分类
  202. // @author: Roc
  203. // @datetime 2024-06-18 15:03:49
  204. // @return items []*Classify
  205. // @return err error
  206. func ParentClassify() (items []*Classify, err error) {
  207. sql := `SELECT * FROM classify WHERE parent_id=0 order by id desc `
  208. o := orm.NewOrmUsingDB("rddp")
  209. _, err = o.Raw(sql).QueryRows(&items)
  210. return
  211. }
  212. // 根据id获取分类详情
  213. func FindByIdClassify(classifyId int) (item *Classify, err error) {
  214. sql := `SELECT * FROM classify WHERE id=? `
  215. o := orm.NewOrmUsingDB("rddp")
  216. err = o.Raw(sql, classifyId).QueryRow(&item)
  217. return
  218. }
  219. type ClassifyList struct {
  220. Id int `orm:"column(id);pk"`
  221. ClassifyName string `description:"分类名称"`
  222. Sort int `description:"排序"`
  223. ParentId int `description:"父级分类id"`
  224. CreateTime time.Time `description:"创建时间"`
  225. ModifyTime time.Time `description:"修改时间"`
  226. Abstract string `description:"简介"`
  227. Descript string `description:"描述"`
  228. ClassifyLabel string `description:"分类标签"`
  229. ShowType int `description:"展示类型:1-列表 2-专栏"`
  230. HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  231. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  232. YbFiccSort int `description:"小程序FICC页排序"`
  233. YbFiccIcon string `description:"小程序FICC页icon"`
  234. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  235. YbIconUrl string `description:"小程序已购页icon"`
  236. YbBgUrl string `description:"小程序已购详情背景图"`
  237. YbListImg string `description:"小程序研报列表封面图"`
  238. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  239. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  240. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  241. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`
  242. Enabled int `description:"是否可用,1可用,0禁用"`
  243. Child []*ClassifyList
  244. ClassifyMenuId int `description:"二级分类-子目录ID"`
  245. ClassifyMenuList []*ClassifyMenu
  246. ChartPermissionIdList []int `description:"绑定的权限ID"`
  247. Level int `description:"层级"`
  248. HasChild int `description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  249. IsEnableDelete int `description:"是否允许删除: 0-否; 1-是"`
  250. }
  251. type ClassifyItem struct {
  252. Classify
  253. ClassifyMenuId int `description:"二级分类-子目录ID"`
  254. ClassifyMenuList []*ClassifyMenu
  255. ChartPermissionIdList []int `description:"绑定的权限ID"`
  256. Child []*ClassifyItem
  257. }
  258. type ClassifyListResp struct {
  259. List []*ClassifyList
  260. }
  261. type ClassifyPermissionListResp struct {
  262. List []*ClassifyList
  263. Paging *paging.PagingItem `description:"分页数据"`
  264. }
  265. // 获取分类列表
  266. func GetClassifyList(keyWord string, enabled int) (items []*ClassifyList, err error) {
  267. sql := ``
  268. companyTypeSqlStr := ``
  269. if enabled == 1 {
  270. companyTypeSqlStr += ` AND enabled = 1 `
  271. }
  272. pars := make([]interface{}, 0)
  273. if keyWord != "" {
  274. sql = `SELECT * FROM (
  275. SELECT * FROM classify
  276. WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE ?
  277. UNION
  278. SELECT * FROM classify
  279. WHERE id IN( SELECT parent_id FROM classify
  280. WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE ? )
  281. )AS t
  282. ORDER BY sort ASC,create_time ASC`
  283. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  284. } else {
  285. sql = `SELECT * FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  286. sql += ` ORDER BY sort ASC, create_time ASC`
  287. }
  288. pars = append(pars)
  289. o := orm.NewOrmUsingDB("rddp")
  290. _, err = o.Raw(sql, pars...).QueryRows(&items)
  291. return
  292. }
  293. func GetClassifyListCount(keyWord, companyType string, hideDayWeek int) (count int, err error) {
  294. sqlCount := ``
  295. companyTypeSqlStr := ``
  296. if companyType == "ficc" {
  297. companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  298. } else if companyType == "权益" {
  299. companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  300. }
  301. pars := make([]interface{}, 0)
  302. if keyWord != "" {
  303. sqlCount = `SELECT COUNT(1) AS count FROM (
  304. SELECT * FROM classify
  305. WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE ?
  306. UNION
  307. SELECT * FROM classify
  308. WHERE id IN(SELECT parent_id FROM classify
  309. WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE ? )
  310. )AS t `
  311. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  312. } else {
  313. sqlCount = `SELECT COUNT(1) AS count FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  314. if hideDayWeek == 1 {
  315. sqlCount += ` AND classify_name <> '晨报' AND classify_name <> '周报' `
  316. }
  317. }
  318. o := orm.NewOrmUsingDB("rddp")
  319. err = o.Raw(sqlCount, pars...).QueryRow(&count)
  320. return
  321. }
  322. type CheckDeleteClassifyReq struct {
  323. ClassifyId int `description:"分类ID"`
  324. }
  325. type CheckDeleteClassifyResp struct {
  326. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  327. Msg string `description:"描述信息"`
  328. }
  329. type DeleteClassifyReq struct {
  330. ClassifyId int `description:"分类ID"`
  331. }
  332. type EditClassifyReq struct {
  333. ClassifyId int `description:"分类ID"`
  334. ClassifyAddReq
  335. }
  336. type FindByIdClassifyReq struct {
  337. ClassifyId int `description:"分类ID"`
  338. }
  339. func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  340. o := orm.NewOrmUsingDB("rddp")
  341. sql := ``
  342. pars := make([]interface{}, 0)
  343. if keyWord != "" {
  344. sql = `SELECT * FROM classify WHERE classify_name LIKE ? AND parent_id=? ORDER BY create_time ASC `
  345. pars = append(pars, utils.GetLikeKeyword(keyWord))
  346. } else {
  347. sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  348. }
  349. pars = append(pars, parentId)
  350. _, err = o.Raw(sql, pars...).QueryRows(&items)
  351. return
  352. }
  353. func GetClassifyChildByParentIds(parentId []int, keyWord string, enabled int) (items []*Classify, err error) {
  354. parentIdLen := len(parentId)
  355. if parentIdLen == 0 {
  356. return
  357. }
  358. o := orm.NewOrmUsingDB("rddp")
  359. sql := ``
  360. pars := make([]interface{}, 0)
  361. pars = append(pars, parentId)
  362. if keyWord != "" {
  363. sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) AND classify_name LIKE ? `
  364. pars = append(pars, utils.GetLikeKeyword(keyWord))
  365. } else {
  366. sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) `
  367. }
  368. if enabled == 1 {
  369. sql += ` AND enabled=1 `
  370. }
  371. sql += ` ORDER BY create_time ASC `
  372. _, err = o.Raw(sql, pars...).QueryRows(&items)
  373. return
  374. }
  375. // EditClassifyPermissionReq 编辑分类权限请求
  376. type EditClassifyPermissionReq struct {
  377. ClassifyId int `description:"分类ID"`
  378. ChartPermissionIdList []int `description:"权限id数组"`
  379. }
  380. // GetAllClassify 获取所有分类
  381. func GetAllClassify() (list []*Classify, err error) {
  382. o := orm.NewOrmUsingDB("rddp")
  383. sql := ` SELECT * FROM classify `
  384. _, err = o.Raw(sql).QueryRows(&list)
  385. return
  386. }
  387. // GetAllClassifyWithDesc 获取所有倒序分类
  388. func GetAllClassifyWithDesc() (list []*Classify, err error) {
  389. o := orm.NewOrmUsingDB("rddp")
  390. sql := ` SELECT * FROM classify ORDER BY id DESC `
  391. _, err = o.Raw(sql).QueryRows(&list)
  392. return
  393. }
  394. // GetClassifyByKeyword 名称获取分类
  395. func GetClassifyByKeyword(keyword string) (item Classify, err error) {
  396. o := orm.NewOrmUsingDB("rddp")
  397. sql := ` SELECT * FROM classify WHERE classify_name = ? LIMIT 1 `
  398. err = o.Raw(sql, keyword).QueryRow(&item)
  399. return
  400. }
  401. // UpdateClassify 更新分类
  402. func (classifyInfo *Classify) UpdateClassify(cols []string) (err error) {
  403. o := orm.NewOrmUsingDB("rddp")
  404. _, err = o.Update(classifyInfo, cols...)
  405. return
  406. }
  407. // SimpleClassifyList 简版分类列表
  408. type SimpleClassifyList struct {
  409. Id int `description:"分类ID"`
  410. ClassifyName string `description:"分类名称"`
  411. ParentId int `description:"父级ID"`
  412. Sort int `description:"排序"`
  413. Child []*SimpleClassifyList
  414. }
  415. // GetClassifyByCondition 获取分类列表
  416. func GetClassifyByCondition(condition, orderRule string, pars []interface{}) (items []*SimpleClassifyList, err error) {
  417. o := orm.NewOrmUsingDB("rddp")
  418. sql := `SELECT * FROM classify WHERE 1 = 1 `
  419. if condition != `` {
  420. sql += condition
  421. }
  422. order := `sort ASC, create_time ASC`
  423. if orderRule != `` {
  424. order = orderRule
  425. }
  426. sql += ` ORDER BY ` + order
  427. _, err = o.Raw(sql, pars).QueryRows(&items)
  428. return
  429. }
  430. // UpdateChildClassifyRelateSetting 更新子分类关联设置
  431. func UpdateChildClassifyRelateSetting(parentId, relateTel, relateVideo int) (err error) {
  432. o := orm.NewOrmUsingDB("rddp")
  433. sql := `UPDATE classify SET relate_tel = ?, relate_video = ? WHERE parent_id = ?`
  434. _, err = o.Raw(sql, relateTel, relateVideo, parentId).Exec()
  435. return
  436. }
  437. // RelateTelSecClassifyWithPermissions 关联了电话会的二级分类及权限
  438. type RelateTelSecClassifyWithPermissions struct {
  439. Id int `description:"分类ID"`
  440. ClassifyName string `description:"分类名称"`
  441. ChartPermissionIds string `description:"权限IDs"`
  442. }
  443. // UpdateClassifySortByParentId 根据父类id更新排序
  444. func UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
  445. o := orm.NewOrmUsingDB("rddp")
  446. sql := ` update classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  447. if permissionId > 0 {
  448. sql += ` or ( id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  449. }
  450. _, err = o.Raw(sql, parentId, nowSort).Exec()
  451. return
  452. }
  453. // GetMaxSortByParentId 获取最大的排序值
  454. func (classifyInfo *Classify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  455. o := orm.NewOrmUsingDB("rddp")
  456. sql := `SELECT max(sort) AS sort FROM classify WHERE parent_id = ? `
  457. err = o.Raw(sql, parentId).QueryRow(&maxSort)
  458. return
  459. }
  460. // GetMaxSort 获取最大的排序值
  461. func (classifyInfo *Classify) GetMaxSort() (maxSort int, err error) {
  462. o := orm.NewOrmUsingDB("rddp")
  463. sql := `SELECT max(sort) AS sort FROM classify`
  464. err = o.Raw(sql).QueryRow(&maxSort)
  465. return
  466. }
  467. // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  468. func (classifyInfo *Classify) GetFirstClassifyByParentId(parentId int) (item *Classify, err error) {
  469. o := orm.NewOrmUsingDB("rddp")
  470. sql := `SELECT * FROM classify WHERE parent_id = ? order by sort asc, id asc limit 1`
  471. err = o.Raw(sql, parentId).QueryRow(&item)
  472. return
  473. }
  474. type ClassifyMoveReq struct {
  475. ClassifyId int `description:"分类ID"`
  476. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  477. NextClassifyId int `description:"下一个兄弟节点分类id"`
  478. }
  479. type ClassifySetEnabledReq struct {
  480. ClassifyId int `description:"分类ID"`
  481. Enabled int `description:"是否可用,1可用,0禁用"`
  482. }
  483. func (classifyInfo *Classify) SetEnabled(id, enabled int) (err error) {
  484. o := orm.NewOrmUsingDB("rddp")
  485. to, err := o.Begin()
  486. if err != nil {
  487. return
  488. }
  489. defer func() {
  490. if err != nil {
  491. _ = to.Rollback()
  492. } else {
  493. _ = to.Commit()
  494. }
  495. }()
  496. sql := ` UPDATE classify SET enabled =? WHERE id = ?`
  497. _, err = to.Raw(sql, enabled, id).Exec()
  498. if err != nil {
  499. return
  500. }
  501. sql = ` UPDATE classify SET enabled =? WHERE parent_id = ?`
  502. _, err = to.Raw(sql, enabled, id).Exec()
  503. if err != nil {
  504. return
  505. }
  506. return
  507. }
  508. // GetCountClassifyChildByParentId
  509. // @Description: 获取父级分类下子分类数量
  510. // @author: Roc
  511. // @datetime 2024-06-17 10:58:46
  512. // @param parentId int
  513. // @return total int
  514. // @return err error
  515. func GetCountClassifyChildByParentId(parentId int) (total int, err error) {
  516. o := orm.NewOrmUsingDB("rddp")
  517. sql := `SELECT count(1) AS total FROM classify WHERE parent_id = ? `
  518. err = o.Raw(sql, parentId).QueryRow(&total)
  519. return
  520. }
  521. // GetClassifyListByKeyword
  522. // @Description: 获取分类列表
  523. // @author: Roc
  524. // @datetime 2024-06-19 09:49:33
  525. // @param keyWord string
  526. // @param enabled int
  527. // @return items []*ClassifyList
  528. // @return err error
  529. func GetClassifyListByKeyword(keyWord string, enabled int) (items []*ClassifyList, err error) {
  530. sql := ``
  531. pars := make([]interface{}, 0)
  532. sql = `SELECT * FROM classify WHERE 1=1 `
  533. if enabled == 1 {
  534. sql += ` AND enabled = 1 `
  535. }
  536. if keyWord != `` {
  537. sql += ` AND classify_name LIKE ? `
  538. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  539. }
  540. sql += ` ORDER BY sort ASC, create_time ASC`
  541. o := orm.NewOrmUsingDB("rddp")
  542. _, err = o.Raw(sql, pars...).QueryRows(&items)
  543. return
  544. }
  545. // GetClassifyListByParentIdList
  546. // @Description: 获取分类列表
  547. // @author: Roc
  548. // @datetime 2024-06-19 09:49:33
  549. // @param keyWord string
  550. // @param enabled int
  551. // @return items []*ClassifyList
  552. // @return err error
  553. func GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*ClassifyList, err error) {
  554. num := len(parentClassifyIdList)
  555. if num <= 0 {
  556. return
  557. }
  558. sql := `SELECT * FROM classify WHERE id in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort ASC, create_time ASC`
  559. o := orm.NewOrmUsingDB("rddp")
  560. _, err = o.Raw(sql, parentClassifyIdList).QueryRows(&items)
  561. return
  562. }
  563. // GetClassifyListByIdList
  564. // @Description: 根据指标ID列表,获取分类列表
  565. // @author: Roc
  566. // @datetime 2024-06-27 15:23:57
  567. // @param classifyIdList []int
  568. // @return items []*Classify
  569. // @return err error
  570. func GetClassifyListByIdList(classifyIdList []int) (items []*Classify, err error) {
  571. num := len(classifyIdList)
  572. if num <= 0 {
  573. return
  574. }
  575. sql := `SELECT * FROM classify WHERE id IN (` + utils.GetOrmInReplace(num) + `) `
  576. o := orm.NewOrmUsingDB("rddp")
  577. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  578. return
  579. }
  580. // GetClassifyListByParentId 查询父分类下所有的子分类
  581. func GetClassifyListByParentId(parentId int) (items []*Classify, err error) {
  582. o := orm.NewOrmUsingDB("rddp")
  583. sql := `SELECT * FROM classify WHERE parent_id = ? and enabled = 1`
  584. _, err = o.Raw(sql, parentId).QueryRows(&items)
  585. return items, err
  586. }