| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package result
- import (
- "lims_adapter/dao"
- "dashoo.cn/common_definition/admin/result_def"
- "dashoo.cn/micro_libary/request"
- )
- type Service struct {
- ConsequentDao *dao.ConsequentDao
- ConsequentDetailDao *dao.ConsequentDetailDao
- Tenant string
- }
- // NewSrv 服务初始化
- func NewSrv(tenant string) Service {
- return Service{ConsequentDao: dao.NewConsequentDao(tenant), ConsequentDetailDao: dao.NewConsequentDetailDao(tenant), Tenant: tenant}
- }
- func (s Service) BatchInsertPapers(insertList []result_def.BatchInsertPapersReq, userInfo request.UserInfo) (int, int, int, []interface{}, error) {
- failCount := 0
- duplicatedCount := 0
- failList := []interface{}{}
- successCount := 0
- for _, p := range insertList {
- p.Detail.CreatedBy = userInfo.RealName
- p.Detail.CreatedById = int(userInfo.Id)
- p.Detail.UpdatedBy = userInfo.RealName
- p.Detail.UpdatedById = int(userInfo.Id)
- p.Entity.CreatedBy = userInfo.RealName
- // 由于使用string接收,应该对时间字符串校验,无法解析直接nil
- if *p.Entity.PublishTime == "" {
- p.Entity.PublishTime = nil
- }
- // TODO 查询优化
- // 防止重复插入论文
- r, _ := s.ConsequentDetailDao.M.Where("PaperTopic=? or PaperPage=?", p.Detail.PaperTopic, p.Detail.PaperPage).Where("PaperPage!=''").One()
- if len(r) > 0 {
- duplicatedCount += 1
- continue
- }
- InsertId, err := s.ConsequentDao.M.InsertAndGetId(p.Entity)
- if err == nil {
- p.Detail.ConsequentId = int(InsertId)
- _, err = s.ConsequentDetailDao.M.Insert(p.Detail)
- // 如果Detail插入失败则删除成果主表记录
- if err != nil {
- s.ConsequentDao.Where("id=?", InsertId).Delete()
- failCount += 1
- failList = append(failList, map[string]string{
- "title": p.Detail.PaperTopic,
- "error": "",
- })
- } else {
- successCount += 1
- }
- } else {
- failCount += 1
- failList = append(failList, map[string]string{
- "title": p.Detail.PaperTopic,
- "error": "",
- })
- }
- }
- return failCount, successCount, duplicatedCount, failList, nil
- }
|