<?php

namespace app\common\model\AboutUs;

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 DevelopmentHistory extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'development_history_delete_time';
    // 设置主键名
    protected $pk = 'development_history_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [
        "development_history_id" => "int",
        "development_history_guid" => "string",
        "development_history_year" => "int",
        "development_history_title" => "string",
        "development_history_content" => "string",
        "development_history_create_time" => "datetime",
        "development_history_create_user_guid" => "string",
        "development_history_update_time" => "datetime",
        "development_history_update_user_guid" => "string",
        "development_history_delete_time" => "datetime",
        "development_history_delete_user_guid" => "string",
    ];
    // 设置json类型字段
    protected $json = [''];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 创建时间
    protected $createTime = 'development_history_create_time';
    // 修改时间
    protected $updateTime = 'development_history_update_time';


    // excel导入/下载模板表头
    public const EXCELFIELD = [
        'development_history_year' => '年份',
        'development_history_title' => '标题',
        'development_history_content' => '内容',
    ];

    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        Validate::unique(self::class, $model->development_history_guid, $model->getData(), [
            'development_history_year' => '年份',
        ]);
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        Validate::unique(self::class, $model->development_history_guid, $model->getData(), [
            'development_history_year' => '年份',
        ]);
        $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['development_history_year'],
                $val['development_history_title'],
                $val['development_history_content'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('发展历程.xlsx');
    }

    /**
     * 导入excel
     * 
     * @param \app\common\arw\adjfut\src\UploadFile $file excel
     */
    public static function importExcel($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 {
                    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 每行数据
     */
    public static function importExcelInit($value): string
    {
        $development_history_year = $value['development_history_year'];
        $development_history_title = $value['development_history_title'];
        $development_history_content = $value['development_history_content'];
        return self::create([
            'development_history_year' => $development_history_year,
            'development_history_title' => $development_history_title,
            'development_history_content' => $development_history_content,
        ]);
    }
}
