<?php

namespace app\common\model\ContactUs;

use app\common\arw\adjfut\src\Validate;
use app\BaseModel;
use app\common\model\Dictionary\Dictionary as ModelDictionary;
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 JoinApply extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'join_apply_delete_time';
    // 设置主键名
    protected $pk = 'join_apply_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [

        "join_apply_id" => "int",

        "join_apply_guid" => "string",

        "join_apply_user_name" => "string",

        "join_apply_user_phone" => "int",

        "join_apply_user_position" => "string",

        "join_apply_user_area" => "string",

        "join_apply_user_org" => "string",

        "join_apply_status" => "int",

        "join_apply_create_time" => "datetime",

        "join_apply_create_user_guid" => "string",

        "join_apply_update_time" => "datetime",

        "join_apply_update_user_guid" => "string",

        "join_apply_delete_time" => "datetime",

        "join_apply_delete_user_guid" => "string",

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




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

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

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

    /**
     * 导出Excel
     */
    public static function exportExcel($select)
    {
        $data = [[
            '加盟申请用户名字',
            '加盟申请用户电话',
            '加盟申请用户职位',
            '加盟申请用户所在地区',
            '加盟申请用户所属机构名称',
            '加盟申请受理状态'
        ]];
        foreach ($select as $key => $val) {
            // 字典取值
            $processing_state = ModelDictionary::getDictionaryData('processing_state');
            $val['join_apply_status'] =  ModelDictionary::getDataDictionaryName($processing_state, $val['join_apply_status']);

            $data[] = [
                $val['join_apply_user_name'],
                $val['join_apply_user_phone'],
                $val['join_apply_user_position'],
                $val['join_apply_user_area'],
                $val['join_apply_user_org'],
                $val['join_apply_status'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('加盟申请.xlsx');
    }
}
