<?php

namespace app\common\model\ExaminationInformation;

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 InfoArticleType extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'info_article_type_delete_time';
    // 设置主键名
    protected $pk = 'info_article_type_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [

        "info_article_type_id" => "int",

        "info_article_type_guid" => "string",

        "info_article_type_name" => "string",

        "info_article_type_sort" => "int",

        "info_article_type_create_time" => "datetime",

        "info_article_type_create_user_guid" => "string",

        "info_article_type_update_time" => "datetime",

        "info_article_type_update_user_guid" => "string",

        "info_article_type_delete_time" => "datetime",

        "info_article_type_delete_user_guid" => "string",

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


    // excel导入/下载模板表头
    public const EXCELFIELD = [
        'info_article_type_name' => '资讯类型名称',
        'info_article_type_sort' => '资讯类型排序',
    ];


    //排序字段
    public $order_field = 'info_article_type_sort';
    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        Tool::sortInsertProc(
            self::class,
            $model->info_article_type_sort,
        );
        $model->completeCreateField();
    }

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

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

    /**
     * 导出Excel
     */
    public static function exportExcel($select)
    {
        $data = [[
            '资讯类型名称',
            '资讯类型排序'
        ]];
        foreach ($select as $key => $val) {
            $data[] = [
                $val['info_article_type_name'],
                $val['info_article_type_sort'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('资讯文章类型.xlsx');
    }

    /**
     * 导入excel
     */
    public static function importExcel($file)
    {
        $msg = [];

        Db::startTrans();
        try {
            $excel = new Excel($file);
            $data = $excel->parseExcel(
                Tool::getExcelRule(self::EXCELFIELD),
                [
                    'titleLine' => [1]
                ]
            );
            if (!$data)  throwErrorMsg('excel无数据', 1);
            $msg = [];
            foreach ($data as $line => $value) {
                try {
                    $model = self::importExcelInit($value);
                    $msg[] = "{$line} <span style='color:#27af49'>【{$value->info_article_type_name}】新增成功!</span><br>";
                } catch (\Throwable $th) {
                    $msg[] = "{$line} <span style='color:red'>{$th->getMessage()}</span><br>";
                }
            }
            Db::commit();
            return implode(', ', $msg);
        } catch (\Throwable $th) {
            Db::rollback();
            throw $th;
        }
    }

    /**
     * 导入excel初始化
     */
    public static function importExcelInit($value)
    {
        $info_article_type_name = $value['info_article_type_name'];
        $info_article_type_sort = $value['info_article_type_sort'];
        return self::create([
            'info_article_type_name' => $info_article_type_name,
            'info_article_type_sort' => $info_article_type_sort,
        ]);
    }
}
