activity_special_trip.go 32 KB

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