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