<?php

namespace app\common\model\Test;

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 Test extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'test_delete_time';
    // 设置主键名
    protected $pk = 'test_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [
	 'test_id' => 'int' , 
 'test_guid' => 'string' , 
 'test_name' => 'string' , 
 'test_show_status' => 'string' , 
 'test_score' => '' , 
 'test_sort' => 'int' , 
 'test_create_time' => 'datetime' , 
 'test_create_user_guid' => 'string' , 
 'test_update_time' => 'datetime' , 
 'test_update_user_guid' => 'string' , 
 'test_delete_time' => 'datetime' , 
 'test_delete_user_guid' => 'string' , 

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

    
        // excel导入/下载模板表头
        public const EXCELFIELD = [
'test_name' => '名称',
'test_show_status' => '首页是否展示',
'test_score' => '评分',
'test_sort' => '排序',
];
        


    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        // self::checkRepeatData($model);
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        // self::checkRepeatData($model);
        $model->completeUpdateField();
    }

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

    
        /**
          * 导出Excel
          * 
          * @param array $select 导出的数据
          */
        public static function exportExcel(array $select): void
        {
            $data = [[
'名称',
'首页是否展示',
'评分',
'排序'
]];
            foreach ($select as $key => $val) {
                $data[] = [
$val['test_name'],
$val['test_show_status'],
$val['test_score'],
$val['test_sort'],
];
            }
            $excel = (new Excel())->exporTsheet($data);
            $excel->save('测试.xlsx');
        }

    
        /**
          * 导入excel
          * 
          * @param \app\common\arw\adjfut\src\UploadFile $file excel
        */
        public static function importExcel(\app\common\arw\adjfut\src\UploadFile $file): string
        {
            $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'>新增成功!</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初始化
          * 
          * @param array $value excel每行数据
        */
        public static function importExcelInit(array $value):void
        {
            $test_name = $value['test_name'];$test_show_status = $value['test_show_status'];$test_score = $value['test_score'];$test_sort = $value['test_sort'];

            self::create(
                ['test_name' => $test_name,
'test_show_status' => $test_show_status,
'test_score' => $test_score,
'test_sort' => $test_sort,
],
                ['test_name','test_show_status','test_score','test_sort',]
            );
        }
     


}
