|  | @@ -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
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // 规则校验
 |