<?php

namespace app\admin\controller\life;

use app\admin\validate\life\FarmLandValidate;
use app\controller\Curd;
use app\model\FarmLand as FarmLandModel;
use support\Request;
use support\Response;

class FarmLandController extends Curd
{
    public function __construct()
    {
        $this->model = new FarmLandModel();
        $this->validate = true;
        $this->validateClass = new FarmLandValidate();
    }

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

    /**
     * @Desc 查询后数据处理
     * @Author Gorden
     * @Date 2024/3/22 10:56
     *
     * @param $items
     * @return mixed
     */
    public function afterQuery($items)
    {
        foreach ($items as &$item){
            $item->land_long = rtrim($item->land_long,'0');
            $item->land_lat = rtrim($item->land_lat,'0');
        }

        return $items;
    }

    /**
     * @Desc 详情数据处理
     * @Author Gorden
     * @Date 2024/3/22 10:48
     *
     * @param $data
     * @return mixed
     */
    public function afterInfoQuery($data)
    {
        $data['land_long'] = rtrim($data['land_long'],'0');
        $data['land_lat'] = rtrim($data['land_lat'],'0');

        return $data;
    }

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

        return json_success('success');
    }
}