scrm-go

activity_service.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package marketing_tool_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func GetValidActivities(orgID int64, appID int64, keyWord string, page int, count int) ([]*models.Activity, int64, error) {
  9. if count <= 0 {
  10. return []*models.Activity{}, 0, nil
  11. }
  12. if page < 1 {
  13. page = 1
  14. }
  15. var activities []*models.Activity
  16. var totalCount int64
  17. db := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? AND user_app_id = ? AND status <> 9", orgID, appID)
  18. if len(keyWord) > 0 {
  19. likeParam := "%" + keyWord + "%"
  20. db = db.Where("title LIKE ?", likeParam)
  21. }
  22. err := db.
  23. Count(&totalCount).
  24. Order("ctime desc").Limit(count).Offset((page - 1) * count).Find(&activities).
  25. Error
  26. if err == nil {
  27. return activities, totalCount, nil
  28. } else {
  29. return nil, 0, err
  30. }
  31. }
  32. func GetActivitiesWithStatus(orgID int64, appID int64, keyWord string, status int, page int, count int) ([]*models.Activity, int64, error) {
  33. if count <= 0 {
  34. return []*models.Activity{}, 0, nil
  35. }
  36. if page < 1 {
  37. page = 1
  38. }
  39. var activities []*models.Activity
  40. var totalCount int64
  41. db := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? AND user_app_id = ? AND status = ?", orgID, appID, status)
  42. if len(keyWord) > 0 {
  43. likeParam := "%" + keyWord + "%"
  44. db = db.Where("title LIKE ?", likeParam)
  45. }
  46. err := db.
  47. Count(&totalCount).
  48. Order("ctime desc").Limit(count).Offset((page - 1) * count).Find(&activities).
  49. Error
  50. if err == nil {
  51. return activities, totalCount, nil
  52. } else {
  53. return nil, 0, err
  54. }
  55. }
  56. func GetDidEndedActivities(orgID int64, appID int64, keyWord string, page int, count int) ([]*models.Activity, int64, error) {
  57. if count <= 0 {
  58. return []*models.Activity{}, 0, nil
  59. }
  60. if page < 1 {
  61. page = 1
  62. }
  63. var activities []*models.Activity
  64. var totalCount int64
  65. now := time.Now().Unix()
  66. db := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? AND user_app_id = ? AND status = 1 AND start_time < ?", orgID, appID, now)
  67. if len(keyWord) > 0 {
  68. likeParam := "%" + keyWord + "%"
  69. db = db.Where("title LIKE ?", likeParam)
  70. }
  71. err := db.
  72. Count(&totalCount).
  73. Order("ctime desc").Limit(count).Offset((page - 1) * count).Find(&activities).
  74. Error
  75. if err == nil {
  76. return activities, totalCount, nil
  77. } else {
  78. return nil, 0, err
  79. }
  80. }
  81. func GetActivityWithID(orgID int64, activityID int64) (*models.Activity, error) {
  82. var activity models.Activity
  83. err := service.PatientReadDB().Model(&models.Activity{}).Where("user_org_id = ? and id = ?", orgID, activityID).First(&activity).Error
  84. if err != nil {
  85. if err == gorm.ErrRecordNotFound {
  86. return nil, nil
  87. } else {
  88. return nil, err
  89. }
  90. }
  91. return &activity, nil
  92. }
  93. func GetActivityParagraphByActivityID(activityID int64) (*models.ActivityParagraph, error) {
  94. var paragraph models.ActivityParagraph
  95. err :=
  96. service.PatientReadDB().Model(&models.ActivityParagraph{}).
  97. Where("activity_id = ? and status = 1", activityID).
  98. First(&paragraph).
  99. Error
  100. if err != nil {
  101. if err == gorm.ErrRecordNotFound {
  102. return nil, nil
  103. } else {
  104. return nil, err
  105. }
  106. }
  107. return &paragraph, nil
  108. }
  109. func GetActivityWxShareByActivityID(activityID int64) (*models.ActivityWxShare, error) {
  110. var model models.ActivityWxShare
  111. err :=
  112. service.PatientReadDB().Model(&models.ActivityWxShare{}).
  113. Where("activity_id = ?", activityID).
  114. First(&model).
  115. Error
  116. if err != nil {
  117. if err == gorm.ErrRecordNotFound {
  118. return nil, nil
  119. } else {
  120. return nil, err
  121. }
  122. }
  123. return &model, nil
  124. }
  125. func SaveActivityAndParagraph(activity *models.Activity, paragraph *models.ActivityParagraph) error {
  126. tx := service.PatientWriteDB().Begin()
  127. saveActivityErr := tx.Save(activity).Error
  128. if saveActivityErr != nil {
  129. tx.Rollback()
  130. return saveActivityErr
  131. }
  132. if paragraph != nil {
  133. if paragraph.Id == 0 {
  134. paragraph.ActivityId = activity.Id
  135. }
  136. saveParagraphErr := tx.Save(paragraph).Error
  137. if saveParagraphErr != nil {
  138. tx.Rollback()
  139. return saveParagraphErr
  140. }
  141. }
  142. tx.Commit()
  143. return nil
  144. }
  145. func SaveActivityWxShare(model *models.ActivityWxShare) error {
  146. return service.PatientWriteDB().Save(model).Error
  147. }
  148. func GetValidActivitySignupUsers(activityID int64, keyWord string, page int) ([]*models.ActivityUser, int, error) {
  149. var users []*models.ActivityUser
  150. countPerPage := 10
  151. var totalCount int
  152. db := service.PatientReadDB().Model(&models.ActivityUser{}).Where("activity_id = ? AND status = 1", activityID)
  153. if len(keyWord) > 0 {
  154. likeParam := "%" + keyWord + "%"
  155. db = db.Where("mobile LIKE ? OR realname LIKE ?", likeParam, likeParam)
  156. }
  157. err := db.
  158. Count(&totalCount).
  159. Order("id asc").Limit(countPerPage).Offset((page - 1) * countPerPage).
  160. Find(&users).
  161. Error
  162. if err == nil {
  163. return users, totalCount, nil
  164. } else {
  165. return nil, 0, err
  166. }
  167. }
  168. func DeleteActivity(orgID int64, activityID int64) error {
  169. tx := service.PatientWriteDB().Begin()
  170. deleteActivityErr := tx.Model(&models.Activity{}).Where("user_org_id = ? AND id = ?", orgID, activityID).Updates(map[string]interface{}{
  171. "status": 9,
  172. "mtime": time.Now().Unix(),
  173. }).Error
  174. if deleteActivityErr != nil {
  175. tx.Rollback()
  176. return deleteActivityErr
  177. }
  178. // deletePErr := tx.Model(&models.ActivityParagraph{}).Where("activity_id = ?", activityID).Updates(map[string]interface{}{
  179. // "status": 0,
  180. // "mtime": time.Now().Unix(),
  181. // }).Error
  182. // if deletePErr != nil {
  183. // tx.Rollback()
  184. // return deletePErr
  185. // }
  186. // deleteShareErr := tx.Model(&models.ActivityWxShare{}).Where("activity_id = ?", activityID).Updates(map[string]interface{}{
  187. // "status": 0,
  188. // "mtime": time.Now().Unix(),
  189. // }).Error
  190. // if deleteShareErr != nil {
  191. // tx.Rollback()
  192. // return deleteShareErr
  193. // }
  194. tx.Commit()
  195. return nil
  196. }