<?php
namespace app\admin\server\package;

use app\admin\model\Package;
use app\admin\model\PackageGoods;
use app\admin\model\PackageOrder;
use app\admin\model\PackageOrderDetail;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\model\SystemRole;

class PackageServer
{
    /**
     * Notes:获取角色列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/17
     */
    public static function getPackageList(int $page, int $limit, $keywords)
    {
        [$list, $count] =  Package::getPackageList($page, $limit, $keywords);

        return compact('list', 'page', 'limit', 'count');
    }
    /**
     * Notes: 添加权益包
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/17
     */
    public static function insertPackage(array $param)
    {
        SystemRole::affairBegin();
        try {
            $param['package_create_time'] = time();
            $param['package_update_time'] = time();
            $result =  Package::insertGetId($param);
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:修改角色
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/19
     */
    public static function updatePackage($param)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['package_id'] = $param['package_id'];
            $param['package_update_time'] = time();
            $result = Package::where($where)->update($param);
            if ($result !== false){
                SystemRole::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:任务
     * @param int $role_id
     * @return int
     * User: QJF
     * Date: 2022/9/19
     */
    public static function delPackage($package_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['package_id'] = $package_id;
            $data['package_is_del'] = 1;
            $data['package_update_time'] = time();

            $result = Package::where($where)->update($data);
            if (!empty($result)){
                SystemRole::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:获取权益包详情
     * @param int $package_id
     * @return int
     * User: QJF
     * Date: 2022/9/19
     */
    public static function infoPackage($package_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['package_id'] = $package_id;
            $result = Package::where($where)->first();
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:赠送会员权益包
     * @param $user_id
     * @param $package_id
     * @return int
     * @throws \Exception
     * User: yym
     * Date: 2022/11/21
     */
    public static function givePackage($user_id, $package_id)
    {
        try {
            $package_info = Package::getInfo($package_id);
            if(empty($package_info) || $package_info['package_status'] == Package::PACKAGE_LOWER_SHELF || $package_info['package_delete_time'] > 0)
            {
                throw new \Exception('产品已下架或已删除');
            }
            if(!isset($package_info['goods']) || empty($package_info['goods']))
            {
                throw new \Exception('套餐包暂未关联产品');
            }
            $time = time();
            $_order = array(
                'order_dno' => createOrderSn(16),
                'order_user_id' => $user_id,
                'order_package_id' => $package_id,
                'order_pay_type'   => 1,
                'order_place_time' => $time,
                'order_create_time' => $time,
                'order_pay_status' => PackageOrder::PAY_STATUS_YES,
                'order_pay_time' => $time,
                'order_end_time' => $time + 86400 * 365
            );

            $_order_detail = array();
            $order_price = 0;
            foreach ($package_info['goods'] as $k => $item)
            {
                $order_detail = array(
                    'detail_goods_id'   => $item['goods_id'],
                    'detail_user_id'    => $user_id,
                    'detail_goods_name' => $item['goods_name'],
                    'detail_img'        => $item['goods_img'],
                    'detail_nape'       => $item['goods_nape'],
                    'detail_equity'     => $item['goods_equity'],
                    'detail_price'      => $item['goods_price'],
                    'detail_create_time' => $time,
                    'detail_update_time' => $time
                );
                $_order_detail[] = $order_detail;
                $order_price += $item['goods_price'];
            }
            $_order['order_price'] = $order_price;
            //生成主订单
            $order_id = PackageOrder::insertData($_order);
            if($order_id)
            {
                foreach ($_order_detail as $row)
                {
                    $row['detail_order_id'] = $order_id;
                    PackageOrderDetail::insertData($row);
                }
                return $order_id;
            }
            throw new \Exception('生成订单失败！');
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
