blog_log.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package blog_log
  2. import (
  3. "gfast/plugin/blog/model/blog_classification"
  4. "github.com/gogf/gf/database/gdb"
  5. "github.com/gogf/gf/errors/gerror"
  6. "github.com/gogf/gf/frame/g"
  7. "github.com/gogf/gf/os/gtime"
  8. )
  9. // AddReq 用于存储新增请求的请求参数
  10. type AddReq struct {
  11. LogType int `p:"logType" v:"required#名称不能为空"` // 所属分类
  12. LogSign int `p:"logSign"` // 0.一般 1.置顶,2.幻灯,3.推荐
  13. LogTitle string `p:"logTitle" v:"required#标题不能为空"` // 日志标题
  14. LogAuthor string `p:"logAuthor"` // 作者名
  15. LogUrl string `p:"logUrl"` // 跳转地址
  16. LogThumbnail string `p:"logThumbnail"` // 缩略图
  17. LogStatus uint `p:"logStatus"` // 状态:1发布,0未发布
  18. LogSort int `P:"logSort"` // 排序
  19. LogContent string `p:"logContent" v:"required#内容不能为空"` // 内容
  20. }
  21. // EditReq 用于存储修改请求参数
  22. type EditReq struct {
  23. LogId int64 `p:"logId" v:"required|min:1#日志ID不能为空|日志ID错误"`
  24. AddReq
  25. }
  26. // SelectPageReq 用于存储分页查询的请求参数
  27. type SelectPageReq struct {
  28. LogTitle string `p:"logTitle"` // 日志标题
  29. PageNum int64 `p:"pageNum"` // 当前页
  30. PageSize int64 `p:"pageSize"` // 每页显示记录数
  31. CateTypeId int `p:"cateId"` // 分类类型id
  32. Status int // 状态
  33. }
  34. // 用于存储联合查询的数据
  35. type ListEntity struct {
  36. Entity
  37. ClassificationName string `orm:"classification_name" json:"classification_name" ` // 所属分类名
  38. }
  39. // GetLogByID 根据ID查询记录
  40. func GetLogByID(id int64) (entity *ListEntity, err error) {
  41. entity = new(ListEntity)
  42. model := g.DB().Table(Table + " log")
  43. model = model.Where("log_id = ?", id)
  44. model = model.LeftJoin(blog_classification.Table+" cf", "cf.classification_id = log.log_type")
  45. var res gdb.Record
  46. res, err = model.Fields("log.*,cf.classification_name").FindOne()
  47. if err != nil {
  48. g.Log().Error(err)
  49. return nil, gerror.New("根据ID查询记录出错!")
  50. }
  51. err = res.Struct(entity)
  52. if err != nil {
  53. g.Log().Error(err)
  54. return nil, gerror.New("根据ID查询转换时出错")
  55. }
  56. if entity == nil {
  57. return nil, gerror.New("根据ID未能查询到记录")
  58. }
  59. return entity, nil
  60. }
  61. // AddSave 添加的方法
  62. func AddSave(req *AddReq) error {
  63. var entity Entity
  64. entity.LogType = req.LogType
  65. entity.LogSign = req.LogSign
  66. entity.LogTitle = req.LogTitle
  67. entity.LogAuthor = req.LogAuthor
  68. entity.LogUrl = req.LogUrl
  69. entity.LogThumbnail = req.LogThumbnail
  70. entity.CreatTime = uint(gtime.Timestamp())
  71. entity.LogStatus = req.LogStatus
  72. entity.LogSort = req.LogSort
  73. entity.LogContent = req.LogContent
  74. // 保存实体
  75. _, err := entity.Insert()
  76. if err != nil {
  77. g.Log().Error(err)
  78. return gerror.New("添加记录入库失败!")
  79. }
  80. return nil
  81. }
  82. // 批量删除记录
  83. func DeleteByIDs(ids []int) error {
  84. _, err := Model.Delete("log_id in(?)", ids)
  85. if err != nil {
  86. g.Log().Error(err)
  87. return gerror.New("删除记录失败!")
  88. }
  89. return nil
  90. }
  91. // 根据ID修改记录
  92. func EditSave(req *EditReq) error {
  93. // 先根据ID来查询要修改的记录
  94. entity, err := GetLogByID(req.LogId)
  95. if err != nil {
  96. return err
  97. }
  98. // 修改实体
  99. entity.LogType = req.LogType
  100. entity.LogSign = req.LogSign
  101. entity.LogTitle = req.LogTitle
  102. entity.LogAuthor = req.LogAuthor
  103. entity.LogUrl = req.LogUrl
  104. entity.LogThumbnail = req.LogThumbnail
  105. entity.LogStatus = req.LogStatus
  106. entity.LogSort = req.LogSort
  107. entity.LogContent = req.LogContent
  108. _, err = Model.Filter().Save(entity)
  109. if err != nil {
  110. g.Log().Error(err)
  111. return gerror.New("修改记录失败!")
  112. }
  113. return nil
  114. }
  115. // 分页查询,返回值total总记录数,page当前页
  116. func SelectListByPage(req *SelectPageReq) (total int, page int64, list []*ListEntity, err error) {
  117. model := g.DB().Table(Table + " log")
  118. if req != nil {
  119. if req.LogTitle != "" {
  120. model.Where("log.log_title like ?", "%"+req.LogTitle+"%")
  121. }
  122. if req.Status == 1 {
  123. model.Where("log.log_status = 1")
  124. }
  125. if req.CateTypeId != 0 {
  126. model.Where("log_type = ?", req.CateTypeId)
  127. }
  128. }
  129. model = model.LeftJoin(blog_classification.Table+" cf", "cf.classification_id=log.log_type")
  130. // 查询广告位总记录数(总行数)
  131. total, err = model.Count()
  132. if err != nil {
  133. g.Log().Error(err)
  134. err = gerror.New("获取总记录数失败")
  135. return 0, 0, nil, err
  136. }
  137. if req.PageNum == 0 {
  138. req.PageNum = 1
  139. }
  140. page = req.PageNum
  141. if req.PageSize == 0 {
  142. req.PageSize = 10
  143. }
  144. // 分页排序查询
  145. var res gdb.Result
  146. res, err = model.Fields("log.*,cf.classification_name").
  147. Page(int(page), int(req.PageSize)).Order("log.log_sort asc,log.log_id desc").All()
  148. if err != nil {
  149. g.Log().Error(err)
  150. err = gerror.New("分页查询失败")
  151. return 0, 0, nil, err
  152. }
  153. err = res.Structs(&list)
  154. if err != nil {
  155. g.Log().Error(err)
  156. err = gerror.New("分页查询失败")
  157. return 0, 0, nil, err
  158. }
  159. return total, page, list, nil
  160. }
  161. // 按时间倒序查询size篇标志为sign分类id为typeId,状态为status的文章,标志值0.一般,1.置顶,2.幻灯,3.推荐,typeId等于0时不区分分类
  162. func FindSizeArticleBySign(size int, status int, sign int, typeId int) (list []*ListEntity, err error) {
  163. model := g.DB().Table(Table + " log")
  164. if status == 1 {
  165. model = model.Where("log.log_status = ?", 1)
  166. }
  167. if status == 0 {
  168. model = model.Where("log.log_status = ?", 0)
  169. }
  170. if sign == 0 {
  171. model = model.Where("log.log_sign = 0")
  172. }
  173. if sign == 1 {
  174. model = model.Where("log.log_sign = 1")
  175. }
  176. if sign == 2 {
  177. model = model.Where("log.log_sign = 2")
  178. }
  179. if sign == 3 {
  180. model = model.Where("log.log_sign = 3")
  181. }
  182. if typeId != 0 {
  183. model = model.Where("log_type = ?", typeId)
  184. }
  185. model = model.LeftJoin(blog_classification.Table+" cf", "cf.classification_id=log.log_type")
  186. // 分页排序查询
  187. var res gdb.Result
  188. res, err = model.Fields("log.*,cf.classification_name").
  189. Order("log.log_sort asc,log.creat_time desc").Limit(size).All()
  190. if err != nil {
  191. g.Log().Error(err)
  192. return nil, gerror.New("根据标志查询出错")
  193. }
  194. err = res.Structs(&list)
  195. if err != nil {
  196. g.Log().Error(err)
  197. return nil, gerror.New("根据标志查询出错")
  198. }
  199. return
  200. }
  201. // 按时间倒序查询size篇分类id为typeId,状态为status的文章,typeId等于0时不区分分类
  202. func FindSizeArticle(size int, status int, typeId int) (list []*ListEntity, err error) {
  203. model := g.DB().Table(Table + " log")
  204. if status == 1 {
  205. model = model.Where("log.log_status = ?", 1)
  206. }
  207. if status == 0 {
  208. model = model.Where("log.log_status = ?", 0)
  209. }
  210. if typeId != 0 {
  211. model = model.Where("log_type = ?", typeId)
  212. }
  213. model = model.LeftJoin(blog_classification.Table+" cf", "cf.classification_id=log.log_type")
  214. // 分页排序查询
  215. var res gdb.Result
  216. res, err = model.Fields("log.*,cf.classification_name").Order("log.log_sort asc,log.creat_time desc").Limit(size).All()
  217. if err != nil {
  218. g.Log().Error(err)
  219. return nil, gerror.New("根据标志查询出错")
  220. }
  221. err = res.Structs(&list)
  222. if err != nil {
  223. g.Log().Error(err)
  224. return nil, gerror.New("根据标志查询出错")
  225. }
  226. return
  227. }
  228. // 查询size篇文章并根据点击数排序
  229. func FindArticleByHits(size int, status int) (list []*Entity, err error) {
  230. model := Model
  231. if status == 1 {
  232. model.Where("log_status = 1")
  233. }
  234. list, err = model.Order("log_hits desc").FindAll()
  235. if err != nil {
  236. g.Log().Error(err)
  237. return nil, gerror.New("根据点击数排序查询失败")
  238. }
  239. return
  240. }