<?php
namespace app\jinyou\model;

use fastphp\base\Model;

class Package extends Model
{
    protected $table = 'product_package';

     /**
     * 获取套餐
     * @param bool $isset_price 是否使用设置的价格
     * @param int $discount 折扣
     * @param array $user_price 用户的价格;
     */
    public function get_package($origin,$isset_price= false,$discount = 0,$user_price = []) {
        $where['OriginName'] = ['like', $origin . '%'];
        $where['Status'] = 1;
        $where['DeleteTag'] = 0;
        $package_list =  $this->getList($where,'*', 'id asc');

        $list = [];
        
        foreach ($package_list as $package) {
            $package_info = explode('-',$package['OriginName']);

            //判断价格
            if ($isset_price) {
                $price = 1000000;
                $package_price = $package['Price'];
                if ($discount != 0) {
                    $price = $package['LinePrice'] * $discount / 100;
                } else {
                    if (!empty($user_price)) {
                        foreach ($user_price as $k=>$v) {
                            if ($package['Id'] == $k) {
                                $price = $v;
                            }
                        }
                    }
                }
                //判读最低价
                if ($price < $package['MinPrice']) {
                    $price = $package['MinPrice'];
                }
                if ($price < $package_price) {
                    $package['Price'] = $price;
                }

                if ($price > $package_price && $price < 1000000) {
                    $package['Price'] = $price;
                }
            }

            if (isset($package_info[3])) {
                $package['package_name'] = $package_info[3];
            } else {
                $package['package_name'] = '1小时';
            }

            if($package_info[1] == "项目") {
                $package_info[1] = 'game';
            }
            if($package_info[1] == "线路") {
                $package_info[1] = 'line';
            }
            if ($package_info[2] == '测试') {
                $package_info[2] = 'test';
            }
            $list[$package_info[1]][$package_info[2]][] = $package;
        }
        return $list;
    }
}