<?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 \App\Services\goods\ElasticGoodService;


class GoodsController extends Base
{
    /**
     * 获取商品分类列表
     */
    public function get_goods_category_listAction()
    {
        $categoryList = CategoryService::getCategoryList();
        $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 GoodsAuditValidate())->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["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["life_account_id"]);
        $this->success(["result" => $data]);
    }
}