소스 검색

Merge branch 'master' of http://192.168.1.41:3000/caipin/construction_management

caipin 4 년 전
부모
커밋
5faa357559
8개의 변경된 파일319개의 추가작업 그리고 213개의 파일을 삭제
  1. 171 0
      dao/approver_dao.go
  2. 27 27
      dao/safe_audit_dao.go
  3. 2 2
      dao/safe_dao.go
  4. 52 108
      services/safe_audit.service.go
  5. 7 8
      services/safe_service.go
  6. 38 66
      web/api/safe_audit_api.go
  7. 20 0
      web/viewmodels/approver.go
  8. 2 2
      web/viewmodels/safe.go

+ 171 - 0
dao/approver_dao.go

@@ -0,0 +1,171 @@
+/*
+ * @description: 审批流程数据库操作相关
+ * @Author: LanJianRong
+ * @Date: 2021-01-06
+ * @FilePath: \construction_management\dao\approver_dao.go
+ */
+
+package dao
+
+import (
+	"fmt"
+
+	"github.com/go-xorm/xorm"
+	"go.mod/comm"
+	"go.mod/conf"
+	"go.mod/models"
+	"go.mod/web/viewmodels"
+)
+
+// 数据库操作引擎
+type ApproverDao struct {
+	engine *xorm.Engine
+}
+
+// 获得一个DAO对象
+func NewApproverDao(engine *xorm.Engine) *ApproverDao {
+	return &ApproverDao{
+		engine: engine,
+	}
+}
+
+// 批量插入数据
+func (d *ApproverDao) InsertData(times int, bid int, pid int, dataType int, dataId int, auditors []int, reAuditors []int) error {
+	data := make([]models.CmApprover, 0)
+	for i, item := range auditors {
+		approverVM := models.CmApprover{}
+		approverVM.AuditId = item
+		approverVM.AuditOrder = i + 1
+		approverVM.Progress = 0
+		approverVM.BidsectionId = bid
+		approverVM.ProjectId = pid
+		approverVM.DataType = dataType
+		approverVM.DataId = dataId
+		approverVM.Status = 0
+		approverVM.Times = times
+		if i == 0 {
+			approverVM.Status = 1
+		}
+		data = append(data, approverVM)
+	}
+	checkVM := models.CmApprover{}
+	checkVM.AuditOrder = len(auditors) + 1
+	checkVM.Progress = 1
+	checkVM.BidsectionId = bid
+	checkVM.ProjectId = pid
+	checkVM.DataType = dataType
+	checkVM.DataId = dataId
+	checkVM.Times = times
+	data = append(data, checkVM)
+	for i, item := range reAuditors {
+		approverVM := models.CmApprover{}
+		approverVM.AuditId = item
+		approverVM.AuditOrder = i + 2 + len(auditors)
+		approverVM.Progress = 2
+		approverVM.BidsectionId = bid
+		approverVM.ProjectId = pid
+		approverVM.DataType = dataType
+		approverVM.DataId = dataId
+		approverVM.Times = times
+		data = append(data, approverVM)
+	}
+	_, err := d.engine.Insert(data)
+	return err
+}
+
+// 更改状态
+func (d *ApproverDao) ChangeStatus(id int, status int) error {
+	data := &models.CmApprover{Status: status}
+	_, err := d.engine.Where("id = ?", id).Cols("status").Update(data)
+	return err
+}
+
+// 更改审批人
+func (d *ApproverDao) ChangeAuditId(bid int, dataType int, dataId int, auditOrder int, uid int) error {
+	data := &models.CmApprover{AuditId: uid}
+	_, err := d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", bid, dataType, dataId, auditOrder).Cols("audit_id").Update(data)
+	return err
+}
+
+// 更改下一个审批人的状态
+func (d *ApproverDao) ChangeNextStatus(id int, status int) error {
+	data := &models.CmApprover{Id: id}
+	_, err := d.engine.Get(data)
+	_, 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)
+	return err
+}
+
+// 根据当前times,获取审批流程(包括原报)
+func (d *ApproverDao) GetAuditorsWithOwner(bid int, dataType int, dataId int, times int, cur_uid int) []viewmodels.Auditors {
+	auditors := make([]viewmodels.Auditors, 0)
+	auditor := viewmodels.Auditors{Progress: "", Id: ""}
+
+	_, err := d.engine.Sql("select name, company, position, mobile, id as `audit_id` from `cm_project_account` where id = ?", cur_uid).Get(&auditor)
+
+	if err != nil {
+		fmt.Println(err)
+	}
+	auditors = append(auditors, auditor)
+
+	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)
+	// 原报
+	return auditors
+}
+
+func (d *ApproverDao) FindApproverById(id int) (*models.CmApprover, error) {
+	data := &models.CmApprover{}
+	_, err := d.engine.ID(id).Get(data)
+	return data, err
+}
+
+// 获取最新的审核人
+func (d *ApproverDao) GetLastedAuditor(bid int, dataType int, dataId int) *viewmodels.Approver {
+	data := &viewmodels.Approver{BidsectionId: string(bid), DataType: dataType, DataId: string(dataId), Status: 1}
+	has, _ := d.engine.Get(data)
+	if has == true {
+		id, _ := comm.AesEncrypt(data.Id, conf.SignSecret)
+		data.Id = id
+		pid, _ := comm.AesEncrypt(data.ProjectId, conf.SignSecret)
+		data.Id = pid
+		bid, _ := comm.AesEncrypt(data.BidsectionId, conf.SignSecret)
+		data.Id = bid
+		dataId, _ := comm.AesEncrypt(data.DataId, conf.SignSecret)
+		data.DataId = dataId
+		auditId, _ := comm.AesEncrypt(data.AuditId, conf.SignSecret)
+		data.AuditId = auditId
+	}
+	return data
+}
+
+// 获取最后一个审批人
+func (d *ApproverDao) GetLastAuditor(bid int, dataType int, dataId int) (*models.CmApprover, error) {
+	data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
+	_, err := d.engine.Desc("audit_order").Limit(1).Get(data)
+	return data, err
+}
+
+// 初始化审批流程状态
+func (d *ApproverDao) InitStatus(bid int, dataType int, dataId int) error {
+	data := &models.CmApprover{Status: 0}
+	_, err := d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ?").Cols("status").Update(data)
+	return err
+}
+
+// 审批流程-退回
+func (d *ApproverDao) BackHandlerWithId(id int) error {
+	auditor, err := d.FindApproverById(id)
+	data := &models.CmApprover{Status: 0}
+	// 将往后的所有记录的status改为0
+	_, 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)
+	// 将当前的记录改为1-待审批
+	data.Status = 1
+	_, err = d.engine.ID(id).Cols("status").Update(data)
+	return err
+}
+
+// 删除旧的审批流程
+func (d *ApproverDao) DeleteOldAuditors(bid int, dataType int, dataId int) error {
+	data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
+	_, err := d.engine.Delete(data)
+	return err
+}

