<?php
/**
 * Created by PhpStorm.
 * User: zilongs
 * Date: 20-10-2
 * Time: 下午10:57
 */

namespace App\Models;

class OrderPack extends BaseModel
{
    protected $appends = ['label_texts', 'usable_status'];
    protected $casts = [
        'label' => 'json',
        'team_id' => 'json'
    ];

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function getLabelTextsAttribute()
    {
        $data = [];
        if (!empty($this->label) && is_array($this->label)) {
            $map = config('config.pack_label_map');
            foreach ($this->label as $k => $v) {
                $data[$k]['name'] = $map[$v]??'';
            }
        }

        return $data;
    }

    public function getUsableStatusAttribute()
    {
        if ($this->phone_minutes == 0 && $this->chat_num == 0 && $this->appoint_num == 0 && $this->vaccine_limit_amount == 0 && $this->nurses_limit_amount == 0) {
            return 0;
        }

        return 1;
    }

    public static function checkUserServicePack($order_pack_id, $user_id, $product_type, $payment_amount)
    {
        $data = self::where('id', $order_pack_id)->where('user_id', $user_id)->first();
        if (empty($data)) {
            exit_out(null, 10001, '服务包不存在');
        }
        if ($data['end_time'] < time()) {
            exit_out(null, 10002, '服务包已过期');
        }

        if ($product_type == 1) {
            if ($data['phone_minutes'] < 10) {
                exit_out(null, 10003, '服务包的电话咨询时间不足');
            }
        }
        elseif ($product_type == 2) {
            if ($data['chat_num'] <= 0) {
                exit_out(null, 10004, '服务包的图文咨询次数不足');
            }
        }
        elseif ($product_type == 3) {
            if ($data['appoint_num'] <= 0) {
                exit_out(null, 10005, '服务包的门诊预约次数不足');
            }
        }
        elseif ($product_type == 4) {
            if ($data['vaccine_limit_amount'] < $payment_amount) {
                exit_out(null, 10006, '服务包疫苗金额不足');
            }
        }
        elseif ($product_type == 5) {
            if ($data['nurses_limit_amount'] < $payment_amount) {
                exit_out(null, 10007, '服务包儿保金额不足');
            }
        }

        return true;
    }

    public static function deductPackData($order_pack_id, $product_type, $payment_amount)
    {
        if ($product_type == 1) {
            self::where('id', $order_pack_id)->decrement('phone_minutes', 10);
        }
        elseif ($product_type == 2) {
            self::where('id', $order_pack_id)->decrement('chat_num');
        }
        elseif ($product_type == 3) {
            self::where('id', $order_pack_id)->decrement('appoint_num');
        }
        elseif ($product_type == 4) {
            self::where('id', $order_pack_id)->decrement('vaccine_limit_amount', $payment_amount);
        }
        elseif ($product_type == 5) {
            self::where('id', $order_pack_id)->decrement('nurses_limit_amount', $payment_amount);
        }

        return true;
    }
}
