audit_flow_process.go 9.0 KB
package models

import (
	"fmt"
	"opp/internal/utils"
	"time"

	"github.com/astaxie/beego/orm"
)

type AuditFlowProcess struct {
	Id                int64     `orm:"column(id);pk" description:"唯一标识"`
	ChanceId          int64     `orm:"column(chance_id)" description:"实例id,关联chance表id"`
	Uid               int64     `orm:"column(uid)" description:"用户id(审批人)"`
	Level             int       `orm:"column(level)" description:"审批步骤"`
	IsActive          int8      `orm:"column(is_active)" description:"是否激活"`
	ApproveTime       time.Time `orm:"column(approve_time);type(timestamp)" description:"审批时间"`
	BasicScore        float64   `orm:"column(basic_score);null;digits(4);decimals(1)" description:"基础评分"`
	ExtraScore        float64   `orm:"column(extra_score);null;digits(4);decimals(1)" description:"附加评分"`
	ValueScore        float64   `orm:"column(value_score);null;digits(4);decimals(1)" description:"价值评分"`
	DiscoveryScore    float64   `orm:"column(discovery_score);null;digits(4);decimals(1)" description:"发现得分(发现得分=基础评分*系数 + 附加评分*系数 + 价值评分*系数)"`
	ReviewStatus      int8      `orm:"column(review_status)" description:"审核状态 0:审核中 1:被退回 2:已通过 3.自动通过 "`
	AuditFlowType     int       `orm:"column(audit_flow_type)" description:"审核流类型 1.部门长 2.指定成员 3.指定角色 4.特殊审核人"`
	FlowType          int       `orm:"column(flow_type)" description:"审批类型 1:正常审核  2:特殊审核"`
	ActionType        int       `orm:"column(action_type)" description:"审批执行方式【1:or】【2:and】"`
	CreateAt          time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
	UpdateAt          time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
	EnableStatus      int8      `orm:"column(enable_status)" description:"有效状态 0:无效 1:有效 (被驳回以后,未完成的审核置为无效)"`
	ApproveData       string    `orm:"column(approve_data);size(500)" description:"审核数据 json{公开状态 公开对象 分数}"`
	UserName          string    `orm:"column(user_name);size(50)" description:"用户名-冗余"`
	RoleName          string    `orm:"column(role_name);size(50)" description:"角色名-冗余"`
	RoleId            int       `orm:"column(role_id);size(50)" description:"角色id-冗余"`
	ApproveMessage    string    `orm:"column(approve_message);size(50)" description:"审核消息-冗余"`
	TemplateId        int       `orm:"column(template_id);size(50)" description:"模板编号-冗余"`
	SelfChecks        string    `orm:"column(self_checks);null" description:"自查内容"`
	SubmitCheckTime   time.Time `orm:"column(submit_check_time);type(timestamp)" description:"提交筛选时间"`
	SubmitCheckStatus int       `orm:"column(submit_check_status)" description:"提交筛选状态 0:未提交 1:已提交"`
	Reason            string    `orm:"column(reason);null" description:"理由(退回)"`
}

func (t *AuditFlowProcess) TableName() string {
	return "audit_flow_process"
}

func init() {
	orm.RegisterModel(new(AuditFlowProcess))
}