+ 27 - 27
dao/safe_audit_dao.go

@@ -30,10 +30,10 @@ func NewSafeAuditDao(engine *xorm.Engine) *SafeAuditDao {
 	}
 }
 
-// 添加审批
-func (d *SafeAuditDao) AddAuditor(safeId int, bId int, auditId int, times int) error {
+// 添加审批记录
+func (d *SafeAuditDao) AddAuditRecord(safeId int, bId int, auditId int, times int, status int, progress int, rectifiedInfo string) error {
 	auditOrder := d.GetNewOrder(safeId, times)
-	auditor := &models.CmSafeAudit{SafeId: safeId, BidsectionId: bId, AuditId: auditId, Times: times, AuditOrder: auditOrder, CreateTime: time.Now()}
+	auditor := &models.CmSafeAudit{SafeId: safeId, BidsectionId: bId, AuditId: auditId, Times: times, AuditOrder: auditOrder, CreateTime: time.Now(), Status: status, Progress: progress, Rectifiedinfo: rectifiedInfo}
 	_, err := d.engine.InsertOne(auditor)
 	return err
 }
@@ -57,22 +57,22 @@ func (d *SafeAuditDao) GetAuditors(id int, times int) []viewmodels.Auditors {
 	return auditors
 }
 
-// 根据当前times,获取审批人(包括原报)
-func (d *SafeAuditDao) GetAuditorsWithOwner(id int, times int, cur_uid int) []viewmodels.Auditors {
-	auditors := make([]viewmodels.Auditors, 0)
-	auditor := viewmodels.Auditors{Progress: ""}
+// // 根据当前times,获取审批人(包括原报)
+// func (d *SafeAuditDao) GetAuditorsWithOwner(id int, times int, cur_uid int) []viewmodels.Auditors {
+// 	auditors := make([]viewmodels.Auditors, 0)
+// 	auditor := viewmodels.Auditors{Progress: ""}
 
-	_, err := d.engine.Sql("select name, company, position, mobile, id as `audit_id` from `cm_project_account` where id = ?", cur_uid).Get(&auditor)
+// 	_, err := d.engine.Sql("select name, company, position, mobile, id as `audit_id` from `cm_project_account` where id = ?", cur_uid).Get(&auditor)
 
-	if err != nil {
-		fmt.Println(err)
-	}
-	auditors = append(auditors, auditor)
+// 	if err != nil {
+// 		fmt.Println(err)
+// 	}
+// 	auditors = append(auditors, auditor)
 
-	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_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)
-	// 原报
-	return auditors
-}
+// 	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_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)
+// 	// 原报
+// 	return auditors
+// }
 
 // times从1开始循环,往history里面push
 func (d *SafeAuditDao) GetAuditHistory(id int, times int) map[int][]viewmodels.HistorySafeAudit {
@@ -107,11 +107,11 @@ func (d *SafeAuditDao) FindById(id int) (*models.CmSafeAudit, error) {
 }
 
 // 改变审批记录状态
-func (d *SafeAuditDao) ChangeStatusById(id int, opinion string, status int, rectifiedInfo string) error {
-	data := &models.CmSafeAudit{EndTime: time.Now(), Opinion: opinion, Status: status, Rectifiedinfo: rectifiedInfo}
-	_, err := d.engine.ID(id).Cols("end_time", "opinion", "status").Update(data)
-	return err
-}
+// func (d *SafeAuditDao) ChangeStatusById(id int, opinion string, status int, rectifiedInfo string) error {
+// 	data := &models.CmSafeAudit{EndTime: time.Now(), Opinion: opinion, Status: status, Rectifiedinfo: rectifiedInfo}
+// 	_, err := d.engine.ID(id).Cols("end_time", "opinion", "status").Update(data)
+// 	return err
+// }
 
 // 拷贝新的审批流程
 func (d *SafeAuditDao) CopyAuditors(safeId int, times int, uid int) error {
@@ -142,15 +142,15 @@ func (d *SafeAuditDao) CopyAuditors(safeId int, times int, uid int) error {
 	return err
 }
 
-// 增加整改人
+// 修改整改人id
 func (d *SafeAuditDao) AddCheckWorker(id int, auditId int) error {
 	auditor, err := d.FindById(id)
 	if err != nil {
 		return err
 	}
-
-	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()}
-	_, err = d.engine.Insert(data)
+	//  _, err = d.engine.Where().Update()
+	data := &models.CmSafeAudit{AuditId: auditId, Status: 1, CreateTime: time.Now()}
+	_, err = d.engine.Where("safe_id = ? and bidsection_id = ? and audit_order = ? and times = ?", auditor.SafeId, auditor.BidsectionId, auditor.AuditOrder+1, auditor.Times).Cols("audit_id", "status", "create_time").Update(data)
 	if err != nil {
 		return err
 	}
@@ -176,14 +176,14 @@ func (d *SafeAuditDao) GetLastedAuditor(times int, safeId int) (*viewmodels.Late
 }
 
 // 插入审批记录
-func (d *SafeAuditDao) InsertData(data []models.CmSafeAudit) error {
+func (d *SafeAuditDao) InsertData(data models.CmSafeAudit) error {
 	_, err := d.engine.Insert(&data)
 	return err
 }
 
 // 改变下一条审批记录为待审核状态
 func (d *SafeAuditDao) ChangeNextRecord(times int, audit_order int) error {
-	data := &models.CmSafeAudit{Status: 1}
+	data := &models.CmSafeAudit{Status: 1, CreateTime: time.Now()}
 	_, err := d.engine.Where("times = ? and audit_order = ?", times, audit_order).Update(data)
 	return err
 }

+ 2 - 2
dao/safe_dao.go

@@ -83,8 +83,8 @@ func (d *SafeDao) CountRuleCode(bid int) (int64, error) {
 }
 
 // 更改status
-func (d *SafeDao) ChangeStatus(id int, status int, times int) error {
-	data := &models.CmSafe{Status: status, Times: times}
+func (d *SafeDao) ChangeStatus(id int, status int) error {
+	data := &models.CmSafe{Status: status}
 	_, err := d.engine.ID(id).Update(data)
 	return err
 }

+ 52 - 108
services/safe_audit.service.go

@@ -9,23 +9,21 @@ package services
 import (
 	"errors"
 	"log"
-	"time"
 
 	"github.com/kataras/iris/v12"
 	"go.mod/dao"
 	"go.mod/datasource"
-	"go.mod/models"
 	"go.mod/web/viewmodels"
 )
 
 type SafeAuditService interface {
 	ValidRule(ctx iris.Context) (viewmodels.SafeAudit, error)
 	ValidStart(ctx iris.Context) (viewmodels.AuditStart, error)
-	AddAuditor(safeId int, bId int, auditId int, times int) error
+	// AddAuditor(safeId int, bId int, auditId int, times int) error
 	CloseAudit(id int, opinion string, curUid int, saveId int) error
 	BackAudit(id int, opinion string, curUid int, saveId int, auditId int) error
 	PassAudit(id int, uid int, auditId int, opinion string, rectifiedInfo string) error
-	StartAudit(safeId int, bidsectionId int, times int, auditors []int, reAuditors []int, uid int) error
+	StartAudit(safeId int, bidsectionId int, auditors []int, reAuditors []int, uid int, pid int) error
 }
 
 type safeAuditService struct {
@@ -35,6 +33,7 @@ type safeAuditService struct {
 	validBack    string
 	daoSafeAudit *dao.SafeAuditDao
 	daoSafe      *dao.SafeDao
+	daoApprover  *dao.ApproverDao
 }
 
 //创建项目用户service
@@ -46,165 +45,110 @@ func NewSafeAuditService() SafeAuditService {
 		validPass:    "/api/safe_audit/pass",
 		daoSafeAudit: dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
 		daoSafe:      dao.NewSafeDao(datasource.InstanceDbMaster()),
+		daoApprover:  dao.NewApproverDao(datasource.InstanceDbMaster()),
 	}
 }
 
-// 增加审批人
-func (s *safeAuditService) AddAuditor(safeId int, bId int, auditId int, times int) error {
-	err := s.daoSafeAudit.AddAuditor(safeId, bId, auditId, times)
-	return err
-}
-
 // 关闭审批流程
 func (s *safeAuditService) CloseAudit(id int, opinion string, curUid int, saveId int) error {
-	auditor, err := s.daoSafeAudit.FindById(id)
-	if err != nil {
-		return err
-	}
+	auditor, err := s.daoApprover.FindApproverById(id)
 	if auditor.AuditId != curUid {
 		return errors.New("该用户没有审批权限!")
 	}
-	// 更改cm_safe_audit的记录
-	err = s.daoSafeAudit.ChangeStatusById(id, opinion, 4, "")
-	if err != nil {
-		return err
-	}
+	safe := s.daoSafe.FindById(auditor.DataId)
+	// 增加审批日志记录
+	err = s.daoSafeAudit.AddAuditRecord(auditor.DataId, auditor.BidsectionId, auditor.AuditId, safe.Times, 2, auditor.Progress, "")
 	// 更改cm_safe的记录
-	err = s.daoSafe.ChangeStatus(saveId, 5, auditor.Times)
-	if err != nil {
-		return err
-	}
-	return nil
+	err = s.daoSafe.ChangeStatus(saveId, 5)
+	// 更改cm_approver的记录
+	err = s.daoApprover.ChangeStatus(id, 3)
+	return err
 }
 
 // 审批退回
 func (s *safeAuditService) BackAudit(id int, opinion string, curUid int, saveId int, auditId int) error {
-	auditor, err := s.daoSafeAudit.FindById(id)
-	if err != nil {
-		return err
-	}
+	auditor, err := s.daoApprover.FindApproverById(id)
 	if auditor.AuditId != curUid {
 		return errors.New("该用户没有审批权限!")
 	}
-	// 更改cm_safe_audit的记录
-	err = s.daoSafeAudit.ChangeStatusById(id, opinion, 3, "")
+	safe := s.daoSafe.FindById(saveId)
+	// 增加审批日志
+	err = s.daoSafeAudit.AddAuditRecord(auditor.DataId, auditor.BidsectionId, auditor.AuditId, safe.Times, 1, auditor.Progress, "")
 	if err != nil {
 		return err
 	}
 
+	// 退回到了检查人
+	if auditId == 0 {
+		// 初始化审批流程
+		err = s.daoApprover.InitStatus(auditor.BidsectionId, auditor.DataType, auditor.DataId)
+	} else {
+		// 退回到审批流程中的某一个人
+		err = s.daoApprover.BackHandlerWithId(auditId)
+	}
 	safeData := s.daoSafe.FindById(saveId)
 	err = s.daoSafeAudit.CopyAuditors(saveId, safeData.Times, auditId)
-	if err != nil {
-		return err
-	}
-	// 更改cm_safe的记录
-	// if progress == "uncheck" {
-	// 	err = s.daoSafe.ChangeStatus(saveId, 0, auditor.Times+1)
-	// } else if progress == "checked" {
-	// 	err = s.daoSafe.ChangeStatus(saveId, 1, auditor.Times+1)
-	// } else if progress == "checking" {
-	// 	err = s.daoSafe.ChangeStatus(saveId, 2, auditor.Times+1)
-	// } else {
-	// 	err = s.daoSafe.ChangeStatus(saveId, 3, auditor.Times+1)
-
-	// }
 	return err
+
 }
 
 // 审批通过
 func (s *safeAuditService) PassAudit(id int, uid int, auditId int, opinion string, rectifiedInfo string) error {
-	auditor, err := s.daoSafeAudit.FindById(id)
+	auditor, err := s.daoApprover.FindApproverById(id)
 	if err != nil {
 		return err
 	}
+	// 获取安全巡检记录
+	safe := s.daoSafe.FindById(auditor.DataId)
 	if auditor.AuditId != uid {
 		return errors.New("该用户没有审批权限!")
 	}
+	// 增加审批日志
+	err = s.daoSafeAudit.AddAuditRecord(auditor.DataId, auditor.BidsectionId, auditor.AuditId, safe.Times, 0, auditor.Progress, rectifiedInfo)
+	// 改变审批流程中当前审批人以及下一个审批人的审批流程状态
+	err = s.daoApprover.ChangeStatus(id, 2)
+	err = s.daoApprover.ChangeNextStatus(id, 1)
+	// 审批人选择了整改人
 	if auditId != 0 {
-		// 审批人选择了整改人
-		// 改变当前审批记录的状态为已通过
-		err := s.daoSafeAudit.ChangeStatusById(auditor.Id, opinion, 2, rectifiedInfo)
 		// 修改cm_safe表的status状态为待整改
-		err = s.daoSafe.ChangeStatus(auditor.SafeId, 2, auditor.Times)
-		// 增加整改人的审批记录
-		err = s.daoSafeAudit.AddCheckWorker(id, auditId)
-		if err != nil {
-			return err
-		}
+		err = s.daoSafe.ChangeStatus(auditor.DataId, 2)
+		// 改变审批流程中的整改人id
+		err = s.daoApprover.ChangeAuditId(auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1, auditId)
 	} else {
-		// 改变当前记录的状态为已通过
-		err := s.daoSafeAudit.ChangeStatusById(auditor.Id, opinion, 2, rectifiedInfo)
 		if auditor.Progress == 1 {
 			// 整改人审批流程
 			// 修改cm_safe表的status状态为待复查
-			err = s.daoSafe.ChangeStatus(auditor.SafeId, 3, auditor.Times)
+			err = s.daoSafe.ChangeStatus(auditor.DataId, 3)
 		} else {
 			// 复查流程
 			// 查找最后一个审批人
-			lastAuditor, err := s.daoSafeAudit.GetLastAuditor(auditor.Times, auditor.SafeId)
+			lastAuditor, err := s.daoApprover.GetLastAuditor(auditor.BidsectionId, auditor.DataType, auditor.DataId)
 			if err != nil {
 				return err
 			}
-			if lastAuditor.Id == auditor.Id {
+			if lastAuditor.AuditId == auditor.Id {
 				// 说明审批流程已经走完
-				err = s.daoSafe.ChangeStatus(auditor.SafeId, 4, auditor.Times)
+				err = s.daoSafe.ChangeStatus(auditor.DataId, 4)
 			}
 		}
-		if err != nil {
-			return err
-		}
 	}
-	// 改变下一条记录为待审核
-	err = s.daoSafeAudit.ChangeNextRecord(auditor.Times, auditor.AuditOrder+1)
-	if err != nil {
-		return err
-	}
-	return nil
+	return err
 }
 
-func (s *safeAuditService) StartAudit(safeId int, bidsectionId int, times int, auditors []int, reAuditors []int, uid int) error {
+func (s *safeAuditService) StartAudit(safeId int, bidsectionId int, auditors []int, reAuditors []int, uid int, pid int) error {
 	safe := s.daoSafe.FindById(safeId)
 	if safe.Uid != uid {
 		return errors.New("该用户没有操作权限!")
 	}
-	data := make([]models.CmSafeAudit, 0)
-	for i, item := range auditors {
-		auditVM := models.CmSafeAudit{}
-		auditVM.AuditId = item
-		auditVM.BidsectionId = bidsectionId
-		auditVM.SafeId = safeId
-		auditVM.AuditOrder = i + 1
-		auditVM.CreateTime = time.Now()
-		auditVM.Progress = 0
-		if i == 0 {
-			auditVM.Status = 1
-		} else {
-			auditVM.Status = 0
-		}
-		auditVM.Times = times
-		data = append(data, auditVM)
-	}
-	for i, item := range reAuditors {
-		auditVM := models.CmSafeAudit{}
-		auditVM.AuditId = item
-		auditVM.BidsectionId = bidsectionId
-		auditVM.SafeId = safeId
-		auditVM.AuditOrder = i + 2 + len(auditors)
-		auditVM.CreateTime = time.Now()
-		auditVM.Progress = 2
-		auditVM.Status = 0
-		auditVM.Times = times
-		data = append(data, auditVM)
-	}
-	err := s.daoSafeAudit.InsertData(data)
-	if err != nil {
-		return err
-	}
-	err = s.daoSafe.ChangeStatus(safeId, 1, times)
-	if err != nil {
-		return err
-	}
-	return nil
+	// 清除可能有的旧的审批流程
+	err := s.daoApprover.DeleteOldAuditors(bidsectionId, 1, safeId)
+	// 增加审批日志
+	err = s.daoSafeAudit.AddAuditRecord(safeId, bidsectionId, uid, safe.Times, 0, 0, "")
+	// 改变安全巡检表的状态
+	err = s.daoSafe.ChangeStatus(safeId, 1)
+	// 创建审批流程
+	err = s.daoApprover.InsertData(safe.Times, bidsectionId, pid, 1, safeId, auditors, reAuditors)
+	return err
 }
 
 // 规则校验

+ 7 - 8
services/safe_service.go

@@ -32,6 +32,7 @@ type safeService struct {
 	daoProjectAccount *dao.ProjectAccountDao
 	daoAnnex          *dao.AnnexDao
 	daoRule           *dao.RuleDao
+	daoApprover       *dao.ApproverDao
 	validDetail       string
 }
 
@@ -43,6 +44,7 @@ func NewSafeService() SafeService {
 		daoAnnex:          dao.NewAnnexDao(datasource.InstanceDbMaster()),
 		daoSafeAudit:      dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
 		daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
+		daoApprover:       dao.NewApproverDao(datasource.InstanceDbMaster()),
 	}
 }
 func (s *safeService) Get(id int, pid int, pageNo int, pageSize int) ([]viewmodels.SafeList, int64) {
@@ -128,18 +130,15 @@ func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {
 	fileVM.FileList = fileArr
 	fileVM.Total = total
 	data.File = fileVM
-	auditors := s.daoSafeAudit.GetAuditorsWithOwner(safeData.Id, safeData.Times, account.Id)
+	auditors := s.daoApprover.GetAuditorsWithOwner(safeData.BidsectionId, int(1), safeData.Id, safeData.Times, account.Id)
 	encryptAuditors := make([]viewmodels.Auditors, 0)
-	for i, item := range auditors {
+	for _, item := range auditors {
 		auditorVM := viewmodels.Auditors{}
-		auditId, _ := comm.AesEncrypt(item.AuditId, conf.SignSecret)
-		if i == 0 {
-			auditorVM.Id = ""
-
-		} else {
+		if item.Id != "" {
 			id, _ := comm.AesEncrypt(item.Id, conf.SignSecret)
 			auditorVM.Id = id
 		}
+		auditId, _ := comm.AesEncrypt(item.AuditId, conf.SignSecret)
 		auditorVM.AuditId = auditId
 		auditorVM.Name = item.Name
 		auditorVM.Position = item.Position
@@ -158,7 +157,7 @@ func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {
 	data.RectifiedInfo = rectifiedInfo
 
 	// 最新审批人信息
-	latestAuditor, _ := s.daoSafeAudit.GetLastedAuditor(safeData.Times, safeData.Id)
+	latestAuditor := s.daoApprover.GetLastedAuditor(safeData.BidsectionId, 1, safeData.Id)
 	data.LatestdAuditor = latestAuditor
 	data.Auditors = encryptAuditors
 

+ 38 - 66
web/api/safe_audit_api.go

@@ -22,53 +22,6 @@ type SafeAuditApi struct {
 	SafeAuditService services.SafeAuditService
 }
 
-// @Summary 添加审批流程
-// @Tags 安全巡检-审批
-// @Description 增加审批人进审批流程
-// @Accept  json
-// @Produce  json
-// @Security ApiKeyAuth
-// @Param   safe_id     body    string     true        "质量巡检id"
-// @Param   bidsection_id     body    string     true        "标段id"
-// @Param   times     body    string     true        "审批次数"
-// @Param   audit_id     body    string     true        "审批人id"
-// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
-// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
-// @Router /api/safe_audit/add [post]
-func (c *SafeAuditApi) PostAdd() {
-	// 1.规则验证
-	safeAuditData, err := c.SafeAuditService.ValidRule(c.Ctx)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
-		return
-	}
-	bidsectionId, err := utils.GetDecryptId(safeAuditData.BidsectionId)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-		return
-	}
-
-	auditId, err := utils.GetDecryptId(safeAuditData.AuditId)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-		return
-	}
-	safeId, err := utils.GetDecryptId(safeAuditData.SafeId)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-		return
-	}
-	err = c.SafeAuditService.AddAuditor(safeId, bidsectionId, auditId, safeAuditData.Times)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
-		return
-	}
-	c.Ctx.JSON(iris.Map{
-		"code": 0,
-		"msg":  "插入成功",
-	})
-}
-
 // @Summary 开始审批
 // @Tags 安全巡检-审批
 // @Description 开始审批
@@ -89,16 +42,19 @@ func (c *SafeAuditApi) PostStart() {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
 		return
 	}
+	// 2.获取bid
 	bidsectionId, err := utils.GetDecryptId(safeAuditData.BidsectionId)
 	if err != nil {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
 		return
 	}
+	// 3.获取safeId
 	safeId, err := utils.GetDecryptId(safeAuditData.SafeId)
 	if err != nil {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
 		return
 	}
+	// 4.解析审批组id、复查组id
 	var auditors []int
 	for _, item := range safeAuditData.Auditors {
 		auditId, _ := utils.GetDecryptId(item)
@@ -109,12 +65,20 @@ func (c *SafeAuditApi) PostStart() {
 		auditId, _ := utils.GetDecryptId(item)
 		reAuditors = append(reAuditors, auditId)
 	}
+	// 5.获取当前用户id
 	uid, err := utils.GetProjectAccountId(c.Ctx)
 	if err != nil {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
 		return
 	}
-	err = c.SafeAuditService.StartAudit(safeId, bidsectionId, safeAuditData.Times, auditors, reAuditors, uid)
+	// 6.获取当前项目id
+	pid, err := utils.GetProjectId(c.Ctx)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	// 7.执行sql
+	err = c.SafeAuditService.StartAudit(safeId, bidsectionId, auditors, reAuditors, uid, pid)
 	if err != nil {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
 		return
@@ -130,7 +94,7 @@ func (c *SafeAuditApi) PostStart() {
 // @Description 直接停止审批流程
 // @Accept  json
 // @Produce  json
-// @Param   id     body    string     true        "审批id"
+// @Param   id     body    string     true        "审批流程id"
 // @Param   safe_id     body    string     true        "巡检id"
 // @Param   opinion     body    string     true        "审批意见"
 // @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
@@ -181,8 +145,7 @@ func (c *SafeAuditApi) PostClose() {
 // @Produce  json
 // @Param   id     body    string     true        "审批id"
 // @Param   safe_id     body    string     true        "巡检id"
-// @Param   audit_id     body    string     true        "回滚的审批人id"
-// @Param   progress     body    string     true        "退回的类型,uncheck退回检查人,checking整改人,checkNo复查人,checked审批人"
+// @Param   audit_id     body    string     true        "回滚的审批流程id"
 // @Param   opinion     body    string     true        "审批意见"
 // @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
 // @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
@@ -200,29 +163,38 @@ func (c *SafeAuditApi) PostBack() {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
 		return
 	}
-	// 3.获取安全巡检记录id、回滚的审批人id
-	safeId, err := utils.GetDecryptId(safeAuditData.SafeId)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-		return
-	}
-	auditId, err := utils.GetDecryptId(safeAuditData.AuditId)
-	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-		return
-	}
-	// 4.解析登录id
+	// 3.解析登录id
 	uid, err := utils.GetProjectAccountId(c.Ctx)
 	if err != nil {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
 		return
 	}
-	// 5. 执行sql
-	err = c.SafeAuditService.BackAudit(id, safeAuditData.Opinion, uid, safeId, auditId)
+	// 4.获取安全巡检记录id、回滚的审批流程id
+	safeId, err := utils.GetDecryptId(safeAuditData.SafeId)
 	if err != nil {
-		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
 		return
 	}
+	// 说明退回到了检查人
+	if safeAuditData.AuditId == "" {
+		err = c.SafeAuditService.BackAudit(id, safeAuditData.Opinion, uid, safeId, 0)
+		if err != nil {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+			return
+		}
+	} else {
+		auditId, err := utils.GetDecryptId(safeAuditData.AuditId)
+		if err != nil {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
+			return
+		}
+		err = c.SafeAuditService.BackAudit(id, safeAuditData.Opinion, uid, safeId, auditId)
+		if err != nil {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+			return
+		}
+	}
+
 	// 6. 返回结果
 	c.Ctx.JSON(iris.Map{
 		"code": 0,

+ 20 - 0
web/viewmodels/approver.go

@@ -0,0 +1,20 @@
+/*
+ * @description:审批流程相关
+ * @Author: LanJianRong
+ * @Date: 2021-01-07 16:00:19
+ * @FilePath: \construction_management\web\viewmodels\approver.go
+ */
+package viewmodels
+
+type Approver struct {
+	Id           string `from:"id" json:"json`
+	ProjectId    string `from:"project_id" json:"project_id"`
+	BidsectionId string `from:"bidsection_id" json:"bidsection_id"`
+	DataType     int    `from:"data_type" json:"data_type"`
+	DataId       string `from:"data_id" json:"data_id"`
+	Status       int    `from:"status" json:"status"`
+	Progress     int    `from:"progress" json:"progress"`
+	Times        int    `from:"times" json:"times"`
+	AuditOrder   int    `from:"audit_order" json:"audit_order"`
+	AuditId      string `from:"audit_id" json:"audit_id"`
+}

+ 2 - 2
web/viewmodels/safe.go

@@ -58,7 +58,7 @@ type SafeDetail struct {
 	Auditors         []Auditors                 `form:"auditors" json:"auditors"`
 	AuditHistory     map[int][]HistorySafeAudit `from:"auditHistory" json:"auditHistory"`
 	RectifiedInfo    []CheckOrderVM             `from:"rectifiedInfo" json:"rectifiedInfo"`
-	LatestdAuditor   *LatestAuditorVM           `from:"latestAuditor" json:"latestAuditor"`
+	LatestdAuditor   *Approver                  `from:"latestAuditor" json:"latestAuditor"`
 }
 
 // 单独出一个struct方便分页使用
@@ -74,6 +74,7 @@ type SafeFile struct {
 	CreateTime  time.Time `from:"create_time" json:"createTime"`
 }
 type Auditors struct {
+	Id           string `from:"id" json:id"`
 	Name         string `from:"name" json:"name"`
 	Progress     string `from:"progress" json:"progress"`
 	AuditId      string `from:"audit_id" json:"audit_id"`
@@ -82,7 +83,6 @@ type Auditors struct {
 	AccountGroup int    `from:"account_group" json:"account_group"`
 	Company      string `from:"company" json:"company"`
 	Status       int    `from:"status" json:"status"`
-	Id           string `from:"id" json:"id"`
 	Mobile       string `from:"mobile" json:"mobile"`
 }