12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace app\event\order;
- use app\model\Coupon;
- use app\model\CouponDetail;
- use app\model\Goods;
- use app\model\Member;
- use app\model\MemberAccount;
- use app\model\SysSerial;
- class KangyangCityEvent
- {
- public function grant($params)
- {
- // 会员VIP 标识
- Member::where('member_id', $params['member_id'])->update(['member_is_vip' => 'Y']);
- // 康养城VIP账户
- $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'VIP')->first();
- if (!$memberAccount) {
- $account = [
- 'join_account_member_id' => $params['member_id'],
- 'member_account_classify' => 'VIP',
- 'member_account_status' => 'ACTIVED',
- 'member_account_category' => 'NORMAL',
- 'member_account_nbr' => $params['member_id'] . '-VIP',
- 'member_account_name' => 'VIP账户',
- 'member_account_income' => $params['order_amount_pay'],
- 'member_account_surplus' => $params['order_amount_pay'],
- 'member_account_addtimes' => time(),
- ];
- MemberAccount::insert($account);
- } else {
- // 重复购买
- $memberAccount->member_account_income = $memberAccount->member_account_income + $params['order_amount_pay'];
- $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $params['order_amount_pay'];
- $memberAccount->save();
- }
- //发放优惠券
- $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])->select('goods_attribute_json')->first();
- if (!empty($goods) && !empty($goods->goods_attribute_json)) {
- $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
- if (!empty($goodsAttributeJson['coupon'])) {
- foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
- $couponModel = Coupon::where('coupon_id', $key)->select('coupon_id', 'coupon_validdate_day', 'coupon_validdate_end')->first();
- if (empty($couponModel)) {
- continue;
- }
- // 券是否过期
- if (!empty($couponModel->coupon_validdate_end) && strtotime($couponModel->coupon_validdate_end) < time()){
- continue;
- }
- $endDate = '';
- if (!empty($couponModel->coupon_validdate_end)) {
- $endDate = $couponModel->coupon_validdate_end;
- } elseif ($couponModel->coupon_validdate_day > 0) {
- $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600));
- }
- $num = $coupon['num'];
- for ($i = 0; $i < $num; $i++) {
- CouponDetail::insert([
- 'coupon_detail_id' => 'CUDT' . str_pad(SysSerial::getSerial(), 8, "0", STR_PAD_LEFT) . random_string(6, 'up'),
- 'join_detail_coupon_id' => $key,
- 'join_coupon_detail_member_id' => $params['member_id'],
- 'coupon_detail_status' => 'ACTIVED',
- 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
- 'coupon_detail_deadline_datetime' => $endDate,
- 'coupon_detail_json' => '[]',
- 'coupon_detail_extend_json' => json_encode(['gettype'=>'VIP']),
- 'coupon_detail_addtimes' => time()
- ]);
- }
- }
- }
- }
- }
- }
|