package model import ( "github.com/jinzhu/gorm" "time" "fmt" ) type TProvider struct { Id int64 `gorm:"column:ID" json:"id" form:"id"` Name string `gorm:"column:Name" json:"name" form:"name"` SocialCode string `gorm:"column:SocialCode" json:"social_code" form:"social_code"` LegalPerson string `gorm:"column:LegalPerson" json:"legal_person" form:"legal_person"` IdCert string `gorm:"column:IdCert" json:"id_cert" form:"id_cert"` EnterpriseLocation int32 `gorm:"column:EnterpriseLocation" json:"enterprise_location" form:"enterprise_location"` BusinessLicense string `gorm:"column:BusinessLicense" json:"business_license" form:"business_license"` IntegrityManagementLetter string `gorm:"column:IntegrityManagementLetter" json:"integrity_management_letter" form:"integrity_management_letter"` LegalPersonLetter string `gorm:"column:LegalPersonLetter" json:"legal_person_letter" form:"legal_person_letter"` BusinessResponsibleLetter string `gorm:"column:BusinessResponsibleLetter" json:"business_responsible_letter" form:"business_responsible_letter"` UserName string `gorm:"column:UserName" json:"user_name" form:"user_name"` Passwd string `gorm:"column:Passwd" json:"passwd" form:"passwd"` BusinessContact string `gorm:"column:BusinessContact" json:"business_contact" form:"business_contact"` BusinessContactPhone string `gorm:"column:BusinessContactPhone" json:"business_contact_phone" form:"business_contact_phone"` BusinessResponsible string `gorm:"column:BusinessResponsible" json:"business_responsible" form:"business_responsible"` BusinessResponsiblePhone string `gorm:"column:BusinessResponsiblePhone" json:"business_responsible_phone" form:"business_responsible_phone"` EmergencyContact string `gorm:"column:EmergencyContact" json:"emergency_contact" form:"emergency_contact"` EmergencyContactPhone string `gorm:"column:EmergencyContactPhone" json:"emergency_contact_phone" form:"emergency_contact_phone"` Status int64 `gorm:"column:Status" json:"status" form:"status"` CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at" form:"created_at"` UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at" form:"updated_at"` Feedback string `gorm:"column:Feedback"` ApproveTime time.Time `gorm:"column:ApproveTime"` Email string `gorm:"column:Email"` } func PublicityList(db *gorm.DB, provider string) ([]TProvider, int64, error) { sql := fmt.Sprintf("select t1.* from Provider as t1 where(select count(1) from ProviderDeviceTesting where ProviderId=t1.ID and Status = 1) > 0 and t1.Status = 1") countSql := fmt.Sprintf("select count(1) as count from Provider as t1 where(select count(1) from ProviderDeviceTesting where ProviderId=t1.id and Status = 1) > 0 ") type CountResult struct { Count int64 } if provider != "" { sql = fmt.Sprintf("%s and t1.Name like '%%%s%%'", sql, provider) countSql = fmt.Sprintf("%s and t1.Name like '%%%s%%'", countSql, provider) } array := []CountResult{} err := db.Raw(countSql).Scan(&array).Error if err != nil { return nil, 0, err } total := array[0].Count if total == 0 { return nil, 0, nil } list := []TProvider{} err = db.Raw(sql).Scan(&list).Error if err != nil { return nil, 0, err } return list, total, nil }