<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $order_product_id 主键id
 * @property int $order_inquiry_id 订单-问诊id
 * @property int $order_prescription_id 订单-处方id
 * @property int $doctor_id 医生id
 * @property int $patient_id 患者id
 * @property int $family_id 家庭成员id（就诊用户）
 * @property string $order_product_no 订单编号
 * @property string $escrow_trade_no 第三方支付流水号
 * @property int $order_product_status 订单状态（1:待支付 2:待发货 3:已发货 4:已签收 5:已取消）
 * @property int $pay_channel 支付渠道（1:小程序支付 2:微信扫码支付）
 * @property int $pay_status 支付状态（1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款）
 * @property int $is_delete 删除状态（0:否 1:是）
 * @property int $cancel_reason 订单取消原因（1:主动取消 2:复核失败/库存不足 3:支付超时 4:客服取消）
 * @property string $amount_total 订单金额
 * @property string $payment_amount_total 实际付款金额
 * @property string $logistics_fee 运费金额
 * @property string $logistics_no 物流编号
 * @property string $delivery_time 发货时间
 * @property string $pay_time 支付时间
 * @property string $remarks 订单备注
 * @property int $refund_status 商品订单退款状态（0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭  6:退款异常）
 * @property string $cancel_time 订单取消时间
 * @property string $cancel_remarks 订单取消备注（自动添加）
 * @property int $province_id 省份id
 * @property string $province 省份
 * @property int $city_id 城市id
 * @property string $city 城市
 * @property int $county_id 区县id
 * @property string $county 区县
 * @property string $address 详细地址
 * @property string $address_mask 详细地址（掩码）
 * @property string $consignee_name 收货人姓名
 * @property string $consignee_name_mask 收货人姓名（掩码）
 * @property string $consignee_tel 收货人电话
 * @property string $consignee_tel_mask 收货人电话（掩码）
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property-read OrderPrescription $OrderPrescription 
 * @property-read \Hyperf\Database\Model\Collection|OrderPrescriptionIcd[] $OrderPrescriptionIcd 
 * @property-read \Hyperf\Database\Model\Collection|OrderProductItem[] $OrderProductItem 
 * @property-read PatientFamily $PatientFamily 
 */
class OrderProduct extends Model
{
    use Snowflake;

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

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['order_product_id', 'order_inquiry_id', 'order_prescription_id', 'doctor_id', 'patient_id', 'family_id', 'order_product_no', 'escrow_trade_no', 'order_product_status', 'pay_channel', 'pay_status', 'is_delete', 'cancel_reason', 'amount_total', 'payment_amount_total', 'logistics_fee', 'logistics_no', 'delivery_time', 'pay_time', 'remarks', 'refund_status', 'cancel_time', 'cancel_remarks', 'province_id', 'province', 'city_id', 'city', 'county_id', 'county', 'address', 'address_mask', 'consignee_name', 'consignee_name_mask', 'consignee_tel', 'consignee_tel_mask', 'created_at', 'updated_at'];

    protected string $primaryKey = "order_product_id";

    /**
     * 关联订单商品item表
     */
    public function OrderProductItem(): HasMany
    {
        return $this->HasMany(OrderProductItem::class, 'order_product_id','order_product_id');
    }

    /**
     * 关联订单-处方表
     */
    public function OrderPrescription(): HasOne
    {
        return $this->hasOne(OrderPrescription::class, 'order_prescription_id','order_prescription_id');
    }

    /**
     * 关联患者家庭成员信息表-基本信息
     */
    public function PatientFamily(): HasOne
    {
        return $this->hasOne(PatientFamily::class, 'family_id','family_id');
    }

    /**
     * 关联处方关联疾病表
     */
    public function OrderPrescriptionIcd(): HasMany
    {
        return $this->HasMany(OrderPrescriptionIcd::class, 'order_prescription_id','order_prescription_id');
    }

    /**
     * 获取信息-单条
     * @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
     * @return int
     */
    public static function getCount(array $params): int
    {
        return self::where($params)->count();
    }

    /**
     * 获取药品订单分页
     * @param array $params
     * @param array $fields
     * @param int|null $page
     * @param int|null $per_page
     * @return array
     */
    public static function getPatientOrderProductPage(array $params,array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $result = self::with([
            "OrderProductItem:product_item_id,order_product_id,product_id,product_name,amount,manufacturer,product_cover_img,product_spec",
        ])
            ->where($params)
            ->paginate($per_page, $fields, "page", $page);

        $data = array();
        $data['current_page'] = $result->currentPage();// 当前页码
        $data['total'] = $result->total();// 数据总数
        $data['data'] = $result->items();// 数据
        $data['per_page'] = $result->perPage();// 每页个数
        $data['last_page'] = $result->lastPage();// 最后一页

        return $data;
    }

    /**
     * 修改
     * @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|OrderProduct
     */
    public static function addOrderProduct(array $data): \Hyperf\Database\Model\Model|OrderProduct
    {
        return self::create($data);
    }


    /**
     * 患者用药记录查询
     * @param array $params
     * @param array $fields
     * @param int|null $page
     * @param int|null $per_page
     * @return array
     */
    public static function getProductRecordPage(array $params,array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $result = self::with([
            "OrderProductItem:product_item_id,order_product_id,product_id,product_name,amount,manufacturer,product_cover_img,product_spec",
            "OrderPrescriptionIcd:prescription_icd_id,order_prescription_id,icd_id,icd_name",
            "PatientFamily:family_id,card_name_mask,sex,age",
        ])
            ->where($params)
            ->paginate($per_page, $fields, "page", $page);

        $data = array();
        $data['current_page'] = $result->currentPage();// 当前页码
        $data['total'] = $result->total();// 数据总数
        $data['data'] = $result->items();// 数据
        $data['per_page'] = $result->perPage();// 每页个数
        $data['last_page'] = $result->lastPage();// 最后一页

        return $data;
    }
}
