scrm-go

activity_controller.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package marketing_tool
  2. import (
  3. "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/models"
  6. "SCRM/service/marketing_tool_service"
  7. "SCRM/utils"
  8. "encoding/json"
  9. "time"
  10. "github.com/astaxie/beego"
  11. )
  12. func ActivityCtlRegistRouters() {
  13. beego.Router("/api/activities", &ActivityAPIController{}, "get:Activities")
  14. beego.Router("/api/activity", &ActivityAPIController{}, "get:GetActivity")
  15. beego.Router("/api/activity/submit", &ActivityAPIController{}, "post:ActivitySubmit")
  16. beego.Router("/api/activity/detailview", &ActivityAPIController{}, "get:GetActivityDetailView")
  17. beego.Router("/api/activity/signups", &ActivityAPIController{}, "get:SignupUsers")
  18. beego.Router("/api/activity/delete", &ActivityAPIController{}, "post:DeleteActivity")
  19. beego.Router("/api/activity/publish", &ActivityAPIController{}, "post:PublishActivity")
  20. beego.Router("/api/activity/shareinfo", &ActivityAPIController{}, "get:GetActivityShareInfo")
  21. }
  22. type ActivityAPIController struct {
  23. controllers.BaseAuthAPIController
  24. }
  25. // /api/activities [get]
  26. // @param page?:int
  27. // @param keyword?:string
  28. // @param status?:int 1.已发布 2.待发布 3.未通过 4.已结束 其他取全部
  29. func (this *ActivityAPIController) Activities() {
  30. page, _ := this.GetInt("page")
  31. keyword := this.GetString("keyword")
  32. status, _ := this.GetInt("status")
  33. if page <= 0 {
  34. page = 1
  35. }
  36. if status < 1 || status > 4 {
  37. status = 0
  38. }
  39. adminUserInfo := this.GetAdminUserInfo()
  40. var activities []*models.Activity
  41. var totalCount int64
  42. var getActivityErr error
  43. if status == 1 {
  44. activities, totalCount, getActivityErr = marketing_tool_service.GetActivitiesWithStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, 1, page, 10)
  45. } else if status == 2 {
  46. activities, totalCount, getActivityErr = marketing_tool_service.GetActivitiesWithStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, 4, page, 10)
  47. } else if status == 3 {
  48. activities, totalCount, getActivityErr = marketing_tool_service.GetActivitiesWithStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, 3, page, 10)
  49. } else if status == 4 {
  50. activities, totalCount, getActivityErr = marketing_tool_service.GetDidEndedActivities(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, page, 10)
  51. } else {
  52. activities, totalCount, getActivityErr = marketing_tool_service.GetValidActivities(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, page, 10)
  53. }
  54. if getActivityErr != nil {
  55. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  56. return
  57. }
  58. activityJSONList := make([]map[string]interface{}, 0, len(activities))
  59. for _, activity := range activities {
  60. var json map[string]interface{}
  61. // 活动状态 1:已发布 2:待审核 3:未通过 4:草稿 9:已删除
  62. if activity.Status == 1 { // 已发布/已结束
  63. json = this._convertToPublishedActivityViewJSON(activity)
  64. } else if activity.Status == 3 { // 未通过
  65. json = this._convertToUnapprovedActivityViewJSON(activity)
  66. } else if activity.Status == 4 { // 草稿
  67. json = this._convertToActivityDraftsViewJSON(activity)
  68. }
  69. activityJSONList = append(activityJSONList, json)
  70. }
  71. this.ServeSuccessJSON(map[string]interface{}{
  72. "activities": activityJSONList,
  73. "total": totalCount,
  74. })
  75. }
  76. func (this *ActivityAPIController) _convertToPublishedActivityViewJSON(activity *models.Activity) map[string]interface{} {
  77. json := make(map[string]interface{})
  78. now := time.Now().Unix()
  79. if activity.StartTime < now {
  80. json["status"] = 4
  81. } else {
  82. json["status"] = 1
  83. }
  84. json["id"] = activity.Id
  85. json["poster_photo"] = activity.PosterPhoto
  86. json["title"] = activity.Title
  87. json["is_recommend"] = activity.IsRecommend
  88. json["read_num"] = activity.ReadNum
  89. json["comment_num"] = activity.CommentNum
  90. json["star_num"] = activity.StarNum
  91. json["join_num"] = activity.JoinNum
  92. json["limit_num"] = activity.LimitNum
  93. json["start_time"] = activity.StartTime
  94. return json
  95. }
  96. func (this *ActivityAPIController) _convertToActivityDraftsViewJSON(activity *models.Activity) map[string]interface{} {
  97. json := make(map[string]interface{})
  98. json["status"] = 2
  99. json["id"] = activity.Id
  100. json["poster_photo"] = activity.PosterPhoto
  101. json["title"] = activity.Title
  102. return json
  103. }
  104. func (this *ActivityAPIController) _convertToUnapprovedActivityViewJSON(activity *models.Activity) map[string]interface{} {
  105. json := make(map[string]interface{})
  106. json["status"] = 3
  107. json["id"] = activity.Id
  108. json["poster_photo"] = activity.PosterPhoto
  109. json["title"] = activity.Title
  110. json["reason"] = activity.Reason
  111. json["start_time"] = activity.StartTime
  112. return json
  113. }
  114. // /api/activity [get]
  115. // @param id:int
  116. func (this *ActivityAPIController) GetActivity() {
  117. activityID, _ := this.GetInt64("id")
  118. if activityID <= 0 {
  119. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  120. return
  121. }
  122. adminUserInfo := this.GetAdminUserInfo()
  123. activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID)
  124. if getActivityErr != nil {
  125. this.ErrorLog("获取活动失败:%v", getActivityErr)
  126. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  127. return
  128. } else if activity == nil {
  129. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  130. return
  131. }
  132. paragraph, getParagraphErr := marketing_tool_service.GetActivityParagraphByActivityID(activity.Id)
  133. if getParagraphErr != nil {
  134. this.ErrorLog("获取活动段落失败:%v", getParagraphErr)
  135. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  136. return
  137. }
  138. paragraphTitle := ""
  139. paragraphContent := ""
  140. if paragraph != nil {
  141. paragraphTitle = paragraph.Title
  142. paragraphContent = paragraph.Text
  143. }
  144. currentOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  145. this.ServeSuccessJSON(map[string]interface{}{
  146. "activity": activity,
  147. "paragraph": map[string]interface{}{
  148. "title": paragraphTitle,
  149. "content": paragraphContent,
  150. },
  151. "org": map[string]interface{}{
  152. "logo": currentOrg.OrgLogo,
  153. "name": currentOrg.OrgName,
  154. },
  155. })
  156. }
  157. // /api/activity/submit [post]
  158. // @param publish:bool 是否发布
  159. // @data 如下格式
  160. /*
  161. {
  162. id?:int
  163. title:string
  164. subtitle:string
  165. poster_photo:string
  166. address:string
  167. limit_num?:int
  168. sign_up_deadline:string ("yyyy-MM-dd HH:mm")
  169. start_time:string ("yyyy-MM-dd HH:mm")
  170. phone_number?:string
  171. sign_up_notice?:string
  172. paragraph: {
  173. title:string
  174. content:string
  175. }
  176. }
  177. */
  178. func (this *ActivityAPIController) ActivitySubmit() {
  179. publish, _ := this.GetBool("publish")
  180. var activityForm ActivityForm
  181. parseErr := json.Unmarshal(this.Ctx.Input.RequestBody, &activityForm)
  182. if parseErr != nil {
  183. this.ErrorLog("解析 activity form 失败:%v", parseErr)
  184. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  185. return
  186. }
  187. adminUserInfo := this.GetAdminUserInfo()
  188. var activity *models.Activity
  189. if activityForm.ID > 0 {
  190. var getActivityErr error
  191. activity, getActivityErr = marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityForm.ID)
  192. if getActivityErr != nil {
  193. this.ErrorLog("获取活动失败:%v", getActivityErr)
  194. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  195. return
  196. } else if activity == nil || activity.Status == 9 {
  197. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  198. return
  199. } else if activity.Status == 1 {
  200. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityPublishedCannotModify)
  201. return
  202. }
  203. }
  204. if len(activityForm.Title) == 0 || len(activityForm.Subtitle) == 0 || len(activityForm.PosterPhoto) == 0 || len(activityForm.Address) == 0 || len(activityForm.SignUpDeadline) == 0 || len(activityForm.StartTime) == 0 {
  205. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  206. return
  207. }
  208. timeFmt := "2006-01-02 15:04"
  209. signUpDeadlineTime, parseSUDErr := utils.ParseTimeStringToTime(timeFmt, activityForm.SignUpDeadline)
  210. if parseSUDErr != nil {
  211. this.ErrorLog("解析报名截止时间失败:%v", parseSUDErr)
  212. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  213. return
  214. }
  215. activityTime, parseActivityTimeErr := utils.ParseTimeStringToTime(timeFmt, activityForm.StartTime)
  216. if parseActivityTimeErr != nil {
  217. this.ErrorLog("解析活动开始时间失败:%v", parseActivityTimeErr)
  218. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  219. return
  220. }
  221. if len(activityForm.PhoneNumber) > 0 {
  222. if utils.PhoneRegexp().MatchString(activityForm.PhoneNumber) == false {
  223. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  224. return
  225. }
  226. }
  227. if activityForm.LimitNum < 0 {
  228. activityForm.LimitNum = 0
  229. }
  230. if activity == nil {
  231. activity = &models.Activity{
  232. UserOrgId: adminUserInfo.CurrentOrgId,
  233. UserAppId: adminUserInfo.CurrentAppId,
  234. Title: activityForm.Title,
  235. Subtitle: activityForm.Subtitle,
  236. Address: activityForm.Address,
  237. SignUpDeadline: signUpDeadlineTime.Unix(),
  238. SignUpNotice: activityForm.SignUpNotice,
  239. StartTime: activityTime.Unix(),
  240. PosterPhoto: activityForm.PosterPhoto,
  241. PosterPhotoThumb: activityForm.PosterPhoto,
  242. LimitNum: activityForm.LimitNum,
  243. PhoneNumber: activityForm.PhoneNumber,
  244. CreateTime: time.Now().Unix(),
  245. }
  246. } else {
  247. activity.Title = activityForm.Title
  248. activity.Subtitle = activityForm.Subtitle
  249. activity.Address = activityForm.Address
  250. activity.SignUpDeadline = signUpDeadlineTime.Unix()
  251. activity.SignUpNotice = activityForm.SignUpNotice
  252. activity.StartTime = activityTime.Unix()
  253. activity.PosterPhoto = activityForm.PosterPhoto
  254. activity.PosterPhotoThumb = activityForm.PosterPhoto
  255. activity.LimitNum = activityForm.LimitNum
  256. activity.PhoneNumber = activityForm.PhoneNumber
  257. }
  258. activity.ModifyTime = time.Now().Unix()
  259. if publish {
  260. activity.Status = 1
  261. } else {
  262. activity.Status = 4
  263. }
  264. var paragraph *models.ActivityParagraph
  265. if activity.Id > 0 {
  266. var getPErr error
  267. paragraph, getPErr = marketing_tool_service.GetActivityParagraphByActivityID(activity.Id)
  268. if getPErr != nil {
  269. this.ErrorLog("获取活动段落失败:%v", getPErr)
  270. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  271. return
  272. }
  273. }
  274. if len(activityForm.Paragraph.Title) > 0 {
  275. if paragraph == nil {
  276. paragraph = &models.ActivityParagraph{
  277. Title: activityForm.Paragraph.Title,
  278. Text: activityForm.Paragraph.Content,
  279. Status: 1,
  280. CreateTime: time.Now().Unix(),
  281. ModifyTime: time.Now().Unix(),
  282. }
  283. } else {
  284. paragraph.Title = activityForm.Paragraph.Title
  285. paragraph.Text = activityForm.Paragraph.Content
  286. paragraph.ModifyTime = time.Now().Unix()
  287. }
  288. } else {
  289. if paragraph != nil {
  290. paragraph.Status = 0
  291. paragraph.ModifyTime = time.Now().Unix()
  292. }
  293. }
  294. saveErr := marketing_tool_service.SaveActivityAndParagraph(activity, paragraph)
  295. if saveErr != nil {
  296. this.ErrorLog("保存活动失败:%v", saveErr)
  297. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  298. return
  299. }
  300. wxShareModel, getWxShareErr := marketing_tool_service.GetActivityWxShareByActivityID(activity.Id)
  301. if getWxShareErr != nil {
  302. this.ErrorLog("获取活动微信分享信息失败:%v", getWxShareErr)
  303. } else if wxShareModel != nil {
  304. wxShareModel.ModifyTime = time.Now().Unix()
  305. wxShareModel.Status = 1
  306. wxShareModel.Title = activity.Title
  307. wxShareModel.Subtitle = activity.Subtitle
  308. wxShareModel.Image = activity.PosterPhoto
  309. saveWxShareErr := marketing_tool_service.SaveActivityWxShare(wxShareModel)
  310. if saveWxShareErr != nil {
  311. this.ErrorLog("更新活动微信分享信息失败:%v", saveWxShareErr)
  312. }
  313. }
  314. this.ServeSuccessJSON(map[string]interface{}{
  315. "activity_id": activity.Id,
  316. })
  317. }
  318. // /api/activity/detailview [get]
  319. // @param id:int
  320. func (this *ActivityAPIController) GetActivityDetailView() {
  321. activityID, _ := this.GetInt64("id")
  322. if activityID <= 0 {
  323. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  324. return
  325. }
  326. adminUserInfo := this.GetAdminUserInfo()
  327. activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID)
  328. if getActivityErr != nil {
  329. this.ErrorLog("获取活动失败:%v", getActivityErr)
  330. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  331. return
  332. } else if activity == nil {
  333. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  334. return
  335. }
  336. if activity.Status != 1 && activity.Status != 4 {
  337. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  338. return
  339. }
  340. signupUsers, _, getUsersErr := marketing_tool_service.GetValidActivitySignupUsers(activityID, "", 1)
  341. if getUsersErr != nil {
  342. this.ErrorLog("获取活动报名客户失败:%v", getUsersErr)
  343. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  344. return
  345. }
  346. this.ServeSuccessJSON(map[string]interface{}{
  347. "activity": activity,
  348. "users": signupUsers,
  349. })
  350. }
  351. // /api/activity/signups [get]
  352. // @param id:int
  353. // @param keyword?:string
  354. // @param page?:int
  355. func (this *ActivityAPIController) SignupUsers() {
  356. activityID, _ := this.GetInt64("id")
  357. if activityID <= 0 {
  358. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  359. return
  360. }
  361. page, _ := this.GetInt("page")
  362. if page <= 0 {
  363. page = 1
  364. }
  365. keyword := this.GetString("keyword")
  366. adminUserInfo := this.GetAdminUserInfo()
  367. activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID)
  368. if getActivityErr != nil {
  369. this.ErrorLog("获取活动失败:%v", getActivityErr)
  370. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  371. return
  372. } else if activity == nil {
  373. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  374. return
  375. }
  376. signupUsers, total, getUsersErr := marketing_tool_service.GetValidActivitySignupUsers(activityID, keyword, page)
  377. if getUsersErr != nil {
  378. this.ErrorLog("获取活动报名客户失败:%v", getUsersErr)
  379. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  380. return
  381. }
  382. this.ServeSuccessJSON(map[string]interface{}{
  383. "users": signupUsers,
  384. "total": total,
  385. })
  386. }
  387. // /api/activity/delete [post]
  388. // @param id:int
  389. func (this *ActivityAPIController) DeleteActivity() {
  390. activityID, _ := this.GetInt64("id")
  391. if activityID <= 0 {
  392. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  393. return
  394. }
  395. adminUserInfo := this.GetAdminUserInfo()
  396. activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID)
  397. if getActivityErr != nil {
  398. this.ErrorLog("获取活动失败:%v", getActivityErr)
  399. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  400. return
  401. } else if activity == nil || activity.Status == 9 {
  402. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  403. return
  404. }
  405. deleteErr := marketing_tool_service.DeleteActivity(adminUserInfo.CurrentOrgId, activityID)
  406. if deleteErr != nil {
  407. this.ErrorLog("删除活动失败:%v", deleteErr)
  408. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  409. return
  410. }
  411. this.ServeSuccessJSON(nil)
  412. }
  413. // /api/activity/publish [post]
  414. // @param id:int
  415. func (this *ActivityAPIController) PublishActivity() {
  416. activityID, _ := this.GetInt64("id")
  417. if activityID <= 0 {
  418. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  419. return
  420. }
  421. adminUserInfo := this.GetAdminUserInfo()
  422. activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID)
  423. if getActivityErr != nil {
  424. this.ErrorLog("获取活动失败:%v", getActivityErr)
  425. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  426. return
  427. } else if activity == nil || activity.Status == 9 {
  428. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  429. return
  430. }
  431. if activity.Status == 1 {
  432. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityPublishRepeat)
  433. return
  434. }
  435. activity.Status = 1
  436. activity.ModifyTime = time.Now().Unix()
  437. updateErr := marketing_tool_service.SaveActivityAndParagraph(activity, nil)
  438. if updateErr != nil {
  439. this.ErrorLog("发布活动失败:%v", updateErr)
  440. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  441. return
  442. }
  443. this.ServeSuccessJSON(nil)
  444. }
  445. // /api/activity/shareinfo [get]
  446. // @param id:int
  447. func (this *ActivityAPIController) GetActivityShareInfo() {
  448. activityID, _ := this.GetInt64("id")
  449. if activityID <= 0 {
  450. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  451. return
  452. }
  453. adminUserInfo := this.GetAdminUserInfo()
  454. activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID)
  455. if getActivityErr != nil {
  456. this.ErrorLog("获取活动失败:%v", getActivityErr)
  457. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  458. return
  459. } else if activity == nil {
  460. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist)
  461. return
  462. }
  463. wxShareModel, getWxShareErr := marketing_tool_service.GetActivityWxShareByActivityID(activity.Id)
  464. if getWxShareErr != nil || wxShareModel == nil {
  465. this.ErrorLog("获取活动微信分享信息失败:%v", getWxShareErr)
  466. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  467. return
  468. }
  469. this.ServeSuccessJSON(map[string]interface{}{
  470. "url": wxShareModel.ShortURL,
  471. })
  472. }