classify.go 8.7 KB

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