group.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/workflow/model"
  5. "go-common/app/admin/main/workflow/model/param"
  6. "github.com/jinzhu/gorm"
  7. "github.com/pkg/errors"
  8. )
  9. // GroupByID will select a group by group id
  10. func (d *Dao) GroupByID(c context.Context, gid int64) (g *model.Group, err error) {
  11. g = &model.Group{}
  12. if db := d.ReadORM.Table("workflow_group").Where("id=?", gid).First(g); db.Error != nil {
  13. err = db.Error
  14. if db.RecordNotFound() {
  15. g = nil
  16. err = nil
  17. } else {
  18. err = errors.Wrapf(err, "group(%d)", gid)
  19. }
  20. }
  21. return
  22. }
  23. // GroupByOid will select a group by oid and business
  24. func (d *Dao) GroupByOid(c context.Context, oid int64, business int8) (g *model.Group, err error) {
  25. g = &model.Group{}
  26. err = d.ReadORM.Table("workflow_group").Where("oid=? AND business=?", oid, business).Find(g).Error
  27. return
  28. }
  29. // TxGroupsByOidsStates will select a set of groups by oids, business and states
  30. func (d *Dao) TxGroupsByOidsStates(tx *gorm.DB, oids []int64, business, state int8) (groups map[int64]*model.Group, err error) {
  31. var groupSlice []*model.Group
  32. groups = make(map[int64]*model.Group, len(oids))
  33. if len(oids) <= 0 {
  34. return
  35. }
  36. if err = tx.Table("workflow_group").Where("oid IN (?) AND business=?", oids, business).Find(&groupSlice).Error; err != nil {
  37. return
  38. }
  39. for _, g := range groupSlice {
  40. groups[g.ID] = g
  41. }
  42. return
  43. }
  44. // Groups will select a set of groups by group ids
  45. func (d *Dao) Groups(c context.Context, gids []int64) (groups map[int64]*model.Group, err error) {
  46. var groupSlice []*model.Group
  47. groups = make(map[int64]*model.Group, len(gids))
  48. if len(gids) <= 0 {
  49. return
  50. }
  51. if err = d.ReadORM.Table("workflow_group").Where("id IN (?)", gids).Find(&groupSlice).Error; err != nil {
  52. return
  53. }
  54. for _, g := range groupSlice {
  55. groups[g.ID] = g
  56. }
  57. return
  58. }
  59. // TxGroups .
  60. func (d *Dao) TxGroups(tx *gorm.DB, gids []int64) (groups map[int64]*model.Group, err error) {
  61. var groupSlice []*model.Group
  62. groups = make(map[int64]*model.Group, len(gids))
  63. if len(gids) <= 0 {
  64. return
  65. }
  66. if err = tx.Set("gorm:query_option", "FOR UPDATE").Table("workflow_group").Where("id IN (?)", gids).Find(&groupSlice).Error; err != nil {
  67. return
  68. }
  69. for _, g := range groupSlice {
  70. groups[g.ID] = g
  71. }
  72. return
  73. }
  74. // TxUpGroup will update a group
  75. func (d *Dao) TxUpGroup(tx *gorm.DB, oid int64, business int8, tid int64, note string, rid int8) (err error) {
  76. err = tx.Table("workflow_group").Where("oid=? AND business=?", oid, business).UpdateColumn(map[string]interface{}{
  77. "tid": tid,
  78. "note": note,
  79. "rid": rid,
  80. }).Error
  81. return
  82. }
  83. // UpGroupRole update tid note rid of groups
  84. func (d *Dao) UpGroupRole(c context.Context, grsp *param.GroupRoleSetParam) (err error) {
  85. err = d.ORM.Table("workflow_group").Where("id IN (?)", grsp.GID).UpdateColumn(map[string]interface{}{
  86. "tid": grsp.TID,
  87. "note": grsp.Note,
  88. "rid": grsp.RID,
  89. }).Error
  90. return
  91. }
  92. // TxUpGroupState will update a group state
  93. func (d *Dao) TxUpGroupState(tx *gorm.DB, gid int64, state int8) (err error) {
  94. err = tx.Table("workflow_group").Where("id=? AND state=0", gid).Update("state", state).Error
  95. return
  96. }
  97. // TxUpGroupHandling will use gorm update a group handling stat field
  98. func (d *Dao) TxUpGroupHandling(tx *gorm.DB, gid int64, handling int32) (err error) {
  99. err = tx.Table("workflow_group").Where("id=?", gid).Update("handling", handling).Error
  100. return
  101. }
  102. // TxBatchUpGroupHandling will update a set of groups handling stat field
  103. func (d *Dao) TxBatchUpGroupHandling(tx *gorm.DB, gids []int64, handling int32) (err error) {
  104. if len(gids) <= 0 {
  105. return
  106. }
  107. return tx.Table("workflow_group").Where("id IN (?)", gids).Update("handling", handling).Error
  108. }
  109. // TxBatchUpGroupState will update a group state
  110. func (d *Dao) TxBatchUpGroupState(tx *gorm.DB, gids []int64, state int8) (err error) {
  111. if len(gids) <= 0 {
  112. return
  113. }
  114. return tx.Table("workflow_group").Where("id IN (?) AND state=0", gids).Update("state", state).Error
  115. }
  116. // TxSetGroupStateTid set group state,tid,rid
  117. func (d *Dao) TxSetGroupStateTid(tx *gorm.DB, gids []int64, state, rid int8, tid int64) (err error) {
  118. if len(gids) <= 0 {
  119. return
  120. }
  121. return tx.Table("workflow_group").Where("id IN (?)", gids).UpdateColumn(map[string]interface{}{
  122. "state": state,
  123. "tid": tid,
  124. "rid": rid,
  125. }).Error
  126. }
  127. // TxSimpleSetGroupState only set group state
  128. func (d *Dao) TxSimpleSetGroupState(tx *gorm.DB, gids []int64, state int8) (err error) {
  129. if len(gids) <= 0 {
  130. return
  131. }
  132. return tx.Table("workflow_group").Where("id IN (?)", gids).Update("state", state).Error
  133. }