approver_dao.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * @description: 审批流程数据库操作相关
  3. * @Author: LanJianRong
  4. * @Date: 2021-01-06
  5. * @FilePath: \construction_management\dao\approver_dao.go
  6. */
  7. package dao
  8. import (
  9. "fmt"
  10. "github.com/go-xorm/xorm"
  11. "go.mod/comm"
  12. "go.mod/conf"
  13. "go.mod/models"
  14. "go.mod/web/viewmodels"
  15. )
  16. // 数据库操作引擎
  17. type ApproverDao struct {
  18. engine *xorm.Engine
  19. }
  20. // 获得一个DAO对象
  21. func NewApproverDao(engine *xorm.Engine) *ApproverDao {
  22. return &ApproverDao{
  23. engine: engine,
  24. }
  25. }
  26. // 批量插入数据
  27. func (d *ApproverDao) InsertData(times int, bid int, pid int, dataType int, dataId int, auditors []int, reAuditors []int) error {
  28. data := make([]models.CmApprover, 0)
  29. for i, item := range auditors {
  30. approverVM := models.CmApprover{}
  31. approverVM.AuditId = item
  32. approverVM.AuditOrder = i + 1
  33. approverVM.Progress = 0
  34. approverVM.BidsectionId = bid
  35. approverVM.ProjectId = pid
  36. approverVM.DataType = dataType
  37. approverVM.DataId = dataId
  38. approverVM.Status = 0
  39. approverVM.Times = times
  40. if i == 0 {
  41. approverVM.Status = 1
  42. }
  43. data = append(data, approverVM)
  44. }
  45. checkVM := models.CmApprover{}
  46. checkVM.AuditOrder = len(auditors) + 1
  47. checkVM.Progress = 1
  48. checkVM.BidsectionId = bid
  49. checkVM.ProjectId = pid
  50. checkVM.DataType = dataType
  51. checkVM.DataId = dataId
  52. checkVM.Times = times
  53. data = append(data, checkVM)
  54. for i, item := range reAuditors {
  55. approverVM := models.CmApprover{}
  56. approverVM.AuditId = item
  57. approverVM.AuditOrder = i + 2 + len(auditors)
  58. approverVM.Progress = 2
  59. approverVM.BidsectionId = bid
  60. approverVM.ProjectId = pid
  61. approverVM.DataType = dataType
  62. approverVM.DataId = dataId
  63. approverVM.Times = times
  64. data = append(data, approverVM)
  65. }
  66. _, err := d.engine.Insert(data)
  67. return err
  68. }
  69. // 更改状态
  70. func (d *ApproverDao) ChangeStatus(id int, status int) error {
  71. data := &models.CmApprover{Status: status}
  72. _, err := d.engine.Where("id = ?", id).Cols("status").Update(data)
  73. return err
  74. }
  75. // 更改审批人
  76. func (d *ApproverDao) ChangeAuditId(bid int, dataType int, dataId int, auditOrder int, uid int) error {
  77. data := &models.CmApprover{AuditId: uid}
  78. _, err := d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", bid, dataType, dataId, auditOrder).Cols("audit_id").Update(data)
  79. return err
  80. }
  81. // 更改下一个审批人的状态
  82. func (d *ApproverDao) ChangeNextStatus(id int, status int) error {
  83. data := &models.CmApprover{Id: id}
  84. _, err := d.engine.Get(data)
  85. _, err = d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", data.BidsectionId, data.DataType, data.DataId, data.AuditOrder+1).Cols("status").Update(data)
  86. return err
  87. }
  88. // 根据当前times,获取审批流程(包括原报)
  89. func (d *ApproverDao) GetAuditorsWithOwner(bid int, dataType int, dataId int, times int, cur_uid int) []viewmodels.Auditors {
  90. auditors := make([]viewmodels.Auditors, 0)
  91. auditor := viewmodels.Auditors{Progress: "", Id: ""}
  92. _, err := d.engine.Sql("select name, company, position, mobile, id as `audit_id` from `cm_project_account` where id = ?", cur_uid).Get(&auditor)
  93. if err != nil {
  94. fmt.Println(err)
  95. }
  96. auditors = append(auditors, auditor)
  97. d.engine.Sql("select pa.`company`, pa.`name`, pa.`account_group`,pa.`mobile`, pa.`position`,ca.`audit_id` as `audit_id`, ca.`id`, ca.`status`, ca.`audit_order`, ca.`progress` from `cm_project_account` as pa, `cm_approver` as ca where ca.`bidsection_id` = ? and ca.`data_type` = ? and ca.`data_id` = ? and ca.`times` = ? and ca.audit_id = pa.id order by `audit_order`", bid, dataType, dataId, times).Find(&auditors)
  98. // 原报
  99. return auditors
  100. }
  101. func (d *ApproverDao) FindApproverById(id int) (*models.CmApprover, error) {
  102. data := &models.CmApprover{}
  103. _, err := d.engine.ID(id).Get(data)
  104. return data, err
  105. }
  106. // 获取最新的审核人
  107. func (d *ApproverDao) GetLastedAuditor(bid int, dataType int, dataId int) *viewmodels.Approver {
  108. data := &viewmodels.Approver{BidsectionId: string(bid), DataType: dataType, DataId: string(dataId), Status: 1}
  109. has, _ := d.engine.Get(data)
  110. if has == true {
  111. id, _ := comm.AesEncrypt(data.Id, conf.SignSecret)
  112. data.Id = id
  113. pid, _ := comm.AesEncrypt(data.ProjectId, conf.SignSecret)
  114. data.Id = pid
  115. bid, _ := comm.AesEncrypt(data.BidsectionId, conf.SignSecret)
  116. data.Id = bid
  117. dataId, _ := comm.AesEncrypt(data.DataId, conf.SignSecret)
  118. data.DataId = dataId
  119. auditId, _ := comm.AesEncrypt(data.AuditId, conf.SignSecret)
  120. data.AuditId = auditId
  121. }
  122. return data
  123. }
  124. // 获取最后一个审批人
  125. func (d *ApproverDao) GetLastAuditor(bid int, dataType int, dataId int) (*models.CmApprover, error) {
  126. data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
  127. _, err := d.engine.Desc("audit_order").Limit(1).Get(data)
  128. return data, err
  129. }
  130. // 初始化审批流程状态
  131. func (d *ApproverDao) InitStatus(bid int, dataType int, dataId int) error {
  132. data := &models.CmApprover{Status: 0}
  133. _, err := d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ?").Cols("status").Update(data)
  134. return err
  135. }
  136. // 审批流程-退回
  137. func (d *ApproverDao) BackHandlerWithId(id int) error {
  138. auditor, err := d.FindApproverById(id)
  139. data := &models.CmApprover{Status: 0}
  140. // 将往后的所有记录的status改为0
  141. _, err = d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order > ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder).Cols("status").Update(data)
  142. // 将当前的记录改为1-待审批
  143. data.Status = 1
  144. _, err = d.engine.ID(id).Cols("status").Update(data)
  145. return err
  146. }
  147. // 删除旧的审批流程
  148. func (d *ApproverDao) DeleteOldAuditors(bid int, dataType int, dataId int) error {
  149. data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
  150. _, err := d.engine.Delete(data)
  151. return err
  152. }