package marketing_tool_service import ( "SCRM/models" "SCRM/service" "time" "github.com/jinzhu/gorm" ) func GetValidActivities(orgID int64, appID int64, keyWord string, page int, count int) ([]*models.Activity, int64, error) { if count <= 0 { return []*models.Activity{}, 0, nil } if page < 1 { page = 1 } var activities []*models.Activity var totalCount int64 db := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? AND user_app_id = ? AND status <> 9", orgID, appID) if len(keyWord) > 0 { likeParam := "%" + keyWord + "%" db = db.Where("title LIKE ?", likeParam) } err := db. Count(&totalCount). Order("ctime desc").Limit(count).Offset((page - 1) * count).Find(&activities). Error if err == nil { return activities, totalCount, nil } else { return nil, 0, err } } func GetActivitiesWithStatus(orgID int64, appID int64, keyWord string, status int, page int, count int) ([]*models.Activity, int64, error) { if count <= 0 { return []*models.Activity{}, 0, nil } if page < 1 { page = 1 } var activities []*models.Activity var totalCount int64 db := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? AND user_app_id = ? AND status = ?", orgID, appID, status) if len(keyWord) > 0 { likeParam := "%" + keyWord + "%" db = db.Where("title LIKE ?", likeParam) } err := db. Count(&totalCount). Order("ctime desc").Limit(count).Offset((page - 1) * count).Find(&activities). Error if err == nil { return activities, totalCount, nil } else { return nil, 0, err } } func GetDidEndedActivities(orgID int64, appID int64, keyWord string, page int, count int) ([]*models.Activity, int64, error) { if count <= 0 { return []*models.Activity{}, 0, nil } if page < 1 { page = 1 } var activities []*models.Activity var totalCount int64 now := time.Now().Unix() db := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? AND user_app_id = ? AND status = 1 AND start_time < ?", orgID, appID, now) if len(keyWord) > 0 { likeParam := "%" + keyWord + "%" db = db.Where("title LIKE ?", likeParam) } err := db. Count(&totalCount). Order("ctime desc").Limit(count).Offset((page - 1) * count).Find(&activities). Error if err == nil { return activities, totalCount, nil } else { return nil, 0, err } } func GetActivityWithID(orgID int64, activityID int64) (*models.Activity, error) { var activity models.Activity err := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? and id = ?", orgID, activityID).First(&activity).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &activity, nil } func GetActivityParagraphByActivityID(activityID int64) (*models.ActivityParagraph, error) { var paragraph models.ActivityParagraph err := service.PatientReadDB().Model(&models.ActivityParagraph{}). Where("activity_id = ? and status = 1", activityID). First(¶graph). Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return ¶graph, nil } func GetActivityWxShareByActivityID(activityID int64) (*models.ActivityWxShare, error) { var model models.ActivityWxShare err := service.PatientReadDB().Model(&models.ActivityWxShare{}). Where("activity_id = ?", activityID). First(&model). Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &model, nil } func SaveActivityAndParagraph(activity *models.Activity, paragraph *models.ActivityParagraph) error { tx := service.PatientWriteDB().Begin() saveActivityErr := tx.Save(activity).Error if saveActivityErr != nil { tx.Rollback() return saveActivityErr } if paragraph != nil { if paragraph.Id == 0 { paragraph.ActivityId = activity.Id } saveParagraphErr := tx.Save(paragraph).Error if saveParagraphErr != nil { tx.Rollback() return saveParagraphErr } } tx.Commit() return nil } func SaveActivityWxShare(model *models.ActivityWxShare) error { return service.PatientWriteDB().Save(model).Error } func GetValidActivitySignupUsers(activityID int64, keyWord string, page int) ([]*models.ActivityUser, int, error) { var users []*models.ActivityUser countPerPage := 10 var totalCount int db := service.PatientReadDB().Model(&models.ActivityUser{}).Where("activity_id = ? AND status = 1", activityID) if len(keyWord) > 0 { likeParam := "%" + keyWord + "%" db = db.Where("mobile LIKE ? OR realname LIKE ?", likeParam, likeParam) } err := db. Count(&totalCount). Order("id asc").Limit(countPerPage).Offset((page - 1) * countPerPage). Find(&users). Error if err == nil { return users, totalCount, nil } else { return nil, 0, err } } func DeleteActivity(orgID int64, activityID int64) error { tx := service.PatientWriteDB().Begin() deleteActivityErr := tx.Model(&models.Activity{}).Where("user_org_id = ? AND id = ?", orgID, activityID).Updates(map[string]interface{}{ "status": 9, "mtime": time.Now().Unix(), }).Error if deleteActivityErr != nil { tx.Rollback() return deleteActivityErr } // deletePErr := tx.Model(&models.ActivityParagraph{}).Where("activity_id = ?", activityID).Updates(map[string]interface{}{ // "status": 0, // "mtime": time.Now().Unix(), // }).Error // if deletePErr != nil { // tx.Rollback() // return deletePErr // } // deleteShareErr := tx.Model(&models.ActivityWxShare{}).Where("activity_id = ?", activityID).Updates(map[string]interface{}{ // "status": 0, // "mtime": time.Now().Unix(), // }).Error // if deleteShareErr != nil { // tx.Rollback() // return deleteShareErr // } tx.Commit() return nil }