<?php

namespace app\common\model\HonoraryAchievement;

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;
use app\common\model\HonoraryAchievement\SchoolCategory as ModelSchoolCategory;

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

        "school_category_id" => "int",

        "school_category_guid" => "string",

        "school_category_title" => "string",

        "school_category_name" => "string",

        "school_category_img" => "string",

        "school_category_icon" => "string",

        "school_category_total" => "int",

        "school_category_status" => "int",

        "school_category_sort" => "int",

        "school_category_create_time" => "datetime",

        "school_category_create_user_guid" => "string",

        "school_category_update_time" => "datetime",

        "school_category_update_user_guid" => "string",

        "school_category_delete_time" => "datetime",

        "school_category_delete_user_guid" => "string",

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


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

    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        Validate::unique(self::class, $model->school_category_guid, $model->getData(), [
            'school_category_name' => '录取院校类型名称',
        ]);
        Tool::sortInsertProc(
            self::class,
            $model->school_category_sort,
        );
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        Validate::unique(self::class, $model->school_category_guid, $model->getData(), [
            'school_category_name' => '录取院校类型名称',
        ]);
        Tool::sortEditProc(
            self::class,
            $model->school_category_guid,
            $model->school_category_sort,
        );
        $model->completeUpdateField();
    }

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


    /**
     * 导出Excel
     */
    public static function exportExcel($select)
    {
        $data = [[
            '录取院校分类标题',
            '录取院校分类名称',
            '录取院校分类图片',
        ]];
        foreach ($select as $key => $val) {
            $data[] = [
                $val['school_category_title'],
                $val['school_category_name'],
                Excel::ExportImgFiled($val['school_category_img']),
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('录取院校分类.xlsx');
    }
}
