ProcInstHistory.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package dao
  2. import (
  3. "server/global"
  4. "strings"
  5. "sync"
  6. "github.com/jinzhu/gorm"
  7. )
  8. // ProcInstHistory ProcInstHistory
  9. type ProcInstHistory struct {
  10. ProcInst
  11. }
  12. // StartHistoryByMyself 查询我发起的流程
  13. func StartHistoryByMyself(userID, company string, pageIndex, pageSize int) ([]*ProcInstHistory, int64, error) {
  14. maps := map[string]interface{}{
  15. "start_user_id": userID,
  16. "company": company,
  17. }
  18. return findProcInstsHistory(maps, pageIndex, pageSize)
  19. }
  20. func findProcInstsHistory(maps map[string]interface{}, pageIndex, pageSize int) ([]*ProcInstHistory, int64, error) {
  21. var datas []*ProcInstHistory
  22. var count int64
  23. selectDatas := func(in chan<- error, wg *sync.WaitGroup) {
  24. go func() {
  25. err := global.GVA_DB.Where(maps).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Order("start_time desc").Find(&datas).Error
  26. in <- err
  27. wg.Done()
  28. }()
  29. }
  30. selectCount := func(in chan<- error, wg *sync.WaitGroup) {
  31. err := global.GVA_DB.Model(&ProcInstHistory{}).Where(maps).Count(&count).Error
  32. in <- err
  33. wg.Done()
  34. }
  35. var err1 error
  36. var wg sync.WaitGroup
  37. numberOfRoutine := 2
  38. wg.Add(numberOfRoutine)
  39. errStream := make(chan error, numberOfRoutine)
  40. // defer fmt.Println("close channel")
  41. selectDatas(errStream, &wg)
  42. selectCount(errStream, &wg)
  43. wg.Wait()
  44. defer close(errStream) // 关闭通道
  45. for i := 0; i < numberOfRoutine; i++ {
  46. // log.Printf("send: %v", <-errStream)
  47. if err := <-errStream; err != nil {
  48. err1 = err
  49. }
  50. }
  51. // fmt.Println("结束")
  52. return datas, count, err1
  53. }
  54. // FindProcHistory 查询历史纪录
  55. func FindProcHistory(userID, company string, pageIndex, pageSize int) ([]*ProcInstHistory, int64, error) {
  56. var datas []*ProcInstHistory
  57. var count int64
  58. var err1 error
  59. var wg sync.WaitGroup
  60. numberOfRoutine := 2
  61. errStream := make(chan error, numberOfRoutine)
  62. selectDatas := func(wg *sync.WaitGroup) {
  63. go func() {
  64. err := global.GVA_DB.Where("id in (select distinct proc_inst_id from identitylink_history where company=? and user_id=?)", company, userID).
  65. Offset((pageIndex - 1) * pageSize).Limit(pageSize).
  66. Order("start_time desc").Find(&datas).Error
  67. errStream <- err
  68. wg.Done()
  69. }()
  70. }
  71. selectCount := func(wg *sync.WaitGroup) {
  72. go func() {
  73. err := global.GVA_DB.Model(&ProcInstHistory{}).
  74. Where("id in (select distinct proc_inst_id from identitylink_history where company=? and user_id=?)", company, userID).
  75. Count(&count).Error
  76. errStream <- err
  77. wg.Done()
  78. }()
  79. }
  80. wg.Add(numberOfRoutine)
  81. selectDatas(&wg)
  82. selectCount(&wg)
  83. wg.Wait()
  84. close(errStream)
  85. for i := 0; i < numberOfRoutine; i++ {
  86. if err := <-errStream; err != nil {
  87. err1 = err
  88. }
  89. }
  90. return datas, count, err1
  91. }
  92. // SaveProcInstHistory SaveProcInstHistory
  93. func SaveProcInstHistory(p *ProcInst) error {
  94. return global.GVA_DB.Table("proc_inst_history").Create(p).Error
  95. }
  96. // DelProcInstHistoryByID DelProcInstHistoryByID
  97. func DelProcInstHistoryByID(id int) error {
  98. return global.GVA_DB.Where("id=?", id).Delete(&ProcInstHistory{}).Error
  99. }
  100. // SaveProcInstHistoryTx SaveProcInstHistoryTx
  101. func SaveProcInstHistoryTx(p *ProcInst, tx *gorm.DB) error {
  102. return tx.Table("proc_inst_history").Create(p).Error
  103. }
  104. // FindProcHistoryNotify 查询抄送我的历史纪录
  105. func FindProcHistoryNotify(userID, company string, groups []string, pageIndex, pageSize int) ([]*ProcInstHistory, int64, error) {
  106. var datas []*ProcInstHistory
  107. var count int64
  108. var sql string
  109. if len(groups) != 0 {
  110. var s []string
  111. for _, val := range groups {
  112. s = append(s, "\""+val+"\"")
  113. }
  114. sql = "select proc_inst_id from identitylink_history i where i.type='notifier' and i.company='" + company + "' and (i.user_id='" + userID + "' or i.group in (" + strings.Join(s, ",") + "))"
  115. } else {
  116. sql = "select proc_inst_id from identitylink_history i where i.type='notifier' and i.company='" + company + "' and i.user_id='" + userID + "'"
  117. }
  118. err := global.GVA_DB.Where("id in (" + sql + ")").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Order("start_time desc").Find(&datas).Error
  119. if err != nil {
  120. return datas, count, err
  121. }
  122. err = global.GVA_DB.Model(&ProcInstHistory{}).Where("id in (" + sql + ")").Count(&count).Error
  123. if err != nil {
  124. return nil, count, err
  125. }
  126. return datas, count, err
  127. }