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

        "leave_message_id" => "int",

        "leave_message_guid" => "string",

        "leave_message_content" => "string",

        "leave_message_status" => "int",

        "leave_message_user_name" => "string",

        "leave_message_user_phone" => "int",

        "leave_message_create_time" => "datetime",

        "leave_message_create_user_guid" => "string",

        "leave_message_update_time" => "datetime",

        "leave_message_update_user_guid" => "string",

        "leave_message_delete_time" => "datetime",

        "leave_message_delete_user_guid" => "string",

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




    /**
     * 新增前
     */
    public static function onBeforeInsert(self $model): void
    {
        BaseModel::setUserGuid(false);
        Tool::initModelFieldValue($model, ['signup_status' => 2]);
        $model->completeCreateField();
    }

    /**
     * 更新前
     */
    public static function onBeforeUpdate(self $model): void
    {
        $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['leave_message_status'] =  ModelDictionary::getDataDictionaryName($processing_state, $val['leave_message_status']);

            $data[] = [
                $val['leave_message_user_name'],
                $val['leave_message_user_phone'],
                $val['leave_message_content'],
                $val['leave_message_status'],
            ];
        }
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('用户留言.xlsx');
    }
}
