<?php
namespace app\model;

use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;

/**
 * 活动商品订单
 *
 * Class OrderActivity
 * @package app\model
 */
class OrderActivity extends Base
{
    /**
     * 设为已付款
     *
     * @param  string  $orderCoding
     * @return bool
     */
    public static function setPaid(string $orderCoding): bool
    {
        return self::where('coding', $orderCoding)->save([
            'is_paid' => self::COMMON_ON,
            'paid_at' => date('Y-m-d H:i:s')
        ]);
    }

    /**
     * 订单相关信息取消(软删除)
     *
     * @param  string  $orderCoding
     * @return bool
     */
    public static function cancel(string $orderCoding): bool
    {
        return self::where('coding', $orderCoding)->save([
            'status'  => Order::STATUS_CANCEL,
            'deleted_at' => date('Y-m-d H:i:s')
        ]);
    }

    /**
     * 历史记录
     *
     * @param  array  $where
     * @param  array  $field
     * @return OrderActivity[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function history(array $where, array $field = [])
    {
        return self::where($where)->field($field)->select();
    }

    /**
     * 订单信息
     *
     * @return HasOne
     */
    public function orderInfo(): HasOne
    {
        return $this->hasOne(Order::class, 'coding', 'coding');
    }

    /**
     * 用户信息
     *
     * @return HasOne
     */
    public function account(): HasOne
    {
        return $this->hasOne(Account::class, 'id', 'account_id')->withField(['id', 'nickname', 'real_name']);
    }
}