sys_attachment_dao.go 4.4 KB

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