<?php

namespace app\admin\controller\life;

use support\exception\BusinessException;
use support\Request;
use app\admin\validate\life\TravelSchedulingValidate;
use app\controller\Curd;
use app\model\TravelScheduling;
use support\Response;

class TravelSchedulingController extends Curd
{
    public function __construct()
    {
        $this->model = new TravelScheduling();
        $this->validate = true;
        $this->validateClass = new TravelSchedulingValidate();
    }

    /**
     * @Desc 列表
     * @Author Gorden
     * @Date 2024/3/5 14:20
     *
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'line_add_time';
        $where['line_is_del'] = 0;
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * @Desc 过滤插入参数
     * @Author Gorden
     * @Date 2024/3/5 14:14
     *
     * @param Request $request
     * @return array
     * @throws \support\exception\BusinessException
     */
    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        $data['week'] = chinese_week(date('w',strtotime($data['day'])));

        return $data;
    }

    /**
     * @Desc 过滤更新参数
     * @Author Gorden
     * @Date 2024/3/5 14:15
     *
     * @param Request $request
     * @return array
     * @throws BusinessException
     */
    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        $data['week'] = chinese_week(date('w',strtotime($data['day'])));
        $model = $this->model->find($id);
        if (!$model) {
            throw new BusinessException('记录不存在', 2);
        }

        unset($data[$primary_key]);
        return [$id, $data];
    }

    /**
     * @Desc 删除
     * @Author Gorden
     * @Date 2024/3/5 14:18
     *
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doSoftDelete($ids, ['is_del' => 1]);

        return json_success('success');
    }
}