activity_special_trip.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/models/company"
  7. "hongze/hongze_clpt/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func SpecialTripPopupMsg(activityInfo *models.CygxActivitySpecialDetail, user *models.WxUserItem) (signupStatus int, popupMsg, popupMsg2 string, err error) {
  13. //SignupStatus int `description:"返回状态:1:成功 、2 :人数已满 、3:调研次数已用完、 4:超时"`
  14. signupStatus = 1
  15. resultTime := utils.StrTimeToTime(activityInfo.ActivityTime) //时间字符串格式转时间格式
  16. if time.Now().After(resultTime.Add(-time.Minute * 60)) {
  17. signupStatus = 4
  18. popupMsg = "活动开始前1小时内无法报名,请联系对口销售处理"
  19. return
  20. }
  21. errMsg, e := CheckTripRemainingtimesBycompany(user, activityInfo)
  22. if e != nil {
  23. err = errors.New("获取客户剩余报名次数失败 GetActivitySpecialUserType, Err: " + e.Error())
  24. return
  25. }
  26. if errMsg != "" {
  27. popupMsg = errMsg
  28. signupStatus = 3
  29. return
  30. }
  31. var condition string
  32. var pars []interface{}
  33. condition += ` AND activity_id = ? AND is_cancel = 0 `
  34. pars = append(pars, activityInfo.ActivityId)
  35. tripTota, e := models.GetActivitySpecialTripCountByActivityId(condition, pars)
  36. if e != nil {
  37. err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
  38. return
  39. }
  40. if activityInfo.LimitPeopleNum-tripTota < 1 {
  41. signupStatus = 2
  42. popupMsg = "此活动报名人数已满,请留意下期活动"
  43. return
  44. }
  45. //signupStatus = 4
  46. popupMsg = "感谢参与,本次报名会扣除一次贵司在弘则的调研点数。"
  47. popupMsg2 = "由于每场活动人数有限,如果不能参加请提前48小时取消,未及时取消导致影响其他客户报名将会维持扣点。"
  48. return
  49. }
  50. // 获取用户剩余报名次数
  51. // 获取用户剩余报名次数
  52. func GetTripRemainingtimesBycompany(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (errMsg string, tripRemaining int, err error) {
  53. //获取 专项调研客户类型 //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户;6、冻结客户;7、流失客户 8:行业升级套餐客户
  54. msgTemplate := "您的专项调研次数已用完,如仍想参加,请与您的对口销售商议"
  55. //var tripTota int
  56. //var airborneTota int
  57. //userType, _, e := GetActivitySpecialUserType(user.CompanyId)
  58. userType, _, e := GetUserType(user.CompanyId)
  59. if e != nil {
  60. err = errors.New("获取客户身份信息失败 GetActivitySpecialUserType, Err: " + e.Error())
  61. return
  62. }
  63. if userType == 0 {
  64. errMsg = msgTemplate
  65. return
  66. } else if userType == 1 {
  67. tripRemaining = 999
  68. return
  69. } else if userType == 2 {
  70. var condition string
  71. var pars []interface{}
  72. condition += ` AND company_id = ? `
  73. pars = append(pars, user.CompanyId)
  74. //airborneTota, e = models.GetActivitySpecialTripAirborneCountByActivitySpecial(condition, pars)
  75. //if e != nil {
  76. // err = errors.New("GetActivitySpecialTripAirborneCountByActivitySpecial, Err: " + e.Error())
  77. // return
  78. //}
  79. //condition += ` AND is_valid = 1 `
  80. //查询当年的数据
  81. condition += ` AND create_time >= ? `
  82. pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
  83. listTripBill, e := models.GetCygxActivitySpecialTripBill(condition, pars)
  84. if e != nil {
  85. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  86. return
  87. }
  88. var TripBillNum int
  89. for _, v := range listTripBill {
  90. TripBillNum += v.BillDetailed
  91. }
  92. var packageType int
  93. //大套餐客户,数据库添加标识,
  94. companyUserTypeDetail, errs := models.GetCygxCompanyUserType(user.CompanyId)
  95. if errs != nil && errs.Error() != utils.ErrNoRow() {
  96. err = errs
  97. return
  98. }
  99. if companyUserTypeDetail != nil {
  100. packageType = companyUserTypeDetail.PackageType
  101. if companyUserTypeDetail.CustomerTypeId != 0 {
  102. userType = companyUserTypeDetail.CustomerTypeId
  103. return
  104. }
  105. }
  106. packageTypeMap := map[int]int{1: 16, 2: 10}
  107. totalTrip := packageTypeMap[packageType]
  108. //tripRemaining = 10 - airborneTota + TripBillNum
  109. tripRemaining = totalTrip + TripBillNum
  110. if tripRemaining <= 0 {
  111. errMsg = msgTemplate
  112. return
  113. }
  114. //tripRemaining = 12 - tripTota
  115. } else {
  116. var condition string
  117. var pars []interface{}
  118. condition += ` AND company_id = ? `
  119. pars = append(pars, user.CompanyId)
  120. condition += ` AND chart_permission_id = ? `
  121. pars = append(pars, activityInfo.ChartPermissionId)
  122. //airborneTota, e = models.GetActivitySpecialTripAirborneCountByActivitySpecial(condition, pars)
  123. //if e != nil {
  124. // err = errors.New("GetActivitySpecialTripAirborneCountByActivitySpecial, Err: " + e.Error())
  125. // return
  126. //}
  127. //查询当年的数据
  128. condition += ` AND create_time >= ? `
  129. pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
  130. listTripBill, e := models.GetCygxActivitySpecialTripBill(condition, pars)
  131. if e != nil {
  132. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  133. return
  134. }
  135. var TripBillNum int
  136. for _, v := range listTripBill {
  137. TripBillNum += v.BillDetailed
  138. }
  139. //tripRemaining = 5 - airborneTota + TripBillNum
  140. tripRemaining = 5 + TripBillNum
  141. if tripRemaining <= 0 {
  142. errMsg = msgTemplate
  143. return
  144. }
  145. }
  146. return
  147. }
  148. // 校验是否还有剩余次数
  149. func CheckTripRemainingtimesBycompany(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (errMsg string, err error) {
  150. msgTemplate := "您的专项调研次数已用完,如仍想参加,请与您的对口销售商议"
  151. companyId := user.CompanyId
  152. chartPermissionName := activityInfo.ChartPermissionName
  153. companyDetail, e := models.GetCompanyDetailByIdGroup(companyId)
  154. if e != nil {
  155. err = errors.New("GetCompanyDetailByIdGroup, Err:" + e.Error())
  156. return
  157. }
  158. //永续客户不限制次数
  159. if companyDetail.Status == utils.COMPANY_STATUS_FOREVER {
  160. return
  161. }
  162. var condition string
  163. var pars []interface{}
  164. condition = " AND company_id = ? AND points > 0 ORDER BY chart_permission_id DESC "
  165. pars = append(pars, companyId)
  166. activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  167. if e != nil && e.Error() != utils.ErrNoRow() {
  168. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  169. return
  170. }
  171. //如果没有剩余的点数,就返回
  172. if len(activitySpecialPermissionPointsList) == 0 {
  173. errMsg = msgTemplate
  174. return
  175. }
  176. //判断是不是策略、固收、周期行业的活动,如果不是策略就获取对应行业的点数 ,策略的任何一个行业有剩余点数都能参加
  177. //if chartPermissionName != utils.CE_LUE_NAME && chartPermissionName != utils.GU_SHOU_NAME && chartPermissionName != utils.ZHOU_QI_NAME {
  178. if !utils.InArrayByStr(utils.ACTIVITY_SPECIAL_TRIP_PERMISSION_NAME_OTHER, activityInfo.ChartPermissionName) {
  179. return
  180. } else {
  181. for _, v := range activitySpecialPermissionPointsList {
  182. if v.ChartPermissionName == chartPermissionName || v.ChartPermissionId == 0 {
  183. return
  184. } // 如果对应行业有剩余次数,或者是属于大套餐且有次数,那么校验也通过
  185. }
  186. }
  187. return
  188. }
  189. // GetSpecialBillMaxChartPermissionId 获取专项调研剩余点数最多的行业
  190. func GetSpecialBillMaxChartPermissionId(user *models.WxUserItem) (maxChartPermissionId int, maxChartPermissionName string, err error) {
  191. companyId := user.CompanyId
  192. var condition string
  193. var pars []interface{}
  194. condition = " AND company_id = ? AND points > 0 ORDER BY points DESC LIMIT 1 "
  195. pars = append(pars, companyId)
  196. activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  197. if e != nil && e.Error() != utils.ErrNoRow() {
  198. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  199. return
  200. }
  201. for _, v := range activitySpecialPermissionPointsList {
  202. maxChartPermissionId = v.ChartPermissionId
  203. maxChartPermissionName = v.ChartPermissionName
  204. }
  205. return
  206. }
  207. // DeductTripRemainingtimesByUser 扣除用户专项调研剩余次数
  208. func DeductTripRemainingtimesByUser(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (err error) {
  209. msgTemplate := "您的专项调研次数已用完,如仍想参加,请与您的对口销售商议"
  210. companyId := user.CompanyId
  211. //chartPermissionName := activityInfo.ChartPermissionName
  212. companyProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  213. if e != nil {
  214. err = errors.New("GetCompanyProductByCompanyIdAndProductId, Err:" + e.Error())
  215. return
  216. }
  217. var condition string
  218. var pars []interface{}
  219. var maxChartPermissionId int // 剩余点数最多的行业
  220. var points int // 剩余点数
  221. var activitySpecialPermissionPointsList []*models.CygxActivitySpecialPermissionPoints
  222. //永续客户不限次数需要单独处理
  223. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER {
  224. condition = " AND company_id = ? ORDER BY points DESC LIMIT 1 "
  225. pars = append(pars, companyId)
  226. activitySpecialPermissionPointsList, e = models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  227. if e != nil && e.Error() != utils.ErrNoRow() {
  228. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  229. return
  230. }
  231. //如果永续客户不存在单独的行业剩余点数记录,那么就添加一条 -1 的记录作为初始化数据
  232. if len(activitySpecialPermissionPointsList) == 0 {
  233. item := new(models.CygxActivitySpecialPermissionPoints)
  234. item.CompanyId = companyProduct.CompanyId
  235. item.Points = -1
  236. item.CompanyName = companyProduct.CompanyName
  237. //item.ChartPermissionId = activityInfo.ChartPermissionId
  238. //item.ChartPermissionName = activityInfo.ChartPermissionName
  239. item.CreateTime = time.Now()
  240. item.ModifyTime = time.Now()
  241. e = models.AddCygxActivitySpecialPermissionPoints(item)
  242. if e != nil {
  243. err = errors.New("AddCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  244. return
  245. }
  246. return
  247. }
  248. } else {
  249. condition = " AND company_id = ? AND points > 0 ORDER BY points DESC LIMIT 1 "
  250. pars = append(pars, companyId)
  251. //判断是不是策略、固收、周期行业的活动,如果不是策略就获取对应行业的点数 ,策略的任何一个行业有剩余点数都能参加
  252. //if chartPermissionName != utils.CE_LUE_NAME && chartPermissionName != utils.GU_SHOU_NAME && chartPermissionName != utils.ZHOU_QI_NAME {
  253. if !utils.InArrayByStr(utils.ACTIVITY_SPECIAL_TRIP_PERMISSION_NAME_OTHER, activityInfo.ChartPermissionName) {
  254. pars = make([]interface{}, 0)
  255. condition = " AND company_id = ? AND ( chart_permission_id = ? OR chart_permission_id =0 ) AND points > 0 ORDER BY points DESC LIMIT 1 "
  256. pars = append(pars, companyId, activityInfo.ChartPermissionId)
  257. }
  258. activitySpecialPermissionPointsList, e = models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  259. if e != nil && e.Error() != utils.ErrNoRow() {
  260. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  261. return
  262. }
  263. }
  264. //如果没有剩余的点数,就返回
  265. if len(activitySpecialPermissionPointsList) == 0 {
  266. err = errors.New(msgTemplate)
  267. return
  268. }
  269. for _, v := range activitySpecialPermissionPointsList {
  270. maxChartPermissionId = v.ChartPermissionId
  271. points = v.Points - 1
  272. }
  273. //更新对应行业剩余点数
  274. e = models.UpdateCygxActivitySpecialPermissionPoints(points, companyId, maxChartPermissionId)
  275. if e != nil {
  276. err = errors.New("UpdateCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  277. return
  278. }
  279. return
  280. }
  281. // RebateTripRemainingtimesByUser 返点用户专项调研剩余次数
  282. func RebateTripRemainingtimesByUser(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (err error) {
  283. companyId := user.CompanyId
  284. chartPermissionName := activityInfo.ChartPermissionName
  285. var condition string
  286. var pars []interface{}
  287. condition = " AND company_id = ? ORDER BY points DESC LIMIT 1 "
  288. pars = append(pars, companyId)
  289. //判断是不是策略、固收行业的活动,如果不是策略就获取对应行业的点数 ,策略的任何一个行业有剩余点数都能参加
  290. //if chartPermissionName != utils.CE_LUE_NAME && chartPermissionName != utils.GU_SHOU_NAME && chartPermissionName != utils.ZHOU_QI_NAME {
  291. if !utils.InArrayByStr(utils.ACTIVITY_SPECIAL_TRIP_PERMISSION_NAME_OTHER, chartPermissionName) {
  292. pars = make([]interface{}, 0)
  293. condition = " AND company_id = ? AND ( chart_permission_id = ? OR chart_permission_id = 0 ) LIMIT 1 "
  294. pars = append(pars, companyId, activityInfo.ChartPermissionId)
  295. } else {
  296. lastTripBill, e := models.GetCygxActivitySpecialTripBillLastDetialByActivityId(activityInfo.ActivityId, user.UserId)
  297. if e != nil {
  298. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  299. return
  300. }
  301. pars = make([]interface{}, 0)
  302. condition = " AND company_id = ? AND ( chart_permission_id = ? OR chart_permission_id = 0 ) LIMIT 1 "
  303. pars = append(pars, companyId, lastTripBill.ChartPermissionId)
  304. }
  305. activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  306. if e != nil && e.Error() != utils.ErrNoRow() {
  307. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  308. return
  309. }
  310. var maxChartPermissionId int // 剩余点数最多的行业(返点行业)
  311. var points int // 剩余点数
  312. for _, v := range activitySpecialPermissionPointsList {
  313. maxChartPermissionId = v.ChartPermissionId
  314. points = v.Points + 1
  315. }
  316. //更新对应行业剩余点数
  317. e = models.UpdateCygxActivitySpecialPermissionPoints(points, companyId, maxChartPermissionId)
  318. if e != nil {
  319. err = errors.New("UpdateCygxActivitySpecialPermissionPoints, Err:" + e.Error())
  320. return
  321. }
  322. return
  323. }
  324. // 预处理专项调研流水明细表描述内容
  325. //func HandleActivitySpecialTripBillTotalText(user *models.WxUserItem) (totalText string, err error) {
  326. // companyId := user.CompanyId
  327. // companyDetail, e := models.GetCompanyDetailByIdGroup(companyId)
  328. // if e != nil {
  329. // err = errors.New("GetCompanyDetailByIdGroup, Err:" + e.Error())
  330. // return
  331. // }
  332. // //永续客户不限制次数
  333. // if companyDetail.Status == utils.COMPANY_STATUS_FOREVER {
  334. // totalText = "0次"
  335. // return
  336. // }
  337. // var condition string
  338. // var pars []interface{}
  339. // pars = make([]interface{}, 0)
  340. // condition = " AND company_id = ? AND points != 0 ORDER BY chart_permission_id DESC "
  341. // pars = append(pars, companyId)
  342. // activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  343. // if e != nil && e.Error() != utils.ErrNoRow() {
  344. // err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  345. // return
  346. // }
  347. //
  348. // var itemBillTotal []string
  349. // for _, v := range activitySpecialPermissionPointsList {
  350. // if v.ChartPermissionId > 0 {
  351. // itemBillTotal = append(itemBillTotal, fmt.Sprint(v.ChartPermissionName, v.Points, "次")) // 行业套餐客户展示规则
  352. // }
  353. // if v.ChartPermissionId == 0 {
  354. // totalText = fmt.Sprint(v.Points, "次") // 大套餐客户展示规则
  355. // }
  356. // }
  357. // if len(itemBillTotal) > 0 {
  358. // totalText = strings.Join(itemBillTotal, "+")
  359. // }
  360. // fmt.Println(totalText)
  361. // return
  362. //}
  363. // 预处理专项调研流水明细表描述内容
  364. func HandleActivitySpecialTripBillTotalText(companyId int) (totalText string, err error) {
  365. //companyId := user.CompanyId
  366. companyDetail, e := models.GetCompanyDetailByIdGroup(companyId)
  367. if e != nil {
  368. err = errors.New("GetCompanyDetailByIdGroup, Err:" + e.Error())
  369. return
  370. }
  371. var condition string
  372. var pars []interface{}
  373. pars = make([]interface{}, 0)
  374. condition = " AND company_id = ? AND points != 0 ORDER BY chart_permission_id DESC "
  375. pars = append(pars, companyId)
  376. activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
  377. if e != nil && e.Error() != utils.ErrNoRow() {
  378. err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
  379. return
  380. }
  381. //永续客户次数累加
  382. if companyDetail.Status == utils.COMPANY_STATUS_FOREVER {
  383. var points int
  384. for _, v := range activitySpecialPermissionPointsList {
  385. points += v.Points
  386. }
  387. totalText = fmt.Sprint(points, "次") //追加永续客户展示规则(2024-02-23)
  388. return
  389. }
  390. var itemBillTotal []string
  391. itemBillTotalMap := make(map[string]string)
  392. for _, v := range activitySpecialPermissionPointsList {
  393. if v.ChartPermissionId > 0 {
  394. itemBillTotal = append(itemBillTotal, fmt.Sprint(v.ChartPermissionName, v.Points, "次")) // 行业套餐客户展示规则
  395. itemBillTotalMap[v.ChartPermissionName] = fmt.Sprint(v.ChartPermissionName, v.Points, "次")
  396. }
  397. if v.ChartPermissionId == 0 {
  398. totalText = fmt.Sprint(v.Points, "次") // 大套餐客户展示规则,
  399. }
  400. }
  401. if len(itemBillTotal) > 0 {
  402. permissionNameSlice := []string{"医药", "消费", "科技", "智造"}
  403. var itemBillTotalNew []string
  404. for _, v := range permissionNameSlice {
  405. if itemBillTotalMap[v] == "" {
  406. itemBillTotalNew = append(itemBillTotalNew, fmt.Sprint(v, "0次"))
  407. } else {
  408. itemBillTotalNew = append(itemBillTotalNew, itemBillTotalMap[v])
  409. }
  410. }
  411. totalText = strings.Join(itemBillTotalNew, "+")
  412. }
  413. return
  414. }
  415. // 用户搜专项调研操作操作行为,模板消息推送
  416. func ActivitySpecialUserRmind(user *models.WxUserItem, activityId, TripStatus int) (err error) {
  417. defer func() {
  418. if err != nil {
  419. go utils.SendAlarmMsg("用户搜专项调研操作操作行为,模板消息推送失败"+err.Error(), 2)
  420. }
  421. }()
  422. countUser, err := models.GetUserRemind(user.UserId)
  423. if err != nil {
  424. return err
  425. }
  426. if countUser == 0 {
  427. return err
  428. }
  429. var first string
  430. var keyword1 string
  431. var keyword2 string
  432. var keyword3 string
  433. var keyword4 string
  434. var remark string
  435. //获取销售手机号
  436. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  437. if err != nil && err.Error() != utils.ErrNoRow() {
  438. return err
  439. }
  440. if sellerItemQy != nil {
  441. sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号
  442. if e != nil {
  443. err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error())
  444. return
  445. }
  446. sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",")
  447. openIdList, e := models.GetWxOpenIdByMobileList(sllerAndShareMobiles)
  448. if e != nil {
  449. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  450. return
  451. }
  452. activityInfo, e := models.GetCygxActivitySpecialDetailById(user.UserId, activityId)
  453. if e != nil {
  454. err = errors.New("GetCygxActivitySpecialDetailById, Err: " + e.Error())
  455. return
  456. }
  457. if activityInfo == nil {
  458. return
  459. }
  460. //TripStatus int `description:"行程进行状态 1:预报名,2:确定行程"`
  461. if TripStatus == 1 {
  462. first = fmt.Sprint(user.RealName, "--", user.CompanyName, ",有新的互动:专项调研预报名")
  463. } else {
  464. first = fmt.Sprint(user.RealName, "--", user.CompanyName, ",有新的互动:活动报名")
  465. }
  466. keyword1 = activityInfo.ResearchTheme
  467. keyword2 = "__"
  468. remark = "点击查看活动详情"
  469. openIdArr := make([]string, 0)
  470. for _, v := range openIdList {
  471. openIdArr = append(openIdArr, v.OpenId)
  472. }
  473. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  474. sendInfo := new(SendWxTemplate)
  475. sendInfo.First = first
  476. sendInfo.Keyword1 = keyword1
  477. sendInfo.Keyword2 = keyword2
  478. sendInfo.Keyword3 = keyword3
  479. sendInfo.Keyword4 = keyword4
  480. sendInfo.Remark = remark
  481. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  482. sendInfo.RedirectUrl = redirectUrl
  483. sendInfo.RedirectTarget = 3
  484. sendInfo.Resource = strconv.Itoa(activityId)
  485. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  486. sendInfo.OpenIdArr = openIdArr
  487. err = PublicSendTemplateMsg(sendInfo)
  488. if err != nil {
  489. return
  490. }
  491. }
  492. return
  493. }
  494. // GetChartPermissionSpecialSurplusByCompany 获取公司专项调研次数-分品种
  495. func GetChartPermissionSpecialSurplusByCompany(companyId int) (userType int, tripRemaining int, mapChartName map[string]int, err error) {
  496. companyDetail, e := models.GetCompanyDetailByIdGroup(companyId)
  497. if e != nil {
  498. err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
  499. }
  500. if companyDetail == nil {
  501. return
  502. }
  503. if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
  504. return
  505. }
  506. var specialSurplus string
  507. if companyDetail.Status == "永续" {
  508. specialSurplus = "不限次数"
  509. }
  510. //chartMap := map[int]string{utils.YI_YAO_ID:utils.YI_YAO_NAME, utils.XIAO_FEI_ID:utils.XIAO_FEI_NAME, utils.KE_JI_ID:utils.KE_JI_NAME, utils.ZHI_ZAO_ID:utils.ZHI_ZAO_NAME}
  511. //chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
  512. chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
  513. if companyDetail.Status == "正式" {
  514. //var packageType int
  515. userType, _, _ = GetUserType(companyId)
  516. var condition string
  517. var pars []interface{}
  518. condition += ` AND company_id = ? `
  519. pars = append(pars, companyId)
  520. //查询当年的数据
  521. condition += ` AND b.create_time >= ? `
  522. pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
  523. listTripBill, e := models.GetCygxActivitySpecialTripBillList(condition, pars)
  524. if e != nil {
  525. err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
  526. return
  527. }
  528. //// 获取继承点数
  529. //inheritList, e := models.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
  530. //if e != nil && e.Error() != utils.ErrNoRow() {
  531. // err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
  532. //}
  533. if userType == 2 {
  534. var tripBillNum int
  535. for _, v := range listTripBill {
  536. //if v.ActivityId == 0 {
  537. // continue
  538. //}
  539. tripBillNum += v.BillDetailed
  540. }
  541. //if len(inheritList) > 0 {
  542. // for _, v := range inheritList {
  543. // if v.ChartPermissionId == 0 {
  544. // tripBillNum += v.Points
  545. // }
  546. // }
  547. //}
  548. tripRemaining = tripBillNum
  549. if tripRemaining < 0 {
  550. tripRemaining = 0
  551. }
  552. } else {
  553. list, e := models.GetCompanyReportPermissionByCompanyIdAndProductId(companyId, 2)
  554. if e != nil && e.Error() != utils.ErrNoRow() {
  555. err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
  556. }
  557. if len(list) == 0 {
  558. return
  559. }
  560. var chartPermissionIdSlice []string
  561. mapChartName = make(map[string]int)
  562. mapUpgradeId := make(map[int]int)
  563. mapPermissionNameTrip := make(map[string]int)
  564. mapInheritChartName := make(map[string]int)
  565. //mapPermissionName := make(map[int]string)
  566. for _, v := range list {
  567. chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
  568. //是升级套餐才有点数
  569. if v.IsUpgrade == 1 {
  570. mapUpgradeId[v.ChartPermissionId] = 1
  571. }
  572. }
  573. chartList := make([]*models.ChartPermission, 0)
  574. if len(chartPermissionIdSlice) > 0 {
  575. chartList, e = models.GetChartPermissionByIds(chartPermissionIdSlice)
  576. if e != nil {
  577. err = errors.New("获取品种信息失败, Err:" + e.Error())
  578. return
  579. }
  580. }
  581. if len(chartList) == 0 {
  582. return
  583. }
  584. for _, v := range listTripBill {
  585. if v.ActivityId == 0 {
  586. continue
  587. }
  588. mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
  589. }
  590. for _, v := range chartList {
  591. //如果是升级则加点
  592. if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
  593. mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.PermissionName]
  594. } else {
  595. mapChartName[v.PermissionName] = mapPermissionNameTrip[v.PermissionName]
  596. }
  597. }
  598. // 通过继承获得的加点
  599. //for _, v := range inheritList {
  600. // mapInheritChartName[v.ChartPermissionName] = v.Points
  601. //}
  602. for k, _ := range chartNameMap {
  603. if _, ok := mapChartName[k]; ok {
  604. if inherit, ok2 := mapInheritChartName[k]; ok2 {
  605. mapChartName[k] += inherit
  606. }
  607. }
  608. }
  609. for k, v := range mapChartName {
  610. if v > 0 {
  611. specialSurplus += k + strconv.Itoa(v) + "次+"
  612. }
  613. }
  614. specialSurplus = strings.TrimRight(specialSurplus, "+")
  615. }
  616. }
  617. return
  618. }
  619. // 确定行程用户报名模版消息推送
  620. func ActivitySpecialUserAddTrip(user *models.WxUserItem, activityId int) (err error) {
  621. defer func() {
  622. if err != nil {
  623. go utils.SendAlarmMsg("确定行程用户报名模版消息推送,模板消息推送失败"+err.Error(), 2)
  624. }
  625. }()
  626. var first string
  627. var keyword1 string
  628. var keyword2 string
  629. var keyword3 string
  630. var keyword4 string
  631. var remark string
  632. //获取销售手机号
  633. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  634. if err != nil && err.Error() != utils.ErrNoRow() {
  635. return err
  636. }
  637. cnf, _ := models.GetConfigByCode("tpl_msg")
  638. if cnf != nil && sellerItemQy != nil {
  639. openIdList, e := models.GetWxOpenIdByMobileList(cnf.ConfigValue + "," + sellerItemQy.Mobile)
  640. if e != nil {
  641. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  642. return
  643. }
  644. activityInfo, e := models.GetCygxActivitySpecialDetailById(user.UserId, activityId)
  645. if e != nil {
  646. err = errors.New("GetCygxActivitySpecialDetailById, Err: " + e.Error())
  647. return
  648. }
  649. if activityInfo == nil {
  650. return
  651. }
  652. keyword1 = fmt.Sprint(user.RealName, "--", user.CompanyName, "(所属销售:", sellerItemQy.RealName, ")")
  653. keyword2 = user.Mobile
  654. keyword3 = time.Now().Format(utils.FormatDateTime)
  655. keyword4 = "报名专项调研【" + activityInfo.ResearchTheme + "】 "
  656. remark = ""
  657. openIdArr := make([]string, 0)
  658. for _, v := range openIdList {
  659. openIdArr = append(openIdArr, v.OpenId)
  660. }
  661. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  662. sendInfo := new(SendWxTemplate)
  663. sendInfo.First = first
  664. sendInfo.Keyword1 = keyword1
  665. sendInfo.Keyword2 = keyword2
  666. sendInfo.Keyword3 = keyword3
  667. sendInfo.Keyword4 = keyword4
  668. sendInfo.Remark = remark
  669. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  670. sendInfo.RedirectUrl = redirectUrl
  671. sendInfo.RedirectTarget = 3
  672. sendInfo.Resource = strconv.Itoa(activityId)
  673. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  674. sendInfo.OpenIdArr = openIdArr
  675. err = PublicSendTemplateMsg(sendInfo)
  676. if err != nil {
  677. return
  678. }
  679. }
  680. return
  681. }