// AddAuditFlowProcess insert a new AuditFlowProcess into database and returns
// last inserted Id on success.
func AddAuditFlowProcess(m *AuditFlowProcess) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetAuditFlowProcessById retrieves AuditFlowProcess by Id. Returns error if
// Id doesn't exist
func GetAuditFlowProcessById(id int64) (v *AuditFlowProcess, err error) {
	o := orm.NewOrm()
	v = &AuditFlowProcess{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateAuditFlowProcess updates AuditFlowProcess by Id and returns error if
// the record to be updated doesn't exist
func UpdateAuditFlowProcessById(m *AuditFlowProcess) (err error) {
	o := orm.NewOrm()
	v := AuditFlowProcess{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteAuditFlowProcess deletes AuditFlowProcess by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAuditFlowProcess(id int64) (err error) {
	o := orm.NewOrm()
	v := AuditFlowProcess{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&AuditFlowProcess{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

//审核流程编号 按用户编号
//获取审核流节点
func GetAuditFlowProcessBy(processId, uid int64) (v *AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_flow_process where id=? and uid=? and enable_status=1"
	if err = o.Raw(sql, processId, uid).QueryRow(&v); err == nil {
		return v, nil
	}
	return nil, err
}

//当前审批批次已经结束 唤醒下一批次审批人
func UpdatetAuditFlowProcessToNext(o orm.Ormer, chanceId int64, level int, uids []int64) (err error) {
	sql := "update audit_flow_process set enable_status =1,is_active=1,update_at=now()  where chance_id=? and level=? and uid in (?)"
	if err = utils.ExecuteSQLWithOrmer(o, sql, chanceId, level, utils.JoinInt64s(uids, ",")); err != nil {
		return
	}
	return
}

//或签 有一人已经通过 同批次在审核状态置为无效
func UpdatetAuditFlowProcessNoApprove(o orm.Ormer, chanceId int64, level int, reviewStatus int) (err error) {
	sql := "update audit_flow_process set enable_status =0,is_active=0,update_at=now() where chance_id=? and level=? and review_status=?"
	if err = utils.ExecuteSQLWithOrmer(o, sql, chanceId, level, reviewStatus); err != nil {
		return
	}
	return
}

//获取机会审核流信息
func GetAuditFlowProcessList(chanceId int64) (v []*AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := `select id ,chance_id,uid,level,is_active,IFNULL(approve_time,NOW()) approve_time,basic_score,extra_score,value_score,discovery_score,
review_status,audit_flow_type,flow_type,action_type,update_at,create_at,enable_status,approve_data,user_name,role_name,role_id,approve_message,
template_id,self_checks,reason from audit_flow_process 
where chance_id =? and enable_status=1 order by approve_time,level desc`
	if _, err = o.Raw(sql, chanceId).QueryRows(&v); err == nil {
		return v, nil
	}
	return nil, err
}

//审核人 获取机会 有效的待审核节点
func GetAuditFlowProcessListByAuditUser(chanceId int64, auditUserId int64) (v []*AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := "select id,review_status from audit_flow_process where chance_id=? and enable_status=1 and uid=?"
	if _, err = o.Raw(sql, chanceId, auditUserId).QueryRows(&v); err == nil {
		return v, nil
	}
	return nil, err
}

//删除机会时关闭所有审核信息
func CloseAuditFlowProcess(orm orm.Ormer, chanceId int64) (err error) {
	sql := "update audit_flow_process set enable_status =0,is_active=0,update_at=now() where chance_id=?"
	if err = utils.ExecuteSQLWithOrmer(orm, sql, chanceId); err != nil {
		return
	}
	return
}

//获取当前审批节点 是否已经有人审核通过的
func GetAuditFlowProcessApproved(chanceId int64, level int) (v *[]AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := "select id from audit_flow_process  where chance_id=? and level=? and is_active=1 and review_status<>1 and enable_status=1"
	if _, err = o.Raw(sql, chanceId, level).QueryRows(&v); err != nil {
		return
	}
	return
}

//按审核状态获取审核流程
func GetAuditFlowProcessByReview(chanceId int64, level int, reviewStatus int) (v *AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_flow_process  where chance_id=? and level=? and is_active=1 and review_status=? and enable_status=1"
	if err = utils.ExecuteQueryOneWithOrmer(o, &v, sql, chanceId, level, reviewStatus); err != nil {
		return
	}
	return
}

//更新提交
func UpdatetAuditFlowProcessToSubmit(o orm.Ormer, chanceId int64, level int, reviewStatus int, userId int64) (err error) {
	sql := "update audit_flow_process set enable_status =1,is_active=0,update_at=now(),review_status=?  where chance_id=? and uid=? and level=? and review_status=0 and enable_status=1"
	if err = utils.ExecuteSQLWithOrmer(o, sql, reviewStatus, chanceId, userId, level); err != nil {
		return
	}
	return
}

//获取审核人最新的审核节点
func GetAuditorLatestAuditFlowProcess(chanceId int64, userId int64) (v *AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_flow_process  where chance_id=? and level>0 and uid=? order by id desc limit 1"
	if err = utils.ExecuteQueryOneWithOrmer(o, &v, sql, chanceId, userId); err != nil {
		return
	}
	return
}

//获取机会所有审核人
func GetChanceAllAuditors(chanceId int64) (v []*AuditFlowProcess, err error) {
	o := orm.NewOrm()
	sql := "select MAX(id) AS id,uid from audit_flow_process where chance_id=? and `level`>0  group by chance_id,uid"
	if err = utils.ExecuteQueryAllWithOrmer(o, &v, sql, chanceId); err != nil {
		return
	}
	return
}