作者 yangfu

批量插入自查数据

@@ -17,8 +17,10 @@ type ChanceSelfCheck struct { @@ -17,8 +17,10 @@ type ChanceSelfCheck struct {
17 Answer string `orm:"column(answer);size(50);null" description:"回答"` 17 Answer string `orm:"column(answer);size(50);null" description:"回答"`
18 Reason string `orm:"column(reason);size(200);null" description:"理由"` 18 Reason string `orm:"column(reason);size(200);null" description:"理由"`
19 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"` 19 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
20 - UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`  
21 - Enable int8 `orm:"column(enable);size(200);null" description:"是否有效【0:无效】【1:有效】"` 20 + //UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  21 + //Enable int8 `orm:"column(enable);size(200);null" description:"是否有效【0:无效】【1:有效】"`
  22 + CompanyId int64 `orm:"column(company_id);null" description:"公司编号"`
  23 + Type int `orm:"column(type);null" description:"数据来源 1:提交人 2:审核人 "`
22 } 24 }
23 25
24 func (t *ChanceSelfCheck) TableName() string { 26 func (t *ChanceSelfCheck) TableName() string {
@@ -9,6 +9,11 @@ import ( @@ -9,6 +9,11 @@ import (
9 "strings" 9 "strings"
10 ) 10 )
11 11
  12 +const (
  13 + TypeSubmit = iota + 1
  14 + TypeApprove
  15 +)
  16 +
12 /*机会-自查内容*/ 17 /*机会-自查内容*/
13 var CheckOptionsCommit = []CheckOption{{Item: "是", NeedOther: false}, {Item: "否", NeedOther: false}, {Item: "不清楚", NeedOther: false}} 18 var CheckOptionsCommit = []CheckOption{{Item: "是", NeedOther: false}, {Item: "否", NeedOther: false}, {Item: "不清楚", NeedOther: false}}
14 19
@@ -314,50 +314,38 @@ func getQuestionTitle(groupIdx int, idx int, title string) string { @@ -314,50 +314,38 @@ func getQuestionTitle(groupIdx int, idx int, title string) string {
314 314
315 //自查问题回答批量入库 315 //自查问题回答批量入库
316 //@isDelete 316 //@isDelete
317 -func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int64, selfChecks protocol.SelfChecks, isDeleteExisted bool) { 317 +func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int64, selfChecks protocol.SelfChecks, isDeleteExisted bool, checkerType int) {
318 defer func() { 318 defer func() {
319 if p := recover(); p != nil { 319 if p := recover(); p != nil {
320 log.Warn(p) 320 log.Warn(p)
321 } 321 }
322 }() 322 }()
323 var ( 323 var (
324 - count int  
325 - err error  
326 - Exists = `select count(0) from chance_self_check where chanceId=? and relateId=?`  
327 - DeleteExistedRow = `update chance_self_check set enable=0,update_at=Now() where relateId=? and enable=1` 324 + count int
  325 + err error
  326 + Exists = `select count(0) from chance_self_check where chanceId=? and relateId=?`
  327 + DeleteExistedSubmitRow = `delete from chance_self_check where relateId=? and type=? and user_company_id=?` //删除提交人自查数据
328 ) 328 )
329 if len(selfChecks) == 0 { 329 if len(selfChecks) == 0 {
330 return 330 return
331 } 331 }
332 o := orm.NewOrm() 332 o := orm.NewOrm()
333 o.Begin() 333 o.Begin()
334 - if err = utils.ExecuteQueryOneWithOrmer(o, &count, Exists, relateId, chanceId); err != nil {  
335 - log.Error(err)  
336 - o.Rollback()  
337 - return  
338 - }  
339 if isDeleteExisted { 334 if isDeleteExisted {
340 - if err = utils.ExecuteSQLWithOrmer(o, DeleteExistedRow, chanceId); err != nil { 335 + if err = utils.ExecuteSQLWithOrmer(o, DeleteExistedSubmitRow, chanceId, checkerType, header.UserId); err != nil {
341 log.Error(err) 336 log.Error(err)
342 o.Rollback() 337 o.Rollback()
343 return 338 return
344 } 339 }
345 } 340 }
  341 + if err = utils.ExecuteQueryOneWithOrmer(o, &count, Exists, relateId, chanceId); err != nil {
  342 + log.Error(err)
  343 + o.Rollback()
  344 + return
  345 + }
346 if count == 0 { 346 if count == 0 {
347 insertFunc := func(check protocol.SelfCheck, r int64) (err error) { 347 insertFunc := func(check protocol.SelfCheck, r int64) (err error) {
348 - m := &models.ChanceSelfCheck{  
349 - Id: idgen.Next(),  
350 - UserCompanyId: header.UserId,  
351 - ChanceId: chanceId,  
352 - RelateId: r,  
353 - CheckItem: check.CheckItem,  
354 - GroupId: check.GroupId,  
355 - Answer: check.Answer,  
356 - Reason: check.Reason,  
357 - CreateAt: time.Now(),  
358 - UpdateAt: time.Now(),  
359 - Enable: protocol.Valid,  
360 - } 348 + m := NewChanceSelfCheck(header, check, chanceId, r, checkerType)
361 //插入审核的自查内容 349 //插入审核的自查内容
362 if _, err = o.Insert(m); err != nil { 350 if _, err = o.Insert(m); err != nil {
363 log.Error(err) 351 log.Error(err)
@@ -372,7 +360,7 @@ func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int @@ -372,7 +360,7 @@ func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int
372 return 360 return
373 } 361 }
374 //审核通过更新之前机会的自查内容(重新插入一条机会的自查内容) 362 //审核通过更新之前机会的自查内容(重新插入一条机会的自查内容)
375 - if isDeleteExisted { 363 + if checkerType == protocol.TypeApprove && isDeleteExisted {
376 if err = insertFunc(check, chanceId); err != nil { 364 if err = insertFunc(check, chanceId); err != nil {
377 return 365 return
378 } 366 }
@@ -381,3 +369,20 @@ func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int @@ -381,3 +369,20 @@ func BulkInsertSelfChecks(header *protocol.RequestHeader, relateId, chanceId int
381 } 369 }
382 o.Commit() 370 o.Commit()
383 } 371 }
  372 +
  373 +func NewChanceSelfCheck(header *protocol.RequestHeader, check protocol.SelfCheck, chanceId, r int64, checkerType int) *models.ChanceSelfCheck {
  374 + return &models.ChanceSelfCheck{
  375 + Id: idgen.Next(),
  376 + UserCompanyId: header.UserId,
  377 + ChanceId: chanceId,
  378 + RelateId: r,
  379 + CheckItem: check.CheckItem,
  380 + GroupId: check.GroupId,
  381 + Answer: check.Answer,
  382 + Reason: check.Reason,
  383 + CreateAt: time.Now(),
  384 + //UpdateAt: time.Now(),
  385 + CompanyId: header.CompanyId,
  386 + Type: checkerType,
  387 + }
  388 +}
@@ -457,7 +457,7 @@ func ChanceApprove(header *protocol.RequestHeader, request *protocol.ChanceAppro @@ -457,7 +457,7 @@ func ChanceApprove(header *protocol.RequestHeader, request *protocol.ChanceAppro
457 } 457 }
458 } 458 }
459 459
460 - go agg.BulkInsertSelfChecks(header, process.Id, chance.Id, request.SelfChecks, request.ReviewStatus == protocol.ReviewStatusPass) 460 + go agg.BulkInsertSelfChecks(header, process.Id, chance.Id, request.SelfChecks, request.ReviewStatus == protocol.ReviewStatusPass, protocol.TypeApprove)
461 orm.Commit() 461 orm.Commit()
462 return 462 return
463 } 463 }
@@ -486,7 +486,7 @@ func ChanceSubmit(header *protocol.RequestHeader, request *protocol.ChanceSubmit @@ -486,7 +486,7 @@ func ChanceSubmit(header *protocol.RequestHeader, request *protocol.ChanceSubmit
486 return 486 return
487 } 487 }
488 } 488 }
489 - go agg.BulkInsertSelfChecks(header, chance.Id, chance.Id, request.SelfChecks, false) 489 + go agg.BulkInsertSelfChecks(header, chance.Id, chance.Id, request.SelfChecks, false, protocol.TypeSubmit)
490 orm.Commit() 490 orm.Commit()
491 491
492 rsp = &protocol.ChanceSubmitResponse{} 492 rsp = &protocol.ChanceSubmitResponse{}
@@ -704,6 +704,7 @@ func ChanceUpdate(header *protocol.RequestHeader, request *protocol.ChanceUpdate @@ -704,6 +704,7 @@ func ChanceUpdate(header *protocol.RequestHeader, request *protocol.ChanceUpdate
704 updateMap["DiscoveryScore"] = chance.DiscoveryScore 704 updateMap["DiscoveryScore"] = chance.DiscoveryScore
705 } 705 }
706 } 706 }
  707 + go agg.BulkInsertSelfChecks(header, chance.Id, chance.Id, request.SelfChecks, true, protocol.TypeSubmit)
707 } 708 }
708 } 709 }
709 710