<?php

use App\Base\Base;
use App\Services\goods\GoodsService;
use App\Services\goods\CategoryService;
use \Validate\CheckGoodsNameValidate;
use \Validate\GoodsAuditValidate;
use \Validate\GoodsAddValidate;
use \Validate\GoodsEditValidate;
use \Validate\GoodsInfoOpValidate;
use \Validate\GoodsInfoFeValidate;
use \Validate\GoodsListCValidate;
use \Validate\OrderGoodsValidate;
use \Validate\GoodsOnlineOfflineValidate;
use \App\Services\goods\ElasticGoodService;


class GoodsController extends Base
{
    /**
     * 获取商品分类列表
     */
    public function get_goods_category_listAction()
    {
        $name = empty($this->params["category_name"]) ? "" : $this->params["category_name"];
        $categoryList = CategoryService::getCategoryList($name);
        $this->success(["result" => $categoryList]);
    }

    /**
     * 增加商品
     */
    public function addAction()
    {
        (new GoodsAddValidate())->validate();

        $params = $this->params;
        GoodsService::addGoods($params);
        $this->success();
    }

    /**
     * 编辑商品
     */
    public function editAction()
    {
        (new GoodsEditValidate())->validate();

        $params = $this->params;
        GoodsService::editGoods($params);
        $this->success();
    }

    /**
     * 后台商品详情
     */
    public function info_opAction()
    {
        (new GoodsInfoOpValidate())->validate();

        $params = $this->params;
        $data = GoodsService::getGoodsSpuInfo($params);
        $this->success(["result" => $data]);
    }

    /**
     * 前端商品详情
     */
    public function info_feAction()
    {
        (new GoodsInfoFeValidate())->validate();

        $params = $this->params;
        $data = GoodsService::getGoodsSkuInfo($params);
        $this->success(["result" => $data]);
    }

    /**
     * 上下架
     * @throws \App\Exception\custom\GoodsException
     * @throws \App\Exception\custom\ParamException
     */
    public function online_offlineAction()
    {
        (new GoodsOnlineOfflineValidate())->validate();

        $params = $this->params;
        GoodsService::onlineOrOffline($params);
        $this->success();
    }

    /**
     * 审核通过/驳回
     */
    public function auditAction()
    {
        (new GoodsAuditValidate())->validate();

        $params = $this->params;
        GoodsService::audit($params);
        $this->success();
    }

    /**
     * 判断商品名称是否可用
     */
    public function check_goods_nameAction()
    {
        (new CheckGoodsNameValidate())->validate();

        $params = $this->params;
        $res = GoodsService::checkGoodsName($params["goods_name"], $params["life_account_id"]);
        $this->success(["result" => $res]);
    }

    /**
     * op后台：商品列表 (spu list)
     */
    public function goods_list_opAction()
    {
        $params = $this->params;

        $data = GoodsService::getOpGoodsSpuList($params);
        $this->success(["result" => $data]);
    }

    /**
     * b端：商品列表（spu list）
     */
    public function goods_list_bAction()
    {

    }

    /**
     * c端 es：商品列表 (sku list)
     * @throws Exception
     */
    public function goods_list_esAction()
    {
        $params = $this->params;

        $data = ElasticGoodService::searchDoc($params);
        $this->success(["result" => $data]);
    }

    /**
     * 商家 - 商品列表 (sku list)
     * @throws Exception
     */
    public function goods_list_cAction()
    {
        (new GoodsListCValidate())->validate();

        $params = $this->params;

        $data = GoodsService::getCGoodsSkuList($params);
        $this->success(["result" => $data]);
    }

    /**
     * 商品实时信息
     */
    public function goods_real_time_infoAction()
    {
        (new OrderGoodsValidate())->validate();

        $params = $this->params;
        $params["goods_sku_id"] = $params["id"];
        $data = GoodsService::getGoodsRealTimeInfo($params);
        $this->success(["result" => $data]);
    }

    /**
     * 商品快照
     * @throws \App\Exception\custom\ParamException
     */
    public function goods_snapshot_infoAction()
    {
        (new OrderGoodsValidate())->validate();
        $params = $this->params;
        $params["goods_sku_id"] = $params["id"];
        $data = GoodsService::getGoodsSnapshotInfo($params);
        $this->success(["result" => $data]);
    }

    /**
     * 商品营销活动
     */
    public function goods_marketingAction()
    {
        (new OrderGoodsValidate())->validate();
        $params = $this->params;
        $params["goods_sku_id"] = $params["id"];

        $data = GoodsService::checkGoodsMarketing($params);
        $this->success(["result" => $data]);
    }


    public function goods_sku_listAction()
    {
        $params = $this->params;
        $data = GoodsService::getGoodsSkuList($params);
        $this->success(["result" => $data]);
    }

}