safe_audit_dao.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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) AddAuditRecord(safeId int, bId int, auditId int, times int, status int, progress int, opinion string, rectifiedInfo string) error {
  29. auditor := &models.CmSafeAudit{SafeId: safeId, BidsectionId: bId, AuditId: auditId, Times: times, CreateTime: time.Now(), Status: status, Progress: progress, Opinion: opinion, Rectifiedinfo: rectifiedInfo}
  30. _, err := d.engine.InsertOne(auditor)
  31. return err
  32. }
  33. // 获取最新审批顺序
  34. func (d *SafeAuditDao) GetNewOrder(safeId int, times int) int {
  35. var max_order int
  36. _, 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)
  37. fmt.Println(err)
  38. if max_order == 0 {
  39. return 1
  40. } else {
  41. return max_order + 1
  42. }
  43. }
  44. // times从1开始循环,往history里面push
  45. func (d *SafeAuditDao) GetAuditHistory(id int, times int) map[int][]viewmodels.HistorySafeAudit {
  46. auditorHistory := make(map[int][]viewmodels.HistorySafeAudit, 0)
  47. for i := 1; i <= times; i++ {
  48. auditors := make([]viewmodels.HistorySafeAudit, 0)
  49. d.engine.Sql("select ca.`id`, pa.`name`, pa.`position`, ca.`create_time`, 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 id desc", id, i).Find(&auditors)
  50. auditorsArr := make([]viewmodels.HistorySafeAudit, 0)
  51. for _, auditor := range auditors {
  52. auditorVM := viewmodels.HistorySafeAudit{}
  53. id, _ := comm.AesEncrypt(auditor.Id, conf.SignSecret)
  54. auditorVM.Id = id
  55. auditorVM.CreateTime = auditor.CreateTime
  56. auditorVM.Name = auditor.Name
  57. auditorVM.Position = auditor.Position
  58. auditorVM.Status = auditor.Status
  59. auditorVM.Opinion = auditor.Opinion
  60. auditorVM.Progress = auditor.Progress
  61. auditorsArr = append(auditorsArr, auditorVM)
  62. }
  63. auditorHistory[i] = auditorsArr
  64. }
  65. return auditorHistory
  66. }
  67. // 根据id获取记录
  68. func (d *SafeAuditDao) FindById(id int) (*models.CmSafeAudit, error) {
  69. data := &models.CmSafeAudit{Id: id}
  70. _, err := d.engine.Get(data)
  71. return data, err
  72. }
  73. // 查找最新的整改单列表
  74. func (d *SafeAuditDao) GetRectifications(bid int, pid int, dataId int, dataType int) ([]viewmodels.CheckOrderVM, error) {
  75. data := make([]viewmodels.CheckOrderVM, 0)
  76. err := d.engine.Sql("select cm.`content` as opinion, cm.`rectified_time` as `create_time`, pa.`name` from `cm_rectification` as cm left join `cm_project_account` as pa on pa.`id` = cm.`account_id` where cm.`bidsection_id` = ? and cm.`project_id` = ? and cm.`data_id` = ? and cm.`data_type` = ? ", bid, pid, dataId, dataType).Find(&data)
  77. return data, err
  78. }
  79. // 插入审批记录
  80. func (d *SafeAuditDao) InsertData(data models.CmSafeAudit) error {
  81. _, err := d.engine.Insert(&data)
  82. return err
  83. }
  84. // 改变下一条审批记录为待审核状态
  85. func (d *SafeAuditDao) ChangeNextRecord(times int, audit_order int) error {
  86. data := &models.CmSafeAudit{Status: 1, CreateTime: time.Now()}
  87. _, err := d.engine.Where("times = ? and audit_order = ?", times, audit_order).Update(data)
  88. return err
  89. }
  90. // 获取最后一个审批人
  91. func (d *SafeAuditDao) GetLastAuditor(times int, safeId int) (*models.CmSafeAudit, error) {
  92. data := &models.CmSafeAudit{}
  93. _, err := d.engine.Where("times = ? and safe_id = ?", times, safeId).Desc("audit_order").Limit(1).Get(data)
  94. return data, err
  95. }