123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /*
- * @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
- }
|