result.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package result
  2. import (
  3. "lims_adapter/dao"
  4. "dashoo.cn/common_definition/admin/result_def"
  5. "dashoo.cn/micro_libary/request"
  6. )
  7. type Service struct {
  8. ConsequentDao *dao.ConsequentDao
  9. ConsequentDetailDao *dao.ConsequentDetailDao
  10. Tenant string
  11. }
  12. // NewSrv 服务初始化
  13. func NewSrv(tenant string) Service {
  14. return Service{ConsequentDao: dao.NewConsequentDao(tenant), ConsequentDetailDao: dao.NewConsequentDetailDao(tenant), Tenant: tenant}
  15. }
  16. func (s Service) BatchInsertPapers(insertList []result_def.BatchInsertPapersReq, userInfo request.UserInfo) (int, int, int, []interface{}, error) {
  17. failCount := 0
  18. duplicatedCount := 0
  19. failList := []interface{}{}
  20. successCount := 0
  21. for _, p := range insertList {
  22. p.Detail.CreatedBy = userInfo.RealName
  23. p.Detail.CreatedById = int(userInfo.Id)
  24. p.Detail.UpdatedBy = userInfo.RealName
  25. p.Detail.UpdatedById = int(userInfo.Id)
  26. p.Entity.CreatedBy = userInfo.RealName
  27. // 由于使用string接收,应该对时间字符串校验,无法解析直接nil
  28. if *p.Entity.PublishTime == "" {
  29. p.Entity.PublishTime = nil
  30. }
  31. // TODO 查询优化
  32. // 防止重复插入论文
  33. r, _ := s.ConsequentDetailDao.M.Where("PaperTopic=? or PaperPage=?", p.Detail.PaperTopic, p.Detail.PaperPage).Where("PaperPage!=''").One()
  34. if len(r) > 0 {
  35. duplicatedCount += 1
  36. continue
  37. }
  38. InsertId, err := s.ConsequentDao.M.InsertAndGetId(p.Entity)
  39. if err == nil {
  40. p.Detail.ConsequentId = int(InsertId)
  41. _, err = s.ConsequentDetailDao.M.Insert(p.Detail)
  42. // 如果Detail插入失败则删除成果主表记录
  43. if err != nil {
  44. s.ConsequentDao.Where("id=?", InsertId).Delete()
  45. failCount += 1
  46. failList = append(failList, map[string]string{
  47. "title": p.Detail.PaperTopic,
  48. "error": "",
  49. })
  50. } else {
  51. successCount += 1
  52. }
  53. } else {
  54. failCount += 1
  55. failList = append(failList, map[string]string{
  56. "title": p.Detail.PaperTopic,
  57. "error": "",
  58. })
  59. }
  60. }
  61. return failCount, successCount, duplicatedCount, failList, nil
  62. }