CouponTransferController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/9/26 下午1:44
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\frontend\modules\coupon\controllers;
  10. use app\common\components\ApiController;
  11. use app\common\exceptions\AppException;
  12. use app\common\exceptions\ShopException;
  13. use app\common\facades\Setting;
  14. use app\common\models\coupon\CouponUseLog;
  15. use app\frontend\models\Member;
  16. use app\frontend\modules\coupon\models\MemberCoupon;
  17. use app\frontend\modules\coupon\services\CouponSendService;
  18. use app\backend\modules\coupon\services\MessageNotice;
  19. use Illuminate\Support\Facades\DB;
  20. use Yunshop\GiftCouponFee\frontend\CouponFeeController;
  21. class CouponTransferController extends ApiController
  22. {
  23. public $memberModel;
  24. public function index()
  25. {
  26. $coupon_set = \Setting::getByGroup('coupon');
  27. $recipient = trim(\YunShop::request()->recipient);
  28. $record_id = trim(\YunShop::request()->record_id);
  29. $transfer_num = trim(\YunShop::request()->transfer_num);
  30. if (!$this->getMemberInfo()) {
  31. return $this->errorJson('未获取到会员信息');
  32. }
  33. if (!Member::uniacid()->select('uid')->where('uid',$recipient)->first()) {
  34. return $this->errorJson('被转让者不存在');
  35. }
  36. if ($this->memberModel->uid == $recipient) {
  37. return $this->errorJson('转让者不能是自己');
  38. }
  39. $_model = MemberCoupon::select('id','coupon_id','get_time','uid')->where('id',$record_id)->where('uid',\YunShop::app()->getMemberId())->with(['belongsToCoupon'])->first();
  40. if (!$_model) {
  41. return $this->errorJson('未获取到该优惠券记录ID');
  42. }
  43. //开启手续费后直接走手续费入口
  44. if (app('plugins')->isEnabled('gift-coupon-fee') && Setting::get("couponbase_setting")["switch"]==1 && $coupon_set['transfer_num'] != 1) {
  45. return (new CouponFeeController())->index();
  46. }
  47. if (!$coupon_set['transfer_num'] && $transfer_num > 1) {
  48. return $this->errorJson('未开启多张转赠功能!');
  49. }
  50. if ($coupon_set['transfer_num']) {
  51. $this->judgeNum($transfer_num,$_model);
  52. if ($coupon_set['transfer_choice'] == 1) {
  53. //最新日期
  54. $finder = MemberCoupon::uniacid()
  55. ->where(['used'=>0,'is_member_deleted'=>0,'is_expired'=>0,'uid'=>$_model->uid,'coupon_id'=>$_model->coupon_id])
  56. ->orderBy('get_time','desc')
  57. ->limit($transfer_num)
  58. ->get();
  59. } else {
  60. //快过期
  61. $finder = MemberCoupon::uniacid()
  62. ->where(['used'=>0,'is_member_deleted'=>0,'is_expired'=>0,'uid'=>$_model->uid,'coupon_id'=>$_model->coupon_id])
  63. ->orderBy('get_time','asc')
  64. ->limit($transfer_num)
  65. ->get();
  66. }
  67. DB::beginTransaction();
  68. try {
  69. foreach ($finder as $find) {
  70. //因为需要继承获得时间,所以此处遍历
  71. $couponService = new CouponSendService();
  72. $result = $couponService->sendCouponsToMember($recipient, [$_model->coupon_id], '5', '', $this->memberModel->uid, strtotime($find->get_time));
  73. if (!$result) {
  74. throw new AppException('转让失败:(写入出错)');
  75. }
  76. $this->handleTransfer($_model->coupon_id, $find->id, $recipient);
  77. }
  78. DB::commit();
  79. return $this->successJson('转让成功,');
  80. } catch (\Exception $e) {
  81. DB::rollBack();
  82. throw new AppException($e->getMessage());
  83. }
  84. }
  85. $couponService = new CouponSendService();
  86. $result = $couponService->sendCouponsToMember($recipient,[$_model->coupon_id],'5','',$this->memberModel->uid,strtotime($_model->get_time));
  87. if (!$result) {
  88. return $this->errorJson('转让失败:(写入出错)');
  89. }
  90. $this->handleTransfer($_model->coupon_id,$record_id,$recipient);
  91. return $this->successJson('转让成功,');
  92. }
  93. /**
  94. * @param $coupon_id
  95. * @param $record_id
  96. * @param $recipient
  97. * @return \Illuminate\Http\JsonResponse
  98. * 减去持有者优惠券&记录
  99. */
  100. private function handleTransfer($coupon_id,$record_id,$recipient)
  101. {
  102. $result = MemberCoupon::where('id',$record_id)->update(['used' => 1,'use_time' => time(),'deleted_at' => time()]);
  103. if (!$result) {
  104. return $this->errorJson('转让失败:(记录修改出错)');
  105. }
  106. $log_data = [
  107. 'uniacid' => \YunShop::app()->uniacid,
  108. 'member_id' => \YunShop::app()->getMemberId(),
  109. 'detail' => '会员(ID为' . \YunShop::app()->getMemberId() . ')转赠一张优惠券(ID为' . $coupon_id . '),受赠会员(ID为' . $recipient . ')',
  110. 'coupon_id' => $coupon_id,
  111. 'member_coupon_id' => $record_id,
  112. 'type' => CouponUseLog::TYPE_TRANSFER
  113. ];
  114. $model = new CouponUseLog();
  115. $model->fill($log_data);
  116. $model->save();
  117. }
  118. /**
  119. * @param $transfer_num
  120. * @throws AppException
  121. * 判断输入转赠数量
  122. */
  123. private function judgeNum($transfer_num,$_model)
  124. {
  125. if($transfer_num < 0 || !is_numeric($transfer_num) || floor($transfer_num) != $transfer_num)
  126. {
  127. throw new AppException('请输入正确张数');
  128. }
  129. $coupons_num = MemberCoupon::uniacid()
  130. ->where(['used'=>0,'is_member_deleted'=>0,'is_expired'=>0,'uid'=>$_model->uid,'coupon_id'=>$_model->coupon_id])
  131. ->count();
  132. if($transfer_num > $coupons_num)
  133. {
  134. throw new AppException('数量不足,请重新输入');
  135. }
  136. }
  137. private function getMemberInfo()
  138. {
  139. return $this->memberModel = Member::select('uid')->where('uid',\YunShop::app()->getMemberId())->first();
  140. }
  141. }