1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075 |
- /*
- * @description: 审批流程数据库操作相关
- * @Author: LanJianRong
- * @Date: 2021-01-06
- * @FilePath: \construction_management\dao\approver_dao.go
- */
- package dao
- import (
- "errors"
- "fmt"
- "strconv"
- "time"
- "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(cur_uid int, bid int, pid int, dataType int, dataId int, auditors []int, reAuditors []int) error {
- data := make([]models.CmApprover, 0)
- msg := make([]models.CmProjectMessage, 0)
- var content string
- var title string
- if dataType == 1 {
- safe := &models.CmSafe{Id: dataId}
- _, err := d.engine.Get(safe)
- if err != nil {
- return err
- }
- account := &models.CmProjectAccount{Id: safe.Uid}
- _, err = d.engine.Get(account)
- title = fmt.Sprintf("%s - %s", account.Name, account.Position)
- content = fmt.Sprintf("%s|%s|已上报,审批中", safe.Code, safe.InspectionDetail)
- } else {
- quality := &models.CmQuality{Id: dataId}
- _, err := d.engine.Get(quality)
- if err != nil {
- return err
- }
- account := &models.CmProjectAccount{Id: quality.Uid}
- _, err = d.engine.Get(account)
- if err != nil {
- return err
- }
- title = fmt.Sprintf("%s - %s", account.Name, account.Position)
- content = fmt.Sprintf("%s|%s|已上报,审批中", quality.Code, quality.InspectionDetail)
- }
- _, err := d.engine.Exec("delete from project_message where data_type = ? and data_id = ?", dataType, dataId)
- 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.CreateTime = time.Now()
- approverVM.Status = 0
- if i == 0 {
- approverVM.Status = 1
- }
- data = append(data, approverVM)
- // 消息推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item
- msgVM.BidsectionId = bid
- msgVM.ProjectId = pid
- msgVM.DataId = dataId
- msgVM.DataType = 1
- msgVM.Content = content
- msgVM.Title = title
- msgVM.CreateTime = time.Now()
- msg = append(msg, msgVM)
- }
- checkVM := models.CmApprover{}
- checkVM.AuditOrder = len(auditors) + 1
- checkVM.Progress = 1
- checkVM.BidsectionId = bid
- checkVM.ProjectId = pid
- checkVM.DataType = dataType
- checkVM.DataId = dataId
- 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.CreateTime = time.Now()
- data = append(data, approverVM)
- // 消息推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item
- msgVM.BidsectionId = bid
- msgVM.ProjectId = pid
- msgVM.DataId = dataId
- msgVM.DataType = 1
- msgVM.Content = content
- msgVM.Title = title
- msgVM.CreateTime = time.Now()
- msg = append(msg, msgVM)
- }
- // 再推送自己
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = cur_uid
- msgVM.BidsectionId = bid
- msgVM.ProjectId = pid
- msgVM.DataId = dataId
- msgVM.DataType = 1
- msgVM.Content = content
- msgVM.Title = title
- msgVM.CreateTime = time.Now()
- msg = append(msg, msgVM)
- _, err = d.engine.Insert(data)
- _, err = d.engine.Insert(msg)
- 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)
- newData := &models.CmApprover{Status: status}
- _, 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(newData)
- return err
- }
- // 根据当前times,获取审批流程(包括原报)
- func (d *ApproverDao) GetAuditorsWithOwner(bid int, dataType int, dataId int, cur_uid int) []viewmodels.Auditors {
- auditors := make([]viewmodels.Auditors, 0)
- auditor := viewmodels.Auditors{Progress: "", Id: "0", Status: 2}
- _, 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.audit_id = pa.id order by `audit_order`", bid, dataType, dataId).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 := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId, Status: 1}
- has, _ := d.engine.Get(data)
- approverVM := &viewmodels.Approver{}
- if has == true {
- id, _ := comm.AesEncrypt(strconv.Itoa(data.Id), conf.SignSecret)
- approverVM.Id = id
- pid, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
- approverVM.ProjectId = pid
- bid, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
- approverVM.BidsectionId = bid
- dataId, _ := comm.AesEncrypt(strconv.Itoa(data.DataId), conf.SignSecret)
- approverVM.DataId = dataId
- auditId, _ := comm.AesEncrypt(strconv.Itoa(data.AuditId), conf.SignSecret)
- approverVM.AuditId = auditId
- approverVM.AuditOrder = data.AuditOrder
- approverVM.DataType = data.DataType
- approverVM.Progress = strconv.Itoa(data.Progress)
- approverVM.Status = data.Status
- }
- return approverVM
- }
- // 获取最后一个审批人
- 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(auditType string, bid int, dataType int, dataId int, auditId int, times int, progress int, opinion string) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- if err != nil {
- return errors.New("操作失败-db")
- }
- // 消息推送,先删除旧的推送
- _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditType, dataId)
- if err != nil {
- session.Rollback()
- return err
- }
- approvers := make([]models.CmApprover, 0)
- err = session.Where("data_type = ? and data_id = ? and audit_id != 0", dataType, dataId).Find(&approvers)
- if err != nil {
- session.Rollback()
- return err
- }
- data := &models.CmApprover{Status: 0}
- _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ?", bid, dataType, dataId).Cols("status").Update(data)
- if err != nil {
- session.Rollback()
- return err
- }
- if auditType == "safe" {
- safe := &models.CmSafe{}
- _, err = session.ID(dataId).Get(safe)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: safe.Uid}
- _, err = d.engine.Get(account)
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", safe.Code, safe.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = safe.Uid
- msgVM.BidsectionId = safe.BidsectionId
- msgVM.ProjectId = safe.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = safe.Id
- msgVM.DataType = 1
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", safe.Code, safe.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- _, err = session.Exec("update cm_safe set status = ?, times = ? where id = ?", 0, times+1, dataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- safeAudit := &models.CmSafeAudit{BidsectionId: bid, SafeId: dataId, Times: times, AuditId: auditId, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
- _, err = session.Insert(safeAudit)
- if err != nil {
- session.Rollback()
- return err
- }
- // 说明本身除于整改人流程,退回原报,要将整改总数-1,未整改总数+1
- if progress-1 != 0 {
- _, err = session.Exec("update cm_tree set safe_rectification = safe_rectification + 1, safe_rectification_in = if(safe_rectification_in >= 1, safe_rectification_in - 1, 0) where bidsection_id = ?", bid)
- }
- } else {
- quality := &models.CmQuality{}
- _, err = session.ID(dataId).Get(quality)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: quality.Uid}
- _, err = d.engine.Get(account)
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", quality.Code, quality.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = quality.Uid
- msgVM.BidsectionId = quality.BidsectionId
- msgVM.ProjectId = quality.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = quality.Id
- msgVM.DataType = 1
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", quality.Code, quality.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- _, err = session.Exec("update cm_quality set status = ?, times = ? where id = ?", 0, times+1, dataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- qualityAudit := &models.CmQualityAudit{BidsectionId: bid, QualityId: dataId, Times: times, AuditId: auditId, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
- _, err = session.Insert(qualityAudit)
- if err != nil {
- session.Rollback()
- return err
- }
- if progress-1 != 0 {
- _, err = session.Exec("update cm_tree set quality_rectification = quality_rectification + 1, quality_rectification_in = if(quality_rectification_in >= 1, quality_rectification_in - 1, 0) where bidsection_id = ?", bid)
- }
- }
- err = session.Commit()
- return err
- }
- // 审批通过
- func (d *ApproverDao) PassHandler(auditType string, id int, uid int, auditId int, opinion string, content string, rectifiedTime string) error {
- session := d.engine.NewSession()
- defer session.Close()
- // add Begin() before any action
- err := session.Begin()
- auditor, err := d.FindApproverById(id)
- if err != nil {
- return err
- }
- if auditor.AuditId != uid {
- return errors.New("该用户没有审批权限")
- }
- var msgContent string
- switch auditor.Progress {
- case 0:
- msgContent = "审批完成,待整改"
- case 1:
- msgContent = "整改完成,待复查"
- case 2:
- msgContent = "复查完成"
- }
- // 消息推送,先删除旧的推送
- _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- approvers := make([]models.CmApprover, 0)
- err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
- if err != nil {
- session.Rollback()
- return err
- }
- // 安全巡检
- if auditType == "safe" {
- safe := &models.CmSafe{}
- _, err = session.ID(auditor.DataId).Get(safe)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: safe.Uid}
- _, err = d.engine.Get(account)
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, safe.Code, safe.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = safe.Uid
- msgVM.BidsectionId = safe.BidsectionId
- msgVM.ProjectId = safe.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = safe.Id
- msgVM.DataType = 1
- msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, safe.Code, safe.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- auditReacord := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, AuditId: auditor.AuditId, Times: safe.Times, CreateTime: time.Now(), Status: 0, Progress: auditor.Progress + 1, Opinion: opinion}
- _, err = session.Insert(auditReacord)
- if err != nil {
- session.Rollback()
- return err
- }
- // 改变审批流程中当前审批人以及下一个审批人的审批流程状态
- curData := &models.CmApprover{Status: 2}
- _, err = session.ID(id).Cols("status").Update(curData)
- if err != nil {
- session.Rollback()
- return err
- }
- nextData := &models.CmApprover{Status: 1}
- _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1).Cols("status").Update(nextData)
- if err != nil {
- session.Rollback()
- return err
- }
- if auditId != 0 {
- // 审批人选择了整改人
- // 修改cm_safe表的status状态为待整改
- _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 2, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 将cm_tree的safe_rectification待整改+1
- _, err = session.Exec("update cm_tree set safe_rectification_in = safe_rectification_in + 1, safe_rectification = if(safe_rectification >= 1, safe_rectification - 1, 0) where bidsection_id = ?", auditor.BidsectionId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 改变审批流程中的整改人id, 以及创建时间
- create_time := time.Now().Format(conf.SysTimeform)
- _, err = session.Exec("update `cm_approver` set `audit_id` = ? , `create_time` = ? where `bidsection_id` = ? and `project_id` = ? and `data_type` = ? and `data_id` = ? and `audit_order` = ? limit 1",
- auditId, create_time, auditor.BidsectionId, auditor.ProjectId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1)
- if err != nil {
- session.Rollback()
- return err
- }
- } else {
- if auditor.Progress == 1 {
- // 整改人审批流程
- // 修改cm_safe表的status状态为待复查
- _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 3, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- newRectifiedTime, _ := time.Parse(conf.SysTimeform, rectifiedTime)
- // 将整改单的状态->完成,并且填入整改日期
- rectification := &models.CmRectification{
- BidsectionId: safe.BidsectionId,
- ProjectId: safe.ProjectId,
- DataId: safe.Id,
- DataType: 1,
- AccountId: auditor.AuditId,
- RectifiedTime: newRectifiedTime,
- CreateTime: time.Now(),
- Content: content,
- Status: 1,
- UpdateTime: time.Now(),
- }
- _, err = session.Insert(rectification)
- if err != nil {
- session.Rollback()
- return err
- }
- } else {
- // 复查流程
- // 查找最后一个审批人
- lastAuditor := &models.CmApprover{BidsectionId: auditor.BidsectionId, DataType: auditor.DataType, DataId: auditor.DataId}
- _, err = session.Desc("audit_order").Limit(1).Get(lastAuditor)
- if err != nil {
- session.Rollback()
- return err
- }
- if lastAuditor.Id == auditor.Id {
- // 说明审批流程已经走完
- _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 4, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 将cm_tree的safe_rectification_finish待整改+1
- _, err := session.Exec("update cm_tree set safe_rectification_finish = safe_rectification_finish + 1, safe_rectification_in = if(safe_rectification_in >= 1, safe_rectification_in - 1, 0)where bidsection_id = ?", auditor.BidsectionId)
- if err != nil {
- session.Rollback()
- return err
- }
- }
- }
- }
- } else {
- // 质量巡检
- quality := &models.CmQuality{}
- _, err = session.ID(auditor.DataId).Get(quality)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: quality.Uid}
- _, err = d.engine.Get(account)
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, quality.Code, quality.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = quality.Uid
- msgVM.BidsectionId = quality.BidsectionId
- msgVM.ProjectId = quality.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = quality.Id
- msgVM.DataType = 1
- msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, quality.Code, quality.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- auditReacord := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, AuditId: auditor.AuditId, Times: quality.Times, CreateTime: time.Now(), Status: 0, Progress: auditor.Progress + 1, Opinion: opinion}
- _, err = session.Insert(auditReacord)
- if err != nil {
- session.Rollback()
- return err
- }
- // 改变审批流程中当前审批人以及下一个审批人的审批流程状态
- curData := &models.CmApprover{Status: 2}
- _, err = session.ID(id).Cols("status").Update(curData)
- if err != nil {
- session.Rollback()
- return err
- }
- nextData := &models.CmApprover{Status: 1}
- _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1).Cols("status").Update(nextData)
- if err != nil {
- session.Rollback()
- return err
- }
- if auditId != 0 {
- // 审批人选择了整改人
- // 修改cm_quality表的status状态为待整改
- _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 2, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 将cm_tree的quality_rectification待整改+1
- _, err = session.Exec("update cm_tree set quality_rectification_in = quality_rectification_in + 1, quality_rectification = if(quality_rectification >= 1, quality_rectification - 1, 0) where bidsection_id = ?", auditor.BidsectionId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 改变审批流程中的整改人id
- create_time := time.Now().Format(conf.SysTimeform)
- _, err = session.Exec("update `cm_approver` set `audit_id` = ?, `create_time` = ? where `bidsection_id` = ? and `project_id` = ? and `data_type` = ? and `data_id` = ? and `audit_order` = ? limit 1", auditId, create_time, auditor.BidsectionId, auditor.ProjectId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1)
- if err != nil {
- session.Rollback()
- return err
- }
- } else {
- if auditor.Progress == 1 {
- // 整改人审批流程
- // 修改cm_quality表的status状态为待复查
- _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 3, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 将整改单的状态->完成,并且填入整改日期
- newRectifiedTime, err := time.Parse(conf.SysTimeform, rectifiedTime)
- if err != nil {
- session.Rollback()
- return err
- }
- rectification := &models.CmRectification{
- BidsectionId: quality.BidsectionId,
- ProjectId: quality.ProjectId,
- DataId: quality.Id,
- DataType: 2,
- AccountId: auditor.AuditId,
- CreateTime: time.Now(),
- RectifiedTime: newRectifiedTime,
- Content: content,
- Status: 1,
- UpdateTime: time.Now(),
- }
- _, err = session.Insert(rectification)
- if err != nil {
- session.Rollback()
- return err
- }
- } else {
- // 复查流程
- // 查找最后一个审批人
- lastAuditor := &models.CmApprover{BidsectionId: auditor.BidsectionId, DataType: auditor.DataType, DataId: auditor.DataId}
- _, err = session.Desc("audit_order").Limit(1).Get(lastAuditor)
- if err != nil {
- session.Rollback()
- return err
- }
- if lastAuditor.Id == auditor.Id {
- // 说明审批流程已经走完
- _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 4, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- // 将cm_tree的quality_rectification_finish待整改+1
- _, err := session.Exec("update cm_tree set quality_rectification_finish = quality_rectification_finish + 1, quality_rectification_in = if(quality_rectification_in >= 1, quality_rectification_in - 1, 0) where bidsection_id = ?", auditor.BidsectionId)
- if err != nil {
- session.Rollback()
- return err
- }
- }
- }
- }
- }
- err = session.Commit()
- return err
- }
- // 审批流程-退回
- func (d *ApproverDao) BackHandlerWithId(auditType string, id int, uid int, times int, progress int, opinion string) error {
- session := d.engine.NewSession()
- defer session.Close()
- err := session.Begin()
- auditor := &models.CmApprover{}
- _, err = session.ID(id).Get(auditor)
- // 更改safe/quality表的记录
- data := &models.CmApprover{Status: 0}
- // 将往后的所有记录的status改为0
- _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order > ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder).Cols("status").Update(data)
- if err != nil {
- session.Rollback()
- return err
- }
- // 获取改账号的信息
- account := &models.CmProjectAccount{Id: auditor.AuditId}
- _, err = session.Get(account)
- if err != nil {
- session.Rollback()
- return err
- }
- // 将当前的记录改为1-待审批
- data.Status = 1
- _, err = session.ID(id).Cols("status").Update(data)
- if err != nil {
- session.Rollback()
- return err
- }
- // 消息推送,先删除旧的推送
- _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- approvers := make([]models.CmApprover, 0)
- err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- if auditType == "safe" {
- safe := &models.CmSafe{}
- _, err = session.ID(auditor.DataId).Get(safe)
- if err != nil {
- session.Rollback()
- return err
- }
- safe.Status = auditor.Progress + 1
- _, err = session.Where("id = ? and bidsection_id = ?", safe.Id, safe.BidsectionId).Limit(1).Update(safe)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: safe.Uid}
- _, err = d.engine.Get(account)
- if err != nil {
- session.Rollback()
- return err
- }
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", safe.Code, safe.InspectionDetail, account.Name)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = safe.Uid
- msgVM.BidsectionId = safe.BidsectionId
- msgVM.ProjectId = safe.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = safe.Id
- msgVM.DataType = 1
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", safe.Code, safe.InspectionDetail, account.Name)
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- safeAudit := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, Times: times, AuditId: uid, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
- _, err = session.Insert(safeAudit)
- if err != nil {
- session.Rollback()
- return err
- }
- } else {
- quality := &models.CmQuality{}
- _, err = session.ID(auditor.DataId).Get(quality)
- if err != nil {
- session.Rollback()
- return err
- }
- quality.Status = auditor.Progress + 1
- _, err = session.Where("id =? and bidsection_id = ?", quality.Id, quality.BidsectionId).Limit(1).Update(quality)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: quality.Uid}
- _, err = d.engine.Get(account)
- if err != nil {
- session.Rollback()
- return err
- }
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", quality.Code, quality.InspectionDetail, account.Name)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = quality.Uid
- msgVM.BidsectionId = quality.BidsectionId
- msgVM.ProjectId = quality.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = quality.Id
- msgVM.DataType = 2
- msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", quality.Code, quality.InspectionDetail, account.Name)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- qualityAudit := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, Times: times, AuditId: uid, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
- _, err = session.Insert(qualityAudit)
- if err != nil {
- session.Rollback()
- return err
- }
- }
- curAuditor := &models.CmApprover{}
- _, err = session.ID(uid).Get(curAuditor)
- if err != nil {
- session.Rollback()
- return err
- }
- // 当前审批人是整改或者复查流程的并且退回的是审批流程的
- if curAuditor.Progress >= 1 && auditor.Progress == 0 {
- var sql string
- if auditType == "safe" {
- sql = "update cm_tree set safe_rectification = safe_rectification_in + 1 and safe_rectification_in = if(safe_rectification_in >= 1, safe_rectification_in - 1, 0) where bidsection_id = ?"
- } else {
- sql = "update cm_tree set quality_rectification = quality_rectification_in + 1 and quality_rectification_in = if(quality_rectification_in >= 1, quality_rectification_in - 1, 0) where bidsection_id = ?"
- }
- _, err = session.Exec(sql, auditor.BidsectionId)
- if err != nil {
- session.Rollback()
- return err
- }
- }
- err = session.Commit()
- return err
- }
- func (d *ApproverDao) CloseHandler(auditType string, id int, opinion string, curUid int, saveId int) error {
- session := d.engine.NewSession()
- defer session.Close()
- // add Begin() before any action
- err := session.Begin()
- auditor, err := d.FindApproverById(id)
- if auditor.AuditId != curUid {
- return errors.New("该用户没有审批权限!")
- }
- // 消息推送,先删除旧的推送
- _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
- if err != nil {
- session.Rollback()
- return err
- }
- approvers := make([]models.CmApprover, 0)
- err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
- if err != nil {
- session.Rollback()
- return err
- }
- if auditType == "safe" {
- safe := &models.CmSafe{}
- _, err = session.ID(saveId).Get(safe)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: safe.Uid}
- _, err = d.engine.Get(account)
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", safe.Code, safe.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = safe.Uid
- msgVM.BidsectionId = safe.BidsectionId
- msgVM.ProjectId = safe.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = safe.Id
- msgVM.DataType = 1
- msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", safe.Code, safe.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- safeAudit := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, Times: safe.Times, AuditId: auditor.AuditId, Status: 2, Progress: auditor.Progress, CreateTime: time.Now(), Opinion: opinion}
- _, err = session.Insert(safeAudit)
- if err != nil {
- session.Rollback()
- return err
- }
- // 更改cm_safe的记录
- _, err = session.Exec("update `cm_safe` set status = ? where id = ? ", 5, saveId)
- if err != nil {
- session.Rollback()
- return err
- }
- } else {
- quality := &models.CmQuality{}
- _, err = session.ID(saveId).Get(quality)
- if err != nil {
- session.Rollback()
- return err
- }
- account := &models.CmProjectAccount{Id: quality.Uid}
- _, err = d.engine.Get(account)
- title := fmt.Sprintf("%s - %s", account.Name, account.Position)
- msg := make([]models.CmProjectMessage, 0)
- for _, item := range approvers {
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = item.AuditId
- msgVM.BidsectionId = item.BidsectionId
- msgVM.ProjectId = item.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = item.DataId
- msgVM.DataType = item.DataType
- msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", quality.Code, quality.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- }
- // 添加巡检拥有者的推送
- msgVM := models.CmProjectMessage{}
- msgVM.AccountId = quality.Uid
- msgVM.BidsectionId = quality.BidsectionId
- msgVM.ProjectId = quality.ProjectId
- msgVM.CreateTime = time.Now()
- msgVM.DataId = quality.Id
- msgVM.DataType = 2
- msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", quality.Code, quality.InspectionDetail)
- msgVM.Title = title
- msg = append(msg, msgVM)
- // 插入消息
- _, err = session.Insert(msg)
- if err != nil {
- session.Rollback()
- return err
- }
- // 增加审批日志
- qualityAudit := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, Times: quality.Times, AuditId: auditor.AuditId, Status: 2, Progress: auditor.Progress, CreateTime: time.Now(), Opinion: opinion}
- _, err = session.Insert(qualityAudit)
- if err != nil {
- session.Rollback()
- return err
- }
- // 更改cm_quality的记录
- _, err = session.Exec("update `cm_quality` set status = ? where id = ? ", 5, saveId)
- if err != nil {
- session.Rollback()
- return err
- }
- }
- // 更改cm_approver的记录
- _, err = session.Exec("update `cm_approver` set status = ? where id = ? ", 3, id)
- if err != nil {
- session.Rollback()
- return err
- }
- err = session.Commit()
- 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
- }
- func (d *ApproverDao) GetStatusByProjectAndAccount(projectId int, projectAccountId int, status int) []models.CmApprover {
- datalist := make([]models.CmApprover, 0)
- _ = d.engine.
- Where("project_id = ? and audit_id= ? and status=? ", projectId, projectAccountId, status).
- Desc("id").
- Find(&datalist)
- return datalist
- }
- func (d *ApproverDao) GetDataIdByProgress(dataId int, dataType int, progress int) *models.CmApprover {
- data := &models.CmApprover{}
- _, err := d.engine.
- Where(" data_id=? and data_type=? and progress=? ", dataId, dataType, progress).
- Get(data)
- if err != nil {
- data.Id = 0
- return data
- }
- return data
- }
|