<?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;
use support\Log;

class KangyangCityEvent
{
    public function grant($params)
    {
        // 会员VIP 标识
        $member = Member::where('member_id', $params['member_id'])->select('member_mobile', 'member_is_vip', 'member_extend_json')->first();
        $memberUpdateData = [
            'member_is_vip' => 'Y'
        ];
        if (!empty($params['referee'])) {
            $memberExtendJson = [];
            if (!empty($member->member_extend_json)) {
                $memberExtendJson = json_decode($member->member_extend_json, true);
            }
            $memberExtendJson['referee'][date('Y-m-d H:i:s')] = $params['referee'];
            $memberUpdateData['member_extend_json'] = json_encode($memberExtendJson, JSON_UNESCAPED_UNICODE);
        }
        Member::where('member_id', $params['member_id'])->update($memberUpdateData);
        // 康养城VIP账户
        $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'VIP')->first();
        // 分期付款
        if (isset($params['order_amount_paid']) && $params['order_amount_paid'] > 0){
            $params['order_amount_pay'] = $params['order_amount_paid'] + $params['order_amount_pay'];
        }
        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) - 1);
                    }

                    $num = $coupon['num'];
                    for ($i = 0; $i < $num; $i++) {
                        CouponDetail::insert([
                            'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, '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()
                        ]);
                    }
                }
            }
        }
        // 通知判客
        if (!empty($member->member_mobile)) {
            $url = getenv("MARKET_NOTICE_DOMAIN") . '/custom/market_custom/update';
            $result = http_post($url, ['mobile' => $member->member_mobile]);
            Log::info("MARKET_NOTICE_" . $member->member_mobile, json_decode($result, true));
        }
    }
}