作者 yangfu

增加审核日志

package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type AuditFlowLog struct {
Id int `orm:"column(id);auto" description:"唯一编号"`
ChanceId int64 `orm:"column(chance_id)" description:"表chance.id 所属机会编号"`
Content string `orm:"column(content);size(500)" description:"内容 json"`
ApproveUserId int64 `orm:"column(approve_user_id)" description:"审核人用户编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now" description:"创建时间"`
Code int `orm:"column(code)" description:"消息编码"`
}
func (t *AuditFlowLog) TableName() string {
return "audit_flow_log"
}
func init() {
orm.RegisterModel(new(AuditFlowLog))
}
// AddAuditFlowLog insert a new AuditFlowLog into database and returns
// last inserted Id on success.
func AddAuditFlowLog(m *AuditFlowLog) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetAuditFlowLogById retrieves AuditFlowLog by Id. Returns error if
// Id doesn't exist
func GetAuditFlowLogById(id int) (v *AuditFlowLog, err error) {
o := orm.NewOrm()
v = &AuditFlowLog{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateAuditFlowLog updates AuditFlowLog by Id and returns error if
// the record to be updated doesn't exist
func UpdateAuditFlowLogById(m *AuditFlowLog) (err error) {
o := orm.NewOrm()
v := AuditFlowLog{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
}
// DeleteAuditFlowLog deletes AuditFlowLog by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAuditFlowLog(id int) (err error) {
o := orm.NewOrm()
v := AuditFlowLog{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&AuditFlowLog{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
... ... @@ -131,10 +131,9 @@ func GetAuditFlowProcessList(chanceId int64) (v []*AuditFlowProcess, err error)
}
//删除机会时关闭所有审核信息
func CloseAuditFlowProcess(chanceId int64) (err error) {
o := orm.NewOrm()
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(o, sql, chanceId); err != nil {
if err = utils.ExecuteSQLWithOrmer(orm, sql, chanceId); err != nil {
return
}
return
... ... @@ -154,7 +153,7 @@ func GetAuditFlowProcessApproved(chanceId int64, level int) (v *[]AuditFlowProce
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 = o.Raw(sql, chanceId, level, reviewStatus).QueryRow(&v); err != nil {
if err = utils.ExecuteQueryOneWithOrmer(o, &v, sql, chanceId, level, reviewStatus); err != nil {
return
}
return
... ...
... ... @@ -32,6 +32,7 @@ type Chance struct {
PublishStatus int `orm:"column(publish_status)" description:"公开状态 0未公开、1部门公开、2公司公开"`
ApproveData string `orm:"column(approve_data)" description:"审核数据 冗余"`
AuditLevel int `orm:"column(audit_level)" description:"当前审批步骤"`
ApproveTime time.Time `orm:"column(approve_time);type(timestamp)" description:"审批时间"`
}
func (t *Chance) TableName() string {
... ... @@ -103,7 +104,7 @@ func DeleteChance(id int64) (err error) {
func GetChanceMyChance(uid, cid int64, reviewStatus []int8, lastId int64, pageSize int, v interface{}) (total int, err error) {
sql := `select a.*,b.images,speechs,videos
from (
select id,user_id,create_at,source_content,approve_data,review_status from chance
select id,user_id,create_at,update_at,approve_time chance_approve_time,source_content,approve_data,review_status from chance
where user_id=? and company_id=? and review_status in (?) and (?=0 or id<?)
order by create_at desc
limit ?
... ... @@ -126,7 +127,7 @@ where user_id=? and company_id=? and review_status in (%v)
func GetChanceMyApproveChance(uid, cid int64, reviewStatus []int8, lastId int64, pageSize int, v interface{}) (total int, err error) {
sql := fmt.Sprintf(`select a.*,b.images,b.speechs,b.videos from (
select a.*,b.user_id,b.source_content,b.enable_status,b.review_status from (
select a.*,b.user_id,b.source_content,b.enable_status,b.review_status,b.create_at,b.update_at,b.approve_time chance_approve_time from (
select id,approve_time,approve_data,uid,chance_id,approve_message,create_at process_create_time
from audit_flow_process where uid=? and enable_status =1 and review_status in (%v) and (?=0 or id<?)
)a left outer join chance b on a.chance_id = b.id
... ...
... ... @@ -184,6 +184,8 @@ type ChanceItemOrm struct {
Id int64 `orm:"column(id)"`
Uid int64 `orm:"column(user_id)"`
CreateTime time.Time `orm:"column(create_at)"`
UpdateTime time.Time `orm:"column(update_at)"`
ApproveTime time.Time `orm:"column(approve_time)"`
SourceContent string `orm:"column(source_content)"`
Images string `orm:"column(images)"`
Voices string `orm:"column(speechs)"`
... ...
... ... @@ -43,7 +43,7 @@ var errmessge ErrorMap = map[int]string{
5204: "评分或者公开状态不能为空",
5205: "机会未审核通过,不能修改评分或者公开状态",
5206: "未找到审批节点或者无权限",
//5207:"部门不存在",
5207: "公司管理员未设置",
//模板相关
5301: "机会模板不存在",
... ...
... ... @@ -152,6 +152,26 @@ func logMsg(msg *models.UserMsg, name string) {
msg.MsgType, msg.ReceiveUserId, name, msg.Message, msg.SourceId, msg.SourceType))
}
func SaveApproveLog(orm orm.Ormer, code int, userId int64, param ...interface{}) (err error) {
//保查审核日志
func SaveApproveLog(orm orm.Ormer, code int, userId int64, chanceId int64, param ...interface{}) (err error) {
var (
message string
ok bool
)
if message, ok = protocol.ApproveLog[code]; !ok {
err = fmt.Errorf("approve log not exists code:%v", code)
return
}
message = fmt.Sprintf(message, param...)
if _, err = orm.Insert(&models.AuditFlowLog{
ChanceId: chanceId,
Content: message,
ApproveUserId: userId,
CreateAt: time.Now(),
Code: code,
}); err != nil {
log.Error(err)
}
log.Debug(fmt.Sprintf("保存审核日志: code:%v userId:%v chanceId:%v message:%v", code, userId, chanceId, message))
return
}
... ...
... ... @@ -15,6 +15,7 @@ import (
"opp/models"
"opp/protocol"
"opp/services/agg"
"strings"
"time"
)
... ... @@ -320,6 +321,8 @@ func ChanceApprove(header *protocol.RequestHeader, request *protocol.ChanceAppro
{
mapProcess["ReviewStatus"] = int8(request.ReviewStatus)
mapProcess["ApproveTime"] = time.Now()
mapProcess["IsActive"] = int8(0)
mapChance["ApproveTime"] = time.Now()
if request.ReviewStatus == protocol.ReviewStatusPass {
mapProcess["BasicScore"] = request.ApproveData.Score.BasicScore
mapProcess["ExtraScore"] = request.ApproveData.Score.ExtraScore
... ... @@ -408,6 +411,11 @@ func ChanceApprove(header *protocol.RequestHeader, request *protocol.ChanceAppro
}
}
}
if err = saveApproveMsgByApproveData(orm, request.ReviewStatus, chance, request.ApproveData); err != nil {
orm.Rollback()
log.Error("发送审核日志失败", err)
return
}
if err = utils.UpdateTableByMapWithOrmer(orm, process, mapProcess); err != nil {
log.Error("更新机会审核流程 状态失败 process_id:", process.Id, err)
orm.Rollback()
... ... @@ -465,6 +473,40 @@ func ProcessIntegrate(header *protocol.RequestHeader, request *ProcessIntegrateR
return
}
//发送审核日志
func saveApproveMsgByApproveData(orm orm.Ormer, reviewStatus int, chance *models.Chance, approveData protocol.ApproveData) (err error) {
var (
parames = make([]interface{}, 0)
code = 0
)
if reviewStatus == protocol.ReviewStatusReturn {
//发送退回日志
code = 3
}
if reviewStatus == protocol.ReviewStatusPass {
//发送通过日志
score := approveData.Score
parames = append(parames, []interface{}{score.BasicScore, score.ExtraScore, score.ValueScore}...)
if approveData.PublicData.PublishStatus == protocol.PublicToCompany {
code = 4
}
if approveData.PublicData.PublishStatus == protocol.PublicToDepartment {
code = 5
var department []string
for i := range approveData.PublicData.VisibleObjects {
v := approveData.PublicData.VisibleObjects[i]
department = append(department, v.Name)
}
parames = append(parames, strings.Join(department, ","))
}
}
if err = agg.SaveApproveLog(orm, code, chance.UserId, chance.Id, parames...); err != nil {
log.Error(err)
return
}
return
}
//系统审核状态转为审核类型 reviewStatus - > approveType
func ConverReviewStatus(reviewStatus int) (approveType int) {
if reviewStatus == protocol.ReviewStatusPass {
... ...
... ... @@ -12,6 +12,7 @@ import (
"opp/models"
"opp/protocol"
"opp/services/agg"
"strings"
"time"
)
... ... @@ -403,6 +404,12 @@ func ChanceSubmit(header *protocol.RequestHeader, request *protocol.ChanceSubmit
return
}
}
//发送提交日志
if err = agg.SaveApproveLog(orm, 1, header.UserId, chance.Id); err != nil {
log.Error(err)
orm.Rollback()
return
}
orm.Commit()
rsp = &protocol.ChanceSubmitResponse{}
... ... @@ -449,10 +456,13 @@ func ChanceUpdate(header *protocol.RequestHeader, request *protocol.ChanceUpdate
if _, e := models.GetAuditFlowProcessByReview(request.Id, 0, protocol.ReviewStatusWait); e == nil {
request.IsPublish = true
updateMap["DepartmentId"] = request.RelatedDepartment
//log.Info(fmt.Sprintf("机会编辑 is_publish:%v chance.review_status:%v 是否是本人:%v",request.IsPublish,chance.ReviewStatus,chance.UserId==header.UserId))
} else {
log.Info(fmt.Sprintf("机会编辑 chance_id:%v chance.review_status:%v 无待处理 审核数据", request.Id, chance.ReviewStatus))
}
} else {
request.IsPublish = false
log.Info(fmt.Sprintf("机会编辑 is_publish:%v chance.review_status:%v 是否是本人:%v", request.IsPublish, chance.ReviewStatus, chance.UserId == header.UserId))
}
//TODO:非本人 1.需要验证角色权限 2是否是审核人 3.是否是本人 (目前本人才可以审核)
if chance.UserId != header.UserId {
... ... @@ -561,6 +571,12 @@ func ChanceUpdate(header *protocol.RequestHeader, request *protocol.ChanceUpdate
}
}
}
//发送提交日志
if err = agg.SaveApproveLog(orm, 1, chance.UserId, chance.Id); err != nil {
log.Error(err)
orm.Rollback()
return
}
}
}
orm.Commit()
... ... @@ -621,12 +637,43 @@ func ChanceChangePublish(header *protocol.RequestHeader, request *protocol.Chanc
}
}
}
if err = saveApproveMsgChangePublic(orm, chance, request.PublicData); err != nil {
log.Error(err)
orm.Rollback()
return
}
orm.Commit()
//TODO:添加log
rsp = &protocol.ChanceChangePublishResponse{}
return
}
//发送审核日志
func saveApproveMsgChangePublic(orm orm.Ormer, chance *models.Chance, approveData protocol.PublicData) (err error) {
var (
parames = make([]interface{}, 0)
code = 0
)
if approveData.PublishStatus == protocol.PublicToCompany {
code = 7
}
if approveData.PublishStatus == protocol.PublicToDepartment {
code = 8
var department []string
for i := range approveData.VisibleObjects {
v := approveData.VisibleObjects[i]
department = append(department, v.Name)
}
parames = append(parames, strings.Join(department, ","))
}
if err = agg.SaveApproveLog(orm, code, chance.UserId, chance.Id, parames...); err != nil {
log.Error(err)
return
}
return
}
//修改评分
func ChanceChangeScore(header *protocol.RequestHeader, request *protocol.ChanceChangeScoreRequest) (rsp *protocol.ChanceChangeScoreResponse, err error) {
var (
... ... @@ -668,6 +715,17 @@ func ChanceChangeScore(header *protocol.RequestHeader, request *protocol.ChanceC
orm.Rollback()
return
}
//修改评分日志
if err = agg.SaveApproveLog(orm, 9, chance.UserId, chance.Id, request.Score.BasicScore, request.Score.ExtraScore, request.Score.ValueScore); err != nil {
log.Error(err)
return
}
//if err = agg.SaveApproveLog(orm,9,chance.UserId,chance.Id,request.Score.BasicScore,request.Score.ExtraScore,request.Score.ValueScore);err!=nil{
// log.Error(err)
// return
//}
orm.Commit()
rsp = &protocol.ChanceChangeScoreResponse{
DiscoveryScore: result.DiscoveryScore,
... ... @@ -844,11 +902,13 @@ func GenAuditFlowProcess(header *protocol.RequestHeader, chanceId int64, related
if company.AdminId == 0 {
err = fmt.Errorf("GenAuditFlowProcess:company.admin is not set")
err = protocol.NewErrWithMessage(5207)
return
}
if admin, err = models.GetUserCompanyByUserId(company.AdminId, header.CompanyId); err != nil {
err = fmt.Errorf("GenAuditFlowProcess:company.admin is not set")
err = protocol.NewErrWithMessage(5207)
return
}
... ... @@ -942,7 +1002,8 @@ func resolveActionType(t uint) string {
//获取部门长用户列表
func getDepartmentors(header *protocol.RequestHeader, relatedDeparmentId int64) (ids []int64, err error) {
var (
departments *models.Department
departments *models.Department
lastDepartmentId int
)
//if err = models.GetUserDepartments(header.UserId, header.CompanyId, &departments); err != nil {
// log.Error(header.UserId,header.CompanyId,err)
... ... @@ -952,6 +1013,7 @@ func getDepartmentors(header *protocol.RequestHeader, relatedDeparmentId int64)
log.Error(relatedDeparmentId, err)
return
}
//部门长
for {
if len(departments.Managers) > 0 {
var tmpIds []int64
... ... @@ -966,7 +1028,9 @@ func getDepartmentors(header *protocol.RequestHeader, relatedDeparmentId int64)
//break
}
if departments.ParentId != 0 {
if departments, err = models.GetDepartmentById(int(relatedDeparmentId)); err != nil {
//relatedDeparmentId = int64departments.ParentId
lastDepartmentId = departments.ParentId
if departments, err = models.GetDepartmentById(int(departments.ParentId)); err != nil {
log.Error(relatedDeparmentId, err)
err = nil
break
... ... @@ -979,7 +1043,7 @@ func getDepartmentors(header *protocol.RequestHeader, relatedDeparmentId int64)
if len(ids) == 0 {
ids = append(ids, 0)
}
log.Info(fmt.Sprintf("生成机会审批流-部门:department_id:%v managers:%v ids:%v", relatedDeparmentId, departments.Managers, ids))
log.Info(fmt.Sprintf("生成机会审批流-部门:department_id:%v managers:%v ids:%v last_department_id:%v", relatedDeparmentId, departments.Managers, ids, lastDepartmentId))
return
}
... ... @@ -1080,9 +1144,11 @@ func MySubmitChance(header *protocol.RequestHeader, request *protocol.MySubmitCh
commItem := protocol.CommonListItem{}
{
item := protocol.ChanceItem{
Id: chance.Id,
Provider: provider,
CreateTime: chance.CreateTime.Unix() * 1000,
Id: chance.Id,
Provider: provider,
CreateTime: chance.CreateTime.Unix() * 1000,
UpdateTime: chance.UpdateTime.Unix() * 1000,
ApproveTime: chance.ApproveTime.Unix() * 1000,
}
jsonUnmarshal(chance.SourceContent, &item.FormList)
item.FormList = clearEmptyForm(item.FormList)
... ... @@ -1638,15 +1704,26 @@ func ChanceDelete(header *protocol.RequestHeader, request *protocol.ChanceDelete
err = protocol.NewErrWithMessage(5202)
return
}
if err = utils.UpdateTableByMap(chance, map[string]interface{}{
orm := orm.NewOrm()
orm.Begin()
//发送提交日志
if err = agg.SaveApproveLog(orm, 2, chance.UserId, chance.Id); err != nil {
log.Error(err)
orm.Rollback()
return
}
if err = utils.UpdateTableByMapWithOrmer(orm, chance, map[string]interface{}{
"EnableStatus": int8(0),
}); err != nil {
log.Error(err)
orm.Rollback()
return
}
if err = models.CloseAuditFlowProcess(chance.Id); err != nil {
if err = models.CloseAuditFlowProcess(orm, chance.Id); err != nil {
log.Error(err)
orm.Rollback()
return
}
orm.Commit()
return
}
... ...