activity_special_trip.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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.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.ChartPermissionName = activityInfo.ChartPermissionName
  521. itemBill.AdminId = sysUser.AdminId
  522. resultTime := utils.StrTimeToTime(activityInfo.ActivityTime)
  523. //48小时之内的取消也扣除一次参会记录
  524. var isValid int
  525. if time.Now().Add(+time.Hour * 48).After(resultTime) {
  526. isValid = 1
  527. itemBill.BillDetailed = 0 // 48小时之内取消的,照样扣点,流水不进行加一
  528. }
  529. err = cygx.ActivitySpecialTripCancel(isValid, activityId, userId)
  530. if err != nil {
  531. br.Msg = "修改失败"
  532. br.ErrMsg = "修改失败 Err:" + err.Error()
  533. return
  534. }
  535. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(infoUser.CompanyId)
  536. if err != nil {
  537. br.Msg = "获取专项调研剩余次数失败"
  538. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  539. return
  540. }
  541. if userType == 2 {
  542. tripRemaining += itemBill.BillDetailed
  543. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  544. } else {
  545. for k, num := range mapChartName {
  546. if activityInfo.ChartPermissionName == k {
  547. num += itemBill.BillDetailed
  548. }
  549. itemBill.Total += k + strconv.Itoa(num) + "次+"
  550. }
  551. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  552. }
  553. err = cygx.AddCygxActivitySpecialTripBill(itemBill)
  554. if err != nil {
  555. br.Msg = "修改失败"
  556. br.ErrMsg = "修改失败流水记录添加失败 Err:" + err.Error()
  557. return
  558. }
  559. br.Ret = 200
  560. br.Success = true
  561. br.Msg = "操作成功"
  562. br.IsAddLog = true
  563. }
  564. // @Title 到会详情/提交到会详情
  565. // @Description 报名详情列表接口
  566. // @Param ActivityId query int true "活动ID"
  567. // @Success 200 {object} cygx.GetAppointmentSpecialListRsep
  568. // @router /special/trip/meetDetial [get]
  569. func (this *ActivitySpecialTripCoAntroller) MeetDetial() {
  570. br := new(models.BaseResponse).Init()
  571. defer func() {
  572. this.Data["json"] = br
  573. this.ServeJSON()
  574. }()
  575. AdminUser := this.SysUser
  576. if AdminUser == nil {
  577. br.Msg = "请登录"
  578. br.ErrMsg = "请登录,SysUser Is Empty"
  579. return
  580. }
  581. activityId, _ := this.GetInt("ActivityId")
  582. if activityId < 1 {
  583. br.Msg = "活动不存在"
  584. return
  585. }
  586. activityInfo, err := cygx.GetAddActivityInfoSpecialById(activityId)
  587. if err != nil {
  588. br.Msg = "活动不存在"
  589. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  590. return
  591. }
  592. respList := new(cygx.SpecialMeetingDetailRespList)
  593. if activityInfo.IsSubmitMeeting == 0 {
  594. sqlStr := ` AND s.is_cancel = 0`
  595. list, err := cygx.GetCygxActivitySpecialTripList(activityId, sqlStr)
  596. if err != nil {
  597. br.Msg = "获取失败"
  598. br.ErrMsg = "获取失败,Err:" + err.Error()
  599. return
  600. }
  601. for _, v := range list {
  602. item := new(cygx.CygxActivitySpecialMeetingDetailResp)
  603. item.UserId = v.UserId
  604. item.RealName = v.RealName
  605. item.CompanyName = v.CompanyName
  606. respList.List = append(respList.List, item)
  607. }
  608. } else {
  609. respList.List, err = cygx.GetCygxActivitySpecialMeetingDetailListByActivity(activityId)
  610. if err != nil {
  611. br.Msg = "获取失败"
  612. br.ErrMsg = "获取失败,Err:" + err.Error()
  613. return
  614. }
  615. UserMap, err := cygxService.GetSpecialTripUserMap(activityId)
  616. if err != nil {
  617. br.Msg = "获取失败"
  618. br.ErrMsg = "获取失败,Err:" + err.Error()
  619. return
  620. }
  621. for k, v := range respList.List {
  622. if v.IsMeeting == 1 {
  623. if _, ok := UserMap[v.UserId]; !ok {
  624. respList.List[k].IsMeeting = 2
  625. }
  626. respList.List[k].Operation = true
  627. }
  628. }
  629. }
  630. br.Ret = 200
  631. br.Success = true
  632. br.Msg = "获取成功"
  633. br.Data = respList
  634. }
  635. // @Title 提交线下到会情况
  636. // @Description 提交线下到会情况接口
  637. // @Param request body cygx.MeetingDoRep true "type json string"
  638. // @Success 200 操作成功
  639. // @router /special/trip/meetingDo [post]
  640. func (this *ActivitySpecialTripCoAntroller) MeetingDo() {
  641. br := new(models.BaseResponse).Init()
  642. defer func() {
  643. this.Data["json"] = br
  644. this.ServeJSON()
  645. }()
  646. AdminUser := this.SysUser
  647. if AdminUser == nil {
  648. br.Msg = "请登录"
  649. br.ErrMsg = "请登录,用户信息为空"
  650. br.Ret = 408
  651. return
  652. }
  653. var req cygx.MeetingSpeciaDoRep
  654. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  655. if err != nil {
  656. br.Msg = "参数解析异常!"
  657. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  658. return
  659. }
  660. var userIdArr []int
  661. var newMeetingUserid = make(map[int]bool) //新提交的到会的额用户ID
  662. meetingUids := req.UserIds
  663. if len(meetingUids) == 0 {
  664. meetingUids = "0"
  665. } else {
  666. //过滤字段
  667. slice := strings.Split(meetingUids, ",")
  668. meetingUids = ""
  669. for _, v := range slice {
  670. if v != "" {
  671. meetingUids += v + ","
  672. userId, _ := strconv.Atoi(v)
  673. if userId > 0 {
  674. userIdArr = append(userIdArr, userId)
  675. newMeetingUserid[userId] = true
  676. }
  677. }
  678. }
  679. meetingUids = strings.TrimRight(meetingUids, ",")
  680. }
  681. activityId := req.ActivityId
  682. activityInfo, err := cygx.GetAddActivityInfoSpecialById(activityId)
  683. if err != nil {
  684. br.Msg = "活动不存在"
  685. br.ErrMsg = "活动ID错误,Err:activityId:" + strconv.Itoa(activityId)
  686. return
  687. }
  688. //校验活动后台管理员、销售是否有修改权限
  689. havePower, popupMsg, err := cygxService.CheckActivitySpecialUpdatePower(AdminUser.AdminId, activityInfo)
  690. if err != nil {
  691. br.Msg = "获取管理员身份信息失败"
  692. br.ErrMsg = "获取管理员身份信息失败,Err:" + err.Error()
  693. return
  694. }
  695. if !havePower {
  696. br.Msg = popupMsg
  697. return
  698. }
  699. noMeetingUids, err := cygx.GetSpecialTripUserIds(activityId, meetingUids) //未到会的用户ID
  700. if err != nil {
  701. br.Msg = "获取信息失败"
  702. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  703. return
  704. }
  705. var allUids string
  706. if len(noMeetingUids) > 0 {
  707. allUids = noMeetingUids + "," + meetingUids
  708. } else {
  709. allUids = meetingUids
  710. }
  711. listUser, err := cygx.GetUserAndCompanyNameListByUids(allUids)
  712. if err != nil {
  713. br.Msg = "获取失败"
  714. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  715. return
  716. }
  717. UserMap, err := cygxService.GetSpecialTripUserMap(activityId)
  718. if err != nil {
  719. br.Msg = "获取失败"
  720. br.ErrMsg = "获取失败,Err:" + err.Error()
  721. return
  722. }
  723. var condition string
  724. var pars []interface{}
  725. condition += " AND is_meeting = 1 AND activity_id = ? "
  726. pars = append(pars, activityId)
  727. listOldMeetingDetail, err := cygx.GetCygxActivitySpecialMeetingDetailList(condition, pars)
  728. if err != nil && err.Error() != utils.ErrNoRow() {
  729. br.Msg = "获取失败"
  730. br.ErrMsg = "GetCygxActivitySpecialMeetingDetailList,Err:" + err.Error()
  731. return
  732. }
  733. //获取之前已经到会的人
  734. //var oldMeetingUserid = make(map[int]bool)
  735. pars = make([]interface{}, 0)
  736. condition = " AND activity_id = ? "
  737. pars = append(pars, activityId)
  738. listTripBill, err := cygx.GetCygxActivitySpecialTripBill(condition, pars)
  739. if err != nil && err.Error() != utils.ErrNoRow() {
  740. br.Msg = "获取失败"
  741. br.ErrMsg = "GetCygxActivitySpecialTripBill,Err:" + err.Error()
  742. return
  743. }
  744. //获取用户最后一次的扣点状态
  745. mapUserLastTripBill := make(map[int]*cygx.CygxActivitySpecialTripBill)
  746. for _, v := range listTripBill {
  747. mapUserLastTripBill[v.UserId] = v
  748. }
  749. var items []*cygx.CygxActivitySpecialMeetingDetail
  750. var itemsBill []*cygx.CygxActivitySpecialTripBill
  751. if len(listOldMeetingDetail) > 0 {
  752. for _, v := range listOldMeetingDetail {
  753. if mapUserLastTripBill[v.UserId] == nil {
  754. continue
  755. }
  756. //如果上一次空降的用户,这一次提交的时候没有带入,而且还被扣点了,那么就进行返点处理
  757. if !newMeetingUserid[v.UserId] && v.IsAirborne == 1 && mapUserLastTripBill[v.UserId].BillDetailed < 0 {
  758. var itemBill = new(cygx.CygxActivitySpecialTripBill)
  759. //流水记录表
  760. itemBill.UserId = v.UserId
  761. itemBill.ActivityId = activityInfo.ActivityId
  762. itemBill.CreateTime = time.Now()
  763. itemBill.Mobile = v.Mobile
  764. itemBill.Email = v.Email
  765. itemBill.CompanyId = v.CompanyId
  766. itemBill.CompanyName = v.CompanyName
  767. itemBill.RealName = v.RealName
  768. itemBill.Source = 2
  769. itemBill.DoType = 2
  770. itemBill.BillDetailed = 1 // 流水加一
  771. itemBill.RegisterPlatform = 1
  772. itemBill.ChartPermissionId = activityInfo.ChartPermissionId
  773. itemBill.ChartPermissionName = activityInfo.ChartPermissionName
  774. itemBill.AdminId = AdminUser.AdminId
  775. itemBill.Way = 2
  776. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(v.CompanyId)
  777. if err != nil {
  778. br.Msg = "获取专项调研剩余次数失败"
  779. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  780. return
  781. }
  782. if userType == 2 {
  783. tripRemaining += itemBill.BillDetailed
  784. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  785. } else {
  786. for k, num := range mapChartName {
  787. if activityInfo.ChartPermissionName == k {
  788. num += itemBill.BillDetailed
  789. }
  790. itemBill.Total += k + strconv.Itoa(num) + "次+"
  791. }
  792. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  793. }
  794. itemsBill = append(itemsBill, itemBill)
  795. }
  796. }
  797. }
  798. for _, v := range listUser {
  799. var item = new(cygx.CygxActivitySpecialMeetingDetail)
  800. item.UserId = v.UserId
  801. item.ActivityId = activityId
  802. item.CreateTime = time.Now()
  803. item.Mobile = v.Mobile
  804. item.Email = v.Email
  805. item.CompanyId = v.CompanyId
  806. item.CompanyName = v.CompanyName
  807. item.IsMeeting = 1
  808. item.RealName = v.RealName
  809. //添加空降人员字段
  810. if _, ok := UserMap[v.UserId]; !ok {
  811. item.IsAirborne = 1
  812. }
  813. var itemBill = new(cygx.CygxActivitySpecialTripBill)
  814. //如果是空降客户,(没有扣点记录,或者上一次的流水不为负) 就进行扣点处理
  815. if item.IsAirborne == 1 && (mapUserLastTripBill[v.UserId] == nil || mapUserLastTripBill[v.UserId].BillDetailed >= 0) {
  816. //流水记录表
  817. itemBill.UserId = v.UserId
  818. itemBill.ActivityId = activityInfo.ActivityId
  819. itemBill.CreateTime = time.Now()
  820. itemBill.Mobile = v.Mobile
  821. itemBill.Email = v.Email
  822. itemBill.CompanyId = v.CompanyId
  823. itemBill.CompanyName = v.CompanyName
  824. itemBill.RealName = v.RealName
  825. itemBill.Source = 2
  826. itemBill.DoType = 1
  827. itemBill.BillDetailed = -1 // 流水减一
  828. itemBill.RegisterPlatform = 1
  829. itemBill.ChartPermissionId = activityInfo.ChartPermissionId
  830. itemBill.ChartPermissionName = activityInfo.ChartPermissionName
  831. itemBill.AdminId = AdminUser.AdminId
  832. itemBill.Way = 2
  833. userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(v.CompanyId)
  834. if err != nil {
  835. br.Msg = "获取专项调研剩余次数失败"
  836. br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
  837. return
  838. }
  839. if userType == 2 {
  840. tripRemaining -= 1
  841. itemBill.Total = strconv.Itoa(tripRemaining) + "次"
  842. } else {
  843. for k, num := range mapChartName {
  844. if activityInfo.ChartPermissionName == k {
  845. num -= 1
  846. }
  847. itemBill.Total += k + strconv.Itoa(num) + "次+"
  848. }
  849. itemBill.Total = strings.TrimRight(itemBill.Total, "+")
  850. }
  851. itemsBill = append(itemsBill, itemBill)
  852. }
  853. items = append(items, item)
  854. }
  855. err = cygx.MeetingDopecialMeet(meetingUids, noMeetingUids, activityId, items, itemsBill)
  856. if err != nil {
  857. br.Msg = "操作失败"
  858. br.ErrMsg = "操作失败,Err:" + err.Error()
  859. return
  860. }
  861. go cygxService.ActivitySpecialUserLabelLogAdd(activityId, userIdArr)
  862. //添加操作日志记录
  863. br.Ret = 200
  864. br.Success = true
  865. br.Msg = "操作成功"
  866. br.IsAddLog = true
  867. }
  868. // @Title 修改外呼号码
  869. // @Description 修改外呼号码接口
  870. // @Param request body cygx.OutboundMobileEditResp true "type json string"
  871. // @Success 200 操作成功
  872. // @router /special/trip/outboundMobileEdit [post]
  873. func (this *ActivitySpecialTripCoAntroller) OutboundMobileEdit() {
  874. br := new(models.BaseResponse).Init()
  875. defer func() {
  876. this.Data["json"] = br
  877. this.ServeJSON()
  878. }()
  879. sysUser := this.SysUser
  880. if sysUser == nil {
  881. br.Msg = "请登录"
  882. br.ErrMsg = "请登录,SysUser Is Empty"
  883. return
  884. }
  885. var req cygx.OutboundMobileEditResp
  886. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  887. if err != nil {
  888. br.Msg = "参数解析异常!"
  889. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  890. return
  891. }
  892. id := req.Id
  893. outboundMobile := req.OutboundMobile
  894. countryCode := req.CountryCode
  895. err = cygx.SpecialTripOutboundMobileEdit(id, outboundMobile, countryCode)
  896. if err != nil {
  897. br.Msg = "修改失败"
  898. br.ErrMsg = "修改失败 Err:" + err.Error()
  899. return
  900. }
  901. br.Ret = 200
  902. br.Success = true
  903. br.Msg = "操作成功"
  904. br.IsAddLog = true
  905. }
  906. // @Title 模版消息发送客户类型列表
  907. // @Description 模版消息发送客户类型列表接口
  908. // @Success Ret=200
  909. // @router /special/trip/tempMsg/sendGroupList [get]
  910. func (this *ActivitySpecialTripCoAntroller) SendGroupList() {
  911. br := new(models.BaseResponse).Init()
  912. defer func() {
  913. this.Data["json"] = br
  914. this.ServeJSON()
  915. }()
  916. AdminUser := this.SysUser
  917. if AdminUser == nil {
  918. br.Msg = "请登录"
  919. br.ErrMsg = "请登录,SysUser Is Empty"
  920. return
  921. }
  922. list := make([]cygx.SendGroup, 0)
  923. list = append(list, cygx.SendGroup{
  924. Id: 1,
  925. Name: "已报名客户",
  926. })
  927. br.Ret = 200
  928. br.Success = true
  929. br.Data = list
  930. }
  931. // @Title 发送模版消息
  932. // @Description 发送模版消息接口
  933. // @Param request body cygx.AddOutboundPersonnelItm true "type json string"
  934. // @Success 200 操作成功
  935. // @router /special/trip/tempMsg [post]
  936. func (this *ActivitySpecialTripCoAntroller) TempMsg() {
  937. br := new(models.BaseResponse).Init()
  938. defer func() {
  939. this.Data["json"] = br
  940. this.ServeJSON()
  941. }()
  942. AdminUser := this.SysUser
  943. if AdminUser == nil {
  944. br.Msg = "请登录"
  945. br.ErrMsg = "请登录,SysUser Is Empty"
  946. return
  947. }
  948. var req cygx.ActivitySpecialSignupTempMsgReq
  949. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  950. if err != nil {
  951. br.Msg = "参数解析异常!"
  952. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  953. return
  954. }
  955. idSlice := strings.Split(req.ActivityIds, ",")
  956. for _, sId := range idSlice {
  957. id, e := strconv.Atoi(sId)
  958. if e != nil {
  959. br.Msg = "活动Id参数异常"
  960. br.ErrMsg = "参数解析异常, Err:" + e.Error()
  961. return
  962. }
  963. var openIdList []*cygx.OpenIdList
  964. idMap := make(map[string]string, 0)
  965. //1已报名
  966. if strings.Contains(req.SendGroup, "1") {
  967. listSignup, err := cygx.GetCygxActivitySpecialTripListByActivityId(id)
  968. if err != nil {
  969. br.Msg = "查询报名信息失败失败"
  970. br.ErrMsg = "GetCygxActivitySpecialTripListByActivityId,Err:" + err.Error()
  971. return
  972. }
  973. if len(listSignup) == 0 {
  974. continue
  975. }
  976. var mobileArr []string
  977. for _, v := range listSignup {
  978. if v.Mobile != "" {
  979. mobileArr = append(mobileArr, v.Mobile)
  980. }
  981. }
  982. mobileLen := len(mobileArr)
  983. var condition string
  984. var pars []interface{}
  985. if mobileLen > 0 {
  986. condition += ` AND u.mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  987. pars = append(pars, mobileArr)
  988. }
  989. //list, err := models.GetActivitySpecialOpenIdListMobile(condition, pars)
  990. //if err != nil {
  991. // br.Msg = "查询openId失败"
  992. // br.ErrMsg = "查询openId失败,Err:" + err.Error()
  993. // return
  994. //}
  995. if mobileLen == 0 {
  996. return
  997. }
  998. list, err := cygx.GetUserRecordListByMobileArr(mobileArr)
  999. for _, idList := range list {
  1000. openIdList = append(openIdList, idList)
  1001. idMap[idList.OpenId] = idList.OpenId
  1002. }
  1003. }
  1004. if len(openIdList) > 0 {
  1005. openIdArr := make([]string, len(openIdList))
  1006. for i, v := range openIdList {
  1007. openIdArr[i] = v.OpenId
  1008. }
  1009. sendInfo := new(services.SendWxTemplate)
  1010. sendInfo.Keyword1 = req.ResearchTheme
  1011. sendInfo.Keyword2 = req.Content
  1012. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  1013. sendInfo.RedirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(id)
  1014. if utils.RunMode == "debug" {
  1015. sendInfo.RedirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(110) //测试环境调正式的ID
  1016. }
  1017. sendInfo.RedirectTarget = 3
  1018. sendInfo.Resource = strconv.Itoa(id)
  1019. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_CUSTOMIZE
  1020. sendInfo.OpenIdArr = openIdArr
  1021. e = services.SendTemplateMsg(sendInfo)
  1022. if e != nil {
  1023. br.Msg = "推送模板消息失败!"
  1024. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1025. return
  1026. }
  1027. }
  1028. }
  1029. br.Ret = 200
  1030. br.Success = true
  1031. br.Msg = "发送成功"
  1032. }