<?php

namespace app\common\model\Home;

use app\common\arw\adjfut\src\Validate;
use app\common\model\Dictionary\Dictionary as ModelDictionary;
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 HomeWorks extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'home_works_delete_time';
    // 设置主键名
    protected $pk = 'home_works_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [
        'home_works_id' => 'int',
        'home_works_guid' => 'string',
        'home_works_author_type' => 'string',
        'home_works_img' => 'string',
        'home_works_sort' => 'int',
        'home_works_intro' => 'string',
        'home_works_create_time' => 'datetime',
        'home_works_create_user_guid' => 'string',
        'home_works_update_time' => 'datetime',
        'home_works_update_user_guid' => 'string',
        'home_works_delete_time' => 'datetime',
        'home_works_delete_user_guid' => 'string',

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



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

    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        Tool::sortInsertProc(
            self::class,
            $model->home_works_sort,
            ['home_works_author_type' => $model->home_works_author_type]
        );
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        Tool::sortEditProc(
            self::class,
            $model->home_works_guid,
            $model->home_works_sort,
            ['home_works_author_type' => $model->home_works_author_type]
        );
        $model->completeUpdateField();
    }

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

    /**
     * 导出Excel
     * 
     * @param array $select 导出的数据
     */
    public static function exportExcel(array $select): void
    {
        $data = [[
            '作者类型',
            '图片',
            '排序',
            '介绍'
        ]];
        foreach ($select as $key => $val) {
            $data[] = [
                Excel::ExportImgFiled($val['home_works_author_type']),
                $val['home_works_img'],
                $val['home_works_sort'],
                $val['home_works_intro'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('首页作品.xlsx');
    }

    /**
     * 作品作者类型字典获取器
     */
    public function getHomeWorksAuthorTypeNameAttr($value,$data)
    {
        $home_works_author_type = $data['home_works_author_type'];
        $works_author_type = ModelDictionary::getDictionaryData('works_author_type'); 
        $home_works_author_type =  ModelDictionary::getDataDictionaryName($works_author_type, $home_works_author_type);
        return $home_works_author_type;
    }
}
