result.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package result
  2. import (
  3. "errors"
  4. "lims_adapter/dao"
  5. "log"
  6. "dashoo.cn/common_definition/admin/result_def"
  7. "dashoo.cn/micro_libary/request"
  8. )
  9. type Service struct {
  10. ConsequentDao *dao.ConsequentDao
  11. ConsequentDetailDao *dao.ConsequentDetailDao
  12. Tenant string
  13. }
  14. // NewSrv 服务初始化
  15. func NewSrv(tenant string) Service {
  16. return Service{ConsequentDao: dao.NewConsequentDao(tenant), ConsequentDetailDao: dao.NewConsequentDetailDao(tenant), Tenant: tenant}
  17. }
  18. func (s Service) GetResultList(req result_def.ResultListReq) ([]result_def.ResultList, int, error) {
  19. query := s.ConsequentDao.M
  20. log.Println(req.PublishTime)
  21. // FILTER
  22. if len(req.PublishTime) == 2 {
  23. query = query.Where("(PublishTime BETWEEN ? and ?)", (req.PublishTime)[0], (req.PublishTime)[1])
  24. }
  25. if req.Name != "" {
  26. query = query.Where("Name LIKE ?", "%"+req.Name+"%")
  27. }
  28. if req.ProjectId != 0 {
  29. query = query.Where("ProjectId = ?", req.ProjectId)
  30. }
  31. total, err := query.Count()
  32. if err != nil {
  33. return nil, 0, errors.New("读取行数失败")
  34. }
  35. // 无数据返回空
  36. if total == 0 {
  37. return nil, 0, nil
  38. }
  39. // FIXME owners_name: ["123,123333,实验室管理员,123"],没有拆分类,原接口亦是如此
  40. data, err := query.Page(int(req.Current), int(req.Size)).FindAll()
  41. result := make([]result_def.ResultList, 0, 0)
  42. data.Structs(&result)
  43. return result, total, err
  44. }
  45. func (s Service) BatchInsertPapers(insertList []result_def.BatchInsertPapersReq, userInfo request.UserInfo) (int, int, int, []interface{}, error) {
  46. failCount := 0
  47. duplicatedCount := 0
  48. failList := []interface{}{}
  49. successCount := 0
  50. for _, p := range insertList {
  51. p.Detail.CreatedBy = userInfo.RealName
  52. p.Detail.CreatedById = int(userInfo.Id)
  53. p.Detail.UpdatedBy = userInfo.RealName
  54. p.Detail.UpdatedById = int(userInfo.Id)
  55. p.Entity.CreatedBy = userInfo.RealName
  56. // 由于使用string接收,应该对时间字符串校验,无法解析直接nil
  57. if *p.Entity.PublishTime == "" {
  58. p.Entity.PublishTime = nil
  59. }
  60. // TODO 查询优化
  61. // 防止重复插入论文
  62. r, _ := s.ConsequentDetailDao.M.Where("PaperTopic=? or PaperPage=?", p.Detail.PaperTopic, p.Detail.PaperPage).Where("PaperPage!=''").One()
  63. if len(r) > 0 {
  64. duplicatedCount += 1
  65. continue
  66. }
  67. InsertId, err := s.ConsequentDao.M.InsertAndGetId(p.Entity)
  68. if err == nil {
  69. p.Detail.ConsequentId = int(InsertId)
  70. _, err = s.ConsequentDetailDao.M.Insert(p.Detail)
  71. // 如果Detail插入失败则删除成果主表记录
  72. if err != nil {
  73. s.ConsequentDao.Where("id=?", InsertId).Delete()
  74. failCount += 1
  75. failList = append(failList, map[string]string{
  76. "title": p.Detail.PaperTopic,
  77. "error": "",
  78. })
  79. } else {
  80. successCount += 1
  81. }
  82. } else {
  83. failCount += 1
  84. failList = append(failList, map[string]string{
  85. "title": p.Detail.PaperTopic,
  86. "error": "",
  87. })
  88. }
  89. }
  90. return failCount, successCount, duplicatedCount, failList, nil
  91. }