<?php
namespace app\admin\controller\life;

use app\admin\controller\BaseController;
use app\admin\server\life\FarmGoodsServer;

class FarmGoods extends BaseController
{

    /**
     * Notes:获取绿色蔬菜列表
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/4
     */
    public function getFarmGoodsList()
    {
        [$page, $limit] = $this->getPage();
        $keywords = $this->request->get('keywords');
        $result         = FarmGoodsServer::getFarmGoodsList($page, $limit, $keywords);
        return json_success($result, '成功');
    }


    /**
     * Notes:修改绿色蔬菜
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/4
     */
    public function updateFarmGoods()
    {
        $params    = $this->request->post();
        $admin_id  = $this->request->admin_id;
        $this->validateCheck('life\FarmGoodsValidate',
            [
                'goods_id'  => $params['goods_id']?? '',
                'goods_name'  => $params['goods_name']?? '',
                'goods_img'    => $params['goods_img']?? '',
                'food_silde' => $params['food_silde']?? '',
                'goods_category_id' => $params['goods_category_id']?? '',
                'goods_shop_id' => $params['goods_shop_id']?? '',
                'goods_is_new'    => $params['goods_is_new']?? '',
                'goods_price' => $params['goods_price']?? '',
                'goods_content' => $params['goods_content']?? '',
                'goods_sell_num' => $params['goods_sell_num']?? '',
                'goods_postage_id'    => $params['goods_postage_id']?? '',
                'goods_status' => $params['goods_status']?? '',
                'goods_weight' => $params['goods_weight']?? '',
                'food_storage' => $params['food_storage']?? '',
                'food_packaging' => $params['food_packaging']?? '',
                'food_varieties' => $params['food_varieties']?? '',
                'food_attr'    => $params['food_attr']?? '',
                'goods_sort' => $params['goods_sort']?? '',
            ],
            'update');
        $result       = FarmGoodsServer::updateFarmGoods($params, $admin_id);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:删除绿色蔬菜
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/4
     */
    public function delFarmGoods()
    {
        $goods_id = $this->request->get('goods_id');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('life\FarmGoodsValidate', ['goods_id' => $goods_id], 'info');
        $result         = FarmGoodsServer::delFarmGoods($goods_id,$admin_id);
        if ($result){
            return json_success($result, '删除成功');
        }else{
            throw new \Exception('删除失败！');
        }

    }

    /**
     * Notes:添加绿色蔬菜
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/4
     */
    public function addFarmGoods()
    {
        
        $params    = $this->request->post();
        $admin_id  = $this->request->admin_id;
        $this->validateCheck('life\FarmGoodsValidate',
            [
                'goods_id'  => $params['goods_id']?? '',
                'goods_name'  => $params['goods_name']?? '',
                'goods_img'    => $params['goods_img']?? '',
                'food_silde' => $params['food_silde']?? '',
                'goods_category_id' => $params['goods_category_id']?? '',
                'goods_shop_id' => $params['goods_shop_id']?? '',
                'goods_is_new'    => $params['goods_is_new']?? '',
                'goods_price' => $params['goods_price']?? '',
                'goods_content' => $params['goods_content']?? '',
                'goods_sell_num' => $params['goods_sell_num']?? '',
                'goods_postage_id'    => $params['goods_postage_id']?? '',
                'goods_status' => $params['goods_status']?? '',
                'goods_weight' => $params['goods_weight']?? '',
                'food_storage' => $params['food_storage']?? '',
                'food_packaging' => $params['food_packaging']?? '',
                'food_varieties' => $params['food_varieties']?? '',
                'food_attr'    => $params['food_attr']?? '',
                'goods_sort' => $params['goods_sort']?? '',
            ],
            'create');
        $result         = FarmGoodsServer::insertFarmGoods($params, $admin_id);
        return json_success($result, '添加成功');
    }

    /**
     * Notes:查询绿色蔬菜详情
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/4
     */
    public function farmGoodsInfo()
    {
        $goods_id = $this->request->get('goods_id');
        $this->validateCheck('life\FarmGoodsValidate', ['goods_id' => $goods_id], 'info');
        $result         = FarmGoodsServer::farmGoodsInfo($goods_id);
        return json_success($result, '成功');
    }

    /**
     * Notes:上架下架
     * @return \support\Response
     * User: YCP
     * Date: 2022/11/7
     */
    public function updateStatus()
    {
        $goods_id = $this->request->get('goods_id');
        $goods_status = $this->request->get('goods_status');
        $this->validateCheck('life\FarmGoodsValidate', ['goods_id' => $goods_id], 'info');
        $result       = FarmGoodsServer::updateStatus($goods_id, $goods_status);
        return json_success($result, '修改成功');
    }


}
