classify.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package models
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type 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. }
  43. type ClassifyAddReq struct {
  44. ClassifyName string `description:"分类名称"`
  45. ParentId int `description:"父级分类id,没有父级分类传0"`
  46. ChartPermissionIdList []int `description:"权限id数组"`
  47. /*Abstract string `description:"栏目简介"`
  48. Descript string `description:"分享描述"`
  49. ReportAuthor string `description:"栏目作者"`
  50. AuthorDescript string `description:"作者简介"`
  51. ColumnImgUrl string `description:"栏目配图"`
  52. ReportImgUrl string `description:"报告配图"`
  53. HeadImgUrl string `description:"头部banner"`
  54. AvatarImgUrl string `description:"头像"`
  55. HomeImgUrl string `description:"首页配图"`
  56. ClassifyLabel string `description:"分类标签"`
  57. ShowType int `description:"展示类型:1-列表 2-专栏"`
  58. HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  59. VipTitle string `description:"研究员头衔"`
  60. Sort int `description:"后台排序"`
  61. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  62. YbFiccSort int `description:"小程序FICC页排序"`
  63. YbFiccIcon string `description:"小程序FICC页icon"`
  64. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  65. YbIconUrl string `description:"小程序已购页icon"`
  66. YbBgUrl string `description:"小程序已购详情背景图"`
  67. YbListImg string `description:"小程序研报列表封面图"`
  68. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  69. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  70. MenuList []*ClassifyMenuSaveReq `description:"子目录列表"`
  71. ClassifyMenuId int `description:"二级分类-子目录ID"`
  72. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  73. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`*/
  74. }
  75. func GetClassifyByName(classifyName string, parentId int) (item *Classify, err error) {
  76. sql := `SELECT * FROM classify WHERE classify_name=? AND parent_id=? `
  77. o := orm.NewOrmUsingDB("rddp")
  78. err = o.Raw(sql, classifyName, parentId).QueryRow(&item)
  79. return
  80. }
  81. func GetClassifyById(classifyId int) (item *Classify, err error) {
  82. sql := `SELECT * FROM classify WHERE id=? `
  83. o := orm.NewOrmUsingDB("rddp")
  84. err = o.Raw(sql, classifyId).QueryRow(&item)
  85. return
  86. }
  87. // 添加分类
  88. func AddClassify(item *Classify) (err error) {
  89. o := orm.NewOrmUsingDB("rddp")
  90. id, err := o.Insert(item)
  91. if err != nil {
  92. return
  93. }
  94. item.Id = int(id)
  95. return
  96. }
  97. func GetReportCountByClassifyId(classifyId int) (count int, err error) {
  98. o := orm.NewOrmUsingDB("rddp")
  99. sql := `SELECT COUNT(1) AS count FROM report WHERE classify_id_second=? `
  100. err = o.Raw(sql, classifyId).QueryRow(&count)
  101. return
  102. }
  103. func GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  104. o := orm.NewOrmUsingDB("rddp")
  105. sql := `SELECT COUNT(1) as num FROM classify AS a
  106. INNER JOIN report AS b ON a.id=b.classify_id_second
  107. WHERE a.parent_id=? `
  108. err = o.Raw(sql, classifyId).QueryRow(&count)
  109. return
  110. }
  111. func GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  112. sqlCount := `
  113. SELECT COUNT(1) as num FROM classify AS a
  114. WHERE a.parent_id=? `
  115. o := orm.NewOrmUsingDB("rddp")
  116. err = o.Raw(sqlCount, classifyId).QueryRow(&count)
  117. return
  118. }
  119. // 删除分类
  120. func DeleteClassify(classifyId int) (err error) {
  121. sql := `DELETE FROM classify WHERE id=? `
  122. o := orm.NewOrmUsingDB("rddp")
  123. _, err = o.Raw(sql, classifyId).Exec()
  124. if err != nil {
  125. return
  126. }
  127. deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  128. _, err = o.Raw(deleteImgSql, classifyId).Exec()
  129. return
  130. }
  131. // classifyName, abstract, descript string, parentId, classifyId int
  132. // 修改分类
  133. func EditClassify(req *EditClassifyReq) (err error) {
  134. o := orm.NewOrmUsingDB("rddp")
  135. //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 = ? `
  136. //_, 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()
  137. sql := `UPDATE classify SET classify_name = ?,parent_id= ?,modify_time= NOW() WHERE id = ? `
  138. _, err = o.Raw(sql, req.ClassifyName, req.ParentId, req.ClassifyId).Exec()
  139. return
  140. }
  141. //获取父级分类
  142. func ParentClassify() (items []*Classify, err error) {
  143. sql := `SELECT * FROM classify WHERE parent_id=0 order by id desc `
  144. o := orm.NewOrmUsingDB("rddp")
  145. _, err = o.Raw(sql).QueryRows(&items)
  146. return
  147. }
  148. // 根据id获取分类详情
  149. func FindByIdClassify(classifyId int) (item *Classify, err error) {
  150. sql := `SELECT * FROM classify WHERE id=? `
  151. o := orm.NewOrmUsingDB("rddp")
  152. err = o.Raw(sql, classifyId).QueryRow(&item)
  153. return
  154. }
  155. type ClassifyList struct {
  156. Id int `orm:"column(id);pk"`
  157. ClassifyName string `description:"分类名称"`
  158. Sort int `description:"排序"`
  159. ParentId int `description:"父级分类id"`
  160. CreateTime time.Time `description:"创建时间"`
  161. ModifyTime time.Time `description:"修改时间"`
  162. Abstract string `description:"简介"`
  163. Descript string `description:"描述"`
  164. ClassifyLabel string `description:"分类标签"`
  165. ShowType int `description:"展示类型:1-列表 2-专栏"`
  166. HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  167. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  168. YbFiccSort int `description:"小程序FICC页排序"`
  169. YbFiccIcon string `description:"小程序FICC页icon"`
  170. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  171. YbIconUrl string `description:"小程序已购页icon"`
  172. YbBgUrl string `description:"小程序已购详情背景图"`
  173. YbListImg string `description:"小程序研报列表封面图"`
  174. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  175. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  176. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  177. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`
  178. Enabled int `description:"是否可用,1可用,0禁用"`
  179. Child []*ClassifyItem
  180. ClassifyMenuList []*ClassifyMenu
  181. }
  182. type ClassifyItem struct {
  183. Classify
  184. ClassifyMenuId int `description:"二级分类-子目录ID"`
  185. ClassifyMenuList []*ClassifyMenu
  186. ChartPermissionIdList []int `description:"绑定的权限ID"`
  187. }
  188. type ClassifyListResp struct {
  189. List []*ClassifyList
  190. }
  191. type ClassifyPermissionListResp struct {
  192. List []*ClassifyList
  193. Paging *paging.PagingItem `description:"分页数据"`
  194. }
  195. // 获取分类列表
  196. func GetClassifyList(keyWord, companyType string, hideDayWeek, enabled int) (items []*ClassifyList, err error) {
  197. sql := ``
  198. companyTypeSqlStr := ``
  199. if companyType == "ficc" {
  200. companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  201. } else if companyType == "权益" {
  202. companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  203. }
  204. if enabled == 1 {
  205. companyTypeSqlStr += ` AND enabled = 1 `
  206. }
  207. pars := make([]interface{}, 0)
  208. if keyWord != "" {
  209. sql = `SELECT * FROM (
  210. SELECT * FROM classify
  211. WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE ?
  212. UNION
  213. SELECT * FROM classify
  214. WHERE id IN( SELECT parent_id FROM classify
  215. WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE ? )
  216. )AS t
  217. ORDER BY sort ASC,create_time ASC`
  218. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  219. } else {
  220. sql = `SELECT * FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  221. if hideDayWeek == 1 {
  222. sql += ` AND classify_name <> '晨报' AND classify_name <> '周报' `
  223. }
  224. sql += ` ORDER BY sort ASC, create_time ASC`
  225. }
  226. pars = append(pars)
  227. o := orm.NewOrmUsingDB("rddp")
  228. _, err = o.Raw(sql, pars...).QueryRows(&items)
  229. return
  230. }
  231. func GetClassifyListCount(keyWord, companyType string, hideDayWeek int) (count int, err error) {
  232. sqlCount := ``
  233. companyTypeSqlStr := ``
  234. if companyType == "ficc" {
  235. companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  236. } else if companyType == "权益" {
  237. companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  238. }
  239. pars := make([]interface{}, 0)
  240. if keyWord != "" {
  241. sqlCount = `SELECT COUNT(1) AS count FROM (
  242. SELECT * FROM classify
  243. WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE ?
  244. UNION
  245. SELECT * FROM classify
  246. WHERE id IN(SELECT parent_id FROM classify
  247. WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE ? )
  248. )AS t `
  249. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  250. } else {
  251. sqlCount = `SELECT COUNT(1) AS count FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  252. if hideDayWeek == 1 {
  253. sqlCount += ` AND classify_name <> '晨报' AND classify_name <> '周报' `
  254. }
  255. }
  256. o := orm.NewOrmUsingDB("rddp")
  257. err = o.Raw(sqlCount, pars...).QueryRow(&count)
  258. return
  259. }
  260. type CheckDeleteClassifyReq struct {
  261. ClassifyId int `description:"分类ID"`
  262. }
  263. type CheckDeleteClassifyResp struct {
  264. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  265. Msg string `description:"描述信息"`
  266. }
  267. type DeleteClassifyReq struct {
  268. ClassifyId int `description:"分类ID"`
  269. }
  270. type EditClassifyReq struct {
  271. ClassifyId int `description:"分类ID"`
  272. ClassifyAddReq
  273. }
  274. type FindByIdClassifyReq struct {
  275. ClassifyId int `description:"分类ID"`
  276. }
  277. func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  278. o := orm.NewOrmUsingDB("rddp")
  279. sql := ``
  280. pars := make([]interface{}, 0)
  281. if keyWord != "" {
  282. sql = `SELECT * FROM classify WHERE classify_name LIKE ? AND parent_id=? ORDER BY create_time ASC `
  283. pars = append(pars, utils.GetLikeKeyword(keyWord))
  284. } else {
  285. sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  286. }
  287. pars = append(pars, parentId)
  288. _, err = o.Raw(sql, pars...).QueryRows(&items)
  289. return
  290. }
  291. func GetClassifyChildByParentIds(parentId []int, keyWord string, enabled int) (items []*Classify, err error) {
  292. parentIdLen := len(parentId)
  293. if parentIdLen == 0 {
  294. return
  295. }
  296. o := orm.NewOrmUsingDB("rddp")
  297. sql := ``
  298. pars := make([]interface{}, 0)
  299. pars = append(pars, parentId)
  300. if keyWord != "" {
  301. sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) AND classify_name LIKE ? `
  302. pars = append(pars, utils.GetLikeKeyword(keyWord))
  303. } else {
  304. sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) `
  305. }
  306. if enabled == 1 {
  307. sql += ` AND enabled=1 `
  308. }
  309. sql += ` ORDER BY create_time ASC `
  310. _, err = o.Raw(sql, pars...).QueryRows(&items)
  311. return
  312. }
  313. // EditClassifyPermissionReq 编辑分类权限请求
  314. type EditClassifyPermissionReq struct {
  315. ClassifyId int `description:"分类ID"`
  316. ChartPermissionIdList []int `description:"权限id数组"`
  317. }
  318. // GetAllClassify 获取所有分类
  319. func GetAllClassify() (list []*Classify, err error) {
  320. o := orm.NewOrmUsingDB("rddp")
  321. sql := ` SELECT * FROM classify `
  322. _, err = o.Raw(sql).QueryRows(&list)
  323. return
  324. }
  325. // GetClassifyByKeyword 名称获取分类
  326. func GetClassifyByKeyword(keyword string) (item Classify, err error) {
  327. o := orm.NewOrmUsingDB("rddp")
  328. sql := ` SELECT * FROM classify WHERE classify_name = ? LIMIT 1 `
  329. err = o.Raw(sql, keyword).QueryRow(&item)
  330. return
  331. }
  332. // UpdateClassify 更新分类
  333. func (classifyInfo *Classify) UpdateClassify(cols []string) (err error) {
  334. o := orm.NewOrmUsingDB("rddp")
  335. _, err = o.Update(classifyInfo, cols...)
  336. return
  337. }
  338. // SimpleClassifyList 简版分类列表
  339. type SimpleClassifyList struct {
  340. Id int `description:"分类ID"`
  341. ClassifyName string `description:"分类名称"`
  342. ParentId int `description:"父级ID"`
  343. Sort int `description:"排序"`
  344. Child []*SimpleClassifyList
  345. }
  346. // GetClassifyByCondition 获取分类列表
  347. func GetClassifyByCondition(condition, orderRule string, pars []interface{}) (items []*SimpleClassifyList, err error) {
  348. o := orm.NewOrmUsingDB("rddp")
  349. sql := `SELECT * FROM classify WHERE 1 = 1 `
  350. if condition != `` {
  351. sql += condition
  352. }
  353. order := `sort ASC, create_time ASC`
  354. if orderRule != `` {
  355. order = orderRule
  356. }
  357. sql += ` ORDER BY ` + order
  358. _, err = o.Raw(sql, pars).QueryRows(&items)
  359. return
  360. }
  361. // UpdateChildClassifyRelateSetting 更新子分类关联设置
  362. func UpdateChildClassifyRelateSetting(parentId, relateTel, relateVideo int) (err error) {
  363. o := orm.NewOrmUsingDB("rddp")
  364. sql := `UPDATE classify SET relate_tel = ?, relate_video = ? WHERE parent_id = ?`
  365. _, err = o.Raw(sql, relateTel, relateVideo, parentId).Exec()
  366. return
  367. }
  368. // RelateTelSecClassifyWithPermissions 关联了电话会的二级分类及权限
  369. type RelateTelSecClassifyWithPermissions struct {
  370. Id int `description:"分类ID"`
  371. ClassifyName string `description:"分类名称"`
  372. ChartPermissionIds string `description:"权限IDs"`
  373. }
  374. // UpdateClassifySortByParentId 根据父类id更新排序
  375. func UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
  376. o := orm.NewOrmUsingDB("rddp")
  377. sql := ` update classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  378. if permissionId > 0 {
  379. sql += ` or ( id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  380. }
  381. _, err = o.Raw(sql, parentId, nowSort).Exec()
  382. return
  383. }
  384. // GetMaxSortByParentId 获取最大的排序值
  385. func (classifyInfo *Classify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  386. o := orm.NewOrmUsingDB("rddp")
  387. sql := `SELECT max(sort) AS sort FROM classify WHERE parent_id = ? `
  388. err = o.Raw(sql, parentId).QueryRow(&maxSort)
  389. return
  390. }
  391. // GetMaxSort 获取最大的排序值
  392. func (classifyInfo *Classify) GetMaxSort() (maxSort int, err error) {
  393. o := orm.NewOrmUsingDB("rddp")
  394. sql := `SELECT max(sort) AS sort FROM classify`
  395. err = o.Raw(sql).QueryRow(&maxSort)
  396. return
  397. }
  398. // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  399. func (classifyInfo *Classify) GetFirstClassifyByParentId(parentId int) (item *Classify, err error) {
  400. o := orm.NewOrmUsingDB("rddp")
  401. sql := `SELECT * FROM classify WHERE parent_id = ? order by sort asc, id asc limit 1`
  402. err = o.Raw(sql, parentId).QueryRow(&item)
  403. return
  404. }
  405. type ClassifyMoveReq struct {
  406. ClassifyId int `description:"分类ID"`
  407. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  408. NextClassifyId int `description:"下一个兄弟节点分类id"`
  409. }
  410. type ClassifySetEnabledReq struct {
  411. ClassifyId int `description:"分类ID"`
  412. Enabled int `description:"是否可用,1可用,0禁用"`
  413. }
  414. func (classifyInfo *Classify) SetEnabled(id, enabled int) (err error) {
  415. o := orm.NewOrmUsingDB("rddp")
  416. to, err := o.Begin()
  417. if err != nil {
  418. return
  419. }
  420. defer func() {
  421. if err != nil {
  422. _ = to.Rollback()
  423. } else {
  424. _ = to.Commit()
  425. }
  426. }()
  427. sql := ` UPDATE classify SET enabled =? WHERE id = ?`
  428. _, err = to.Raw(sql, enabled, id).Exec()
  429. if err != nil {
  430. return
  431. }
  432. sql = ` UPDATE classify SET enabled =? WHERE parent_id = ?`
  433. _, err = to.Raw(sql, enabled, id).Exec()
  434. if err != nil {
  435. return
  436. }
  437. return
  438. }