order.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_cygx/models"
  8. "hongze/hongze_cygx/models/order"
  9. "hongze/hongze_cygx/utils"
  10. "time"
  11. )
  12. // CancelCygxOrder 关闭到期个人用户研选权限
  13. func CancelCygxOrder(cont context.Context) (err error) {
  14. defer func() {
  15. if err != nil {
  16. fmt.Println(err)
  17. go utils.SendAlarmMsg(fmt.Sprint("自动取消十分钟之内没有付款的的订单 CancelCygxOrder ,err:", err.Error()), 2)
  18. }
  19. }()
  20. var condition string
  21. var pars []interface{}
  22. endTime := time.Now().Add(-10 * time.Minute)
  23. condition = ` AND order_status = 1 AND create_time < ? `
  24. pars = append(pars, endTime)
  25. orderList, e := order.GetCygxOrderList(condition, pars, 0, 999)
  26. if e != nil && e.Error() != utils.ErrNoRow() {
  27. err = errors.New("GetCygxOrderList, Err: " + e.Error())
  28. return
  29. }
  30. fmt.Println(len(orderList))
  31. if len(orderList) == 0 {
  32. return
  33. }
  34. //订单量不大,先这么写吧
  35. for _, v := range orderList {
  36. e = order.CancelCygxOrder(v)
  37. if e != nil && e.Error() != utils.ErrNoRow() {
  38. err = errors.New("CancelCygxOrder, Err: " + e.Error())
  39. return
  40. }
  41. go ExampleJsapiApiService_CloseOrder(v.OutTradeNo)
  42. }
  43. return
  44. }
  45. //func init() {
  46. // HandleOrderRefundHandleask()
  47. //}
  48. // GetQueryOrderByOutTradeNoTask 定时任务主动获取订单支付状态
  49. func GetQueryOrderByOutTradeNoTask(cont context.Context) (err error) {
  50. //func GetQueryOrderByOutTradeNoTask() (err error) {
  51. defer func() {
  52. if err != nil {
  53. fmt.Println(err)
  54. go utils.SendAlarmMsg(fmt.Sprint("自动取消十分钟之内没有付款的的订单 CancelCygxOrder ,err:", err.Error()), 2)
  55. }
  56. }()
  57. var condition string
  58. var pars []interface{}
  59. endTime := time.Now().Add(-10 * time.Minute)
  60. condition = ` AND order_status = 1 AND create_time > ? ` //获取十分钟之内待支付的订单
  61. pars = append(pars, endTime)
  62. orderList, e := order.GetCygxOrderList(condition, pars, 0, 999)
  63. if e != nil && e.Error() != utils.ErrNoRow() {
  64. err = errors.New("GetCygxOrderList, Err: " + e.Error())
  65. return
  66. }
  67. if len(orderList) == 0 {
  68. return
  69. }
  70. //订单量不大,先这么写吧
  71. for _, v := range orderList {
  72. tradeState, statusCode, itemResp := GetQueryOrderByOutTradeNo(v.OutTradeNo)
  73. if tradeState == "SUCCESS" && statusCode == 200 {
  74. go HandleOrderByWechatPay(itemResp)
  75. }
  76. }
  77. return
  78. }
  79. // 手动处理支付订单回调
  80. func HandleOrderByWechatPay(itemCallback *Transaction) {
  81. var err error
  82. defer func() {
  83. if err != nil {
  84. fmt.Println(err)
  85. go utils.SendAlarmMsg(fmt.Sprint("手动处理支付订单回调失败 HandleOrderHandle, err:", err.Error()), 2)
  86. }
  87. }()
  88. outTradeNo := itemCallback.OutTradeNo
  89. orderDetail, e := order.GetCygxOrderDetailByOutTradeNo(outTradeNo)
  90. if e != nil {
  91. err = errors.New("GetCygxOrderDetailByOrderCode, Err: " + e.Error())
  92. return
  93. }
  94. go AddCygxOrderPayment(itemCallback) // 记录支付交易信息
  95. if itemCallback.TradeState != "SUCCESS" { // 回调显示支付不成功,模版消息推送
  96. if e != nil {
  97. err = errors.New("支付失败, outTradeNo: " + outTradeNo)
  98. return
  99. }
  100. }
  101. //修改过状态的不再二次处理
  102. if orderDetail.OrderStatus == 2 {
  103. return
  104. }
  105. itemOrder := new(order.CygxOrder)
  106. itemOrder.OrderCode = orderDetail.OrderCode
  107. itemOrder.PayTime = itemCallback.SuccessTime
  108. itemOrder.PayMoney = float64(float64(itemCallback.Amount.PayerTotal) / 100) // 金额分转换处理
  109. itemOrder.OrderStatus = 2
  110. itemOrder.OutTradeCode = itemCallback.TransactionId
  111. itemOrder.TradeType = itemCallback.TradeType
  112. //文章处理逻辑 OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  113. if orderDetail.OrderType == 1 {
  114. itemUserCard := new(order.CygxOrderUserCard)
  115. itemUserCard.OrderCode = orderDetail.OrderCode
  116. itemUserCard.UserId = orderDetail.UserId
  117. itemUserCard.Mobile = orderDetail.Mobile
  118. itemUserCard.Email = orderDetail.Email
  119. itemUserCard.CompanyId = orderDetail.CompanyId
  120. itemUserCard.CompanyName = orderDetail.CompanyName
  121. itemUserCard.RealName = orderDetail.RealName
  122. itemUserCard.SellerName = orderDetail.SellerName
  123. itemUserCard.CreateTime = time.Now()
  124. itemUserCard.ModifyTime = time.Now()
  125. itemUserCard.RegisterPlatform = orderDetail.RegisterPlatform
  126. if orderDetail.GoodsId == 1 {
  127. itemUserCard.StartDate = itemOrder.PayTime
  128. itemUserCard.EndDate = itemOrder.PayTime.AddDate(0, 0, 1)
  129. itemUserCard.CardType = "日卡"
  130. } else {
  131. now := time.Now()
  132. itemUserCard.StartDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  133. itemUserCard.EndDate = itemUserCard.StartDate.AddDate(0, 1, 0)
  134. itemUserCard.CardType = "月卡"
  135. }
  136. e = order.AddCygxOrderUserCard(itemUserCard, itemOrder)
  137. if e != nil {
  138. err = errors.New("AddCygxOrderUserCard, Err: " + e.Error())
  139. return
  140. }
  141. } else if orderDetail.OrderType == 2 || orderDetail.OrderType == 3 {
  142. //如果是活动,把单场付费信息的活动写入 用户虚拟资产表
  143. itemOrderVirtualAsset := new(order.CygxOrderVirtualAsset)
  144. itemOrderVirtualAsset.OrderCode = orderDetail.OrderCode
  145. itemOrderVirtualAsset.UserId = orderDetail.UserId
  146. itemOrderVirtualAsset.Mobile = orderDetail.Mobile
  147. itemOrderVirtualAsset.Email = orderDetail.Email
  148. itemOrderVirtualAsset.CompanyId = orderDetail.CompanyId
  149. itemOrderVirtualAsset.CompanyName = orderDetail.CompanyName
  150. itemOrderVirtualAsset.RealName = orderDetail.RealName
  151. itemOrderVirtualAsset.SellerName = orderDetail.SellerName
  152. itemOrderVirtualAsset.Source = orderDetail.Source
  153. itemOrderVirtualAsset.SourceId = orderDetail.SourceId
  154. itemOrderVirtualAsset.CreateTime = time.Now()
  155. itemOrderVirtualAsset.ModifyTime = time.Now()
  156. e = order.AddCygxOrderVirtualAsset(itemOrderVirtualAsset, itemOrder)
  157. if e != nil {
  158. err = errors.New("AddCygxOrderVirtualAsset, Err: " + e.Error())
  159. return
  160. }
  161. }
  162. if orderDetail.Source == utils.CYGX_OBJ_ACTIVITY {
  163. go AddActivitySignupByWechatPay(orderDetail.SourceId, orderDetail.UserId) //微信付款成功自动添加报名
  164. }
  165. go CancelOtherOrder(orderDetail) //付款成功之后自动取消其他相同的订单
  166. return
  167. }
  168. // 添加操作记录
  169. func AddCygxOrderPayment(itemCallback *Transaction) {
  170. var err error
  171. defer func() {
  172. if err != nil {
  173. fmt.Println(err)
  174. go utils.SendAlarmMsg(fmt.Sprint("处理订单回调信息失败 HandleOrderHandle, err:", err.Error()), 2)
  175. }
  176. }()
  177. itemPay := new(order.CygxOrderPayment)
  178. itemPay.OrderCode = itemCallback.OutTradeNo
  179. itemPay.OutTradeCode = itemCallback.TransactionId
  180. itemPay.PayBody = itemCallback.TradeStateDesc
  181. jsonData, _ := json.Marshal(itemCallback)
  182. itemPay.PayDetail = string(jsonData)
  183. itemPay.PayMoney = float64(float64(itemCallback.Amount.PayerTotal) / 100) // 金额分转换处理
  184. itemPay.PayStatus = itemCallback.TradeState
  185. itemPay.CreateTime = time.Now()
  186. itemPay.PaymentType = 1
  187. e := order.AddCygxOrderPayment(itemPay)
  188. if e != nil {
  189. err = errors.New("AddCygxOrderPayment, Err: " + e.Error())
  190. return
  191. }
  192. return
  193. }
  194. // 定时任务处理退款回调
  195. func HandleOrderRefundTask(cont context.Context) (err error) {
  196. //func HandleOrderRefundTask() (err error) {
  197. defer func() {
  198. if err != nil {
  199. fmt.Println(err)
  200. go utils.SendAlarmMsg(fmt.Sprint("定时任务处理退款回调 HandleOrderRefundTask ,err:", err.Error()), 2)
  201. }
  202. }()
  203. var condition string
  204. var pars []interface{}
  205. //endTime := time.Now().Add(-10 * time.Minute)
  206. condition = ` AND order_status = 4 ` //获取十分钟之内待支付的订单
  207. //pars = append(pars, endTime)
  208. orderList, e := order.GetCygxOrderList(condition, pars, 0, 999)
  209. if e != nil && e.Error() != utils.ErrNoRow() {
  210. err = errors.New("GetCygxOrderList, Err: " + e.Error())
  211. return
  212. }
  213. fmt.Println(len(orderList))
  214. if len(orderList) == 0 {
  215. return
  216. }
  217. //订单量不大,先这么写吧
  218. for _, v := range orderList {
  219. tradeState, statusCode, itemResp := GetQueryOrderByOutTradeNo(v.OutTradeNo)
  220. if tradeState == "REFUND" && statusCode == 200 {
  221. go HandleOrderRefundByWechatPay(itemResp)
  222. }
  223. }
  224. return
  225. }
  226. // 处理退款订单回调
  227. func HandleOrderRefundByWechatPay(itemCallback *Transaction) {
  228. var err error
  229. defer func() {
  230. if err != nil {
  231. fmt.Println(err)
  232. go utils.SendAlarmMsg(fmt.Sprint("处理退款订单回调信息失败 HandleOrderRefundByWechatPay, err:", err.Error()), 2)
  233. }
  234. }()
  235. outTradeNo := itemCallback.OutTradeNo
  236. orderDetail, e := order.GetCygxOrderDetailByOutTradeNo(outTradeNo)
  237. if e != nil {
  238. err = errors.New("GetCygxOrderDetailByOrderCode, Err: " + e.Error())
  239. return
  240. }
  241. //go AddCygxOrderPayment(itemCallback) // 记录支付交易信息
  242. //修改过状态的不再二次处理
  243. if orderDetail.OrderStatus == 3 {
  244. return
  245. }
  246. itemOrder := new(order.CygxOrder)
  247. itemOrder.OrderCode = orderDetail.OrderCode
  248. itemOrder.RefundTime = itemCallback.SuccessTime
  249. itemOrder.RefundMoney = float64(float64(itemCallback.Amount.PayerTotal) / 100) // 金额分转换处理
  250. itemOrder.OrderStatus = 3
  251. //文章处理逻辑 OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  252. if orderDetail.OrderType == 1 {
  253. itemUserCard := new(order.CygxOrderUserCard)
  254. itemUserCard.OrderCode = orderDetail.OrderCode
  255. itemUserCard.UserId = orderDetail.UserId
  256. itemUserCard.Mobile = orderDetail.Mobile
  257. itemUserCard.Email = orderDetail.Email
  258. itemUserCard.CompanyId = orderDetail.CompanyId
  259. itemUserCard.CompanyName = orderDetail.CompanyName
  260. itemUserCard.RealName = orderDetail.RealName
  261. itemUserCard.SellerName = orderDetail.SellerName
  262. itemUserCard.CreateTime = time.Now()
  263. itemUserCard.ModifyTime = time.Now()
  264. itemUserCard.RegisterPlatform = orderDetail.RegisterPlatform
  265. if orderDetail.GoodsId == 1 {
  266. itemUserCard.CardType = "日卡"
  267. } else {
  268. itemUserCard.CardType = "月卡"
  269. }
  270. e = order.RemoveCygxOrderUserCard(itemUserCard, itemOrder) // 移除用户
  271. if e != nil {
  272. err = errors.New("RemoveCygxOrderUserCard, Err: " + e.Error())
  273. return
  274. }
  275. } else if orderDetail.OrderType == 2 || orderDetail.OrderType == 3 {
  276. //如果是活动,把单场付费信息的活动写入 用户虚拟资产表
  277. itemOrderVirtualAsset := new(order.CygxOrderVirtualAsset)
  278. itemOrderVirtualAsset.OrderCode = orderDetail.OrderCode
  279. itemOrderVirtualAsset.UserId = orderDetail.UserId
  280. itemOrderVirtualAsset.Mobile = orderDetail.Mobile
  281. itemOrderVirtualAsset.Email = orderDetail.Email
  282. itemOrderVirtualAsset.CompanyId = orderDetail.CompanyId
  283. itemOrderVirtualAsset.CompanyName = orderDetail.CompanyName
  284. itemOrderVirtualAsset.RealName = orderDetail.RealName
  285. itemOrderVirtualAsset.SellerName = orderDetail.SellerName
  286. itemOrderVirtualAsset.Source = orderDetail.Source
  287. itemOrderVirtualAsset.SourceId = orderDetail.SourceId
  288. itemOrderVirtualAsset.CreateTime = time.Now()
  289. itemOrderVirtualAsset.ModifyTime = time.Now()
  290. e = order.RemoveCygxOrderVirtualAsset(itemOrderVirtualAsset, itemOrder)
  291. if e != nil {
  292. err = errors.New("AddCygxOrderVirtualAsset, Err: " + e.Error())
  293. return
  294. }
  295. }
  296. //if orderDetail.Source == utils.CYGX_OBJ_ACTIVITY {
  297. // go CancelActivitySignupByWechatPay(orderDetail.SourceId, orderDetail.UserId) //微信退款成功自动取消报名
  298. //}
  299. return
  300. }
  301. // 微信付款成功自动添加报名
  302. func AddActivitySignupByWechatPay(activityId, userId int) {
  303. var err error
  304. defer func() {
  305. if err != nil {
  306. fmt.Println(err)
  307. go utils.SendAlarmMsg(fmt.Sprint("微信付款成功自动添加报名失败 AddActivitySignupByWechatPay, err:", err.Error(), "activityId:", activityId, "userId:", userId), 2)
  308. }
  309. }()
  310. activityInfo, e := models.GetAddActivityInfoById(activityId)
  311. if e != nil {
  312. err = errors.New("GetAddActivityInfoById, Err: " + e.Error())
  313. return
  314. }
  315. var userPointsNum float64
  316. if activityInfo.IsResearchPoints {
  317. //获取活动对用户要扣的点
  318. userPointsNum, e = models.GetCygxActivityPointsSetUserNum(activityInfo.ActivityId)
  319. if e != nil {
  320. err = errors.New("GetCygxActivityPointsSetUserNum, Err: " + e.Error())
  321. return
  322. }
  323. }
  324. //专家线下沙龙与买方线下交流、扣点的公司调研电话会自动报名
  325. if activityInfo.ActivityTypeId != 5 && activityInfo.ActivityTypeId != 8 && userPointsNum == 0 {
  326. return
  327. }
  328. user, e := models.GetWxUserItemByUserId(userId)
  329. if e != nil {
  330. err = errors.New("GetWxUserItemByUserId, Err: " + e.Error())
  331. return
  332. }
  333. var sellerName string
  334. sellerName, e = models.GetCompanySellerName(user.CompanyId)
  335. if e != nil {
  336. err = errors.New("GetCompanySellerName, Err: " + e.Error())
  337. return
  338. }
  339. item := new(models.CygxActivitySignup)
  340. totalMySuccess, e := models.GetActivitySignupCount(userId, activityId)
  341. if e != nil {
  342. err = errors.New("AddActivitySignup, Err: " + e.Error())
  343. return
  344. }
  345. if totalMySuccess > 0 { //已报名这个活动
  346. return
  347. }
  348. item.UserId = userId
  349. item.RealName = user.RealName
  350. item.SellerName = sellerName
  351. item.ActivityId = activityId
  352. item.CreateTime = time.Now()
  353. item.Mobile = user.Mobile
  354. item.Email = user.Email
  355. item.CompanyId = user.CompanyId
  356. item.CompanyName = user.CompanyName
  357. item.SignupType = 3
  358. item.FailType = 0
  359. item.DoFailType = 0
  360. item.OutboundMobile = user.Mobile
  361. if user.OutboundMobile != "" {
  362. item.OutboundMobile = user.OutboundMobile
  363. if user.OutboundCountryCode == "" {
  364. item.CountryCode = "86"
  365. } else {
  366. item.CountryCode = user.OutboundCountryCode
  367. }
  368. } else {
  369. item.OutboundMobile = user.Mobile
  370. if user.CountryCode == "" {
  371. item.CountryCode = "86"
  372. } else {
  373. item.CountryCode = user.CountryCode
  374. }
  375. }
  376. _, e = models.AddActivitySignup(item)
  377. if e != nil {
  378. err = errors.New("AddActivitySignup, Err: " + e.Error())
  379. return
  380. }
  381. fmt.Println("报名成功")
  382. go ActivityUserRemind(user, activityInfo, 4)
  383. return
  384. }
  385. // 微信退款成功自动取消报名
  386. func CancelActivitySignupByWechatPay(activityId, userId int) {
  387. var err error
  388. defer func() {
  389. if err != nil {
  390. fmt.Println(err)
  391. go utils.SendAlarmMsg(fmt.Sprint("微信退款成功自动取消报名失败 CancelActivitySignupByWechatPay, err:", err.Error(), "activityId:", activityId, "userId:", userId), 2)
  392. }
  393. }()
  394. activityInfo, e := models.GetAddActivityInfoById(activityId)
  395. if e != nil {
  396. err = errors.New("GetAddActivityInfoById, Err: " + e.Error())
  397. return
  398. }
  399. user, e := models.GetWxUserItemByUserId(userId)
  400. if e != nil {
  401. err = errors.New("GetWxUserItemByUserId, Err: " + e.Error())
  402. return
  403. }
  404. item := new(models.CygxActivitySignup)
  405. item.UserId = userId
  406. item.ActivityId = activityId
  407. item.CreateTime = time.Now()
  408. item.Mobile = user.Mobile
  409. item.Email = user.Email
  410. item.CompanyId = user.CompanyId
  411. item.CompanyName = user.CompanyName
  412. resp := new(models.SignupStatus)
  413. resp.ActivityId = activityId
  414. _, e = models.CancelActivitySignup(item)
  415. if e != nil {
  416. err = errors.New("CancelActivitySignup, Err: " + e.Error())
  417. return
  418. }
  419. // 如果是买方研选下的专家沙龙,同时推给内容组四人
  420. if activityInfo.ActivityTypeId == 5 {
  421. go SendActivitieCancelSignTemplateMsg(user, activityInfo)
  422. }
  423. go SendResearchActivitiesTemplateMsg(user, activityInfo, "取消报名") //公司线下调研活动客户报名后给建会人,所属销售推送模板信息
  424. return
  425. }
  426. // 付款成功之后自动取消其他相同的订单
  427. func CancelOtherOrder(item *order.CygxOrder) {
  428. var err error
  429. defer func() {
  430. if err != nil {
  431. fmt.Println(err)
  432. go utils.SendAlarmMsg(fmt.Sprint("付款成功之后自动取消其他相同的订单失败 CancelOtherOrder, err:", err.Error(), "order_id:", item.OrderId), 2)
  433. }
  434. }()
  435. var condition string
  436. var pars []interface{}
  437. userId := item.UserId
  438. //判断是日卡月卡还是单场付费活动
  439. if item.GoodsId == 1 || item.GoodsId == 2 {
  440. condition += ` AND user_id = ? AND goods_id IN (1,2) AND order_id != ? AND order_status = 1 `
  441. pars = append(pars, userId, item.OrderId)
  442. } else {
  443. condition += ` AND user_id = ? AND source_id = ? AND source = ? AND order_id != ? AND order_status = 1 `
  444. pars = append(pars, userId, item.SourceId, item.Source, item.OrderId)
  445. }
  446. listOrder, e := order.GetCygxOrderDetailList(condition, pars) // 这里就一条数据
  447. if e != nil && e.Error() != utils.ErrNoRow() {
  448. err = errors.New("GetCygxOrderDetailList, Err: " + e.Error())
  449. return
  450. }
  451. if len(listOrder) == 0 {
  452. err = nil
  453. return
  454. }
  455. for _, v := range listOrder {
  456. e = order.CancelCygxOrder(v)
  457. if e != nil {
  458. return
  459. }
  460. }
  461. return
  462. }