<?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\RefundGoodsValidate;
use \Validate\GoodsOnlineOfflineValidate;
use \Validate\PaySuccessGoodsCallbackValidate;
use \Validate\GoodsInitShopValidate;
use \Validate\PindanGoodsAddValidate;
use \Validate\GoodsOnlineSiteValidate;
use \App\Services\goods\ElasticGoodService;
use \App\Services\goods\MarketingPindanGoodsService;
use \App\Models\marketing\mysql\Marketing;
use \App\Services\goods\GoodsSnapshotsService;


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]);
    }

    /**
     * 一级商品分类数据
     * @throws Exception
     */
    public function get_level_one_category_listAction()
    {
        $categoryList = CategoryService::getLevelOneCategory();
        $this->success(["result" => $categoryList]);
    }

    /**
     * 增加商品
     */
    public function addAction()
    {
        $params = $this->params;
        if (!empty($params["marketing_type"]) && $params["marketing_type"] == Marketing::MARKETING_TYPE_PINDAN) {
            (new PindanGoodsAddValidate())->validate();
        } else {
            (new GoodsAddValidate())->validate();
        }

        $res = GoodsService::addGoods($params);
        if (!empty($params["marketing_type"]) && $params["marketing_type"] == Marketing::MARKETING_TYPE_PINDAN) {
            $this->success(["result" => $res]);
        } else {
            $this->success();
        }
    }

    /**
     * 编辑商品
     */
    public function editAction()
    {
        $params = $this->params;
        if (!empty($params["marketing_type"]) && $params["marketing_type"] == Marketing::MARKETING_TYPE_PINDAN) {

        } else {
            (new GoodsEditValidate())->validate();
        }

        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();
    }

    /**
     * 修改商品定点上架设置
     * @date      2021-08-25
     * @license   [license]
     * @version   [version]
     */
    public function online_siteAction()
    {
        $params = $this->params;
        (new GoodsOnlineSiteValidate())->validate();
        GoodsService::onlineSite($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 get_goods_skuAction()
    {
        $params = $this->params;
        $data = GoodsService::getGoodsBySkuId($params);
        $this->success(["result" => $data]);
    }


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

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

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

    /**
     * 支付回调商品接口
     * @throws \App\Exception\custom\GoodsException
     * @throws \App\Exception\custom\InterfaceException
     * @throws \App\Exception\custom\ParamException
     */
    public function pay_success_callbackAction()
    {
        (new PaySuccessGoodsCallbackValidate())->validate();

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

    /**
     * 初始化 shop_id
     * @throws \App\Exception\custom\GoodsException
     */
    public function init_shopAction()
    {
        (new GoodsInitShopValidate())->validate();

        $params = $this->params;
        $shopId = GoodsService::initShop($params);
        $this->success(["result" => ["shop_id" => $shopId]]);
    }

    /**
     * 拼单商品后台搜商品
     * @throws Exception
     */
    public function pindan_goodsAction()
    {
        $params = $this->params;
        $list = MarketingPindanGoodsService::getPindanGoodsList($params);
        $this->success(["result" => $list]);
    }

    /**
     * 拼单商品退款商品逻辑
     * @throws Exception
     */
    public function refundAction()
    {
        //(new RefundGoodsValidate())->validate();
        $params = $this->params;
        $res = GoodsService::refundGoods($params);
        $this->success(["result" => $res]);
    }

    /**
     * 拼单商品快照批量获取
     * @throws Exception
     */
    public function pindan_goods_snapshotsAction()
    {
        $res = GoodsSnapshotsService::pindanGoodsSnapshots($this->params);
        $this->success(["result" => $res]);
    }

    /**
     * 通过goods_sku_id 获取 拼单商品的ota信息
     * @throws Exception
     */
    public function take_place_by_goods_sku_idsAction()
    {
        $res = GoodsService::getOtaInfoByGoodsSkuIds($this->params);
        $this->success(["result" => $res]);
    }

    /**
     * 拼单商品快照批量获取
     * @throws Exception
     */
    public function goods_snapshotsAction()
    {
        $res = GoodsSnapshotsService::goodsSnapshots($this->params);
        $this->success(["result" => $res]);
    }

    /**
     * 下单时获取商品数据 sku and spu
     * @throws Exception
     */
    public function place_order_goods_listAction()
    {
        $params = $this->params;
        $data = GoodsService::getPlaceOrderGoodsList($params);
        $this->success(["result" => $data]);
    }
}