<?php
namespace app\api\model\recharge;
use app\common\model\recharge\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\api\model\recharge\Plan as PlanModel;
use app\api\model\recharge\OrderPlan as OrderPlanModel;
use app\common\service\Order as OrderService;
use app\common\enum\recharge\order\PayStatus as PayStatusEnum;
use app\common\enum\recharge\order\RechargeType as RechargeTypeEnum;
use app\common\exception\BaseException;
class Order extends OrderModel
{
protected $hidden = [
'wxapp_id',
];
public function getList($userId)
{
return $this->where('user_id', '=', $userId)
->where('pay_status', '=', PayStatusEnum::SUCCESS)
->order(['create_time' => 'desc'])
->paginate(15, false, [
'query' => request()->request()
]);
}
public static function getPayDetail($orderNo)
{
return self::detail(['order_no' => $orderNo, 'pay_status' => PayStatusEnum::PENDING]);
}
public function createOrder($user, $planId = null, $customMoney = 0.00)
{
$rechargeType = $planId > 0 ? RechargeTypeEnum::PLAN : RechargeTypeEnum::CUSTOM;
if (!$this->validateForm($rechargeType, $planId, $customMoney)) {
$this->error = $this->error ?: '数据验证错误';
return false;
}
$data = $this->getOrderData($user, $rechargeType, $planId, $customMoney);
return $this->saveOrder($data);
}
private function saveOrder($data)
{
$this->save($data['order']);
if (!empty($data['plan'])) {
$PlanModel = new OrderPlanModel;
return $PlanModel->add($this['order_id'], $data['plan']);
}
return true;
}
private function getOrderData($user, $rechargeType, $planId, $customMoney)
{
$data = [
'order' => [
'user_id' => $user['user_id'],
'order_no' => 'RC' . OrderService::createOrderNo(),
'recharge_type' => $rechargeType,
'gift_money' => 0.00,
'wxapp_id' => self::$wxapp_id,
],
'plan' => [] ];
if ($rechargeType == RechargeTypeEnum::CUSTOM) {
$this->createDataByCustom($data, $customMoney);
}
if ($rechargeType == RechargeTypeEnum::PLAN) {
$this->createDataByPlan($data, $planId);
}
$data['order']['actual_money'] = bcadd($data['order']['pay_price'], $data['order']['gift_money'], 2);
return $data;
}
private function createDataByPlan(&$order, $planId)
{
$planInfo = PlanModel::detail($planId);
if (empty($planInfo)) {
throw new BaseException(['msg' => '充值套餐不存在']);
}
$order['plan'] = $planInfo;
$order['order']['plan_id'] = $planInfo['plan_id'];
$order['order']['gift_money'] = $planInfo['gift_money'];
$order['order']['pay_price'] = $planInfo['money'];
return true;
}
private function createDataByCustom(&$order, $customMoney)
{
$order['order']['pay_price'] = $customMoney;
$setting = SettingModel::getItem('recharge');
if ($setting['is_custom'] == false) {
return true;
}
$PlanModel = new PlanModel;
$matchPlanInfo = $PlanModel->getMatchPlan($customMoney);
if (!empty($matchPlanInfo)) {
$order['plan'] = $matchPlanInfo;
$order['order']['plan_id'] = $matchPlanInfo['plan_id'];
$order['order']['gift_money'] = $matchPlanInfo['gift_money'];
}
return true;
}
private function validateForm($rechargeType, $planId, $customMoney)
{
if (empty($planId) && $customMoney <= 0) {
$this->error = '请选择充值套餐或输入充值金额';
return false;
}
if ($rechargeType == RechargeTypeEnum::CUSTOM && $customMoney <= 0) {
$this->error = '请选择充值套餐或输入充值金额';
return false;
}
return true;
}
}