doc_cat_repo.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package repositories
  2. import (
  3. "fmt"
  4. "time"
  5. "xorm.io/xorm"
  6. "xps/datamodel"
  7. "xps/viewmodel"
  8. )
  9. type DocCatRepo struct {
  10. engine *xorm.Engine
  11. }
  12. func NewDocCatRepo(engine *xorm.Engine) *DocCatRepo {
  13. return &DocCatRepo{
  14. engine: engine,
  15. }
  16. }
  17. func (d *DocCatRepo) GetPage(m map[string]interface{}) (*viewmodel.PageResult, error) {
  18. limit := m["limit"].(int)
  19. page := m["page"].(int)
  20. sqlSelect := ` SELECT A.* `
  21. sqlFrom := ` FROM doc_cat AS A `
  22. sqlWhere := ` WHERE A.deleted_flag = 0 `
  23. if docCatId, ok := m["docCatId"]; ok {
  24. sqlWhere += fmt.Sprintf(" AND A.doc_cat_id = %d ", docCatId)
  25. }
  26. if keywords, ok := m["keywords"]; ok {
  27. sqlWhere += " AND A.doc_cat_title like '%" + fmt.Sprintf("%s", keywords) + "%' "
  28. }
  29. sqlCount := ` SELECT COUNT(*) `
  30. total, err1 := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodel.DocCat))
  31. if err1 != nil {
  32. return nil, err1
  33. }
  34. sqlOrderBy := " ORDER BY A.oc_id, A.doc_cat_id "
  35. pageStart := (page - 1) * limit
  36. sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
  37. datalist := make([]datamodel.DocCat, 0)
  38. err2 := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
  39. if err2 != nil {
  40. return nil, err2
  41. }
  42. pageResult := &viewmodel.PageResult{}
  43. pageResult.Data = datalist
  44. pageResult.Total = total
  45. pageResult.PageSize = limit
  46. pageResult.Page = page
  47. return pageResult, nil
  48. }
  49. func (d *DocCatRepo) GetList(m map[string]interface{}) ([]viewmodel.DocCatInfo, error) {
  50. sqlSelect := ` SELECT A.* `
  51. sqlFrom := ` FROM doc_cat AS A `
  52. sqlWhere := ` WHERE A.deleted_flag = 0 `
  53. if ocId, ok := m["ocId"]; ok {
  54. sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
  55. }
  56. if docCatId, ok := m["docCatId"]; ok {
  57. sqlWhere += fmt.Sprintf(" AND A.doc_cat_id = %d ", docCatId)
  58. }
  59. if keywords, ok := m["keywords"]; ok {
  60. sqlWhere += ` AND A.doc_cat_title like ` + "'%" + fmt.Sprintf("%s", keywords) + "%'"
  61. }
  62. sqlOrderBy := " ORDER BY A.oc_id, A.doc_cat_id "
  63. datalist := make([]viewmodel.DocCatInfo, 0)
  64. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
  65. if err != nil {
  66. return nil, err
  67. } else {
  68. return datalist, nil
  69. }
  70. }
  71. func (d *DocCatRepo) GetById(ocId, docCatId int64) (*viewmodel.DocCatInfo, error) {
  72. sqlSelect := ` SELECT A.* `
  73. sqlFrom := ` FROM doc_cat AS A `
  74. sqlWhere := ` WHERE A.deleted_flag = 0 `
  75. sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
  76. sqlWhere += fmt.Sprintf(" AND A.doc_cat_id = %d ", docCatId)
  77. data := &viewmodel.DocCatInfo{}
  78. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
  79. if !has || err != nil {
  80. return nil, err
  81. } else {
  82. return data, nil
  83. }
  84. }
  85. func (d *DocCatRepo) GetListById(ocId int64) ([]viewmodel.DocCatInfo, error) {
  86. sqlSelect := ` SELECT A.* `
  87. sqlFrom := ` FROM doc_cat AS A `
  88. sqlWhere := ` WHERE A.deleted_flag = 0 `
  89. sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
  90. sqlOrderBy := " ORDER BY A.oc_id, A.doc_cat_id "
  91. datalist := make([]viewmodel.DocCatInfo, 0)
  92. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
  93. if err != nil {
  94. return nil, err
  95. } else {
  96. return datalist, nil
  97. }
  98. }
  99. func (d *DocCatRepo) Create(data *datamodel.DocCat) error {
  100. data.CreatedAt = time.Now()
  101. _, err := d.engine.Insert(data)
  102. return err
  103. }
  104. func (d *DocCatRepo) Update(data *datamodel.DocCat) error {
  105. ocId := data.OcId
  106. docCatId := data.DocCatId
  107. data.UpdatedAt = time.Now()
  108. _, err := d.engine.
  109. Where("oc_id = ?", ocId).
  110. Where("doc_cat_id = ?", docCatId).
  111. Update(data)
  112. return err
  113. }
  114. func (d *DocCatRepo) Delete(m map[string]interface{}) error {
  115. ocId := m["ocId"].(int64)
  116. docCatId := m["docCatId"].(int64)
  117. deletedBy := m["deletedBy"].(int64)
  118. data := &datamodel.DocCat{}
  119. data.DeletedFlag = 1
  120. data.DeletedBy = deletedBy
  121. data.DeletedAt = time.Now()
  122. _, err := d.engine.
  123. Where("oc_id = ?", ocId).
  124. Where("doc_cat_id = ?", docCatId).
  125. Update(data)
  126. return err
  127. }