You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/model/user/RetailOrder.php

133 lines
4.1 KiB

5 months ago
<?php
namespace app\api\model\user;
use app\api\service\User as UserService;
use app\common\enum\order\PayStatus;
use app\common\model\RetailOrder as BaseIdentityOrder;
use app\common\service\Order as OrderService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;
use function getPlatform;
class RetailOrder extends BaseIdentityOrder
{
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'order_type',
'create_time',
'update_time',
'store_id',
];
/**
* @notes:创建订单
* @param array $identityInfo
* @param $method
* @return bool
* @throws BaseException
* @author: wanghousheng
*/
public function createOrder(array $identityInfo, $method): bool
5 months ago
{
$price = [];
$identityInfo['order_price'] = $identityInfo['retail_current_price'] * $identityInfo['year'];
$preferentialPrice = [];
if($identityInfo['year']>1){
$price= $identityInfo['retail_current_price'] * $identityInfo['year']-$identityInfo['retail_discounts'];//订单支付价格
$preferentialPrice = $identityInfo['retail_discounts'];
}else {
$price = $identityInfo['retail_current_price'] * $identityInfo['year'];
$preferentialPrice = 0;
}
5 months ago
$data = [
'user_id' => UserService::getCurrentLoginUserId(),//用户id
'order_no' => OrderService::createOrderNo(),//订单号
'retail_price_id' => $identityInfo['retail_price_id'],//会员id
'order_type' => $identityInfo['retail_type'],//订单类型
5 months ago
'order_price' => $identityInfo['order_price'],//订单支付价格
5 months ago
'pay_price' => $price,
5 months ago
'preferential_price' => $preferentialPrice,//优惠价格
5 months ago
'year' => $identityInfo['year'],
'platform' => getPlatform(),
'pay_method' => $method,
'store_id' => self::$storeId,
5 months ago
5 months ago
];
return $this->save($data);
}
/**
* 获取订单详情 (待付款状态)
* @param string $orderNo 订单号
* @return array|null|static
*/
public static function getPayDetail(string $orderNo)
{
return self::detail(['order_no' => $orderNo]);
}
/**
* @notes:编辑
* @param $data
* @return bool
* @author: wanghousheng
*/
public function edit($data): bool
{
return $this->save($data) !== false;
}
public function identity(): HasOne
{
return $this->hasOne(Identity::class, 'identity_id', 'identity_id')
->bind(['identity_name' => 'name']);
}
/**
* @notes:开卡记录
* @param array $where
* @return array
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function cardList(array $where = []): array
{
$userId = UserService::getCurrentLoginUserId();
$params['user_id'] = $userId;
$params['pay_status'] = PayStatus::SUCCESS;
$where = array_merge($where, $params);
$list = $this->where($where)
->with(['identity'])
->order('pay_time', 'desc')
->limit(20)
->select();
$data = [];
if (!empty($list)) {
foreach ($list as $value) {
$end_time = date("Y-m-d", strtotime("+{$value['month']} months", $value['pay_time']));
$is_valid = false;
if (strtotime(date("Y-m-d")) < strtotime($end_time)) {
$is_valid = true;
}
$data[] = [
'start_time' => date("Y-m-d", $value['pay_time']),
'end_time' => $end_time,
'name' => $value['identity_name'],
'month' => $value['month'],
'is_valid' => $is_valid
];
}
}
return $data;
}
}