<?php

namespace app\common\model\Enrol;

use app\common\arw\adjfut\src\Validate;
use app\BaseModel;
use think\model\concern\SoftDelete;
use app\common\arw\adjfut\src\Excel;
use app\Request;
use app\common\exception\Tool;
use think\facade\Db;

class EnterSchoolProcess extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'enter_school_process_delete_time';
    // 设置主键名
    protected $pk = 'enter_school_process_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [

        "enter_school_process_id" => "int",

        "enter_school_process_guid" => "string",

        "enter_school_process_title" => "string",

        "enter_school_process_intro" => "string",

        "enter_school_process_sort" => "int",

        "enter_school_process_create_time" => "datetime",

        "enter_school_process_create_user_guid" => "string",

        "enter_school_process_update_time" => "datetime",

        "enter_school_process_update_user_guid" => "string",

        "enter_school_process_delete_time" => "datetime",

        "enter_school_process_delete_user_guid" => "string",

    ];
    // 设置json类型字段
    protected $json = [''];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 创建时间
    protected $createTime = 'enter_school_process_create_time';
    // 修改时间
    protected $updateTime = 'enter_school_process_update_time';

    //排序字段
    public $order_field = 'enter_school_process_sort';

    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        Validate::unique(self::class, $model->enter_school_process_guid, $model->getData(), [
            'enter_school_process_title' => '入学流程标题',
        ]);
        Tool::sortInsertProc(
            self::class,
            $model->enter_school_process_sort,
        );
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        Validate::unique(self::class, $model->enter_school_process_guid, $model->getData(), [
            'enter_school_process_title' => '入学流程标题',
        ]);
        Tool::sortEditProc(
            self::class,
            $model->enter_school_process_guid,
            $model->enter_school_process_sort,
        );
        $model->completeUpdateField();
    }

    /**
     * 删除前
     */
    public static function onBeforeDelete(self $model): void
    {
        Tool::sortDeleteProc(self::class, $model->enter_school_process_guid);
        $model->completeDeleteField();
    }

    /**
     * 导出Excel
     */
    public static function exportExcel($select)
    {
        $data = [[
            '标题',
            '介绍',
        ]];
        foreach ($select as $key => $val) {
            $data[] = [
                $val['enter_school_process_title'],
                $val['enter_school_process_intro'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('入学流程.xlsx');
    }
}
