PartnerEvent.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\event\order;
  3. use app\model\Coupon;
  4. use app\model\CouponDetail;
  5. use app\model\Goods;
  6. use app\model\Member;
  7. use app\model\MemberAccount;
  8. use app\model\PayDetail;
  9. use app\model\SysConfig;
  10. use app\model\SysSerial;
  11. use support\Log;
  12. class PartnerEvent
  13. {
  14. public function grant($params)
  15. {
  16. $config = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  17. $configParams = json_decode($config->config_value_json, true);
  18. // 更新身份
  19. Member::where('member_id', $params['member_id'])->update(['member_is_partner' => 'Y']);
  20. // 余额账户
  21. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'CASH')->first();
  22. // 分期付款
  23. if (isset($params['order_amount_paid']) && $params['order_amount_paid'] > 0) {
  24. $params['order_amount_pay'] = $params['order_amount_paid'] + $params['order_amount_pay'];
  25. }
  26. // 增值到最后一条支付记录
  27. $added = 0;
  28. $payDetail = PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])->orderBy('pay_addtimes', 'DESC')->first();
  29. if (!empty($configParams['account']['added'])) {
  30. $params['order_amount_pay'] = 10000;
  31. $added = round($params['order_amount_pay'] * $configParams['account']['added'], 2);
  32. $payDetail->pay_extend_json = json_encode(['added_rate' => $configParams['account']['added'], 'added_amount' => $added]);
  33. $payDetail->save();
  34. }
  35. // 入账
  36. $memberAccount->member_account_income = $memberAccount->member_account_income + $params['order_amount_pay'];
  37. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $params['order_amount_pay'];
  38. $memberAccount->member_account_added = $memberAccount->member_account_added + $added;
  39. $memberAccount->save();
  40. //发放优惠券
  41. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])->select('goods_attribute_json')->first();
  42. if (!empty($goods) && !empty($goods->goods_attribute_json)) {
  43. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  44. if (!empty($goodsAttributeJson['coupon'])) {
  45. foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
  46. $couponModel = Coupon::where('coupon_id', $key)->select('coupon_id', 'coupon_validdate_day', 'coupon_validdate_end')->first();
  47. if (empty($couponModel)) {
  48. continue;
  49. }
  50. // 券是否过期
  51. if (!empty($couponModel->coupon_validdate_end) && strtotime($couponModel->coupon_validdate_end) < time()) {
  52. continue;
  53. }
  54. $endDate = '';
  55. if (!empty($couponModel->coupon_validdate_end)) {
  56. $endDate = $couponModel->coupon_validdate_end;
  57. } elseif ($couponModel->coupon_validdate_day > 0) {
  58. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600) - 1);
  59. }
  60. $num = $coupon['num'];
  61. for ($i = 0; $i < $num; $i++) {
  62. CouponDetail::insert([
  63. 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
  64. 'join_detail_coupon_id' => $key,
  65. 'join_coupon_detail_member_id' => $params['member_id'],
  66. 'coupon_detail_status' => 'ACTIVED',
  67. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  68. 'coupon_detail_deadline_datetime' => $endDate,
  69. 'coupon_detail_json' => '[]',
  70. 'coupon_detail_extend_json' => json_encode(['gettype' => 'PARTNER']),
  71. 'coupon_detail_addtimes' => time()
  72. ]);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }