mysql.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "strconv"
  6. "go-common/app/admin/main/card/model"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. // AddGroup add group info.
  11. func (d *Dao) AddGroup(c context.Context, arg *model.AddGroup) error {
  12. return d.DB.Table("card_group").Create(arg).Error
  13. }
  14. // UpdateGroup update group info.
  15. func (d *Dao) UpdateGroup(c context.Context, arg *model.UpdateGroup) error {
  16. return d.DB.Table("card_group").Where("id=?", arg.ID).Update(map[string]interface{}{
  17. "name": arg.Name,
  18. "state": arg.State,
  19. "operator": arg.Operator,
  20. }).Error
  21. }
  22. // Cards query cards.
  23. func (d *Dao) Cards(c context.Context) (res []*model.Card, err error) {
  24. err = d.DB.Table("card_info").Order("order_num desc").Where("state = 0 AND deleted = 0").Find(&res).Error
  25. return
  26. }
  27. // CardsByGid query cards by group id.
  28. func (d *Dao) CardsByGid(c context.Context, gid int64) (res []*model.Card, err error) {
  29. err = d.DB.Table("card_info").Order("order_num desc").Where("group_id=?", gid).Where("deleted = 0").Find(&res).Error
  30. return
  31. }
  32. // CardsByIds query cards by ids.
  33. func (d *Dao) CardsByIds(c context.Context, ids []int64) (res []*model.Card, err error) {
  34. err = d.DB.Table("card_info").Order("order_num desc").Where("id in (?)", ids).Where("deleted = 0").Find(&res).Error
  35. return
  36. }
  37. // GroupsByIds query groups by ids.
  38. func (d *Dao) GroupsByIds(c context.Context, ids []int64) (res []*model.CardGroup, err error) {
  39. err = d.DB.Table("card_group").Order("order_num desc").Where("id in (?)", ids).Where("deleted = 0").Find(&res).Error
  40. return
  41. }
  42. // AddCard add card.
  43. func (d *Dao) AddCard(arg *model.AddCard) error {
  44. return d.DB.Table("card_info").Create(arg).Error
  45. }
  46. // CardByName get card by name.
  47. func (d *Dao) CardByName(name string) (res *model.Card, err error) {
  48. res = new(model.Card)
  49. q := d.DB.Table("card_info").Where("name=?", name).First(res)
  50. if q.Error != nil {
  51. if q.RecordNotFound() {
  52. err = nil
  53. res = nil
  54. return
  55. }
  56. err = errors.Wrapf(err, "card by name")
  57. }
  58. return
  59. }
  60. // GroupByName get group by name.
  61. func (d *Dao) GroupByName(name string) (res *model.CardGroup, err error) {
  62. res = new(model.CardGroup)
  63. q := d.DB.Table("card_group").Where("name=?", name).First(res)
  64. if q.Error != nil {
  65. if q.RecordNotFound() {
  66. err = nil
  67. res = nil
  68. return
  69. }
  70. err = errors.Wrapf(err, "card_group by name")
  71. }
  72. return
  73. }
  74. // UpdateCard update card.
  75. func (d *Dao) UpdateCard(req *model.UpdateCard) error {
  76. args := map[string]interface{}{}
  77. args["name"] = req.Name
  78. args["state"] = req.State
  79. args["is_hot"] = req.IsHot
  80. args["operator"] = req.Operator
  81. if req.CardURL != "" {
  82. args["card_url"] = req.CardURL
  83. }
  84. if req.BigCradURL != "" {
  85. args["big_crad_url"] = req.BigCradURL
  86. }
  87. return d.DB.Table("card_info").Where("id=?", req.ID).Update(args).Error
  88. }
  89. // UpdateCardState update card state.
  90. func (d *Dao) UpdateCardState(c context.Context, id int64, state int8) error {
  91. return d.DB.Table("card_info").Where("id=?", id).Update("state", state).Error
  92. }
  93. // DeleteCard delete card.
  94. func (d *Dao) DeleteCard(c context.Context, id int64) error {
  95. return d.DB.Table("card_info").Where("id=?", id).Delete(&model.Card{}).Error
  96. }
  97. // DeleteGroup delete group.
  98. func (d *Dao) DeleteGroup(c context.Context, id int64) error {
  99. return d.DB.Table("card_group").Where("id=?", id).Delete(&model.CardGroup{}).Error
  100. }
  101. // UpdateGroupState update group state.
  102. func (d *Dao) UpdateGroupState(c context.Context, id int64, state int8) error {
  103. return d.DB.Table("card_group").Where("id=?", id).Update("state", state).Error
  104. }
  105. // MaxCardOrder max card order num.
  106. func (d *Dao) MaxCardOrder() (max int64, err error) {
  107. err = d.DB.Table("card_info").Select("MAX(order_num)").Row().Scan(&max)
  108. return
  109. }
  110. // MaxGroupOrder max card group order num.
  111. func (d *Dao) MaxGroupOrder() (max int64, err error) {
  112. err = d.DB.Table("card_group").Select("MAX(order_num)").Row().Scan(&max)
  113. return
  114. }
  115. // BatchUpdateCardOrder update card order.
  116. func (d *Dao) BatchUpdateCardOrder(c context.Context, cs []*model.Card) error {
  117. var (
  118. buf bytes.Buffer
  119. ids []int64
  120. )
  121. buf.WriteString("UPDATE card_info SET order_num = CASE id")
  122. for _, v := range cs {
  123. buf.WriteString(" WHEN ")
  124. buf.WriteString(strconv.FormatInt(v.ID, 10))
  125. buf.WriteString(" THEN ")
  126. buf.WriteString(strconv.FormatInt(v.OrderNum, 10))
  127. ids = append(ids, v.ID)
  128. }
  129. buf.WriteString(" END WHERE id IN (")
  130. buf.WriteString(xstr.JoinInts(ids))
  131. buf.WriteString(");")
  132. return d.DB.Exec(buf.String()).Error
  133. }
  134. // BatchUpdateCardGroupOrder update card order.
  135. func (d *Dao) BatchUpdateCardGroupOrder(c context.Context, cs []*model.CardGroup) error {
  136. var (
  137. buf bytes.Buffer
  138. ids []int64
  139. )
  140. buf.WriteString("UPDATE card_group SET order_num = CASE id")
  141. for _, v := range cs {
  142. buf.WriteString(" WHEN ")
  143. buf.WriteString(strconv.FormatInt(v.ID, 10))
  144. buf.WriteString(" THEN ")
  145. buf.WriteString(strconv.FormatInt(v.OrderNum, 10))
  146. ids = append(ids, v.ID)
  147. }
  148. buf.WriteString(" END WHERE id IN (")
  149. buf.WriteString(xstr.JoinInts(ids))
  150. buf.WriteString(");")
  151. return d.DB.Exec(buf.String()).Error
  152. }
  153. // Groups query groups.
  154. func (d *Dao) Groups(c context.Context, arg *model.ArgQueryGroup) (res []*model.CardGroup, err error) {
  155. q := d.DB.Table("card_group").Where("deleted = 0")
  156. if arg.GroupID > 0 {
  157. q = q.Where("id = ?", arg.GroupID)
  158. }
  159. if arg.State > -1 {
  160. q = q.Where("state = ?", arg.State)
  161. }
  162. if err = q.Order("order_num desc").Find(&res).Error; err != nil {
  163. if q.RecordNotFound() {
  164. err = nil
  165. return
  166. }
  167. err = errors.Wrapf(err, "card group list")
  168. return
  169. }
  170. return
  171. }