<?php

namespace app\admin\controller\goods;

use app\admin\service\goods\GoodsService;
use app\admin\validate\goods\GoodsValidate;
use app\model\GoodsDetail;
use support\Request;
use support\Response;

class EntityGoodsController
{
    /**
     * @Desc 列表
     * @Author Gorden
     * @Date 2024/3/28 10:08
     *
     * @param Request $request
     * @return Response
     */
    public function select(Request $request)
    {

        return GoodsService::select($request, 'GOODS');
    }

    /**
     * @Desc 搜索产品
     * @Author Gorden
     * @Date 2024/11/8 10:12
     *
     * @param Request $request
     * @return Response
     */
    public function selectIdNameList(Request $request)
    {
        return GoodsService::selectIdNameList($request);
    }

    public function selectAll(Request $request)
    {
        $goodsIds = $request->get('goods_id', '');

        return GoodsService::selectAll($goodsIds);
    }

    public function selectAllByGoodsName(Request $request)
    {
        $goodsName = $request->get('goods_name', '');
        if (!$goodsName) {
            return json_fail("请输入要搜索的产品");
        }

        return GoodsService::selectAllByGoodsName($goodsName);
    }

    public function selectAllByCategoryForRuleAddComponent(Request $request)
    {
        $category = $request->get('category', 'GOODS');

        return GoodsService::selectAllByCategoryForRuleAddComponent($category);
    }

    public function selectPremisesByGoodsId(Request $request)
    {
        return GoodsService::selectPremisesByGoodsId($request->get('goods_id'));
    }

    /**
     * @Desc 商品详情
     * @Author Gorden
     * @Date 2024/3/28 10:25
     *
     * @param Request $request
     * @return Response
     */
    public function info(Request $request)
    {
        $validate = new GoodsValidate();
        if (!$validate->scene('info')->check($request->get())) {
            return json_fail($validate->getError());
        }

        return GoodsService::info($request->get('goods_id'));
    }

    /**
     * @Desc 添加商品
     * @Author Gorden
     * @Date 2024/3/11 10:21
     *
     * @param Request $request
     * @return Response
     */
    public function insert(Request $request): Response
    {
        $params = $request->post();
        $validate = new GoodsValidate();
        if (!$validate->scene('add')->check($params)) {
            return json_fail($validate->getError());
        }

        $params['goods_classify'] = $params['goods_classify'] ?? 'GOODS';
        return GoodsService::insert($params);
    }

    /**
     * @Desc 修改商品
     * @Author Gorden
     * @Date 2024/3/28 13:22
     *
     * @param Request $request
     * @return Response
     */
    public function update(Request $request): Response
    {
        $validate = new GoodsValidate();
        if (!$validate->scene('update')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return GoodsService::update($request->post());
    }

    /**
     * @Desc 删除商品
     * @Author Gorden
     * @Date 2024/3/28 13:22
     *
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request)
    {
        $ids = $request->post('goods_id');

        return GoodsService::delete($ids);
    }
}