<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $evaluation_id 主键id
 * @property int $doctor_id 医生id
 * @property int $patient_id 患者id
 * @property int $order_inquiry_id 订单-问诊id
 * @property string $name_mask 患者姓名（掩码）
 * @property string $reply_quality 回复质量（百分制）
 * @property string $service_attitude 服务态度（百分制）
 * @property string $reply_progress 回复速度（百分制）
 * @property string $avg_score 平均得分（百分制，回复质量占4、服务态度占3、回复速度占3，计算公式：每个得分 * 占比 相加）
 * @property int $type 类型（1:默认评价 2:主动评价）
 * @property string $content 评价内容
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 */
class OrderEvaluation extends Model
{
    use Snowflake;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'order_evaluation';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['evaluation_id', 'doctor_id', 'patient_id', 'order_inquiry_id', 'name_mask', 'reply_quality', 'service_attitude', 'reply_progress', 'avg_score', 'type', 'content', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['evaluation_id' => 'integer', 'doctor_id' => 'integer', 'patient_id' => 'integer', 'order_inquiry_id' => 'integer', 'type' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected string $primaryKey = "evaluation_id";

    /**
     * 获取评价列表-分页
     * @param array $params 条件
     * @param array $fields 字段
     * @param int|null $page 页码
     * @param int|null $per_page 每页个数
     * @return array
     */
    public static function getPage(array $params, array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $raw = self::where($params)->paginate($per_page, $fields, "page", $page);
        $data = array();
        $data['current_page'] = $raw->currentPage();// 当前页码
        $data['total'] = $raw->total();//数据总数
        $data['data'] = $raw->items();//数据
        $data['per_page'] = $raw->perPage();//每页个数
        $data['last_page'] = $raw->lastPage();//最后一页

        return $data;
    }

    /**
     * 获取评分区间列表-分页
     * @param array $params 条件
     * @param array $avg_score_params 评分区间 [0.100]
     * @param array $fields 字段
     * @param int|null $page 页码
     * @param int|null $per_page 每页个数
     * @return array
     */
    public static function getScorePage(array $params, array $avg_score_params,array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $raw = self::where($params)
            ->whereBetween('avg_score',$avg_score_params)
            ->paginate($per_page, $fields, "page", $page);
        $data = array();
        $data['current_page'] = $raw->currentPage();// 当前页码
        $data['total'] = $raw->total();//数据总数
        $data['data'] = $raw->items();//数据
        $data['per_page'] = $raw->perPage();//每页个数
        $data['last_page'] = $raw->lastPage();//最后一页

        return $data;
    }

    /**
     * 获取评分区间数量
     * @param array $params 条件
     * @param array $avg_score_params 评分区间 [0.100]
     */
    public static function getScoreCount(array $params, array $avg_score_params): int
    {
        return self::where($params)
            ->whereBetween('avg_score',$avg_score_params)
            ->count();
    }

    /**
     * 获取信息-单条
     * @param array $params
     * @param array $fields
     * @return object|null
     */
    public static function getOne(array $params, array $fields = ['*']): object|null
    {
        return self::where($params)->first($fields);
    }

    /**
     * 获取数据-多
     * @param array $params
     * @param array $fields
     * @return Collection|array
     */
    public static function getList(array $params = [], array $fields = ['*']): Collection|array
    {
        return self::where($params)->get($fields);
    }

    /**
     * 获取是否存在
     * @param array $params
     * @return bool
     */
    public static function getExists(array $params): bool
    {
        return self::where($params)->exists();
    }

    /**
     * 修改
     * @param array $params
     * @param array $data
     * @return int
     */
    public static function edit(array $params = [], array $data = []): int
    {
        return self::where($params)->update($data);
    }

    /**
     * 新增
     * @param array $data
     * @return \Hyperf\Database\Model\Model|OrderEvaluation
     */
    public static function addOrderEvaluation(array $data): \Hyperf\Database\Model\Model|OrderEvaluation
    {
        return self::create($data);
    }
}
