CouponSendService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/9/25 下午2:15
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\frontend\modules\coupon\services;
  10. use app\common\models\CouponLog;
  11. use app\common\models\MemberCoupon;
  12. use app\backend\modules\coupon\services\MessageNotice;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class CouponSendService
  16. {
  17. protected $memberId;
  18. protected $couponId;
  19. protected $get_type;
  20. protected $send_total;
  21. protected $get_time;
  22. protected $relation; //关联,订单号 或 ID值
  23. protected $transferId; //转让者ID
  24. //todo 确定 get_type 类型,增加类型数据验证,
  25. public function sendCouponsToMember($memberId, array $couponIds, $get_type = '0', $relation = '', $transferId = '',$getTime = '')
  26. {
  27. if (empty($memberId) || !is_numeric($memberId)) {
  28. return null;
  29. }
  30. if (empty($couponIds) || !is_array($couponIds)) {
  31. return null;
  32. }
  33. $this->memberId = $memberId;
  34. $this->get_type = $get_type;
  35. $this->send_total = count($couponIds);
  36. $this->relation = $relation;
  37. $this->transferId = $transferId;
  38. $this->get_time = $getTime;
  39. $data = array();
  40. $log_data = array();
  41. foreach ($couponIds as $couponId) {
  42. $this->couponId = $couponId;
  43. $data[] = $this->getMemberCouponData();
  44. $log_data[] = $this->getCouponLogData();
  45. }
  46. //dump($log_data);
  47. //dd($data);
  48. return $this->updateMemberCoupons($data, $log_data);
  49. }
  50. //多种多张优惠券
  51. public function sendCouponsNumsToMember($memberId, array $couponIds, array $couponNums ,$get_type = '0', $relation = '', $transferId = '')
  52. {
  53. if (empty($memberId) || !is_numeric($memberId)) {
  54. return null;
  55. }
  56. if (empty($couponIds) || !is_array($couponIds)) {
  57. return null;
  58. }
  59. $this->memberId = $memberId;
  60. $this->get_type = $get_type;
  61. $this->relation = $relation;
  62. $this->transferId = $transferId;
  63. $data = array();
  64. $log_data = array();
  65. foreach ($couponIds as $key=>$couponId) {
  66. for ($i=0 ; $i<$couponNums[$key] ; $i++) {
  67. $this->couponId = $couponId;
  68. $data[] = $this->getMemberCouponData();
  69. $log_data[] = $this->getCouponLogData();
  70. }
  71. }
  72. return $this->updateMemberCoupons($data, $log_data);
  73. }
  74. public function sendCouponToMembers(array $memberIds, $couponId, $get_type = '0', $relation = '',$transferId)
  75. {
  76. if (empty($couponId) || !is_numeric($couponId)) {
  77. return null;
  78. }
  79. if (empty($memberIds) || !is_array($memberIds)) {
  80. return null;
  81. }
  82. $this->couponId = $couponId;
  83. $this->get_type = $get_type;
  84. $this->send_total = 1;
  85. $this->relation = $relation;
  86. $this->transferId = $transferId;
  87. $data = array();
  88. $log_data = array();
  89. foreach ($memberIds as $memberId) {
  90. $this->memberId = $memberId;
  91. $data[] = $this->getMemberCouponData();
  92. $log_data[] = $this->getCouponLogData();
  93. }
  94. return $this->updateMemberCoupons($data, $log_data);
  95. }
  96. public function sendCouponToMember($memberId, $couponId, $get_type = '0', $relation = '', $transferId = '')
  97. {//新发放优惠券接口
  98. if (empty($memberId) || !is_numeric($memberId)) {
  99. Log::info('优惠券发送接口调用失败,会员ID错误!', print_r($memberId,true));
  100. return null;
  101. }
  102. $this->memberId = $memberId;
  103. $this->get_type = $get_type;
  104. $this->relation = $relation;
  105. $this->transferId = $transferId;
  106. $data = array();
  107. $log_data = array();
  108. $this->couponId = $couponId;
  109. $data[] = $this->getMemberCouponData();
  110. $log_data[] = $this->getCouponLogData();
  111. return $this->updateMemberCoupons($data, $log_data);
  112. }
  113. protected function updateMemberCoupons(array $data, array $log_data)
  114. {
  115. if (empty($data) || !is_array($data)) {
  116. return null;
  117. }
  118. if (empty($log_data) || !is_array($log_data)) {
  119. return null;
  120. }
  121. DB::transaction(function ()use($data,$log_data) {
  122. CouponLog::insert($log_data);
  123. MemberCoupon::insert($data);
  124. });
  125. foreach ($data as $coupon_data) {
  126. //发送获取优惠券通知
  127. MessageNotice::couponNotice($coupon_data['coupon_id'],$coupon_data['uid']);
  128. }
  129. return true;
  130. }
  131. private function getCouponLogData()
  132. {
  133. return [
  134. 'uniacid' => \YunShop::app()->uniacid,
  135. 'logno' => $this->getCouponLogRemark(),
  136. 'member_id' => $this->memberId,
  137. 'couponid' => $this->couponId,
  138. 'paystatus' => 0, //todo 手动发放的不需要支付?
  139. 'creditstatus' => 0, //todo 手动发放的不需要支付?
  140. 'paytype' => 0, //todo 这个字段什么含义?
  141. 'getfrom' => $this->get_type,
  142. 'status' => 0,
  143. 'createtime' => time(),
  144. ];
  145. }
  146. private function getMemberCouponData()
  147. {
  148. return [
  149. 'uniacid' => \YunShop::app()->uniacid,
  150. 'uid' => $this->memberId,
  151. 'coupon_id' => $this->couponId,
  152. 'get_type' => $this->get_type,
  153. 'used' => 0,
  154. 'get_time' => $this->get_time?:time(),
  155. ];
  156. }
  157. //todo get_type 使用常量判断,目前不确定常量放在哪里
  158. private function getCouponLogRemark()
  159. {
  160. $adminId = \YunShop::app()->uid;
  161. switch ($this->get_type) {
  162. //case '0':
  163. //$remark = '手动发放优惠券: 管理员【ID:' . $adminId . '】成功发放 ' . $this->send_total . ' 张优惠券【优惠券ID:' . $this->couponId . '】给用户【会员ID:' . $this->memberId . '】';
  164. //break;
  165. //case '1':
  166. //$remark = '会员领取优惠券: 会员【ID:' . $this->memberId . '】成功领取' . $this->send_total . ' 张优惠券【优惠券ID:' . $this->couponId . '】';
  167. //break;
  168. //case '3':
  169. //$remark = '';
  170. //break;
  171. case '4':
  172. $remark = '购物赠送优惠券: 订单:'.$this->relation.'完成,成功赠送会员【ID:' . $this->memberId . '】1张优惠券【优惠券ID:' . $this->couponId . '】';
  173. break;
  174. case '5':
  175. $remark = '会员转赠: 会员【ID:' . $this->memberId . '】优惠券变动 1张【优惠券ID:' . $this->couponId . '】转让会员【ID:'.$this->transferId.'】';
  176. break;
  177. case '6':
  178. $remark = '签到奖励: 会员【ID:' . $this->memberId . '】优惠券变动 1张【优惠券ID:' . $this->couponId . '】';
  179. break;
  180. case '7':
  181. $remark = '直播会员观看奖励: 会员【ID:' . $this->memberId . '】获得优惠券 1张【优惠券ID:' . $this->couponId . '】';
  182. break;
  183. case '8':
  184. $remark = '直播间会员奖励: 会员【ID:' . $this->memberId . '】获得优惠券 1张【优惠券ID:' . $this->couponId . '】';
  185. break;
  186. case '9':
  187. $remark = '直播主播奖励: 会员【ID:' . $this->memberId . '】获得优惠券 1张【优惠券ID:' . $this->couponId . '】';
  188. break;
  189. case '10':
  190. $remark = '会员合并转入: 会员【ID:' . $this->memberId . '】获得优惠券 1张【优惠券ID:' . $this->couponId . '】';
  191. break;
  192. default:
  193. $remark = '未知优惠券变动:会员【ID:' . $this->memberId . '】优惠券变动 1张【优惠券ID:' . $this->couponId . '】';
  194. break;
  195. }
  196. return $remark;
  197. }
  198. }