package officialWebsiteser import ( "91porn-server/models/v/officialWebsitemod" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) type RecruitFormReq struct { Name string `json:"name" form:"name" binding:"required"` Sex int `json:"sex" form:"sex"` Age int `json:"age" form:"age"` Country string `json:"country" form:"country"` Address string `json:"address" form:"address"` Contact []ContactInfoReq `json:"contact" form:"contact" binding:"required,dive"` Description string `json:"description" form:"description"` } type ContactInfoReq struct { Type string `json:"type" form:"type"` Value string `json:"value" form:"value"` } type RecruitFormResp struct { Message string `json:"message"` } func RecruitForm(req *RecruitFormReq) (resp RecruitFormResp, err error) { if filter, ok := buildRecruitContactFilter(req.Contact); ok { var exist = &officialWebsitemod.RecruitForm{} err = exist.FindOne(filter) if err != nil { return } if exist.ID != primitive.NilObjectID { resp = RecruitFormResp{ Message: "您已提交过了,我们会尽快联系您", } return resp, nil } } form := &officialWebsitemod.RecruitForm{ Name: req.Name, Sex: req.Sex, Age: req.Age, Country: req.Country, Address: req.Address, Description: req.Description, } for _, v := range req.Contact { form.Contact = append(form.Contact, officialWebsitemod.ContactInfo{ Type: v.Type, Value: v.Value, }) } err = form.Create() if err != nil { return } resp = RecruitFormResp{ Message: "提交成功", } return resp, nil } func buildRecruitContactFilter(contacts []ContactInfoReq) (officialWebsitemod.M, bool) { if len(contacts) == 0 { return nil, false } orConditions := make([]officialWebsitemod.M, 0, len(contacts)) for _, c := range contacts { if c.Type == "" || c.Value == "" { continue } orConditions = append(orConditions, officialWebsitemod.M{ "contact": bson.M{ "$elemMatch": bson.M{ "type": c.Type, "value": c.Value, }, }, }) } if len(orConditions) == 0 { return nil, false } if len(orConditions) == 1 { return orConditions[0], true } return officialWebsitemod.M{"$or": orConditions}, true }