<?php

namespace app\admin\controller\healthy;

use app\admin\validate\healthy\ProductValidate;
use app\controller\Curd;
use app\model\HealthyProduct;
use support\Request;
use support\Response;

class ProductController extends Curd
{
    public function __construct()
    {
        $this->model = new HealthyProduct();
        $this->validate = true;
        $this->validateClass = new ProductValidate();
    }

    /**
     * @Desc 列表
     * @Author Gorden
     * @Date 2024/3/4 13:28
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'product_sort';
        $where['product_is_del'] = 0;
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * @Desc 删除
     * @Author Gorden
     * @Date 2024/3/4 13:33
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doSoftDelete($ids, ['product_is_del' => 1]);

        return json_success('success');
    }

    /**
     * @Desc 软删除
     * @Author Gorden
     * @Date 2024/3/4 13:32
     *
     * @param Request $request
     * @return Response
     */
    public function updateStatus(Request $request)
    {
        return $this->updateField($request->post('product_id'), 'product_status', $request->post('product_status'));
    }
}