作者 yangfu

批量插入自查数据

... ... @@ -17,8 +17,10 @@ type ChanceSelfCheck struct {
Answer string `orm:"column(answer);size(50);null" description:"回答"`
Reason string `orm:"column(reason);size(200);null" description:"理由"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
Enable int8 `orm:"column(enable);size(200);null" description:"是否有效【0:无效】【1:有效】"`
//UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
//Enable int8 `orm:"column(enable);size(200);null" description:"是否有效【0:无效】【1:有效】"`
CompanyId int64 `orm:"column(company_id);null" description:"公司编号"`
Type int `orm:"column(type);null" description:"数据来源 1:提交人 2:审核人 "`
}
func (t *ChanceSelfCheck) TableName() string {
... ...
... ... @@ -9,6 +9,11 @@ import (
"strings"
)
const (
TypeSubmit = iota + 1
TypeApprove
)
/*机会-自查内容*/
var CheckOptionsCommit = []CheckOption{{Item: "是", NeedOther: false}, {Item: "否", NeedOther: false}, {Item: "不清楚", NeedOther: false}}
... ...
... ... @@ -314,50 +314,38 @@ func getQuestionTitle(groupIdx int, idx int, title string) string {
//自查问题回答批量入库
//@isDelete
func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int64, selfChecks protocol.SelfChecks, isDeleteExisted bool) {
func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int64, selfChecks protocol.SelfChecks, isDeleteExisted bool, checkerType int) {
defer func() {
if p := recover(); p != nil {
log.Warn(p)
}
}()
var (
count int
err error
Exists = `select count(0) from chance_self_check where chanceId=? and relateId=?`
DeleteExistedRow = `update chance_self_check set enable=0,update_at=Now() where relateId=? and enable=1`
count int
err error
Exists = `select count(0) from chance_self_check where chanceId=? and relateId=?`
DeleteExistedSubmitRow = `delete from chance_self_check where relateId=? and type=? and user_company_id=?` //删除提交人自查数据
)
if len(selfChecks) == 0 {
return
}
o := orm.NewOrm()
o.Begin()
if err = utils.ExecuteQueryOneWithOrmer(o, &count, Exists, relateId, chanceId); err != nil {
log.Error(err)
o.Rollback()
return
}
if isDeleteExisted {
if err = utils.ExecuteSQLWithOrmer(o, DeleteExistedRow, chanceId); err != nil {
if err = utils.ExecuteSQLWithOrmer(o, DeleteExistedSubmitRow, chanceId, checkerType, header.UserId); err != nil {
log.Error(err)
o.Rollback()
return
}
}
if err = utils.ExecuteQueryOneWithOrmer(o, &count, Exists, relateId, chanceId); err != nil {
log.Error(err)
o.Rollback()
return
}
if count == 0 {
insertFunc := func(check protocol.SelfCheck, r int64) (err error) {
m := &models.ChanceSelfCheck{
Id: idgen.Next(),
UserCompanyId: header.UserId,
ChanceId: chanceId,
RelateId: r,
CheckItem: check.CheckItem,
GroupId: check.GroupId,
Answer: check.Answer,
Reason: check.Reason,
CreateAt: time.Now(),
UpdateAt: time.Now(),
Enable: protocol.Valid,
}
m := NewChanceSelfCheck(header, check, chanceId, r, checkerType)
//插入审核的自查内容
if _, err = o.Insert(m); err != nil {
log.Error(err)
... ... @@ -372,7 +360,7 @@ func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int
return
}
//审核通过更新之前机会的自查内容(重新插入一条机会的自查内容)
if isDeleteExisted {
if checkerType == protocol.TypeApprove && isDeleteExisted {
if err = insertFunc(check, chanceId); err != nil {
return
}
... ... @@ -381,3 +369,20 @@ func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int
}
o.Commit()
}
func NewChanceSelfCheck(header *protocol.RequestHeader, check protocol.SelfCheck, chanceId, r int64, checkerType int) *models.ChanceSelfCheck {
return &models.ChanceSelfCheck{
Id: idgen.Next(),
UserCompanyId: header.UserId,
ChanceId: chanceId,
RelateId: r,
CheckItem: check.CheckItem,
GroupId: check.GroupId,
Answer: check.Answer,
Reason: check.Reason,
CreateAt: time.Now(),
//UpdateAt: time.Now(),
CompanyId: header.CompanyId,
Type: checkerType,
}
}
... ...
... ... @@ -457,7 +457,7 @@ func ChanceApprove(header *protocol.RequestHeader, request *protocol.ChanceAppro
}
}
go agg.BulkInsertSelfChecks(header, process.Id, chance.Id, request.SelfChecks, request.ReviewStatus == protocol.ReviewStatusPass)
go agg.BulkInsertSelfChecks(header, process.Id, chance.Id, request.SelfChecks, request.ReviewStatus == protocol.ReviewStatusPass, protocol.TypeApprove)
orm.Commit()
return
}
... ...
... ... @@ -486,7 +486,7 @@ func ChanceSubmit(header *protocol.RequestHeader, request *protocol.ChanceSubmit
return
}
}
go agg.BulkInsertSelfChecks(header, chance.Id, chance.Id, request.SelfChecks, false)
go agg.BulkInsertSelfChecks(header, chance.Id, chance.Id, request.SelfChecks, false, protocol.TypeSubmit)
orm.Commit()
rsp = &protocol.ChanceSubmitResponse{}
... ... @@ -704,6 +704,7 @@ func ChanceUpdate(header *protocol.RequestHeader, request *protocol.ChanceUpdate
updateMap["DiscoveryScore"] = chance.DiscoveryScore
}
}
go agg.BulkInsertSelfChecks(header, chance.Id, chance.Id, request.SelfChecks, true, protocol.TypeSubmit)
}
}
... ...