doc_article_dao.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package dao
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "reflect"
  5. "strings"
  6. "ulink-admin/modules/doc/models/model"
  7. "ulink-admin/modules/doc/models/req"
  8. "ulink-admin/pkg/base"
  9. "ulink-admin/pkg/page"
  10. "ulink-admin/utils"
  11. "xorm.io/xorm"
  12. )
  13. type DocArticleDao struct {
  14. }
  15. func (d DocArticleDao) selectSql(session *xorm.Session) *xorm.Session {
  16. return session.Table([]string{"doc_article", "o"})
  17. }
  18. // SelectAll 查询所有文章数据,数据库操作
  19. func (d DocArticleDao) SelectAll() []*model.DocArticle {
  20. session := base.SqlDB.NewSession()
  21. docArticle := make([]*model.DocArticle, 0)
  22. err := session.Find(&docArticle)
  23. if err != nil {
  24. gotool.Logs.ErrorLog().Println(err)
  25. return nil
  26. }
  27. return docArticle
  28. }
  29. // Find 查询文章分页数据
  30. func (d DocArticleDao) Find(query req.DocArticleQuery) (*[]model.DocArticle, int64) {
  31. docArticle := make([]model.DocArticle, 0)
  32. session := base.SqlDB.NewSession().Table(model.DocArticle{}.TableName())
  33. if query.Id > 0 {
  34. session.And("id = ?", query.Id)
  35. }
  36. if !gotool.StrUtils.HasEmpty(query.Label) {
  37. session.And("label = ?", query.Label)
  38. }
  39. if query.CatId > 0 {
  40. session.And("cat_id = ?", query.CatId)
  41. }
  42. if !gotool.StrUtils.HasEmpty(query.Profile) {
  43. session.And("profile = ?", query.Profile)
  44. }
  45. if !gotool.StrUtils.HasEmpty(query.Url) {
  46. session.And("url = ?", query.Url)
  47. }
  48. if query.IsTop > 0 {
  49. session.And("is_top = ?", query.IsTop)
  50. }
  51. if query.IsHot > 0 {
  52. session.And("is_hot = ?", query.IsHot)
  53. }
  54. if !gotool.StrUtils.HasEmpty(query.Content) {
  55. session.And("content = ?", query.Content)
  56. }
  57. if query.Sort > 0 {
  58. session.And("sort = ?", query.Sort)
  59. }
  60. if !gotool.StrUtils.HasEmpty(query.CreateBy) {
  61. session.And("create_by = ?", query.CreateBy)
  62. }
  63. if !gotool.StrUtils.HasEmpty(query.UpdateBy) {
  64. session.And("update_by = ?", query.UpdateBy)
  65. }
  66. if !gotool.StrUtils.HasEmpty(query.BeginTime) {
  67. session.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
  68. }
  69. if !gotool.StrUtils.HasEmpty(query.EndTime) {
  70. session.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
  71. }
  72. total, err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).FindAndCount(&docArticle)
  73. if err != nil {
  74. gotool.Logs.ErrorLog().Println(err)
  75. return nil, 0
  76. }
  77. return &docArticle, total
  78. }
  79. // Insert 添加文章数据
  80. func (d DocArticleDao) Insert(docArticle *model.DocArticle) int64 {
  81. session := base.SqlDB.NewSession()
  82. session.Begin()
  83. insert, err := session.Insert(docArticle)
  84. if err != nil {
  85. session.Rollback()
  86. gotool.Logs.ErrorLog().Println(err)
  87. return 0
  88. }
  89. session.Commit()
  90. return insert
  91. }
  92. // GetDocArticleById 根据id查询文章数据
  93. func (d DocArticleDao) GetDocArticleById(docArticle model.DocArticle) *model.DocArticle {
  94. _, err := base.SqlDB.NewSession().Where("id= ?", docArticle.Id).Get(&docArticle)
  95. if err != nil {
  96. gotool.Logs.ErrorLog().Println(err)
  97. return nil
  98. }
  99. return &docArticle
  100. }
  101. // Delete 批量删除文章
  102. func (d DocArticleDao) Delete(docArticle []int64) int64 {
  103. session := base.SqlDB.NewSession()
  104. session.Begin()
  105. i, err := session.In("id", docArticle).Delete(&model.DocArticle{})
  106. if err != nil {
  107. session.Rollback()
  108. gotool.Logs.ErrorLog().Println(err)
  109. return 0
  110. }
  111. session.Commit()
  112. return i
  113. }
  114. // Update 修改文章数据
  115. func (d DocArticleDao) Update(docArticle *model.DocArticle) bool {
  116. session := base.SqlDB.NewSession()
  117. session.Begin()
  118. _, err := session.Where("id= ?", docArticle.Id).Update(docArticle)
  119. if err != nil {
  120. session.Rollback()
  121. gotool.Logs.ErrorLog().Println(err)
  122. return false
  123. }
  124. session.Commit()
  125. return true
  126. }
  127. // CheckUnique 唯一性检查
  128. func (d DocArticleDao) CheckUnique(docArticle model.DocArticle, condition []string) int64 {
  129. session := base.SqlDB.Table(docArticle.TableName())
  130. if docArticle.Id > 0 {
  131. session.And("id != ?", docArticle.Id)
  132. }
  133. for _, item := range condition {
  134. fieldValue := reflect.ValueOf(docArticle).FieldByName(utils.Ucfirst(item))
  135. if strings.Contains(fieldValue.Type().String(), "int") {
  136. session.And(utils.Camel2Case(item)+" = ?", fieldValue.Int())
  137. } else if strings.Contains(fieldValue.Type().String(), "string") {
  138. session.And(utils.Camel2Case(item)+" = ?", fieldValue.String())
  139. } else {
  140. session.And(utils.Camel2Case(item)+" = ?", fieldValue.String())
  141. }
  142. }
  143. count, err := session.Count()
  144. if err != nil {
  145. gotool.Logs.ErrorLog().Println(err)
  146. }
  147. return count
  148. }
  149. func (d DocArticleDao) FindCountByCatId(id int64) int64 {
  150. session := base.SqlDB.Table(model.DocArticle{}.TableName())
  151. session.And("cat_id = ?", id)
  152. count, err := session.Count()
  153. if err != nil {
  154. gotool.Logs.ErrorLog().Println(err)
  155. }
  156. return count
  157. }