classify.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "time"
  6. )
  7. type Classify struct {
  8. Id int `orm:"column(id);pk"`
  9. ClassifyName string `description:"分类名称"`
  10. Sort int `json:"-"`
  11. ParentId int `description:"父级分类id"`
  12. CreateTime time.Time `description:"创建时间"`
  13. ModifyTime time.Time `description:"修改时间"`
  14. Abstract string `description:"栏目简介"`
  15. Descript string `description:"分享描述"`
  16. ReportAuthor string `description:"栏目作者"`
  17. AuthorDescript string `description:"作者简介"`
  18. ColumnImgUrl string `description:"栏目配图"`
  19. HeadImgUrl string `description:"头部banner"`
  20. AvatarImgUrl string `description:"头像"`
  21. ReportImgUrl string `description:"报告配图"`
  22. HomeImgUrl string `description:"首页配图"`
  23. ClassifyLabel string `description:"分类标签"`
  24. IsMassSend int `description:"1:群发,0:非群发"`
  25. }
  26. type ClassifyAddReq struct {
  27. ClassifyName string `description:"分类名称"`
  28. ParentId int `description:"父级分类id,没有父级分类传0"`
  29. Abstract string `description:"栏目简介"`
  30. Descript string `description:"分享描述"`
  31. ReportAuthor string `description:"栏目作者"`
  32. AuthorDescript string `description:"作者简介"`
  33. ColumnImgUrl string `description:"栏目配图"`
  34. ReportImgUrl string `description:"报告配图"`
  35. HeadImgUrl string `description:"头部banner"`
  36. AvatarImgUrl string `description:"头像"`
  37. HomeImgUrl string `description:"首页配图"`
  38. ClassifyLabel string `description:"分类标签"`
  39. }
  40. func GetClassifyByName(classifyName string, parentId int) (item *Classify, err error) {
  41. sql := `SELECT * FROM classify WHERE classify_name=? AND parent_id=? `
  42. o := orm.NewOrmUsingDB("rddp")
  43. err = o.Raw(sql, classifyName, parentId).QueryRow(&item)
  44. return
  45. }
  46. func GetClassifyById(classifyId int) (item *Classify, err error) {
  47. sql := `SELECT * FROM classify WHERE id=? `
  48. o := orm.NewOrmUsingDB("rddp")
  49. err = o.Raw(sql, classifyId).QueryRow(&item)
  50. return
  51. }
  52. // 添加分类
  53. func AddClassify(item *Classify) (err error) {
  54. o := orm.NewOrmUsingDB("rddp")
  55. _, err = o.Insert(item)
  56. return
  57. }
  58. func GetReportCountByClassifyId(classifyId int) (count int, err error) {
  59. o := orm.NewOrmUsingDB("rddp")
  60. sql := `SELECT COUNT(1) AS count FROM report WHERE classify_id_second=? `
  61. err = o.Raw(sql, classifyId).QueryRow(&count)
  62. return
  63. }
  64. func GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  65. o := orm.NewOrmUsingDB("rddp")
  66. sql := `SELECT COUNT(1) as num FROM classify AS a
  67. INNER JOIN report AS b ON a.id=b.classify_id_second
  68. WHERE a.parent_id=? `
  69. err = o.Raw(sql, classifyId).QueryRow(&count)
  70. return
  71. }
  72. func GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  73. sqlCount := `
  74. SELECT COUNT(1) as num FROM classify AS a
  75. WHERE a.parent_id=? `
  76. o := orm.NewOrmUsingDB("rddp")
  77. err = o.Raw(sqlCount, classifyId).QueryRow(&count)
  78. return
  79. }
  80. // 删除分类
  81. func DeleteClassify(classifyId int) (err error) {
  82. sql := `DELETE FROM classify WHERE id=? `
  83. o := orm.NewOrmUsingDB("rddp")
  84. _, err = o.Raw(sql, classifyId).Exec()
  85. if err != nil {
  86. return
  87. }
  88. deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  89. _, err = o.Raw(deleteImgSql, classifyId).Exec()
  90. return
  91. }
  92. // classifyName, abstract, descript string, parentId, classifyId int
  93. // 修改分类
  94. func EditClassify(req *EditClassifyReq) (err error) {
  95. o := orm.NewOrmUsingDB("rddp")
  96. 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=?, modify_time= NOW() WHERE id = ? `
  97. _, 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.ClassifyId).Exec()
  98. return
  99. }
  100. //获取父级分类
  101. func ParentClassify() (items []*Classify, err error) {
  102. sql := `SELECT * FROM classify WHERE parent_id=0 order by id desc `
  103. o := orm.NewOrmUsingDB("rddp")
  104. _, err = o.Raw(sql).QueryRows(&items)
  105. return
  106. }
  107. // 根据id获取分类详情
  108. func FindByIdClassify(classifyId int) (item *Classify, err error) {
  109. sql := `SELECT * FROM classify WHERE id=? `
  110. o := orm.NewOrmUsingDB("rddp")
  111. err = o.Raw(sql, classifyId).QueryRow(&item)
  112. return
  113. }
  114. type ClassifyList struct {
  115. Id int `orm:"column(id);pk"`
  116. ClassifyName string `description:"分类名称"`
  117. Sort int `json:"-"`
  118. ParentId int `description:"父级分类id"`
  119. CreateTime time.Time `description:"创建时间"`
  120. ModifyTime time.Time `description:"修改时间"`
  121. Abstract string `description:"简介"`
  122. Descript string `description:"描述"`
  123. ClassifyLabel string `description:"分类标签"`
  124. Child []*Classify
  125. }
  126. type ClassifyListResp struct {
  127. List []*ClassifyList
  128. Paging *paging.PagingItem `description:"分页数据"`
  129. }
  130. // 获取分类列表
  131. func GetClassifyList(startSize, pageSize int, keyWord, companyType string) (items []*ClassifyList, err error) {
  132. sql := ``
  133. companyTypeSqlStr := ``
  134. if companyType == "ficc" {
  135. companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  136. } else if companyType == "权益" {
  137. companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  138. }
  139. if keyWord != "" {
  140. sql = `SELECT * FROM (
  141. SELECT * FROM classify
  142. WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%'
  143. UNION
  144. SELECT * FROM classify
  145. WHERE id IN(SELECT parent_id FROM classify
  146. WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%')
  147. )AS t
  148. ORDER BY create_time ASC
  149. LIMIT ?,? `
  150. } else {
  151. sql = `SELECT * FROM classify WHERE parent_id=0 ` + companyTypeSqlStr + ` ORDER BY create_time ASC LIMIT ?,? `
  152. }
  153. o := orm.NewOrmUsingDB("rddp")
  154. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
  155. return
  156. }
  157. func GetClassifyListCount(keyWord, companyType string) (count int, err error) {
  158. sqlCount := ``
  159. companyTypeSqlStr := ``
  160. if companyType == "ficc" {
  161. companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  162. } else if companyType == "权益" {
  163. companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  164. }
  165. if keyWord != "" {
  166. sqlCount = `SELECT COUNT(1) AS count FROM (
  167. SELECT * FROM classify
  168. WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%'
  169. UNION
  170. SELECT * FROM classify
  171. WHERE id IN(SELECT parent_id FROM classify
  172. WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%')
  173. )AS t `
  174. } else {
  175. sqlCount = `SELECT COUNT(1) AS count FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  176. }
  177. o := orm.NewOrmUsingDB("rddp")
  178. err = o.Raw(sqlCount).QueryRow(&count)
  179. return
  180. }
  181. type CheckDeleteClassifyReq struct {
  182. ClassifyId int `description:"分类ID"`
  183. }
  184. type CheckDeleteClassifyResp struct {
  185. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  186. Msg string `description:"描述信息"`
  187. }
  188. type DeleteClassifyReq struct {
  189. ClassifyId int `description:"分类ID"`
  190. }
  191. type EditClassifyReq struct {
  192. ClassifyId int `description:"分类ID"`
  193. ClassifyName string `description:"分类名称"`
  194. ParentId int `description:"父级分类id"`
  195. Abstract string `description:"栏目简介"`
  196. Descript string `description:"分享描述"`
  197. ReportAuthor string `description:"栏目作者"`
  198. AuthorDescript string `description:"作者简介"`
  199. ColumnImgUrl string `description:"栏目配图"`
  200. HeadImgUrl string `description:"头部banner"`
  201. AvatarImgUrl string `description:"头像"`
  202. ReportImgUrl string `description:"报告配图"`
  203. HomeImgUrl string `description:"首页配图"`
  204. ClassifyLabel string `description:"分类标签"`
  205. }
  206. type FindByIdClassifyReq struct {
  207. ClassifyId int `description:"分类ID"`
  208. }
  209. func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  210. o := orm.NewOrmUsingDB("rddp")
  211. sql := ``
  212. if keyWord != "" {
  213. sql = `SELECT * FROM classify WHERE parent_id=? AND classify_name LIKE '%` + keyWord + `%' ORDER BY create_time ASC `
  214. } else {
  215. sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  216. }
  217. _, err = o.Raw(sql, parentId).QueryRows(&items)
  218. return
  219. }