activity_special_trip.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/cygx"
  9. "hongze/hz_crm_api/models/system"
  10. "hongze/hz_crm_api/services"
  11. cygxService "hongze/hz_crm_api/services/cygx"
  12. "hongze/hz_crm_api/utils"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // 专项调研行程
  20. type ActivitySpecialTripCoAntroller struct {
  21. controllers.BaseAuthController
  22. }
  23. // @Title 新增报名
  24. // @Description 新增报名接口
  25. // @Param request body cygx.AddMeetingReminderReq true "type json string"
  26. // @Success 200 操作成功
  27. // @router /special/trip/addUser [post]
  28. func (this *ActivitySpecialTripCoAntroller) AddUser() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. AdminUser := this.SysUser
  35. if AdminUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. return
  39. }
  40. var req cygx.AddMeetingReminderReq
  41. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  42. if err != nil {
  43. br.Msg = "参数解析异常!"
  44. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  45. return
  46. }
  47. var items []*cygx.CygxActivitySpecialTrip
  48. var itemsBill []*cygx.CygxActivitySpecialTripBill
  49. var itemsMeet []*cygx.CygxActivitySpecialMeetingDetail
  50. activityIds := req.ActivityIds
  51. uidList := req.List
  52. if activityIds == "" {
  53. br.Msg = "请选择活动"
  54. br.ErrMsg = "活动ID不能为空"
  55. return
  56. }
  57. activityIdList := strings.Split(activityIds, ",")
  58. var uids string
  59. for _, v := range uidList {
  60. uids += strconv.Itoa(v.UserId) + ","
  61. }
  62. uids = strings.TrimRight(uids, ",")
  63. for i, v := range uidList {
  64. uid := v.UserId
  65. wxUser, userErr := models.GetWxUserByUserId(uid)
  66. if userErr != nil {
  67. br.Msg = "编辑失败!查询用户信息失败"
  68. br.ErrMsg = "查询用户信息失败,Err:" + userErr.Error() + "用户UID:" + strconv.Itoa(uid) + "不存在"
  69. return
  70. }
  71. for _, vact := range activityIdList {
  72. activityId, _ := strconv.Atoi(vact)
  73. activityInfo, err := cygx.GetAddActivityInfoSpecialById(activityId)
  74. if err != nil {
  75. br.Msg = "活动不存在"
  76. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  77. return
  78. }
  79. limitPeopleNum := activityInfo.LimitPeopleNum
  80. if limitPeopleNum > 0 {
  81. if AdminUser.RoleTypeCode != "admin" {
  82. havePower, err := cygxService.GetSpecialDetailUserPower(wxUser, activityInfo)
  83. if err != nil {
  84. br.Msg = "用户权限校验失败!"
  85. br.ErrMsg = "GetActivityDetailUserPower,Err:" + err.Error() + fmt.Sprint("UserId", wxUser.UserId, "ActivityId:", activityInfo.ActivityId)
  86. return
  87. }
  88. if !havePower {
  89. br.Msg = "当前活动对该客户不可见,无法报名"
  90. br.ErrMsg = "活动ID:" + strconv.Itoa(activityId) + "活动名称:" + activityInfo.ResearchTheme + "用户ID:" + strconv.Itoa(int(wxUser.UserId))
  91. return
  92. }
  93. }
  94. //获取这个活动已经报名的用户数量
  95. totalSignup, errSignup := cygx.GetActivitySpecialTripCountByActivityId(activityId)
  96. if errSignup != nil {
  97. br.Msg = "获取失败"
  98. br.ErrMsg = "获取失败,Err:" + errSignup.Error()
  99. return
  100. }
  101. //获取这个活动中输入的这些用户的报名数量
  102. totalThisUser, errThisUser := cygx.GetActivitySpecialTripCountByThisUser(activityId, uids)
  103. if errThisUser != nil {
  104. br.Msg = "获取失败"
  105. br.ErrMsg = "获取失败,Err:" + errThisUser.Error()
  106. return
  107. }
  108. //如果是限制人数的就做报名人数限制判断
  109. if limitPeopleNum < totalSignup+len(uidList)-totalThisUser {
  110. br.Msg = "新增失败,活动人数超限"
  111. br.ErrMsg = "当前活动报名人数已满,活动:" + activityInfo.ResearchTheme
  112. return
  113. }
  114. }
  115. total, errtotal := cygx.GetActivitySpecialTripCount(uid, activityId)
  116. if errtotal != nil {
  117. br.Msg = "获取失败"
  118. br.ErrMsg = "获取失败,Err:" + errtotal.Error()
  119. return
  120. }
  121. infoUser, err := cygx.GetUserAndCompanyNameList(uid)
  122. if err != nil {
  123. br.Msg = "获取失败"
  124. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  125. return
  126. }
  127. if total == 0 {
  128. totalAll, err := cygx.GetUserActivitySpecialTripCount(uid, activityId)
  129. if err != nil {
  130. br.Msg = "获取信息失败"
  131. br.ErrMsg = "获取日程数量信息失败,Err:" + err.Error()
  132. return
  133. }
  134. //流水记录表
  135. itemBill := new(cygx.CygxActivitySpecialTripBill)
  136. itemBill.UserId = infoUser.UserId
  137. itemBill.ActivityId = activityInfo.ActivityId
  138. itemBill.CreateTime = time.Now()
  139. itemBill.Mobile = infoUser.Mobile
  140. itemBill.Email = infoUser.Email
  141. itemBill.CompanyId = infoUser.CompanyId
  142. itemBill.CompanyName = infoUser.CompanyName
  143. itemBill.RealName = infoUser.RealName
  144. itemBill.Source = 2
  145. itemBill.DoType = 1
  146. itemBill.BillDetailed = -1 // 流水减一
  147. itemBill.RegisterPlatform = 1
  148. itemBill.ChartPermissionId = activityInfo.ChartPermissionId
  149. itemBill.AdminId = AdminUser.AdminId
  150. if totalAll == 0 {
  151. item := new(cygx.CygxActivitySpecialTrip)
  152. item.UserId = infoUser.UserId
  153. item.RealName = infoUser.RealName
  154. item.SellerName = infoUser.SellerName
  155. item.ActivityId = activityId
  156. item.CreateTime = time.Now()
  157. item.Mobile = infoUser.Mobile
  158. item.Email = infoUser.Email
  159. item.Email = infoUser.Email
  160. item.CompanyId = infoUser.CompanyId
  161. item.CompanyName = infoUser.CompanyName
  162. item.AdminId = AdminUser.AdminId
  163. item.Source = 2
  164. //优先绑定用户修改过的外呼手机号
  165. if infoUser.OutboundMobile != "" {
  166. item.OutboundMobile = infoUser.OutboundMobile
  167. if infoUser.OutboundCountryCode == "" {
  168. item.CountryCode = "86"
  169. } else {
  170. item.CountryCode = infoUser.OutboundCountryCode
  171. }
  172. } else {
  173. item.OutboundMobile = infoUser.Mobile
  174. if infoUser.CountryCode == "" {
  175. item.CountryCode = "86"
  176. } else {
  177. item.CountryCode = infoUser.CountryCode
  178. }
  179. }
  180. items = append(items, item)
  181. } else {
  182. err = cygx.UpdateSpecialTrip(1, 0, uid, activityId)
  183. if err != nil {
  184. br.Msg = "报名失败,"
  185. br.ErrMsg = "二次报名,更改报名是否有效状态失败,Err:" + err.Error()
  186. return
  187. }
  188. resultTime := utils.StrTimeToTime(activityInfo.ActivityTime)
  189. //48小时之内的取消也扣除一次参会记录
  190. if time.Now().Add(+time.Hour * 48).After(resultTime) {
  191. itemBill.BillDetailed = 0 //48小时之内,取消报名之后二次报名,不扣除流水记录
  192. }
  193. }
  194. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(infoUser.CompanyId)
  195. if err != nil {
  196. br.Msg = "获取专项调研剩余次数失败"
  197. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  198. return
  199. }
  200. if userType == 2 {
  201. tripRemaining = tripRemaining + itemBill.BillDetailed - i
  202. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  203. } else {
  204. for k, num := range mapChartName {
  205. if activityInfo.ChartPermissionName == k {
  206. num = num + itemBill.BillDetailed - i
  207. }
  208. itemBill.Total += k + strconv.Itoa(num) + "次+"
  209. }
  210. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  211. }
  212. itemsBill = append(itemsBill, itemBill)
  213. }
  214. var itemMeeting = new(cygx.CygxActivitySpecialMeetingDetail)
  215. itemMeeting.UserId = v.UserId
  216. itemMeeting.ActivityId = activityId
  217. itemMeeting.CreateTime = time.Now()
  218. itemMeeting.Mobile = infoUser.Mobile
  219. itemMeeting.Email = infoUser.Email
  220. itemMeeting.CompanyId = infoUser.CompanyId
  221. itemMeeting.CompanyName = infoUser.CompanyName
  222. itemMeeting.RealName = infoUser.RealName
  223. itemsMeet = append(itemsMeet, itemMeeting)
  224. }
  225. }
  226. err = cygx.AddCygxActivitySpecialTrip(items, itemsBill, itemsMeet)
  227. if err != nil {
  228. br.Msg = "操作失败"
  229. br.ErrMsg = "新增用户失败,Err:" + err.Error()
  230. return
  231. }
  232. br.Ret = 200
  233. br.Success = true
  234. br.Msg = "操作成功"
  235. br.IsAddLog = true
  236. }
  237. // @Title 报名列表
  238. // @Description 报名列表接口
  239. // @Param ActivityId query int true "活动ID"
  240. // @Param IsExport query bool false "是否导出excel,默认是false"
  241. // @Success 200 {object} cygx.GetAppointmentSpecialListRsep
  242. // @router /special/tripList [get]
  243. func (this *ActivitySpecialTripCoAntroller) TripList() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. sysUser := this.SysUser
  250. if sysUser == nil {
  251. br.Msg = "请登录"
  252. br.ErrMsg = "请登录,SysUser Is Empty"
  253. return
  254. }
  255. isExport, _ := this.GetBool("IsExport", false)
  256. activityId, _ := this.GetInt("ActivityId")
  257. if activityId < 1 {
  258. br.Msg = "活动不存在"
  259. return
  260. }
  261. respList := new(cygx.GetAppointmentSpecialListRsep)
  262. activityInfo, _ := cygx.GetAddActivityInfoSpecialById(activityId)
  263. if activityInfo == nil {
  264. br.Msg = "活动不存在"
  265. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  266. return
  267. }
  268. //超级管理员和权益管理员、权益研究员可以下载所有客户,销售组长能下载本组客户,销售只能下载本人名下客户
  269. resp := new(cygx.CanDownload)
  270. adminInfo, errAdmin := system.GetSysUserById(sysUser.AdminId)
  271. if errAdmin != nil {
  272. br.Msg = "获取失败"
  273. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  274. return
  275. }
  276. if adminInfo.Role == "admin" || adminInfo.Role == "researcher" {
  277. resp.IsCanDownload = true
  278. }
  279. memberType := "Admin"
  280. sqlStr := ` AND s.is_cancel = 0`
  281. totalStr := sqlStr
  282. list, err := cygx.GetCygxActivitySpecialTripList(activityId, sqlStr)
  283. if err != nil {
  284. br.Msg = "获取失败"
  285. br.ErrMsg = "获取失败,Err:" + err.Error()
  286. return
  287. }
  288. //线上活动,销售查看自己客户,销售组长查看组员
  289. if resp.IsCanDownload == false && activityInfo.SpecialType == 1 {
  290. mapMobile, err := cygxService.GetAdminLookUserMobile(adminInfo)
  291. if err != nil {
  292. br.Msg = "获取失败"
  293. br.ErrMsg = "获取失败,销售对应权限,Err:" + err.Error()
  294. return
  295. }
  296. for _, v := range list {
  297. if _, ok := mapMobile[v.Mobile]; ok {
  298. respList.List = append(respList.List, v)
  299. }
  300. }
  301. if adminInfo.RoleTypeCode == "rai_group" {
  302. //组长查看本组所有组员
  303. memberType = "GroupLeader"
  304. } else {
  305. //组员查看自己
  306. memberType = "Sale"
  307. }
  308. } else {
  309. respList.List = list
  310. }
  311. if len(respList.List) == 0 {
  312. respList.List = make([]*cygx.CygxActivitySpecialTripResp, 0)
  313. }
  314. //导出excel
  315. if isExport {
  316. TripListExport(this, respList.List)
  317. return
  318. }
  319. total, errtotal := cygx.GetActivitySpecialTripTotal(activityId, totalStr)
  320. if errtotal != nil {
  321. br.Msg = "获取失败"
  322. br.ErrMsg = "客户总数获取失败,Err:" + errtotal.Error()
  323. return
  324. }
  325. myTotal := len(respList.List)
  326. respList.Source = 1
  327. respList.Total = total
  328. respList.MyTotal = myTotal
  329. respList.ActivityId = activityId
  330. respList.SpecialType = activityInfo.SpecialType
  331. if activityInfo.LimitPeopleNum > 0 {
  332. respList.IsLimitPeople = 1
  333. }
  334. respList.MemberType = memberType
  335. br.Ret = 200
  336. br.Success = true
  337. br.Msg = "获取成功"
  338. br.Data = respList
  339. }
  340. // TripListExport 下载报名信息
  341. func TripListExport(this *ActivitySpecialTripCoAntroller, listDate []*cygx.CygxActivitySpecialTripResp) {
  342. br := new(models.BaseResponse).Init()
  343. defer func() {
  344. this.Data["json"] = br
  345. this.ServeJSON()
  346. }()
  347. sysUser := this.SysUser
  348. if sysUser == nil {
  349. br.Msg = "请登录"
  350. br.ErrMsg = "请登录,SysUser Is Empty"
  351. return
  352. }
  353. activityId, _ := this.GetInt("ActivityId")
  354. activityInfo, _ := cygx.GetAddActivityInfoSpecialById(activityId)
  355. if activityInfo == nil {
  356. br.Msg = "活动不存在"
  357. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  358. return
  359. }
  360. //超级管理员和权益管理员、权益研究员可以下载所有客户,销售组长能下载本组客户,销售只能下载本人名下客户
  361. resp := new(cygx.CanDownload)
  362. adminInfo, errAdmin := system.GetSysUserById(sysUser.AdminId)
  363. if errAdmin != nil {
  364. br.Msg = "获取失败"
  365. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  366. return
  367. }
  368. if adminInfo.Role == "admin" || adminInfo.Role == "researcher" {
  369. resp.IsCanDownload = true
  370. }
  371. //创建excel
  372. dir, err := os.Executable()
  373. exPath := filepath.Dir(dir)
  374. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  375. xlsxFile := xlsx.NewFile()
  376. if err != nil {
  377. br.Msg = "生成文件失败"
  378. br.ErrMsg = "生成文件失败"
  379. return
  380. }
  381. style := xlsx.NewStyle()
  382. alignment := xlsx.Alignment{
  383. Horizontal: "center",
  384. Vertical: "center",
  385. WrapText: true,
  386. }
  387. style.Alignment = alignment
  388. style.ApplyAlignment = true
  389. sheetName := "报名名单"
  390. sheet, err := xlsxFile.AddSheet(sheetName)
  391. if err != nil {
  392. br.Msg = "新增Sheet失败"
  393. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  394. return
  395. }
  396. //标头
  397. if activityInfo.SpecialType == 1 || resp.IsCanDownload {
  398. rowTitle := sheet.AddRow()
  399. cellA := rowTitle.AddCell()
  400. cellA.Value = "姓名"
  401. cellB := rowTitle.AddCell()
  402. cellB.Value = "手机号"
  403. cellC := rowTitle.AddCell()
  404. cellC.Value = "公司名称"
  405. cellD := rowTitle.AddCell()
  406. cellD.Value = "所属销售"
  407. for _, item := range listDate {
  408. row := sheet.AddRow()
  409. cellA := row.AddCell()
  410. cellA.Value = item.RealName
  411. cellB := row.AddCell()
  412. cellB.Value = item.Mobile
  413. cellC := row.AddCell()
  414. cellC.Value = item.CompanyName
  415. cellD := row.AddCell()
  416. cellD.Value = item.SellerName
  417. }
  418. } else {
  419. rowTitle := sheet.AddRow()
  420. cellA := rowTitle.AddCell()
  421. cellA.Value = "姓名"
  422. cellB := rowTitle.AddCell()
  423. cellB.Value = "公司名称"
  424. cellC := rowTitle.AddCell()
  425. cellC.Value = "所属销售"
  426. for _, item := range listDate {
  427. row := sheet.AddRow()
  428. cellA := row.AddCell()
  429. cellA.Value = item.RealName
  430. cellB := row.AddCell()
  431. cellB.Value = item.CompanyName
  432. cellC := row.AddCell()
  433. cellC.Value = item.SellerName
  434. }
  435. }
  436. err = xlsxFile.Save(downLoadnFilePath)
  437. if err != nil {
  438. br.Msg = "保存文件失败"
  439. br.ErrMsg = "保存文件失败"
  440. return
  441. }
  442. downloadFileName := activityInfo.ResearchTheme + ".xlsx"
  443. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  444. defer func() {
  445. os.Remove(downLoadnFilePath)
  446. }()
  447. br.Success = true
  448. br.Ret = 200
  449. br.IsAddLog = true
  450. }
  451. // @Title 取消报名
  452. // @Description 取消报名接口
  453. // @Param request body cygx.ActivitySpecialTripCancelReq true "type json string"
  454. // @Success 200 操作成功
  455. // @router /special/trip/cancel [post]
  456. func (this *ActivitySpecialTripCoAntroller) TripCancel() {
  457. br := new(models.BaseResponse).Init()
  458. defer func() {
  459. this.Data["json"] = br
  460. this.ServeJSON()
  461. }()
  462. sysUser := this.SysUser
  463. if sysUser == nil {
  464. br.Msg = "请登录"
  465. br.ErrMsg = "请登录,SysUser Is Empty"
  466. return
  467. }
  468. var req cygx.ActivitySpecialTripCancelReq
  469. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  470. if err != nil {
  471. br.Msg = "参数解析异常!"
  472. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  473. return
  474. }
  475. userId := req.UserId
  476. activityId := req.ActivityId
  477. total, err := cygx.GetActivitySpecialTripCountByThisUser(activityId, strconv.Itoa(userId))
  478. if err != nil {
  479. br.Msg = "获取失败"
  480. br.ErrMsg = "获取失败,Err:" + err.Error()
  481. return
  482. }
  483. if total == 0 {
  484. br.Msg = "获取报名信息失败"
  485. br.ErrMsg = "获取失败,activityId:" + strconv.Itoa(activityId)
  486. return
  487. }
  488. activityInfo, errInfo := cygx.GetAddActivityInfoSpecialById(activityId)
  489. if activityInfo == nil {
  490. br.Msg = "操作失败"
  491. br.ErrMsg = "活动ID错误,不存在activityId:" + strconv.Itoa(activityId)
  492. return
  493. }
  494. if errInfo != nil {
  495. br.Msg = "操作失败"
  496. br.ErrMsg = "操作失败,Err:" + errInfo.Error()
  497. return
  498. }
  499. infoUser, err := cygx.GetUserAndCompanyNameList(userId)
  500. if err != nil {
  501. br.Msg = "获取失败"
  502. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  503. return
  504. }
  505. //流水记录表
  506. itemBill := new(cygx.CygxActivitySpecialTripBill)
  507. itemBill.UserId = infoUser.UserId
  508. itemBill.ActivityId = activityInfo.ActivityId
  509. itemBill.CreateTime = time.Now()
  510. itemBill.Mobile = infoUser.Mobile
  511. itemBill.Email = infoUser.Email
  512. itemBill.CompanyId = infoUser.CompanyId
  513. itemBill.CompanyName = infoUser.CompanyName
  514. itemBill.RealName = infoUser.RealName
  515. itemBill.Source = 2
  516. itemBill.DoType = 2
  517. itemBill.BillDetailed = 1 // 流水加一
  518. itemBill.RegisterPlatform = 1
  519. itemBill.ChartPermissionId = activityInfo.ChartPermissionId
  520. itemBill.AdminId = sysUser.AdminId
  521. resultTime := utils.StrTimeToTime(activityInfo.ActivityTime)
  522. //48小时之内的取消也扣除一次参会记录
  523. var isValid int
  524. if time.Now().Add(+time.Hour * 48).After(resultTime) {
  525. isValid = 1
  526. itemBill.BillDetailed = 0 // 48小时之内取消的,照样扣点,流水不进行加一
  527. }
  528. err = cygx.ActivitySpecialTripCancel(isValid, activityId, userId)
  529. if err != nil {
  530. br.Msg = "修改失败"
  531. br.ErrMsg = "修改失败 Err:" + err.Error()
  532. return
  533. }
  534. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(infoUser.CompanyId)
  535. if err != nil {
  536. br.Msg = "获取专项调研剩余次数失败"
  537. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  538. return
  539. }
  540. if userType == 2 {
  541. tripRemaining += itemBill.BillDetailed
  542. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  543. } else {
  544. for k, num := range mapChartName {
  545. if activityInfo.ChartPermissionName == k {
  546. num += itemBill.BillDetailed
  547. }
  548. itemBill.Total += k + strconv.Itoa(num) + "次+"
  549. }
  550. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  551. }
  552. err = cygx.AddCygxActivitySpecialTripBill(itemBill)
  553. if err != nil {
  554. br.Msg = "修改失败"
  555. br.ErrMsg = "修改失败流水记录添加失败 Err:" + err.Error()
  556. return
  557. }
  558. br.Ret = 200
  559. br.Success = true
  560. br.Msg = "操作成功"
  561. br.IsAddLog = true
  562. }
  563. // @Title 到会详情/提交到会详情
  564. // @Description 报名详情列表接口
  565. // @Param ActivityId query int true "活动ID"
  566. // @Success 200 {object} cygx.GetAppointmentSpecialListRsep
  567. // @router /special/trip/meetDetial [get]
  568. func (this *ActivitySpecialTripCoAntroller) MeetDetial() {
  569. br := new(models.BaseResponse).Init()
  570. defer func() {
  571. this.Data["json"] = br
  572. this.ServeJSON()
  573. }()
  574. AdminUser := this.SysUser
  575. if AdminUser == nil {
  576. br.Msg = "请登录"
  577. br.ErrMsg = "请登录,SysUser Is Empty"
  578. return
  579. }
  580. activityId, _ := this.GetInt("ActivityId")
  581. if activityId < 1 {
  582. br.Msg = "活动不存在"
  583. return
  584. }
  585. activityInfo, err := cygx.GetAddActivityInfoSpecialById(activityId)
  586. if err != nil {
  587. br.Msg = "活动不存在"
  588. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  589. return
  590. }
  591. respList := new(cygx.SpecialMeetingDetailRespList)
  592. if activityInfo.IsSubmitMeeting == 0 {
  593. sqlStr := ` AND s.is_cancel = 0`
  594. list, err := cygx.GetCygxActivitySpecialTripList(activityId, sqlStr)
  595. if err != nil {
  596. br.Msg = "获取失败"
  597. br.ErrMsg = "获取失败,Err:" + err.Error()
  598. return
  599. }
  600. for _, v := range list {
  601. item := new(cygx.CygxActivitySpecialMeetingDetailResp)
  602. item.UserId = v.UserId
  603. item.RealName = v.RealName
  604. item.CompanyName = v.CompanyName
  605. respList.List = append(respList.List, item)
  606. }
  607. } else {
  608. respList.List, err = cygx.GetCygxActivitySpecialMeetingDetailListByActivity(activityId)
  609. if err != nil {
  610. br.Msg = "获取失败"
  611. br.ErrMsg = "获取失败,Err:" + err.Error()
  612. return
  613. }
  614. UserMap, err := cygxService.GetSpecialTripUserMap(activityId)
  615. if err != nil {
  616. br.Msg = "获取失败"
  617. br.ErrMsg = "获取失败,Err:" + err.Error()
  618. return
  619. }
  620. for k, v := range respList.List {
  621. if v.IsMeeting == 1 {
  622. if _, ok := UserMap[v.UserId]; !ok {
  623. respList.List[k].IsMeeting = 2
  624. }
  625. respList.List[k].Operation = true
  626. }
  627. }
  628. }
  629. br.Ret = 200
  630. br.Success = true
  631. br.Msg = "获取成功"
  632. br.Data = respList
  633. }
  634. // @Title 提交线下到会情况
  635. // @Description 提交线下到会情况接口
  636. // @Param request body cygx.MeetingDoRep true "type json string"
  637. // @Success 200 操作成功
  638. // @router /special/trip/meetingDo [post]
  639. func (this *ActivitySpecialTripCoAntroller) MeetingDo() {
  640. br := new(models.BaseResponse).Init()
  641. defer func() {
  642. this.Data["json"] = br
  643. this.ServeJSON()
  644. }()
  645. AdminUser := this.SysUser
  646. if AdminUser == nil {
  647. br.Msg = "请登录"
  648. br.ErrMsg = "请登录,用户信息为空"
  649. br.Ret = 408
  650. return
  651. }
  652. var req cygx.MeetingSpeciaDoRep
  653. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  654. if err != nil {
  655. br.Msg = "参数解析异常!"
  656. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  657. return
  658. }
  659. var userIdArr []int
  660. var newMeetingUserid = make(map[int]bool) //新提交的到会的额用户ID
  661. meetingUids := req.UserIds
  662. if len(meetingUids) == 0 {
  663. meetingUids = "0"
  664. } else {
  665. //过滤字段
  666. slice := strings.Split(meetingUids, ",")
  667. meetingUids = ""
  668. for _, v := range slice {
  669. if v != "" {
  670. meetingUids += v + ","
  671. userId, _ := strconv.Atoi(v)
  672. if userId > 0 {
  673. userIdArr = append(userIdArr, userId)
  674. newMeetingUserid[userId] = true
  675. }
  676. }
  677. }
  678. meetingUids = strings.TrimRight(meetingUids, ",")
  679. }
  680. activityId := req.ActivityId
  681. activityInfo, err := cygx.GetAddActivityInfoSpecialById(activityId)
  682. if err != nil {
  683. br.Msg = "活动不存在"
  684. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  685. return
  686. }
  687. //校验活动后台管理员、销售是否有修改权限
  688. havePower, popupMsg, err := cygxService.CheckActivitySpecialUpdatePower(AdminUser.AdminId, activityInfo)
  689. if err != nil {
  690. br.Msg = "获取管理员身份信息失败"
  691. br.ErrMsg = "获取管理员身份信息失败,Err:" + err.Error()
  692. return
  693. }
  694. if !havePower {
  695. br.Msg = popupMsg
  696. return
  697. }
  698. noMeetingUids, err := cygx.GetSpecialTripUserIds(activityId, meetingUids) //未到会的用户ID
  699. if err != nil {
  700. br.Msg = "获取信息失败"
  701. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  702. return
  703. }
  704. var allUids string
  705. if len(noMeetingUids) > 0 {
  706. allUids = noMeetingUids + "," + meetingUids
  707. } else {
  708. allUids = meetingUids
  709. }
  710. listUser, err := cygx.GetUserAndCompanyNameListByUids(allUids)
  711. if err != nil {
  712. br.Msg = "获取失败"
  713. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  714. return
  715. }
  716. UserMap, err := cygxService.GetSpecialTripUserMap(activityId)
  717. if err != nil {
  718. br.Msg = "获取失败"
  719. br.ErrMsg = "获取失败,Err:" + err.Error()
  720. return
  721. }
  722. var condition string
  723. var pars []interface{}
  724. condition += " AND is_meeting = 1 AND activity_id = ? "
  725. pars = append(pars, activityId)
  726. listOldMeetingDetail, err := cygx.GetCygxActivitySpecialMeetingDetailList(condition, pars)
  727. if err != nil && err.Error() != utils.ErrNoRow() {
  728. br.Msg = "获取失败"
  729. br.ErrMsg = "GetCygxActivitySpecialMeetingDetailList,Err:" + err.Error()
  730. return
  731. }
  732. //获取之前已经到会的人
  733. //var oldMeetingUserid = make(map[int]bool)
  734. pars = make([]interface{}, 0)
  735. condition = " AND activity_id = ? "
  736. pars = append(pars, activityId)
  737. listTripBill, err := cygx.GetCygxActivitySpecialTripBill(condition, pars)
  738. if err != nil && err.Error() != utils.ErrNoRow() {
  739. br.Msg = "获取失败"
  740. br.ErrMsg = "GetCygxActivitySpecialTripBill,Err:" + err.Error()
  741. return
  742. }
  743. //获取用户最后一次的扣点状态
  744. mapUserLastTripBill := make(map[int]*cygx.CygxActivitySpecialTripBill)
  745. for _, v := range listTripBill {
  746. mapUserLastTripBill[v.UserId] = v
  747. }
  748. var items []*cygx.CygxActivitySpecialMeetingDetail
  749. var itemsBill []*cygx.CygxActivitySpecialTripBill
  750. if len(listOldMeetingDetail) > 0 {
  751. for _, v := range listOldMeetingDetail {
  752. if mapUserLastTripBill[v.UserId] == nil {
  753. continue
  754. }
  755. //如果上一次空降的用户,这一次提交的时候没有带入,而且还被扣点了,那么就进行返点处理
  756. if !newMeetingUserid[v.UserId] && v.IsAirborne == 1 && mapUserLastTripBill[v.UserId].BillDetailed < 0 {
  757. var itemBill = new(cygx.CygxActivitySpecialTripBill)
  758. //流水记录表
  759. itemBill.UserId = v.UserId
  760. itemBill.ActivityId = activityInfo.ActivityId
  761. itemBill.CreateTime = time.Now()
  762. itemBill.Mobile = v.Mobile
  763. itemBill.Email = v.Email
  764. itemBill.CompanyId = v.CompanyId
  765. itemBill.CompanyName = v.CompanyName
  766. itemBill.RealName = v.RealName
  767. itemBill.Source = 2
  768. itemBill.DoType = 2
  769. itemBill.BillDetailed = 1 // 流水加一
  770. itemBill.RegisterPlatform = 1
  771. itemBill.ChartPermissionId = activityInfo.ChartPermissionId
  772. itemBill.AdminId = AdminUser.AdminId
  773. itemBill.Way = 2
  774. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(v.CompanyId)
  775. if err != nil {
  776. br.Msg = "获取专项调研剩余次数失败"
  777. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  778. return
  779. }
  780. if userType == 2 {
  781. tripRemaining += itemBill.BillDetailed
  782. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  783. } else {
  784. for k, num := range mapChartName {
  785. if activityInfo.ChartPermissionName == k {
  786. num += itemBill.BillDetailed
  787. }
  788. itemBill.Total += k + strconv.Itoa(num) + "次+"
  789. }
  790. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  791. }
  792. itemsBill = append(itemsBill, itemBill)
  793. }
  794. }
  795. }
  796. for _, v := range listUser {
  797. var item = new(cygx.CygxActivitySpecialMeetingDetail)
  798. item.UserId = v.UserId
  799. item.ActivityId = activityId
  800. item.CreateTime = time.Now()
  801. item.Mobile = v.Mobile
  802. item.Email = v.Email
  803. item.CompanyId = v.CompanyId
  804. item.CompanyName = v.CompanyName
  805. item.IsMeeting = 1
  806. item.RealName = v.RealName
  807. //添加空降人员字段
  808. if _, ok := UserMap[v.UserId]; !ok {
  809. item.IsAirborne = 1
  810. }
  811. var itemBill = new(cygx.CygxActivitySpecialTripBill)
  812. //如果是空降客户,(没有扣点记录,或者上一次的流水不为负) 就进行扣点处理
  813. if item.IsAirborne == 1 && (mapUserLastTripBill[v.UserId] == nil || mapUserLastTripBill[v.UserId].BillDetailed >= 0) {
  814. //流水记录表
  815. itemBill.UserId = v.UserId
  816. itemBill.ActivityId = activityInfo.ActivityId
  817. itemBill.CreateTime = time.Now()
  818. itemBill.Mobile = v.Mobile
  819. itemBill.Email = v.Email
  820. itemBill.CompanyId = v.CompanyId
  821. itemBill.CompanyName = v.CompanyName
  822. itemBill.RealName = v.RealName
  823. itemBill.Source = 2
  824. itemBill.DoType = 1
  825. itemBill.BillDetailed = -1 // 流水减一
  826. itemBill.RegisterPlatform = 1
  827. itemBill.ChartPermissionId = activityInfo.ChartPermissionId
  828. itemBill.AdminId = AdminUser.AdminId
  829. itemBill.Way = 2
  830. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(v.CompanyId)
  831. if err != nil {
  832. br.Msg = "获取专项调研剩余次数失败"
  833. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  834. return
  835. }
  836. if userType == 2 {
  837. tripRemaining -= 1
  838. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  839. } else {
  840. for k, num := range mapChartName {
  841. if activityInfo.ChartPermissionName == k {
  842. num -= 1
  843. }
  844. itemBill.Total += k + strconv.Itoa(num) + "次+"
  845. }
  846. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  847. }
  848. itemsBill = append(itemsBill, itemBill)
  849. }
  850. items = append(items, item)
  851. }
  852. err = cygx.MeetingDopecialMeet(meetingUids, noMeetingUids, activityId, items, itemsBill)
  853. if err != nil {
  854. br.Msg = "操作失败"
  855. br.ErrMsg = "操作失败,Err:" + err.Error()
  856. return
  857. }
  858. go cygxService.ActivitySpecialUserLabelLogAdd(activityId, userIdArr)
  859. //添加操作日志记录
  860. br.Ret = 200
  861. br.Success = true
  862. br.Msg = "操作成功"
  863. br.IsAddLog = true
  864. }
  865. // @Title 修改外呼号码
  866. // @Description 修改外呼号码接口
  867. // @Param request body cygx.OutboundMobileEditResp true "type json string"
  868. // @Success 200 操作成功
  869. // @router /special/trip/outboundMobileEdit [post]
  870. func (this *ActivitySpecialTripCoAntroller) OutboundMobileEdit() {
  871. br := new(models.BaseResponse).Init()
  872. defer func() {
  873. this.Data["json"] = br
  874. this.ServeJSON()
  875. }()
  876. sysUser := this.SysUser
  877. if sysUser == nil {
  878. br.Msg = "请登录"
  879. br.ErrMsg = "请登录,SysUser Is Empty"
  880. return
  881. }
  882. var req cygx.OutboundMobileEditResp
  883. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  884. if err != nil {
  885. br.Msg = "参数解析异常!"
  886. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  887. return
  888. }
  889. id := req.Id
  890. outboundMobile := req.OutboundMobile
  891. countryCode := req.CountryCode
  892. err = cygx.SpecialTripOutboundMobileEdit(id, outboundMobile, countryCode)
  893. if err != nil {
  894. br.Msg = "修改失败"
  895. br.ErrMsg = "修改失败 Err:" + err.Error()
  896. return
  897. }
  898. br.Ret = 200
  899. br.Success = true
  900. br.Msg = "操作成功"
  901. br.IsAddLog = true
  902. }
  903. // @Title 模版消息发送客户类型列表
  904. // @Description 模版消息发送客户类型列表接口
  905. // @Success Ret=200
  906. // @router /special/trip/tempMsg/sendGroupList [get]
  907. func (this *ActivitySpecialTripCoAntroller) SendGroupList() {
  908. br := new(models.BaseResponse).Init()
  909. defer func() {
  910. this.Data["json"] = br
  911. this.ServeJSON()
  912. }()
  913. AdminUser := this.SysUser
  914. if AdminUser == nil {
  915. br.Msg = "请登录"
  916. br.ErrMsg = "请登录,SysUser Is Empty"
  917. return
  918. }
  919. list := make([]cygx.SendGroup, 0)
  920. list = append(list, cygx.SendGroup{
  921. Id: 1,
  922. Name: "已报名客户",
  923. })
  924. br.Ret = 200
  925. br.Success = true
  926. br.Data = list
  927. }
  928. // @Title 发送模版消息
  929. // @Description 发送模版消息接口
  930. // @Param request body cygx.AddOutboundPersonnelItm true "type json string"
  931. // @Success 200 操作成功
  932. // @router /special/trip/tempMsg [post]
  933. func (this *ActivitySpecialTripCoAntroller) TempMsg() {
  934. br := new(models.BaseResponse).Init()
  935. defer func() {
  936. this.Data["json"] = br
  937. this.ServeJSON()
  938. }()
  939. AdminUser := this.SysUser
  940. if AdminUser == nil {
  941. br.Msg = "请登录"
  942. br.ErrMsg = "请登录,SysUser Is Empty"
  943. return
  944. }
  945. var req cygx.ActivitySpecialSignupTempMsgReq
  946. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  947. if err != nil {
  948. br.Msg = "参数解析异常!"
  949. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  950. return
  951. }
  952. idSlice := strings.Split(req.ActivityIds, ",")
  953. for _, sId := range idSlice {
  954. id, e := strconv.Atoi(sId)
  955. if e != nil {
  956. br.Msg = "活动Id参数异常"
  957. br.ErrMsg = "参数解析异常, Err:" + e.Error()
  958. return
  959. }
  960. var openIdList []*cygx.OpenIdList
  961. idMap := make(map[string]string, 0)
  962. //1已报名
  963. if strings.Contains(req.SendGroup, "1") {
  964. listSignup, err := cygx.GetCygxActivitySpecialTripListByActivityId(id)
  965. if err != nil {
  966. br.Msg = "查询报名信息失败失败"
  967. br.ErrMsg = "GetCygxActivitySpecialTripListByActivityId,Err:" + err.Error()
  968. return
  969. }
  970. if len(listSignup) == 0 {
  971. continue
  972. }
  973. var mobileArr []string
  974. for _, v := range listSignup {
  975. if v.Mobile != "" {
  976. mobileArr = append(mobileArr, v.Mobile)
  977. }
  978. }
  979. mobileLen := len(mobileArr)
  980. var condition string
  981. var pars []interface{}
  982. if mobileLen > 0 {
  983. condition += ` AND u.mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  984. pars = append(pars, mobileArr)
  985. }
  986. //list, err := models.GetActivitySpecialOpenIdListMobile(condition, pars)
  987. //if err != nil {
  988. // br.Msg = "查询openId失败"
  989. // br.ErrMsg = "查询openId失败,Err:" + err.Error()
  990. // return
  991. //}
  992. if mobileLen == 0 {
  993. return
  994. }
  995. list, err := cygx.GetUserRecordListByMobileArr(mobileArr)
  996. for _, idList := range list {
  997. openIdList = append(openIdList, idList)
  998. idMap[idList.OpenId] = idList.OpenId
  999. }
  1000. }
  1001. if len(openIdList) > 0 {
  1002. openIdArr := make([]string, len(openIdList))
  1003. for i, v := range openIdList {
  1004. openIdArr[i] = v.OpenId
  1005. }
  1006. sendInfo := new(services.SendWxTemplate)
  1007. sendInfo.Keyword1 = req.ResearchTheme
  1008. sendInfo.Keyword2 = req.Content
  1009. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  1010. sendInfo.RedirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(id)
  1011. if utils.RunMode == "debug" {
  1012. sendInfo.RedirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(110) //测试环境调正式的ID
  1013. }
  1014. sendInfo.RedirectTarget = 3
  1015. sendInfo.Resource = strconv.Itoa(id)
  1016. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_CUSTOMIZE
  1017. sendInfo.OpenIdArr = openIdArr
  1018. e = services.SendTemplateMsg(sendInfo)
  1019. if e != nil {
  1020. br.Msg = "推送模板消息失败!"
  1021. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1022. return
  1023. }
  1024. }
  1025. }
  1026. br.Ret = 200
  1027. br.Success = true
  1028. br.Msg = "发送成功"
  1029. }