package service import ( "encoding/json" "errors" "gorm.io/gorm" "insure/e" "insure/global" "insure/model" "insure/third_party/signer" "insure/third_party/suixing" "insure/utils" "strconv" "time" ) var OrderService = orderService{} type orderService struct { } func (s *orderService) CreateOrder(param suixing.CommonParam) (*model.Order, error) { orderSn := utils.GenerateNo("order") orderParam := &suixing.Order{} err := json.Unmarshal([]byte(param.Data), &orderParam) if err != nil { return nil, err } order, err := s.saveOrder(orderSn, param.RequestID) if err != nil { return nil, err } user, err := s.saveUser(orderSn, orderParam.User) if err != nil { return nil, err } project, err := s.saveProject(orderSn, orderParam.Project) if err != nil { return nil, err } applicant, err := s.saveApplicant(orderSn, orderParam.Applicant) if err != nil { return nil, err } assured, err := s.saveAssured(orderSn, orderParam.Assured) if err != nil { return nil, err } order.User = user order.Applicant = applicant order.Project = project order.Assured = assured return order, nil } func (s *orderService) saveOrder(orderSn string, requestID string) (*model.Order, error) { o := model.Order{} o.Token = utils.Md5(orderSn + utils.GenerateRandomString(10) + strconv.Itoa(int(time.Now().UnixMicro()))) o.OrderSn = orderSn o.RequestID = requestID err := global.DB.Save(&o).Error if err != nil { return nil, err } return &o, nil } func (s *orderService) saveUser(orderSn string, user suixing.User) (*model.User, error) { u := model.User{} u.OrderSn = orderSn u.UserID = user.UserID u.Username = user.Username u.Mobile = user.Mobile u.UnitID = user.UnitID u.UnitName = user.UnitName u.BankUserName = user.BankUserName u.BankName = user.BankName u.BankCardName = user.BankCardName u.BeiandiquCode = user.BeiandiquCode err := global.DB.Save(&u).Error if err != nil { return nil, err } return &u, nil } func (s *orderService) saveProject(orderSn string, project suixing.Project) (*model.Project, error) { p := model.Project{} p.OrderSn = orderSn p.Name = project.Name p.TpID = project.TpID p.BdID = project.BdID p.BdName = project.BdName p.StartDate = project.StartDate p.Amount = project.Amount p.Province = project.Province p.City = project.City p.District = project.District p.BzjEtime = project.BzjEtime p.CreateTime = project.CreateTime p.PlanDate = project.PlanDate p.BuildPrice = project.BuildPrice p.BuildPlace = project.BuildPlace p.TenderProjectType = project.TenderProjectType p.ValidPeriod = project.ValidPeriod p.TenderNoticeUrl = project.TenderNoticeUrl p.TenderFileUrl = project.TenderFileUrl p.TenderFileUrlMd5 = project.TenderFileUrlMd5 p.ProjectApprovalNo = project.ProjectApprovalNo err := global.DB.Save(&p).Error if err != nil { return nil, err } return &p, nil } func (s *orderService) saveApplicant(orderSn string, applicant suixing.Applicant) (*model.Applicant, error) { licenseFiles, err := json.Marshal(applicant.LicenseFiles) if err != nil { return nil, err } a := model.Applicant{} a.OrderSn = orderSn a.ApplicantName = applicant.ApplicantName a.CreditVld = applicant.CreditVld a.CreditCode = applicant.CreditCode a.ContactName = applicant.ContactName a.ContactMobile = applicant.ContactMobile a.ComTelArea = applicant.ComTelArea a.Province = applicant.Province a.City = applicant.City a.District = applicant.District a.Address = applicant.Address a.Email = applicant.Email a.LegalName = applicant.LegalName a.LegalNum = applicant.LegalNum a.ComTelNum = applicant.ComTelNum a.LicenseFiles = string(licenseFiles) a.SignUrl = applicant.SignUrl err = global.DB.Save(&a).Error if err != nil { return nil, err } return &a, nil } func (s *orderService) saveAssured(orderSn string, assured suixing.Assured) (*model.Assured, error) { a := model.Assured{} a.OrderSn = orderSn a.AssuredName = assured.AssuredName a.CreditVld = assured.CreditVld a.CreditCode = assured.CreditCode a.ContactName = assured.ContactName a.ContactMobile = assured.ContactMobile a.ComTelArea = assured.ComTelArea a.Province = assured.Province a.City = assured.City a.District = assured.District a.Address = assured.Address a.TenderBankAccount = assured.TenderBankAccount a.TenderBankOpenbank = assured.TenderBankOpenbank a.TenderAccountName = assured.TenderAccountName a.Agency = assured.Agency a.ComTelNum = assured.ComTelNum err := global.DB.Save(&a).Error if err != nil { return nil, err } return &a, nil } func (s *orderService) GetOrderByToken(token string) (*model.Order, error) { order := model.Order{} err := global.DB.Preload("User").Preload("Project").Preload("Applicant").Preload("Assured").Where("token", token).First(&order).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, e.NewError("记录不存在") } else { return nil, e.NewError("数据异常") } } return &order, nil } func (s *orderService) AddSignLog(orderSn string, param signer.UploadParam) (*model.SignLog, error) { var err error isUploadPdf := 1 if param.IsUploadPdf != "" { isUploadPdf, err = strconv.Atoi(param.IsUploadPdf) if err != nil { return nil, err } } l := model.SignLog{} l.OrderSn = orderSn l.Params = param.Params l.CaInfo = param.CaInfo l.IsUploadPdf = isUploadPdf l.FileInfo = param.FileInfo l.PcInfo = param.PcInfo err = global.DB.Save(&l).Error if err != nil { return nil, err } return &l, nil }