<?php

namespace app\common\model\Consult;

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;
use app\common\model\Product\Product as ModelProduct;

class Consult extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'consult_delete_time';
    // 设置主键名
    protected $pk = 'consult_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [

        "consult_id" => "int",

        "consult_guid" => "string",

        "consult_contact" => "string",

        "product_guid" => "string",

        "consult_user_name" => "string",

        "consult_content" => "string",

        "consult_status" => "int",

        "consult_create_time" => "datetime",

        "consult_create_user_guid" => "string",

        "consult_update_time" => "datetime",

        "consult_update_user_guid" => "string",

        "consult_delete_time" => "datetime",

        "consult_delete_user_guid" => "string",

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




    /**
     * 新增前
     */
    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();
    }

    /**
     * 获取器(咨询产品集合)
     */
    public function getConsultProductsAttr($value, $data): array
    {
        return  ModelProduct::field(['product_name', 'product_img'])
            ->where([['product_guid', 'in', explode(',', $data['product_guid'])]])
            ->select()
            ->toArray();
    }
}
