safe_audit_dao.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * @description: 安全巡检数据库操作相关
  3. * @Author: LanJianRong
  4. * @Date: 2020-11-20
  5. * @FilePath: \construction_management\dao\safe_dao.go
  6. */
  7. package dao
  8. import (
  9. "fmt"
  10. "time"
  11. "github.com/go-xorm/xorm"
  12. "go.mod/comm"
  13. "go.mod/conf"
  14. "go.mod/models"
  15. "go.mod/web/viewmodels"
  16. )
  17. //数据库操作引擎
  18. type SafeAuditDao struct {
  19. engine *xorm.Engine
  20. }
  21. //获得一个DAO对象
  22. func NewSafeAuditDao(engine *xorm.Engine) *SafeAuditDao {
  23. return &SafeAuditDao{
  24. engine: engine,
  25. }
  26. }
  27. // 添加审批人
  28. func (d *SafeAuditDao) AddAuditor(safeId int, bId int, auditId int, times int) error {
  29. auditOrder := d.GetNewOrder(safeId, times)
  30. auditor := &models.CmSafeAudit{SafeId: safeId, BidsectionId: bId, AuditId: auditId, Times: times, AuditOrder: auditOrder, CreateTime: time.Now()}
  31. _, err := d.engine.InsertOne(auditor)
  32. return err
  33. }
  34. // 获取最新审批顺序
  35. func (d *SafeAuditDao) GetNewOrder(safeId int, times int) int {
  36. var max_order int
  37. _, err := d.engine.Sql("select Max(`audit_order`) as max_order from cm_safe_audit where safe_id = ? and times = ?", safeId, times).Get(&max_order)
  38. fmt.Println(err)
  39. if max_order == 0 {
  40. return 1
  41. } else {
  42. return max_order + 1
  43. }
  44. }
  45. // 根据当前times,获取审批人
  46. func (d *SafeAuditDao) GetAuditors(id int, times int) []viewmodels.Auditors {
  47. auditors := make([]viewmodels.Auditors, 0)
  48. d.engine.Sql("select pa.`name` from `cm_project_account` as pa, `cm_safe_audit` as ca where ca.`safe_id` = ? and ca.`times` = ? and ca.audit_id = pa.id order by `audit_order`", id, times).Find(&auditors)
  49. return auditors
  50. }
  51. // 根据当前times,获取审批人(包括原报)
  52. func (d *SafeAuditDao) GetAuditorsWithOwner(id int, times int, cur_uid int) []viewmodels.Auditors {
  53. auditors := make([]viewmodels.Auditors, 0)
  54. auditor := viewmodels.Auditors{Progress: ""}
  55. _, err := d.engine.Sql("select name, company, position, id as `audit_id` from `cm_project_account` where id = ?", cur_uid).Get(&auditor)
  56. if err != nil {
  57. fmt.Println(err)
  58. }
  59. auditors = append(auditors, auditor)
  60. d.engine.Sql("select pa.`company`, pa.`name`, pa.`account_group`, pa.`position`,ca.`audit_id` as `auditor_id`, ca.`id` as `audit_id`, ca.`status`, ca.`audit_order`, ca.`progress` from `cm_project_account` as pa, `cm_safe_audit` as ca where ca.`safe_id` = ? and ca.`times` = ? and ca.audit_id = pa.id order by `audit_order`", id, times).Find(&auditors)
  61. // 原报
  62. return auditors
  63. }
  64. // times从1开始循环,往history里面push
  65. func (d *SafeAuditDao) GetAuditHistory(id int, times int) map[int][]viewmodels.HistorySafeAudit {
  66. auditorHistory := make(map[int][]viewmodels.HistorySafeAudit, 0)
  67. for i := 1; i <= times; i++ {
  68. auditors := make([]viewmodels.HistorySafeAudit, 0)
  69. d.engine.Sql("select ca.`id`, pa.`name`, pa.`position`, ca.`create_time` as createTime, ca.`end_time` as endTime, ca.`progress`, ca.`opinion`, ca.`status` from `cm_project_account` as pa, `cm_safe_audit` as ca where ca.`safe_id` = ? and ca.audit_id = pa.id and times = ? order by `audit_order`", id, i).Find(&auditors)
  70. auditorsArr := make([]viewmodels.HistorySafeAudit, 0)
  71. for _, auditor := range auditors {
  72. auditorVM := viewmodels.HistorySafeAudit{}
  73. id, _ := comm.AesEncrypt(auditor.Id, conf.SignSecret)
  74. auditorVM.Id = id
  75. auditorVM.Createtime = auditor.Createtime
  76. auditorVM.EndTime = auditor.EndTime
  77. auditorVM.Name = auditor.Name
  78. auditorVM.Position = auditor.Position
  79. auditorVM.Status = auditor.Status
  80. auditorVM.Opinion = auditor.Opinion
  81. auditorVM.Progress = auditor.Progress
  82. auditorsArr = append(auditorsArr, auditorVM)
  83. }
  84. auditorHistory[i] = auditorsArr
  85. }
  86. return auditorHistory
  87. }
  88. // 根据id获取记录
  89. func (d *SafeAuditDao) FindById(id int) (*models.CmSafeAudit, error) {
  90. data := &models.CmSafeAudit{Id: id}
  91. _, err := d.engine.Get(data)
  92. return data, err
  93. }
  94. // 改变审批记录状态
  95. func (d *SafeAuditDao) ChangeStatusById(id int, opinion string, status int, rectifiedInfo string) error {
  96. data := &models.CmSafeAudit{EndTime: time.Now(), Opinion: opinion, Status: status, Rectifiedinfo: rectifiedInfo}
  97. _, err := d.engine.ID(id).Cols("end_time", "opinion", "status").Update(data)
  98. return err
  99. }
  100. // 拷贝新的审批流程
  101. func (d *SafeAuditDao) CopyAuditors(safeId int, times int, auditType string, uid int) error {
  102. oldAuditors := make([]models.CmSafeAudit, 0)
  103. err := d.engine.Where("safe_id = ? and times = ?", safeId, times).Find(&oldAuditors)
  104. if err != nil {
  105. return err
  106. }
  107. var copyStatus = true
  108. fmt.Println("auditType", auditType)
  109. newAuditors := make([]models.CmSafeAudit, 0)
  110. for _, auditor := range oldAuditors {
  111. auditorVM := models.CmSafeAudit{}
  112. auditorVM.SafeId = auditor.SafeId
  113. auditorVM.BidsectionId = auditor.BidsectionId
  114. auditorVM.Times = auditor.Times + 1
  115. auditorVM.AuditId = auditor.AuditId
  116. auditorVM.AuditOrder = auditor.AuditOrder
  117. auditorVM.Progress = auditor.Progress
  118. auditorVM.CreateTime = time.Now()
  119. auditorVM.Status = auditor.Status
  120. // 上报人
  121. if auditType == "uncheck" {
  122. auditorVM.Status = 0
  123. } else {
  124. if auditor.AuditId == uid {
  125. auditorVM.Status = 0
  126. copyStatus = false
  127. }
  128. }
  129. if !copyStatus {
  130. auditorVM.Status = 0
  131. }
  132. newAuditors = append(newAuditors, auditorVM)
  133. }
  134. _, err = d.engine.Insert(&newAuditors)
  135. return err
  136. }
  137. // 增加整改人
  138. func (d *SafeAuditDao) AddCheckWorker(id int, auditId int, opinion string) error {
  139. auditor, err := d.FindById(id)
  140. if err != nil {
  141. return err
  142. }
  143. data := &models.CmSafeAudit{SafeId: auditor.SafeId, BidsectionId: auditor.BidsectionId, AuditOrder: auditor.AuditOrder + 1, Times: auditor.Times, AuditId: auditId, Status: 0, Progress: 1, CreateTime: time.Now(), Opinion: opinion}
  144. _, err = d.engine.Insert(data)
  145. if err != nil {
  146. return err
  147. }
  148. return nil
  149. }
  150. // 查找最新的整改单
  151. func (d *SafeAuditDao) GetLastedOrder(safeId int) ([]viewmodels.CheckOrderVM, error) {
  152. data := make([]viewmodels.CheckOrderVM, 0)
  153. err := d.engine.Sql("select cm.`opinion`, cm.`status`, cm.`end_time`, pa.`name` from `cm_safe_audit` as cm left join `cm_project_account` as pa on pa.`id` = cm.`audit_id` where cm.`safe_id` = ? and cm.`progress` = ?", safeId, 1).Find(&data)
  154. return data, err
  155. }
  156. // 获取当前的审核人
  157. func (d *SafeAuditDao) GetLastedAuditor(times int, safeId int) (*viewmodels.LatestAuditorVM, error) {
  158. data := &viewmodels.LatestAuditorVM{}
  159. _, err := d.engine.Sql("select audit_id, progress, status from cm_safe_audit where times = ? and safe_id = ? and status = 0 order by `audit_order` limit 1", times, safeId).Get(data)
  160. auditId, _ := comm.AesEncrypt(data.AuditId, conf.SignSecret)
  161. data.AuditId = auditId
  162. return data, err
  163. }
  164. //
  165. func (d *SafeAuditDao) InsertData(data []models.CmSafeAudit) error {
  166. _, err := d.engine.Insert(&data)
  167. return err
  168. }