classify.go 8.7 KB

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