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

use app\admin\controller\BaseController;
use app\admin\server\package\PackageGoodsServer;

class PackageGoods extends BaseController
{
    /**
     * Notes:获取产品列表
     * @return \support\Response
     * $param type_id 1:正常分页  2：获取1000条数据
     * User: QJF
     * Date: 2022/9/20
     */
    public function getPackageGoodsList()
    {
        [$page, $limit] = $this->getPage();
        $keywords = $this->request->get('keywords');
        $type_id = $this->request->get('type_id');
        //用于展示套餐包列表，不需要分页，所以默认第一页获取1000条数据
        if($type_id == 2){
            $limit = 1000;
        }
        $result         = PackageGoodsServer::getPackageGoodsList($page, $limit, $keywords);

        return json_success($result, '成功');
    }
    /**
     * Notes:添加产品
     * @return \support\Response
     * User: QJF
     * Date: 2022/9/20
     *
     */
    public function addPackageGoods()
    {
        $param     = $this->request->post();

        $this->validateCheck('package\PackageGoodsValidate',
            [
                'goods_name'        => $param['goods_name'],
                'goods_titile'      => $param['goods_titile'],
                'goods_nape'        => $param['goods_nape'],
                'goods_price'        => $param['goods_price'],
                'goods_equity'      => $param['goods_equity'],
                'goods_img'         => $param['goods_img'],
                'goods_category_id' => $param['goods_category_id'],
                'goods_num'         => $param['goods_num'],
                'goods_num_type'    => $param['goods_num_type'],
                'goods_num_unit'    => $param['goods_num_unit'],
                'goods_type'        => $param['goods_type'],
            ],
            'add');
        $result         = PackageGoodsServer::insertPackage($param);
        return json_success($result, '添加成功');
    }
    /**
     * Notes:修改权益包
     * @return \support\Response
     * User: QJF
     * Date: 2022/9/19
     */
    public function updatePackageGoods()
    {
        $param      = $this->request->post();
        $this->validateCheck('package\PackageGoodsValidate',
            [
                'goods_id'            => $param['goods_id'],
            ], 'update');
         $result         = PackageGoodsServer::updatePackageGoods($param);
        return json_success($result, '修改成功');
    }

    /**
     * Notes:删除权益包
     * @return \support\Response
     * User: QJF
     * Date: 2022/9/19
     */
    public function delPackageGoods()
    {
        $goods_id = $this->request->get('goods_id');
        $goods_status = $this->request->get('goods_status');
        $this->validateCheck('package\PackageGoodsValidate', ['goods_id' => $goods_id], 'del');
        $result         = PackageGoodsServer::delPackageGoods($goods_id,$goods_status);
        if ($result){
            return json_success($result, '成功');
        }else{
            throw new \Exception('失败！');
        }

    }


    /**
     * Notes:查看权益包详情
     * @return \support\Response
     * User: QJF
     * Date: 2022/9/19
     */
    public function infoPackageGoods()
    {
        $goods_id = $this->request->get('goods_id');
        $this->validateCheck('package\PackageGoodsValidate', ['goods_id' => $goods_id], 'info');
        $result         = PackageGoodsServer::infoPackage($goods_id);
        if ($result){
            return json_success($result, '获取成功');
        }else{
            throw new \Exception('获取失败！');
        }

    }
}
