// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\model\order; use app\common\model\order\RiceCardOrder as RiceCardOrderModel; use app\api\service\User as UserService; use think\facade\Db; use app\api\model\Receipt as ReceiptModel; use app\common\model\card\RiceCard; /** * 米卡订单模型 * Class Goods * @package app\api\model */ class RiceCardOrder extends RiceCardOrderModel { /** * 追加字段 * @var array */ protected $append = [ 'state_text', // 订单状态 'djs_time' // 订单状态 ]; /** * 待支付订单倒计时时间戳 * @param $value * @param $data * @return int * @author: zjwhust * @Time: 2021/10/8 17:06 */ public function getDjsTimeAttr($value, $data){ if($data['pay_status']==0){ //待支付 $now = time(); $djs_time = 60 *30; if($now - strtotime($data['create_time'])<$djs_time){ return strtotime($data['create_time']) + $djs_time - $now; } } return 0; } /** * 获取用户米卡订单列表 * @return \think\Paginator * @throws \app\common\exception\BaseException * @throws \think\db\exception\DbException */ public function getList(){ // 检索查询条件 $filter = ['is_del'=>0]; // // 售后单状态 // $state > -1 && $filter[] = ['status', '=', $state]; // 当前用户ID $userId = UserService::getCurrentLoginUserId(); // 查询列表记录 return $this->with([]) ->where($filter) ->where('user_id', '=', $userId) ->order(['create_time' => 'desc']) ->paginate(15); } /** * 用户删除订单 * @param $data * @return false|int */ public function del(){ $user = UserService::getCurrentLoginUser(true); if($this['user_id']!=$user->user_id){ $this->error = '当前订单不合法,不允许该操作'; return false; } return $this->transaction(function () use ($user) { $this->save([ 'is_del' => 1, ]); return true; }); $this->error = '当前售后单不合法,不允许该操作'; return false; } //创建订单 public function createOrderEvent($order){ return $this->transaction(function () use ($order) { $this->save($order); $order_id = $this->id; $card_id = $order['rice_card_id']; $rcModel = RiceCard::where('id',$card_id)->find(); $stock = $rcModel->stock - $order['buy_num']; $rcModel->onUpdate($card_id,['stock'=>$stock]); return $order_id; }); $this->error = '添加失败'; return false; } /** * 用户取消订单 * @param $data * @return false|int */ public function cancel($cancel_reason){ $user = UserService::getCurrentLoginUser(true); if($this['user_id']!=$user->user_id){ $this->error = '当前订单不合法,不允许该操作'; return false; } $type = false; if($this['pay_status'] == 1){//已支付 $this->error = '当前订单已经支付,不允许该操作'; return false; } if($this['order_status'] == 30){//已完成 $this->error = '当前订单已经完成,不允许该操作'; return false; } if($this['order_status'] == 40){//已完成 $this->error = '当前订单交易关闭,不允许该操作'; return false; } return $this->transaction(function () use ($cancel_reason) { $this->save([ 'order_status' =>40, 'cancel_reason'=>$cancel_reason, 'cancel_time'=>Date("Y-m-d H:i:s",time()) ]); $card_id = $this['rice_card_id']; $rcModel = RiceCard::where('id',$card_id)->find(); $stock = $rcModel->stock +$this['buy_num']; $rcModel->onUpdate($card_id,['stock'=>$stock]); return true; }); $this->error = '当前订单不合法,不允许该操作'; return false; } /** * 订单状态文字描述 * @param $value * @param $data * @return string */ public function getStateTextAttr($value, $data){ $value = '-'; // 已完成 if($data['order_status'] == 30) { $value = '已完成'; } // 已取消 if($data['order_status'] == 40) { $value = '交易关闭'; } if($data['order_status']==10&&$data['pay_status']==0){ $value = '待支付'; } return $value; } /** * 获取当前用户的米卡订单详情 * @param int $orderRefundId 售后单ID * @param bool $isWith 是否关联 * @return static|null * @throws BaseException */ public static function getDetail(int $orderId){ // 关联查询 $with = []; // 获取记录 $user_id = UserService::getCurrentLoginUserId(); $detail = static::detail([ 'user_id' => $user_id, 'id' => $orderId ], $with); // $detail = static::where("order_refund_id",$orderRefundId)->find(); if (empty($detail)) throwError('未找到该米卡订单'); return $detail; } /** * 获取订单详情(待付款状态) * @param $orderNo * @return array|null|static */ public static function getPayDetail(string $orderNo) { return self::detail(['order_no' => $orderNo, 'pay_status' => 0]); } }