<?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;

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

        "joint_achievement_id" => "int",

        "joint_achievement_guid" => "string",

        "joint_achievement_title" => "string",

        "joint_achievement_cover" => "string",

        "joint_achievement_status" => "int",

        "joint_achievement_sort" => "int",

        "joint_achievement_content" => "string",

        "joint_achievement_create_time" => "datetime",

        "joint_achievement_create_user_guid" => "string",

        "joint_achievement_update_time" => "datetime",

        "joint_achievement_update_user_guid" => "string",

        "joint_achievement_delete_time" => "datetime",

        "joint_achievement_delete_user_guid" => "string",

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



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

    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        Tool::sortInsertProc(
            self::class,
            $model->joint_achievement_sort,
        );
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        Tool::sortEditProc(
            self::class,
            $model->joint_achievement_guid,
            $model->joint_achievement_sort,
        );
        $model->completeUpdateField();
    }

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


    /**
     * 导出Excel
     */
    public static function exportExcel($select)
    {
        $data = [[
            '标题',
            '封面',
            '排序',
        ]];
        foreach ($select as $key => $val) {
            $data[] = [
                $val['joint_achievement_title'],
                Excel::ExportImgFiled($val['joint_achievement_cover']),
                $val['joint_achievement_sort'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('联考成绩.xlsx');
    }
}
