<?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;
use app\common\model\Dictionary\Dictionary as ModelDictionary;

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

        "enrol_aq_id" => "int",

        "enrol_aq_guid" => "string",

        "enrol_aq_question" => "string",

        "enrol_aq_answer" => "string",

        "enrol_aq_sort" => "int",

        "enrol_aq_status" => "int",

        "enrol_aq_answer_status" => "int",

        "enrol_aq_create_time" => "datetime",

        "enrol_aq_create_user_guid" => "string",

        "enrol_aq_update_time" => "datetime",

        "enrol_aq_update_user_guid" => "string",

        "enrol_aq_delete_time" => "datetime",

        "enrol_aq_delete_user_guid" => "string",

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


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

    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        BaseModel::setUserGuid(false);
        Tool::initModelFieldValue($model, [
            'enrol_aq_sort' => 1,
            'enrol_aq_status' => 1,
            'enrol_aq_answer_status' => 1,
        ]);
        Tool::sortInsertProc(
            self::class,
            $model->enrol_aq_sort,
        );
        $model->completeCreateField();
    }

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

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

    /**
     * 导出Excel
     */
    public static function exportExcel($select)
    {
        $data = [[
            '问题',
            '答案',
            '排序',
            '是否展示',
            '回答状态'
        ]];
        foreach ($select as $key => $val) {
            // 字典取值
            $show_status = ModelDictionary::getDictionaryData('show_status');
            $answer_status = ModelDictionary::getDictionaryData('answer_status');
            $val['enrol_aq_status'] =  ModelDictionary::getDataDictionaryName($show_status, $val['enrol_aq_status']);
            $val['enrol_aq_answer_status'] =  ModelDictionary::getDataDictionaryName($answer_status, $val['enrol_aq_answer_status']);


            $data[] = [
                $val['enrol_aq_question'],
                $val['enrol_aq_answer'],
                $val['enrol_aq_sort'],
                $val['enrol_aq_status'],
                $val['enrol_aq_answer_status'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('招生问答.xlsx');
    }

}
