1685 lines
68 KiB
PHP
1685 lines
68 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2020-12-2
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
|
||
namespace app\modules\api\models;
|
||
|
||
use app\models\ActivityUserLog;
|
||
use app\models\auth\RoleUser;
|
||
use app\models\common\PluginService;
|
||
use app\models\MsgCentre;
|
||
use app\models\Option;
|
||
use app\models\PaymentOrder;
|
||
use app\models\PaymentOrderUnion;
|
||
use app\models\StoreEarnings;
|
||
use app\models\StoreUser;
|
||
use function AlibabaCloud\Client\value;
|
||
use app\components\FlashStorage;
|
||
use app\components\SiteHelper;
|
||
use app\components\SysConst;
|
||
use app\models\BallCart;
|
||
use app\models\BallMark;
|
||
use app\models\Coach;
|
||
use app\models\Coupon;
|
||
use app\models\Goods;
|
||
use app\models\GoodsHub;
|
||
use app\models\Order;
|
||
use app\models\OrderDetail;
|
||
use app\models\OrderSale;
|
||
use app\models\Store;
|
||
use app\models\UniqueOrderNo;
|
||
use app\models\User;
|
||
use app\components\auth\AToken;
|
||
use app\components\EncryptHelper;
|
||
use app\models\UserBallArm;
|
||
use app\models\UserCoupon;
|
||
use app\models\UserInformation;
|
||
use app\modules\api\components\ApiHelper;
|
||
use app\modules\api\components\GetDistance;
|
||
use app\modules\api\components\Mqtt;
|
||
use yii\data\Pagination;
|
||
use yii\db\Exception;
|
||
|
||
|
||
class OrderForm extends ApiModel
|
||
{
|
||
|
||
public $limit;//条数
|
||
public $page; //页数
|
||
public $pageCount; // 总页数
|
||
|
||
public $user_type;
|
||
|
||
//球车订单
|
||
public $store_id;
|
||
public $cx_mch_id;
|
||
public $user_id;
|
||
public $plugin_sign;
|
||
public $price; //订单金额
|
||
public $order_id;
|
||
public $ball_number; //球车编号
|
||
public $status;
|
||
public $is_pay;
|
||
|
||
//订单状态 0-完成 1-未完成 2-已支付 3-未支付 4-已取消
|
||
const ORDER_OK = 0;
|
||
const ORDER_NO = 1;
|
||
const ORDER_PAY_OK = 2;
|
||
const ORDER_PAY_NO = 3;
|
||
const ORDER_CANCAL = 4;
|
||
|
||
//支付状态 0=未支付,1=已支付
|
||
const PAY_OK = 1;
|
||
const PAY_NO = 0;
|
||
|
||
public $coupon_id; //优惠券ID
|
||
|
||
|
||
//申请退款
|
||
public $refund_price;
|
||
public $remark;
|
||
public $pic_urls;
|
||
public $type;
|
||
|
||
|
||
//门店管理员审核退款
|
||
public $store_type;
|
||
public $merchant_remark;
|
||
|
||
|
||
public $redis_name = 'api:cxaibc:ball_cart:';
|
||
|
||
public $redis_key_order = 'api:cxaibc:lock_order:';
|
||
|
||
public $visit_type; //1-前台 2-管理端
|
||
|
||
public $pay_type; //支付方式:微信支付=wxpay,微信支付分=wxpay_points
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['limit', 'page', 'order_id'], 'integer'],
|
||
[['page'], 'default', 'value' => 1],
|
||
[['limit'], 'default', 'value' => 20],
|
||
|
||
|
||
[['price'], 'required', 'on' => 'ball_cart'], //价格
|
||
[['plugin_sign'], 'required', 'on' => 'ball_cart'], //商品标识
|
||
[['store_id'], 'required', 'on' => 'ball_cart'], //门店
|
||
[['ball_number'], 'required', 'on' => 'ball_cart'], //球车编号
|
||
[['coupon_id'], 'integer', 'on' => 'ball_cart'], //优惠券ID
|
||
|
||
|
||
[['plugin_sign'], 'required', 'on' => 'return_cart'], //商品标识
|
||
[['order_id'], 'integer', 'on' => 'return_cart'], //商品标识
|
||
[['ball_number'], 'string', 'on' => 'return_cart'], //商品标识
|
||
|
||
|
||
[['plugin_sign'], 'required', 'on' => 'order_sale'], //商品标识
|
||
[['order_id'], 'required', 'on' => 'order_sale'], //订单ID
|
||
[['refund_price'], 'required', 'on' => 'order_sale'], //退款金额
|
||
[['remark'], 'string', 'on' => 'order_sale'], //退款备注
|
||
[['pic_urls'], 'string', 'on' => 'order_sale'], //商品标识
|
||
[['user_id'], 'required', 'on' => 'order_sale'], //用户ID
|
||
[['type'], 'integer', 'on' => 'order_sale'], //退款原因ID
|
||
|
||
[['order_id'], 'required', 'on' => 'order_sale_store'], //退款订单ID
|
||
[['status'], 'required', 'on' => 'order_sale_store'], //状态 2=同意,3=拒绝
|
||
[['store_type'], 'required', 'on' => 'order_sale_store'], //门店人员身份
|
||
[['plugin_sign'], 'required', 'on' => 'order_sale_store'], //商品标识
|
||
[['merchant_remark'], 'string', 'on' => 'order_sale_store'], //商品标识
|
||
|
||
[['plugin_sign'], 'required', 'on' => 'sale_list'], //商品标识
|
||
[['visit_type'], 'required', 'on' => 'sale_list'], //访问端口类型 1-前台 2-管理入口
|
||
// [['order_id'], 'required', 'on' => 'sale_list'], //商品标识
|
||
[['plugin_sign', 'store_id', 'ball_number', 'pay_type'], 'required', 'on' => 'check_deposit'], //校验是否需要缴纳押金
|
||
[['plugin_sign', 'price', 'store_id', 'ball_number', 'pay_type'], 'required', 'on' => 'deposit'], //缴纳押金下单
|
||
];
|
||
}
|
||
|
||
|
||
public function scenarios()
|
||
{
|
||
return [
|
||
'ball_cart' => ['price', 'plugin_sign', 'store_id', 'ball_number', 'coupon_id'],
|
||
'return_cart' => ['plugin_sign', 'order_id', 'ball_number'],
|
||
'order_sale' => ['plugin_sign', 'order_id', 'refund_price', 'remark', 'pic_urls', 'user_id', 'type'],
|
||
'order_sale_store' => ['plugin_sign', 'order_id', 'status', 'store_type', 'merchant_remark'],
|
||
'sale_list' => ['plugin_sign', 'order_id', 'page', 'limit', 'store_type', 'store_id', 'visit_type'],
|
||
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 球车租赁 下单
|
||
*/
|
||
public function createOrder()
|
||
{
|
||
if (empty($this->plugin_sign)) {
|
||
return $this->apiReturnError('标识错误');
|
||
}
|
||
|
||
//查询未完成退款的押金订单是否存在
|
||
// $is_order = Order::find()->where(['user_id' => $this->user_id,'pay_type' => SysConst::$cxPayTypeWxpay,'plugin_sign' => SysConst::$cxPluginSceneDeposit,'is_pay' => 1,'is_delete' => 0,'status' => 1])->exists();
|
||
// if(!$is_order){
|
||
// return $this->apiReturnError('请先缴纳押金');
|
||
// }
|
||
|
||
//加锁
|
||
$redis_key = $this->redis_key_order . $this->user_id . $this->ball_number;
|
||
$this->lock($redis_key);
|
||
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
// 初始化所有订单,把此用户未支付的订单全变更为取消,并返还优惠券
|
||
$select_data = Order::find()->andWhere([
|
||
'user_id' => $this->user_id,
|
||
'is_pay' => 0,
|
||
'cancel_status' => 0,
|
||
'is_delete' => 0
|
||
])->andWhere([
|
||
'in','status',[4,5]
|
||
])->select('*')->all();
|
||
if(!empty($select_data)){
|
||
foreach ($select_data as $key=>$val){
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
$val->status = 4;
|
||
$val->cancel_status = 1;
|
||
$val->is_delete = 0;
|
||
if(!$val->save()){
|
||
$t->rollBack();
|
||
continue;
|
||
}
|
||
// 归还车辆信息
|
||
$order_detail = OrderDetail::findOne(['order_id' => $val->id]);
|
||
if(!empty($order_detail)){
|
||
$ball_cart = BallCart::find()->where(['status' => 3,'is_delete' => 0,'ball_number' => $order_detail->goods_id])->one();
|
||
if(!empty($ball_cart)){
|
||
$ball_cart->status = 0;
|
||
$ball_cart->save();
|
||
}
|
||
}
|
||
if(!empty($val->use_user_coupon_id)){
|
||
// 退回优惠券
|
||
UserCoupon::updateAll([
|
||
'status' => 0,
|
||
'updated_at' => time(),
|
||
],[
|
||
'id' => $val->use_user_coupon_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
}
|
||
$t->commit();
|
||
}
|
||
}
|
||
|
||
//TODO 保留是否注册会员才能进行下单
|
||
//
|
||
// $user = UserInformation::findOne(['user_id' => $this->user_id]);
|
||
//
|
||
// //如果不是购买会员 要先注册成为会员
|
||
// if ($this->plugin_sign != SysConst::$cxPluginSceneMemberMall) {
|
||
// //不是会员 进行会员注册
|
||
// if (empty($user) || $user['member_status'] == 0) {
|
||
// return $this->apiReturnError('请先注册会员');
|
||
// }
|
||
// if ($user['end_time'] < time()) {
|
||
// return $this->apiReturnError('会员已到期,请先续费会员');
|
||
// }
|
||
// //是会员 进行会员信息补充
|
||
// if (!isset($user['ball_age'])) {
|
||
// return $this->apiReturnError('请先进行会员信息补充');
|
||
// }
|
||
// }
|
||
|
||
//保留观看视频
|
||
// $user = ApiHelper::findOneUser($this->user_id);
|
||
// if (empty($user->is_view)) {
|
||
// $content = base64_decode(SiteHelper::getCustomiseOptionByKey("_page_handle_video_", "hump"));
|
||
// $content = SiteHelper::getFullUrl($content);
|
||
// return $this->apiReturnError('请先观看视频', ['data' => $content], 2);
|
||
// }
|
||
|
||
$ball_cart = BallCart::find()->andWhere(['ball_number' => $this->ball_number, 'is_delete' => 0])->orderBy(['id' => SORT_DESC])->one();
|
||
if (empty($ball_cart)) {
|
||
return $this->apiReturnError('球车正在租赁中,请更换一辆');
|
||
}
|
||
if ($ball_cart->status == 1) {
|
||
return $this->apiReturnError('球车正在租赁中,请更换一辆');
|
||
}
|
||
if ($ball_cart->status == 3) {
|
||
return $this->apiReturnError('球车正在支付中,请更换一辆');
|
||
}
|
||
// 判断当前用户所属门店
|
||
$store_data = ApiHelper::findOneUserStoreId($this->user_id);
|
||
|
||
if (empty($store_data) || $store_data->store_id != $ball_cart->store_id) {
|
||
//查看用户是否有进行中的订单
|
||
/*$orderIng = Order::find()->alias('o')
|
||
// ->innerJoin(['rt' => OrderDetail::tableName()], 'o.id=rt.order_id')
|
||
->andWhere(['o.user_id' => $this->user_id,])
|
||
->andWhere(['o.store_id' => $ball_cart->store_id])
|
||
->andWhere(['o.is_pay' => 1])
|
||
->andWhere(['<>','o.cancel_status' , 1])
|
||
->andWhere(['o.is_delete' => 0])
|
||
->andWhere(['IN', 'o.status', [1, 2]])
|
||
->one();
|
||
|
||
if (!empty($orderIng)) {
|
||
return $this->apiReturnError('您有一笔订单正在进行中,请先结束订单', ['user_id' => $this->user_id, 'order' => $orderIng]);
|
||
}*/
|
||
}
|
||
|
||
$qc_zl_kwh_min = "qc_zl_kwh_min"; // 球车最低租赁电量
|
||
$find_option = Option::findOne([
|
||
'key' => $qc_zl_kwh_min,
|
||
]);
|
||
//TODO 测试
|
||
if (!empty($find_option)) {
|
||
// 查找球车信息
|
||
$redis_name = "api:cxaibc:mqtt:data:{$this->ball_number}_Detail";
|
||
$get = \Yii::$app->redis->get($redis_name);
|
||
if (empty($get)) {
|
||
return $this->apiReturnError('车辆无信号,暂无法提供服务');
|
||
}
|
||
try {
|
||
$json_data_base = json_decode($get, true);
|
||
$json_data = json_decode($json_data_base['data'], true);
|
||
if (intval($json_data['ELEC_PERCENT']) < intval($find_option->value)) {
|
||
return $this->apiReturnError('车辆电量低,暂无法提供服务');
|
||
}
|
||
// if(intval($json_data_base['time']) <= time()-60*2){
|
||
// return $this->apiReturnError('车辆无信号,暂无法提供服务');
|
||
// }
|
||
} catch (\Exception $e) {
|
||
|
||
}
|
||
}
|
||
|
||
$this->store_id = $ball_cart->store_id;
|
||
|
||
|
||
$this->price = $ball_cart->money;
|
||
$price_money = $this->price;
|
||
|
||
//订单类型
|
||
$orderType = UniqueOrderNo::ORDER_TYPE_MEMBER;
|
||
$goods_info = '球车租赁';
|
||
|
||
try {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
|
||
$order = new Order();
|
||
$order->cx_mch_id = $this->cx_mch_id;
|
||
$order->user_id = $this->user_id;
|
||
$order->order_no = UniqueOrderNo::generate($orderType, Order::class, 24, 1, 3, 'order_no');
|
||
|
||
//使用的用户优惠券id
|
||
if ($this->coupon_id) {
|
||
|
||
$couponObj = UserCoupon::findOne(['id' => $this->coupon_id]);
|
||
if ($couponObj == null) {
|
||
return $this->apiReturnError('优惠券已不能使用');
|
||
}
|
||
|
||
$form = new CouponForm();
|
||
$form->user_id = $this->user_id;
|
||
$form->user_coupon_id = $this->coupon_id;
|
||
$couponInfo = $form->checkUserCoupon();
|
||
if ($couponInfo['code'] != 0) {
|
||
return $this->apiReturnError($couponInfo['msg']);
|
||
}
|
||
//优惠券信息
|
||
$couponArr = $form->getUserCoupon();
|
||
//立减券
|
||
if ($couponArr['type'] == 1) {
|
||
$order->coupon_discount_price = $couponArr['price'];
|
||
$price_money = $this->price - $couponArr['price'];
|
||
if ($price_money <= 0) { // 最终金额小于0
|
||
$price_money = 0;
|
||
|
||
//存在使用优惠券后金额为0的情况 不需要在跳转支付 直接创建成租赁中的订单
|
||
$order->is_pay = 1;
|
||
$order->pay_type = SysConst::$cxPayTypeCoupon;
|
||
$order->status = 1;
|
||
$order->pay_time = time();
|
||
} else {
|
||
$order->is_pay = 0;
|
||
$order->pay_type = SysConst::$cxPayTypeWxpay;
|
||
//订单状态 5-未支付 支付回调完成后 变更为 1 租赁中
|
||
$order->status = 5;
|
||
}
|
||
|
||
$order->total_pay_price = $price_money; //实际支付
|
||
$order->total_goods_price = $ball_cart->money; //订单商品总金额(优惠后)
|
||
}
|
||
//体验券
|
||
if ($couponArr['type'] == 0) {
|
||
$order->coupon_discount_price = $couponArr['price'];
|
||
$price_money = $couponArr['price'];
|
||
if ($price_money <= 0) { // 最终金额小于0
|
||
$price_money = 0;
|
||
$order->is_pay = 1;
|
||
$order->pay_type = SysConst::$cxPayTypeCoupon;
|
||
$order->status = 1;
|
||
$order->pay_time = time();
|
||
// 直接推送消息
|
||
$ball_mark = BallMark::findOne(['id' => $ball_cart->mark_id]);
|
||
$array = ['ball_mark' => $ball_mark->name, 'ball_cart' => $ball_cart->name, 'start_date' => time(), 'end_date' => 0, 'price' => $order->total_price, 'object_id' => $order->id];
|
||
MsgCentreForm::add($ball_cart->store_id, 1, $array);
|
||
} else {
|
||
$order->is_pay = 0;
|
||
$order->pay_type = SysConst::$cxPayTypeWxpay;
|
||
//订单状态 5-未支付 支付回调完成后 变更为 1 租赁中
|
||
$order->status = 5;
|
||
}
|
||
|
||
$order->total_pay_price = $price_money; //实际支付
|
||
$order->total_goods_price = $ball_cart->money; //订单商品总金额(优惠后)
|
||
}
|
||
$order->use_user_coupon_id = $this->coupon_id;
|
||
|
||
$couponObj->status = 1;
|
||
if (!$couponObj->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('优惠券错误', $this->getModelError($order));
|
||
}
|
||
|
||
} else {
|
||
$order->coupon_discount_price = 0;
|
||
$order->use_user_coupon_id = 0;
|
||
$order->total_pay_price = $this->price; //实际支付
|
||
$order->total_goods_price = $ball_cart->money; //订单商品总金额(优惠后)
|
||
|
||
//没使用优惠券
|
||
$order->is_pay = 0;
|
||
$order->pay_type = SysConst::$cxPayTypeWxpay;
|
||
//订单状态 5-未支付 支付回调完成后 变更为 1 租赁中
|
||
$order->status = 5;
|
||
}
|
||
$order->total_goods_original_price = $this->price; //订单商品总金额(优惠前)
|
||
$order->express_original_price = 0;
|
||
$order->express_price = 0;
|
||
$order->store_id = $this->store_id;
|
||
|
||
$order->plugin_sign = $this->plugin_sign;
|
||
|
||
$order->created_at = time();
|
||
$order->total_price = $price_money; //总金额
|
||
if (!$order->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误', $this->getModelError($order));
|
||
}
|
||
|
||
$orderdetail = new OrderDetail();
|
||
$orderdetail->cx_mch_id = $this->cx_mch_id;
|
||
$orderdetail->order_id = $order->id;
|
||
$orderdetail->goods_id = $this->ball_number;
|
||
$orderdetail->num = 1;
|
||
$orderdetail->unit_price = $this->price; //实际支付
|
||
$orderdetail->total_original_price = $this->price;
|
||
$orderdetail->total_price = $order->total_pay_price;
|
||
$orderdetail->plugin_sign = $this->plugin_sign;
|
||
$orderdetail->goods_attr_info = $goods_info;
|
||
$orderdetail->created_at = time();
|
||
if (!$orderdetail->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误1', $this->getModelError($orderdetail));
|
||
}
|
||
|
||
$ball_cart->status = $order->is_pay === 1 ? 1 : 3;
|
||
$ball_cart->updated_at = time();
|
||
if (!$ball_cart->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('球车错误', $this->getModelError($ball_cart));
|
||
}
|
||
|
||
$redis_name = $this->redis_name . $this->ball_number;
|
||
//当前球车对应用户
|
||
\Yii::$app->redis->set($redis_name, $this->user_id);
|
||
// 添加活动日志操作
|
||
$obj = new ActivityUserLog();
|
||
$obj->user_id = $this->user_id;
|
||
$obj->activity_id = 0;
|
||
$obj->msg = '';
|
||
$obj->created_at = time();
|
||
$obj->updated_at = time();
|
||
$obj->is_delete = 0;
|
||
$obj->deleted_at = 0;
|
||
$obj->data = json_encode([
|
||
'type' => 1,
|
||
'data' => $order->id,
|
||
]);
|
||
$obj->status = 1;
|
||
$obj->save();
|
||
$t->commit();
|
||
return $this->apiReturnSuccess('ok', ['id' => $order->id, 'pay_status' => $order->is_pay]);
|
||
} catch (Exception $exception) {
|
||
$t->rollBack();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '创建订单失败' . $exception->getMessage(),
|
||
'data' => null
|
||
];
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 订单列表
|
||
*/
|
||
public function search()
|
||
{
|
||
if($this->status == ''){
|
||
$this->status = null;
|
||
}
|
||
$query = Order::find()->alias('o')
|
||
->leftJoin(['rt' => OrderDetail::tableName()], 'o.id=rt.order_id')
|
||
->leftJoin(['ball' => BallCart::tableName()], 'rt.goods_id=ball.ball_number')
|
||
->leftJoin(['s' => Store::tableName()], 'o.store_id=s.id')
|
||
->where(['o.is_delete' => 0,])
|
||
->andWhere(['rt.is_delete' => 0,])
|
||
->andWhere(['ball.is_delete' => 0,])
|
||
->andWhere(['s.is_delete' => 0,])
|
||
->andFilterWhere(['o.store_id' => $this->store_id])
|
||
->andFilterWhere(['o.user_id' => $this->user_id])
|
||
->andFilterWhere(['o.is_pay' => $this->is_pay])
|
||
->andFilterWhere(['o.status' => $this->status])
|
||
->select('o.id as order_id,o.is_sale,o.total_pay_price as total_price,rt.goods_id as ball_number,ball.cover_pic,
|
||
o.is_pay,o.store_id,o.plugin_sign,s.phone,o.confirm_time,o.created_at,o.updated_at,o.pay_time,o.cancel_status,o.status,s.name as store_name,o.store_id,o.is_confirm,o.total_pay_price');
|
||
// ->groupBy('order_id');
|
||
|
||
|
||
// if($this->status != 0){
|
||
// $query->andWhere(['plugin_sign' => SysConst::$cxPluginSceneBallCart]);
|
||
// }else{
|
||
// if(!empty($this->store_id)){
|
||
// $query->andWhere(['plugin_sign' => SysConst::$cxPluginSceneBallCart]);
|
||
// }
|
||
// }
|
||
|
||
|
||
$count_query = clone $query;
|
||
$count = $count_query->count();
|
||
|
||
if (empty($this->limit)) {
|
||
$this->limit = 10;
|
||
}
|
||
if (empty($this->page)) {
|
||
$this->page = 1;
|
||
}
|
||
$status_arr = [2,3,1,0,4,5];
|
||
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
|
||
$list = $query->offset($pagination->offset)->limit($pagination->limit)
|
||
->orderBy(["FIELD(o.status, ".join(',',$status_arr).")" => true,'o.id' => SORT_DESC])
|
||
->asArray()->all();
|
||
$this->pageCount = $pagination->pageCount;
|
||
|
||
|
||
$temp_data = [];
|
||
foreach ($list as $key => $value) {
|
||
|
||
$list[$key]['cover_pic'] = SiteHelper::getFullUrl($value['cover_pic']);
|
||
//order_status 订单状态:1=进行中,0=已完成 2-用户申请还车 3-用户已申请退款 4-订单已取消 5-订单已未支付
|
||
//status 订单状态:1=进行中,0=已完成 2-用户申请还车 3-用户已申请退款 4-订单已取消 5-订单已未支付
|
||
//cancal_status 订单取消状态:0=未取消,1=已取消,2=申请取消
|
||
//is_pay 0=未支付,1=已支付
|
||
// $list[$key]['deposit_title'] = '';
|
||
//租赁中 order_status=1
|
||
if ($value['status'] == self::ORDER_NO && $value['is_pay'] == self::ORDER_NO && $value['cancel_status'] == self::ORDER_OK && $value['plugin_sign'] == SysConst::$cxPluginSceneBallCart) {
|
||
$order_status = self::ORDER_NO;
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
$temp_data['user_ball_ing'][] = $list[$key];
|
||
}
|
||
|
||
//已完成
|
||
if ($value['status'] == self::ORDER_OK && $value['is_pay'] == self::ORDER_NO && $value['cancel_status'] == self::ORDER_OK && $value['plugin_sign'] == SysConst::$cxPluginSceneBallCart) {
|
||
$order_status = self::ORDER_OK;
|
||
$list[$key]['begin_time'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['end_time'] = date('Y-m-d H:i:s', $value['confirm_time']);
|
||
$time_often = $value['confirm_time'] - $value['created_at'];
|
||
$list[$key]['time_content'] = GetDistance::mdate($time_often);
|
||
|
||
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
$temp_data['user_ball_ok'][] = $list[$key];
|
||
}
|
||
|
||
//用户申请还车
|
||
if ($value['status'] == self::ORDER_PAY_OK && $value['plugin_sign'] == SysConst::$cxPluginSceneBallCart) {
|
||
$order_status = 2;
|
||
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
|
||
$temp_data['user_ball_return'][] = $list[$key];
|
||
}
|
||
|
||
//用户申请售后
|
||
if ($value['status'] == self::ORDER_PAY_NO && $value['plugin_sign'] == SysConst::$cxPluginSceneBallCart) {
|
||
$order_status = 3;
|
||
|
||
|
||
$list[$key]['begin_time'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['end_time'] = date('Y-m-d H:i:s', $value['confirm_time']);
|
||
$time_often = $value['confirm_time'] - $value['created_at'];
|
||
$list[$key]['time_content'] = GetDistance::mdate($time_often);
|
||
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
$temp_data['user_ball_sale'][] = $list[$key];
|
||
}
|
||
|
||
//订单已取消
|
||
if ($value['status'] == 4 && $value['plugin_sign'] == SysConst::$cxPluginSceneBallCart) {
|
||
$order_status = 4;
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
}
|
||
|
||
//订单已未支付
|
||
if ($value['status'] == 5 && $value['plugin_sign'] == SysConst::$cxPluginSceneBallCart) {
|
||
$order_status = 5;
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
}
|
||
|
||
if ($value['plugin_sign'] == SysConst::$cxPluginSceneDeposit) {
|
||
if ($value['status'] == 0) {
|
||
$order_status = 0; //球车订单已关闭
|
||
if ($value['is_confirm'] == 0) {
|
||
$deposit_title = '待退款'; //未发起
|
||
} elseif ($value['is_confirm'] == 1) {
|
||
$deposit_title = '退款成功';
|
||
} elseif ($value['is_confirm'] == 2) {
|
||
$deposit_title = '退款关闭';
|
||
} elseif ($value['is_confirm'] == 3) {
|
||
$deposit_title = '退款异常';
|
||
}
|
||
} elseif ($value['is_pay'] == 1) {
|
||
$order_status = 1; //已支付
|
||
}
|
||
|
||
$list[$key]['order_status'] = $order_status;
|
||
$list[$key]['deposit_title'] = $deposit_title;
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['updated_at'] = date('Y-m-d H:i:s', $value['updated_at']);
|
||
}
|
||
|
||
//订单超时未支付
|
||
// $end_time = $value['created_at'] + 900; //结束时间
|
||
// $time = time(); //当前时间
|
||
// if ($end_time - $time < 0) {
|
||
// $order_status = self::ORDER_PAY_NO;
|
||
// }
|
||
|
||
}
|
||
|
||
if($this->status != ''){
|
||
$list = [];
|
||
$list = array_merge($list, $temp_data['user_ball_return'] ?? []); //还车
|
||
$list = array_merge($list, $temp_data['user_ball_sale'] ?? []); //售后
|
||
$list = array_merge($list, $temp_data['user_ball_ing'] ?? []); //租赁中
|
||
$list = array_merge($list, $temp_data['user_ball_ok'] ?? []); //完成
|
||
}
|
||
|
||
|
||
$end_flag = $this->page >= $pagination->pageCount ? true : false;
|
||
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $list,
|
||
'count' => $count,
|
||
'page_size' => $this->limit,
|
||
'page_count' => $this->pageCount,
|
||
'page_no' => $this->page,
|
||
'end_flag' => $end_flag,
|
||
'sql' => $query->createCommand()->getRawSql(),
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 订单详情
|
||
*/
|
||
public function searchOne()
|
||
{
|
||
if (!$this->order_id) {
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '订单ID不能为空'
|
||
];
|
||
}
|
||
$orderData = [];
|
||
$orderData = Order::find()->alias('o')
|
||
->innerJoin(['rt' => OrderDetail::tableName()], 'o.id=rt.order_id')
|
||
->innerJoin(['s' => Store::tableName()], 'o.store_id=s.id')
|
||
->innerJoin(['ball' => BallCart::tableName()], 'rt.goods_id=ball.ball_number')
|
||
->innerJoin(['mark' => BallMark::tableName()], 'ball.mark_id=mark.id')
|
||
->select('o.id,o.order_no,o.total_price,o.is_sale,o.total_pay_price,o.pay_type,o.created_at,rt.goods_id as ball_number,o.status,o.cancel_status,s.name as store_name,o.is_pay,o.pay_time,o.plugin_sign,
|
||
o.store_id,s.location_detail,o.updated_at,o.confirm_time,ball.name as ball_name,ball.cover_pic as ball_cover_pic,s.phone,mark.name as mark_name')
|
||
->where(['o.id' => $this->order_id])->asArray()->one();
|
||
|
||
if ($orderData) {
|
||
$orderData['ball_cover_pic'] = SiteHelper::getFullUrl($orderData['ball_cover_pic']);
|
||
$orderData['pay_time'] = $orderData['pay_time'] > 0 ? date('Y-m-d H:i:s', $orderData['pay_time']) : date('Y-m-d H:i:s', $orderData['created_at']);
|
||
|
||
//租赁中 order_status=1
|
||
if ($orderData['status'] == self::ORDER_NO && $orderData['is_pay'] == self::ORDER_NO && $orderData['cancel_status'] == self::ORDER_OK) {
|
||
$order_status = self::ORDER_NO;
|
||
}
|
||
|
||
//已完成
|
||
if ($orderData['status'] == self::ORDER_OK && $orderData['is_pay'] == self::ORDER_NO && $orderData['cancel_status'] == self::ORDER_OK) {
|
||
$order_status = self::ORDER_OK;
|
||
$orderData['begin_time'] = date('Y-m-d H:i:s', $orderData['created_at']);
|
||
$orderData['end_time'] = date('Y-m-d H:i:s', $orderData['confirm_time']);
|
||
$time_often = $orderData['confirm_time'] - $orderData['created_at'];
|
||
$orderData['time_content'] = GetDistance::mdate($time_often);
|
||
|
||
$order_sale = OrderSale::find()->andWhere(['order_id' => $this->order_id])->orderBy(['id' => SORT_DESC])->asArray()->one();
|
||
if ($order_sale == null) {
|
||
//0 没有申请过售后
|
||
$orderData['order_sale'] = 0;
|
||
} else {
|
||
$orderData['order_sale'] = $order_sale['status'];
|
||
}
|
||
}
|
||
|
||
//用户申请还车
|
||
if ($orderData['status'] == self::ORDER_PAY_OK) {
|
||
$order_status = 2;
|
||
}
|
||
|
||
//用户申请售后
|
||
if ($orderData['status'] == self::ORDER_PAY_NO) {
|
||
$order_status = 3;
|
||
}
|
||
|
||
//订单已取消
|
||
if ($orderData['status'] == 4) {
|
||
$order_status = 4;
|
||
}
|
||
|
||
//订单已未支付
|
||
if ($orderData['status'] == 5) {
|
||
$order_status = 5;
|
||
}
|
||
$orderData['order_status'] = $order_status;
|
||
|
||
//优惠金额 总金额 - 实际支付
|
||
$orderData['discount_money'] = $orderData['total_price'] - $orderData['total_pay_price'];
|
||
$pay_type_arr = [
|
||
0 => '余额支付',
|
||
1 => '微信支付',
|
||
2 => '支付宝支付',
|
||
3 => '银行卡支付',
|
||
4 => '积分支付',
|
||
5 => '银联支付',
|
||
6 => '微信支付分',
|
||
7 => '微信支付',
|
||
];
|
||
$orderData['pay_type'] = $pay_type_arr[$orderData['pay_type']]??'';
|
||
if(in_array($orderData['status'],[4,5])){
|
||
// 已取消、待支付
|
||
$orderData['pay_type'] = '';
|
||
$orderData['pay_time'] = '';
|
||
}
|
||
}
|
||
$date_created_at = date('Y-m-d H:i:s', $orderData['created_at']);
|
||
$orderData['created_at'] = $date_created_at;
|
||
return $this->apiReturnSuccess('ok', $orderData);
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 更新订单状态
|
||
*/
|
||
public function orderSave()
|
||
{
|
||
$orderData = Order::find()
|
||
->select('id,created_at')
|
||
->where(['user_id' => $this->user_id, 'is_pay' => 0, 'status' => 1])->asArray()->all();
|
||
foreach ($orderData as $key => $value) {
|
||
$end_time = $value['created_at'] + 900; //结束时间
|
||
$time = time(); //当前时间
|
||
//结束时间 小于当前时间 订单过期未支付 更新订单状态
|
||
if ($end_time - $time < 0) {
|
||
Order::updateAll(['cancel_status' => 1, 'cancel_time' => $time, 'updated_at' => $time, 'status' => 0], ['id' => $value['id']]);
|
||
}
|
||
}
|
||
return $this->apiReturnSuccess("ok");
|
||
}
|
||
|
||
/**
|
||
* 取消订单
|
||
* @return array
|
||
*/
|
||
public function orderCancal()
|
||
{
|
||
|
||
$orderData = Order::findOne(['id' => $this->order_id]);
|
||
if ($orderData == null) {
|
||
return $this->apiReturnError('错误订单');
|
||
}
|
||
if ($orderData['is_pay'] == 1) {
|
||
return $this->apiReturnError('订单已支付');
|
||
}
|
||
$orderData->cancel_status = 1;
|
||
$orderData->cancel_time = time();
|
||
$orderData->updated_at = time();
|
||
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
if (!$orderData->save()){
|
||
$t->rollBack();
|
||
return $this->getModelError($orderData);
|
||
}
|
||
$order_detail = OrderDetail::findOne(['order_id' => $orderData->id]);
|
||
if(!empty($order_detail)){
|
||
$ball_cart = BallCart::find()->where(['status' => 3,'is_delete' => 0,'ball_number' => $order_detail->goods_id])->one();
|
||
if(!empty($ball_cart)){
|
||
$ball_cart->status = 0;
|
||
if(!$ball_cart->save()){
|
||
$t->rollBack();
|
||
}
|
||
}
|
||
}
|
||
if(!empty($orderData->use_user_coupon_id)){
|
||
// 退回优惠券
|
||
UserCoupon::updateAll([
|
||
'status' => 0,
|
||
'updated_at' => time(),
|
||
],[
|
||
'id' => $orderData->use_user_coupon_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
}
|
||
$t->commit();
|
||
return [
|
||
'code' => 0,
|
||
'msg' => '取消成功'
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 还车
|
||
* @return array
|
||
*/
|
||
public function orderReturnCart()
|
||
{
|
||
|
||
if (empty($this->plugin_sign) || $this->plugin_sign != 'return_cart') {
|
||
return $this->apiReturnError('标识错误');
|
||
}
|
||
|
||
//加锁
|
||
$redis_key = $this->redis_key_order . $this->user_id . $this->plugin_sign;
|
||
$this->lock($redis_key);
|
||
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
if (empty($this->order_id)) {
|
||
|
||
|
||
// $OrderDetail = Order::find()->alias('rt')
|
||
// ->innerJoin(['o' => Order::tableName()], 'rt.order_id=o.id')
|
||
// ->select('rt.goods_id as id,rt.goods_id as ball_number')
|
||
// ->where(['o.user_id' => $this->user_id])
|
||
// ->andWhere(['goods_id' => $this->ball_number, 'plugin_sign' => 'ball_cart', 'is_delete' => 0,])
|
||
// ->one();
|
||
|
||
$OrderDetail = OrderDetail::find()->andWhere([
|
||
'goods_id' => $this->ball_number,
|
||
'plugin_sign' => 'ball_cart',
|
||
'is_delete' => 0,
|
||
])->orderBy([
|
||
'id' => SORT_DESC,
|
||
])->one();
|
||
|
||
if (empty($OrderDetail)) {
|
||
return $this->apiReturnError('错误订单,球车编号:' . $this->ball_number);
|
||
}
|
||
$orderData = Order::findOne(['id' => $OrderDetail->order_id]);
|
||
|
||
} else {
|
||
$orderData = Order::findOne(['id' => $this->order_id]);
|
||
$OrderDetail = OrderDetail::findOne(['order_id' => $this->order_id]);
|
||
if ($orderData == null || $OrderDetail == null) {
|
||
return $this->apiReturnError('错误订单,订单编号:' . $this->order_id);
|
||
}
|
||
}
|
||
|
||
|
||
if ($orderData == null) {
|
||
return $this->apiReturnError('错误订单:' . $this->order_id);
|
||
}
|
||
if ($orderData->status == 0) {
|
||
return $this->apiReturnError('订单已完成');
|
||
}
|
||
|
||
if ($orderData->status == 2) {
|
||
return $this->apiReturnError('已申请,请等待门店人员审核');
|
||
}
|
||
|
||
$orderData->status = 2;
|
||
$orderData->updated_at = time();
|
||
$orderData->confirm_time = time();
|
||
if (!$orderData->save())
|
||
return $this->getModelError($orderData);
|
||
$order_detail = OrderDetail::findOne(['order_id' => $orderData->id]);
|
||
if ($order_detail != null) {
|
||
$ball_model = BallCart::find()->alias('bc')
|
||
->select('bc.ball_number,bm.name')
|
||
->leftJoin(['bm' => BallMark::tableName()], 'bc.mark_id=bm.id')
|
||
->where(['bc.ball_number' => $order_detail->goods_id])
|
||
->one();
|
||
if (!empty($order_detail)) {
|
||
$ball_mark = $ball_model == null ? '球车' : $ball_model->name;
|
||
$ball_cart = $ball_model == null ? '~~' : $ball_model->ball_number;
|
||
$array = ['ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'start_date' => $orderData->created_at, 'end_date' => $orderData->confirm_time, 'price' => $orderData->total_price, 'object_id' => $orderData->id];
|
||
MsgCentreForm::add($orderData->store_id, 2, $array);
|
||
}
|
||
|
||
}
|
||
return [
|
||
'code' => 0,
|
||
'msg' => '已提交申请'
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 门店确认还车
|
||
* @return array
|
||
*/
|
||
public function orderConfirmCart()
|
||
{
|
||
|
||
if (empty($this->plugin_sign) || $this->plugin_sign != 'return_cart') {
|
||
return $this->apiReturnError('标识错误');
|
||
}
|
||
|
||
//加锁
|
||
$redis_key = $this->redis_key_order . $this->ball_number . $this->plugin_sign;
|
||
$this->lock($redis_key);
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
|
||
if ($this->user_type != 2) {
|
||
return $this->apiReturnError('账号错误');
|
||
}
|
||
|
||
try {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
if (empty($this->order_id)) {
|
||
$OrderDetail = OrderDetail::find()->andWhere([
|
||
'goods_id' => $this->ball_number,
|
||
'plugin_sign' => 'ball_cart',
|
||
'is_delete' => 0,
|
||
])->orderBy([
|
||
'id' => SORT_DESC,
|
||
])->one();
|
||
if (empty($OrderDetail)) {
|
||
return $this->apiReturnError('错误订单,球车编号:' . $this->ball_number);
|
||
}
|
||
$orderData = Order::findOne(['id' => $OrderDetail->order_id]);
|
||
} else {
|
||
$orderData = Order::findOne(['id' => $this->order_id]);
|
||
$OrderDetail = OrderDetail::findOne(['order_id' => $this->order_id]);
|
||
if ($orderData == null || $OrderDetail == null) {
|
||
return $this->apiReturnError('错误订单,订单编号:' . $this->order_id);
|
||
}
|
||
}
|
||
if (!in_array($orderData['status'], [1, 2])) {
|
||
return $this->apiReturnError('车辆已归还');
|
||
}
|
||
|
||
|
||
$orderData->status = 0;
|
||
$orderData->updated_at = time();
|
||
$orderData->confirm_time = time();
|
||
if (!$orderData->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($orderData);
|
||
}
|
||
|
||
|
||
$ballCart = BallCart::findOne(['ball_number' => $OrderDetail->goods_id, 'is_delete' => 0]);
|
||
if ($ballCart == null) {
|
||
return $this->apiReturnError('球车错误,请联系管理员');
|
||
}
|
||
$ballCart->status = 0;
|
||
if (!$ballCart->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($ballCart);
|
||
}
|
||
MsgCentreForm::sendWxAppletReturnMsg($orderData);
|
||
|
||
$redis_name = $this->redis_name . $OrderDetail->goods_id;
|
||
\Yii::$app->redis->del($redis_name);
|
||
|
||
|
||
//请求第三方关闭车辆
|
||
$mqtt = new Mqtt();
|
||
$mqttData = $mqtt->sendOpen($OrderDetail->goods_id, 0, 0);
|
||
if ($mqttData['code'] != 0) {
|
||
$t->rollBack();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '还车失败,mqtt错误:' . $mqttData['msg'],
|
||
'data' => null
|
||
];
|
||
}
|
||
|
||
$t->commit();
|
||
//查询未完成退款的押金订单是否存在
|
||
// $order_deposit = Order::findOne(['user_id' => $this->user_id,'pay_type' => SysConst::$cxPayTypeWxpay,'plugin_sign' => SysConst::$cxPluginSceneDeposit,'is_pay' => 1,'is_delete' => 0,'status' => 1]);
|
||
// if($order_deposit){
|
||
// $paymentOrder = PaymentOrder::findOne(['order_no' => $order_deposit->order_no]);
|
||
// if($paymentOrder != null){
|
||
// $paymentOrderUnion = PaymentOrderUnion::findOne(['id' => $paymentOrder->payment_order_union_id]);
|
||
// if($paymentOrderUnion != null){
|
||
// $plugin = new \app\models\common\PluginService();
|
||
// $wxmpService = $plugin->getWxmpService(0);
|
||
// $url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/notify/payment/wxpay-deposit-refunds';
|
||
// $res = $wxmpService->pay->refund([
|
||
// 'out_trade_no' => $paymentOrderUnion->out_trade_no,
|
||
// 'out_refund_no' => $paymentOrderUnion->order_no,
|
||
// 'total_fee' => $paymentOrderUnion->amount * 100,
|
||
// 'refund_fee' => $paymentOrderUnion->amount * 100,
|
||
// 'notify_url' => $url,
|
||
// ]);
|
||
// if($res['return_code'] != 'SUCCESS'){
|
||
// \Yii::info($orderData->order_no.'__'.$res['return_code'].'__'.$res['return_msg'],'refund');
|
||
// }
|
||
// }
|
||
// }
|
||
// $order_deposit->status = 0;
|
||
// $order_deposit->save();
|
||
// }
|
||
|
||
// 写redis,变更当前状态未0
|
||
$redis_ball_cart_status_name = "api:cxaibc:ball_cart:{$OrderDetail->goods_id}";
|
||
$get = \Yii::$app->redis->get($redis_ball_cart_status_name);
|
||
$redis_value = "0:0:test";
|
||
if (!empty($get)) {
|
||
$explode = explode(':', $get);
|
||
$explode[0] = "0";
|
||
$redis_value = implode(":", $explode);
|
||
}
|
||
\Yii::$app->redis->set($redis_ball_cart_status_name, $redis_value);
|
||
} catch (Exception $exception) {
|
||
$t->rollBack();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '还车失败' . $exception->getMessage(),
|
||
'data' => null
|
||
];
|
||
}
|
||
return [
|
||
'code' => 0,
|
||
'msg' => '还车成功'
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 用户申请售后
|
||
* @return array
|
||
*/
|
||
public function orderSales()
|
||
{
|
||
|
||
//金额 原因 补充 图片
|
||
|
||
if (empty($this->plugin_sign) || $this->plugin_sign != 'order_sale') {
|
||
return $this->apiReturnError('标识错误');
|
||
}
|
||
$this->type = $this->type ?? 0;
|
||
|
||
//加锁
|
||
$redis_key = $this->redis_key_order . $this->user_id . $this->plugin_sign;
|
||
$this->lock($redis_key);
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
$orderData = Order::findOne(['id' => $this->order_id, 'user_id' => $this->user_id]);
|
||
if ($orderData == null) {
|
||
return $this->apiReturnError('错误订单');
|
||
}
|
||
//要已完成订单才能发起售后退款
|
||
if ($orderData['status'] != 0) {
|
||
return $this->apiReturnError('订单错误');
|
||
}
|
||
|
||
if ($orderData['is_sale'] == 1) {
|
||
return $this->apiReturnError('订单已处理,请勿重复申请');
|
||
}
|
||
|
||
// $orderSaleData = OrderSale::findOne(['user_id' => $this->user_id, 'order_id' => $this->order_id]);
|
||
$orderSaleData = OrderSale::find()->andWhere([
|
||
'user_id' => $this->user_id, 'order_id' => $this->order_id
|
||
])->andWhere([
|
||
'in', 'status', [1, 2]
|
||
])->select('*')->asArray()->one();
|
||
if (!empty($orderSaleData)) {
|
||
if ($orderSaleData['status'] == 2) {
|
||
return $this->apiReturnError('已退款成功');
|
||
}
|
||
return $this->apiReturnError('您已申请退款,请等待门店人员审核');
|
||
}
|
||
|
||
try {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
$orderData->status = 3;
|
||
if (!$orderData->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($orderData);
|
||
}
|
||
|
||
|
||
$orderSaleModel = new OrderSale();
|
||
$orderSaleModel->user_id = $this->user_id;
|
||
$orderSaleModel->order_id = $this->order_id;
|
||
$orderSaleModel->refund_price = $orderData->total_pay_price; //退款金额
|
||
$orderSaleModel->type = $this->type; //退款金额
|
||
$orderSaleModel->store_id = $orderData['store_id']; //退款金额
|
||
$orderType = UniqueOrderNo::ORDER_TYPE_RETURN_ORDER;
|
||
$orderSaleModel->order_no = UniqueOrderNo::generate($orderType, Order::class, 24, 1, 3, 'order_no');
|
||
if ($this->remark) {
|
||
$orderSaleModel->remark = $this->remark; //退款说明
|
||
}
|
||
if ($this->pic_urls) {
|
||
$orderSaleModel->pic_urls = $this->pic_urls; //补充图片
|
||
}
|
||
$orderSaleModel->created_at = time();
|
||
$orderSaleModel->status_time = 0;
|
||
$orderSaleModel->updated_at = 0;
|
||
$orderSaleModel->deleted_at = 0;
|
||
if (!$orderSaleModel->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($orderSaleModel);
|
||
}
|
||
|
||
$order_detail = OrderDetail::findOne(['order_id' => $orderData->id]);
|
||
if ($order_detail != null) {
|
||
$ball_model = BallCart::find()->alias('bc')
|
||
->select('bc.ball_number,bm.name')
|
||
->leftJoin(['bm' => BallMark::tableName()], 'bc.mark_id=bm.id')
|
||
->where(['bc.ball_number' => $order_detail->goods_id])
|
||
->one();
|
||
if ($ball_model != null) {
|
||
$ball_mark = $ball_model == null ? '球车' : $ball_model->name;
|
||
$ball_cart = $ball_model == null ? '~~' : $ball_model->ball_number;
|
||
$array = ['ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'start_date' => $orderData->created_at, 'end_date' => $orderData->updated_at, 'price' => $orderData->total_price, 'object_id' => $orderData->id];
|
||
MsgCentreForm::add($orderData->store_id, 3, $array);
|
||
}
|
||
}
|
||
$t->commit();
|
||
} catch (Exception $exception) {
|
||
$t->rollBack();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '申请退款成功' . $exception->getMessage(),
|
||
'data' => null
|
||
];
|
||
|
||
}
|
||
return [
|
||
'code' => 0,
|
||
'msg' => '申请退款成功'
|
||
];
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 售后审核
|
||
* @return array
|
||
*/
|
||
public function orderSalesStore()
|
||
{
|
||
if ($this->user_type != 2 && $this->store_type == 2) {
|
||
return $this->apiReturnError('权限错误');
|
||
}
|
||
//金额 原因 补充 图片
|
||
if (empty($this->plugin_sign) || $this->plugin_sign != 'order_sale_store') {
|
||
return $this->apiReturnError('标识错误');
|
||
}
|
||
|
||
|
||
//加锁
|
||
$redis_key = $this->redis_key_order . $this->user_id . $this->plugin_sign;
|
||
$this->lock($redis_key);
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
//售后订单
|
||
// $orderData = OrderSale::findOne(['order_id' => $this->order_id]);
|
||
$orderData = OrderSale::find()->andWhere(['order_id' => $this->order_id, 'is_delete' => 0,])->orderBy(['id' => SORT_DESC])->one();
|
||
|
||
//订单
|
||
$orderObj = Order::findOne(['id' => $this->order_id]);
|
||
|
||
|
||
$orderDetailObj = OrderDetail::findOne(['order_id' => $this->order_id]);
|
||
//球车信息
|
||
$ballCart = BallCart::findOne(['ball_number' => $orderDetailObj->goods_id]);
|
||
|
||
if ($orderData == null || $orderObj == null || $orderDetailObj == null || $ballCart == null) {
|
||
return $this->apiReturnError('错误订单');
|
||
}
|
||
|
||
if ($orderData->status != 1) {
|
||
return $this->apiReturnError('售后订单已完成');
|
||
}
|
||
|
||
|
||
//TODO 支付退款 改变用户金额 改变订单状态
|
||
|
||
try {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
|
||
//拒绝
|
||
if ($this->status == 3) {
|
||
if (!$this->merchant_remark) {
|
||
return $this->apiReturnError('拒绝原因不能为空');
|
||
}
|
||
$orderObj->is_sale = 2;
|
||
$orderData->merchant_remark = $this->merchant_remark;
|
||
}
|
||
|
||
$orderData->merchant_remark = $this->merchant_remark == null ? '同意' : $this->merchant_remark;
|
||
//更新售后订单表
|
||
$orderData->status = $this->status;
|
||
$orderData->status_time = time();
|
||
$orderData->updated_at = time();
|
||
if (!$orderData->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($orderData);
|
||
}
|
||
|
||
//更改订单状态为已完成
|
||
$orderObj->status = 0;
|
||
//同意
|
||
if ($this->status == 2) {
|
||
$orderObj->is_sale = 1;
|
||
}
|
||
$orderObj->updated_at = time();
|
||
if (!$orderObj->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($orderObj);
|
||
}
|
||
|
||
//更改球车状态为正常
|
||
$ballCart->status = 0;
|
||
if (!$ballCart->save()) {
|
||
$t->rollBack();
|
||
return $this->getModelError($ballCart);
|
||
}
|
||
MsgCentre::deleteAll(['object_id' => $orderObj->id, 'type' => 3]);
|
||
\Yii::info($orderObj->id . '__进入小程序管理员确认售后用户结果通知', 'wx_applet_msg');
|
||
if ($this->status == 2) {
|
||
$name = "同意";
|
||
|
||
$paymentOrder = PaymentOrder::findOne(['order_no' => $orderObj->order_no]);
|
||
if ($paymentOrder != null) {
|
||
$paymentOrderUnion = PaymentOrderUnion::findOne(['id' => $paymentOrder->payment_order_union_id]);
|
||
if ($paymentOrderUnion != null) {
|
||
$plugin = new \app\models\common\PluginService();
|
||
$wxmpService = $plugin->getWxmpService(0);
|
||
// $url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/notify/payment/wxpay-order-refunds';
|
||
$res = $wxmpService->pay->refund([
|
||
'out_trade_no' => $paymentOrderUnion->out_trade_no,
|
||
'out_refund_no' => $paymentOrderUnion->order_no,
|
||
'total_fee' => $paymentOrderUnion->amount * 100,
|
||
'refund_fee' => $paymentOrderUnion->amount * 100,
|
||
// 'notify_url' => $url,
|
||
]);
|
||
if (isset($res['return_code']) && $res['return_code'] != 'SUCCESS') {
|
||
\Yii::info($orderObj->order_no . '__' . $res['return_code'] . '__' . $res['return_msg'], 'refund');
|
||
$t->rollBack();
|
||
return $this->apiReturnError($res['return_msg']);
|
||
}
|
||
$exists = StoreEarnings::find()->where(['order_id' => $orderObj->id,'is_delete' => 0])->exists();
|
||
if($exists){
|
||
\Yii::$app->db->createCommand()->update(StoreEarnings::tableName(), ['is_delete' => 1], ['order_id' => $orderObj->id,'is_delete' => 0])->execute();
|
||
}
|
||
}
|
||
}
|
||
|
||
} else {
|
||
$name = '拒绝';
|
||
}
|
||
if (!empty($this->merchant_remark)) {
|
||
$merchant_remark = mb_substr($this->merchant_remark, 0, 10);
|
||
} else {
|
||
$merchant_remark = $name;
|
||
}
|
||
MsgCentreForm::sendWxAppletStoreSalesMsg($orderObj, $orderData, $name, $merchant_remark);
|
||
$t->commit();
|
||
} catch (Exception $exception) {
|
||
$t->rollBack();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '操作失败' . $exception->getMessage(),
|
||
'data' => null
|
||
];
|
||
}
|
||
|
||
|
||
return [
|
||
'code' => 0,
|
||
'msg' => '操作成功'
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 售后订单列表
|
||
*/
|
||
public function saleList()
|
||
{
|
||
//金额 原因 补充 图片
|
||
if (empty($this->plugin_sign) || $this->plugin_sign != 'sale_list') {
|
||
return $this->apiReturnError('标识错误');
|
||
}
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
//门店管理员
|
||
if ($this->user_type == 2 && $this->store_type == 1) {
|
||
if (empty($this->store_id)) {
|
||
return $this->apiReturnError('门店ID不能为空');
|
||
}
|
||
}
|
||
//管理员
|
||
|
||
if ($this->visit_type == 2) {
|
||
if ($this->user_type == 8) {
|
||
if (empty($this->store_id)) {
|
||
return $this->apiReturnError('门店ID不能为空');
|
||
}
|
||
}
|
||
}
|
||
$query = Order::find()->alias('o')
|
||
->join('left join', ['od' => OrderDetail::tableName()], 'o.id = od.order_id')
|
||
->join('left join', ['user' => User::tableName()], 'o.user_id = user.id')
|
||
->join('left join', ['ball' => BallCart::tableName()], 'od.goods_id = ball.ball_number')
|
||
->join('left join', ['mark' => BallMark::tableName()], 'ball.mark_id = mark.id')
|
||
->join('left join', ['os' => OrderSale::tableName()], 'os.order_id = o.id')
|
||
->andWhere([
|
||
// 'o.is_sale' => 0,
|
||
// 'o.status' => 3,
|
||
'o.is_delete' => 0,
|
||
'od.is_delete' => 0,
|
||
])->andWhere(['IN', 'o.status', [0, 3]])
|
||
->andFilterWhere(['o.id' => $this->order_id])
|
||
->andFilterWhere(['o.store_id' => $this->store_id]);
|
||
|
||
//用户售后订单列表
|
||
if ($this->visit_type == 1) {
|
||
$query = $query->andWhere(['o.user_id' => $this->user_id]);
|
||
}
|
||
|
||
$query = $query->select('o.id,o.is_sale,o.created_at,os.status as sale_stauts,os.merchant_remark,os.remark,o.confirm_time,ball.cover_pic,ball.ball_number,od.goods_attr_info,user.real_name,user.nickname,user.mobile_phone,od.goods_id,mark.name as mark_name');
|
||
// ->groupBy('o.id');
|
||
$count_query = clone $query;
|
||
$count = $count_query->count();
|
||
|
||
if (empty($this->limit)) {
|
||
$this->limit = 10;
|
||
}
|
||
if (empty($this->page)) {
|
||
$this->page = 1;
|
||
}
|
||
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
|
||
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['os.id' => SORT_DESC])->asArray()->all();
|
||
|
||
foreach ($list as $key => $value) {
|
||
$find = OrderSale::find()->andWhere([
|
||
'order_id' => $value['id'],
|
||
'is_delete' => 0,
|
||
])->orderBy(['id' => SORT_DESC])->asArray()->one();
|
||
if (empty($find)) {
|
||
unset($list[$key]);
|
||
continue;
|
||
}
|
||
$list[$key] = array_merge($value, $find);
|
||
$list[$key]['created_at'] = date('Y-m-d H:i:s', $find['created_at']);
|
||
$list[$key]['status_time'] = date('Y-m-d H:i:s', $find['status_time']);
|
||
$list[$key]['user_name'] = $value['real_name'] ? $value['real_name'] : $value['nickname'];
|
||
$list[$key]['pic_urls'] = SiteHelper::getFullUrl($value['pic_urls']);
|
||
$list[$key]['cover_pic'] = SiteHelper::getFullUrl($value['cover_pic']);
|
||
|
||
$list[$key]['begin_time'] = date('Y-m-d H:i:s', $value['created_at']);
|
||
$list[$key]['end_time'] = date('Y-m-d H:i:s', $value['confirm_time']);
|
||
$time_often = $value['confirm_time'] - $value['created_at'];
|
||
$list[$key]['time_content'] = GetDistance::mdate($time_often);
|
||
|
||
if ($value['sale_stauts'] == 1) {
|
||
$temp_data['sale_order_no'][] = $list[$key];
|
||
} else {
|
||
$temp_data['sale_order_ok'][] = $list[$key];
|
||
}
|
||
}
|
||
|
||
if (isset($temp_data) && empty($this->order_id)) {
|
||
$list = [];
|
||
$list = array_merge($list, $temp_data['sale_order_no'] ?? []);
|
||
$list = array_merge($list, $temp_data['sale_order_ok'] ?? []);
|
||
// $list = array_merge($temp_data, $list);
|
||
}
|
||
|
||
|
||
$this->pageCount = $pagination->pageCount;
|
||
$end_flag = $this->page >= $pagination->pageCount ? true : false;
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $list,
|
||
'count' => $count,
|
||
'page_size' => $this->limit,
|
||
'page_count' => $this->pageCount,
|
||
'page_no' => $this->page,
|
||
'end_flag' => $end_flag,
|
||
];
|
||
}
|
||
|
||
//校验是否需要缴纳押金
|
||
public function check_deposit()
|
||
{
|
||
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
$deposit_money = \Yii::$app->params['deposit_money'];
|
||
if ($this->plugin_sign == SysConst::$cxPluginSceneDeposit) {
|
||
//订单类型为缴纳押金时判断支付方式为支付分时报错
|
||
if ($this->pay_type == SysConst::$cxPayTypeWxpayPoints) {
|
||
return $this->apiReturnError('缴纳押金不能使用支付分');
|
||
}
|
||
//订单类型为缴纳押金时返回【不用缴纳】
|
||
return $this->apiReturnSuccess('ok', ['is_deposit', false, 'deposit_money' => $deposit_money]);
|
||
}
|
||
|
||
//订单类型为球车租赁且支付方式为支付分时返回【不用缴纳】
|
||
if ($this->plugin_sign == SysConst::$cxPluginSceneBallCart && $this->pay_type == SysConst::$cxPayTypeEnWxpayPoints) {
|
||
return $this->apiReturnSuccess('ok', ['is_deposit', false, 'deposit_money' => $deposit_money]);
|
||
}
|
||
|
||
//用户为门店管理员或门店服务员时不用缴纳押金
|
||
if ($this->user_type == User::TYPE_STORE) {
|
||
$is_store_user = StoreUser::find()->where(['user_id' => $this->user_id, 'store_id' => $this->store_id, 'user_type' => [1, 2], 'is_delete' => 0, 'status' => 0])->exists();
|
||
if ($is_store_user) {
|
||
return $this->apiReturnSuccess('ok', ['is_deposit', false, 'deposit_money' => $deposit_money]);
|
||
}
|
||
}
|
||
//查询未退款的押金订单是否存在
|
||
$is_order = Order::find()->where(['user_id' => $this->user_id, 'pay_type' => SysConst::$cxPayTypeWxpay, 'plugin_sign' => SysConst::$cxPluginSceneDeposit, 'is_pay' => 1, 'is_delete' => 0, 'status' => 1])->exists();
|
||
if ($is_order) {
|
||
return $this->apiReturnSuccess('ok', ['is_deposit', false, 'deposit_money' => $deposit_money]);
|
||
}
|
||
return $this->apiReturnSuccess('ok', ['is_deposit', true, 'deposit_money' => $deposit_money]);
|
||
}
|
||
|
||
//创建押金订单
|
||
public function createOrderDeposit()
|
||
{
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
if (empty($this->plugin_sign) || $this->plugin_sign != SysConst::$cxPluginSceneDeposit) {
|
||
return $this->apiReturnError('订单类型错误');
|
||
}
|
||
if ($this->plugin_sign == SysConst::$cxPluginSceneDeposit && $this->pay_type == SysConst::$cxPayTypeWxpayPoints) {
|
||
//订单类型为缴纳押金时判断支付方式为支付分时报错
|
||
return $this->apiReturnError('缴纳押金不能使用支付分');
|
||
}
|
||
|
||
$deposit_money = \Yii::$app->params['deposit_money'];
|
||
if ($this->price != $deposit_money) {
|
||
return $this->apiReturnError('订单金额异常');
|
||
}
|
||
|
||
try {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
|
||
//订单类型
|
||
$orderType = UniqueOrderNo::ORDER_TYPE_DEPOSIT;
|
||
$goods_info = '缴纳押金';
|
||
|
||
$order = new Order();
|
||
$order->cx_mch_id = $this->cx_mch_id;
|
||
$order->user_id = $this->user_id;
|
||
$order->order_no = UniqueOrderNo::generate($orderType, Order::class, 24, 1, 3, 'order_no');
|
||
$order->total_price = (int)$deposit_money; //总金额
|
||
$order->store_id = $this->store_id;
|
||
|
||
$order->coupon_discount_price = 0;
|
||
$order->use_user_coupon_id = 0;
|
||
$order->total_pay_price = (int)$deposit_money; //实际支付
|
||
$order->total_goods_price = (int)$deposit_money; //订单商品总金额(优惠后)
|
||
$order->total_goods_original_price = (int)$deposit_money; //订单商品总金额(优惠前)
|
||
$order->express_original_price = 0;
|
||
$order->express_price = 0;
|
||
|
||
$order->is_pay = 0;
|
||
$order->pay_type = SysConst::$cxPayTypeWxpay;
|
||
$order->plugin_sign = SysConst::$cxPluginSceneDeposit;
|
||
$order->created_at = time();
|
||
if (!$order->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误', $this->getModelError($order));
|
||
}
|
||
|
||
$orderdetail = new OrderDetail();
|
||
$orderdetail->cx_mch_id = $this->cx_mch_id;
|
||
$orderdetail->order_id = $order->id;
|
||
$orderdetail->goods_id = $this->ball_number;
|
||
$orderdetail->num = 1;
|
||
$orderdetail->unit_price = (int)$deposit_money; //实际支付
|
||
$orderdetail->total_original_price = (int)$deposit_money;
|
||
$orderdetail->total_price = (int)$deposit_money;
|
||
$orderdetail->plugin_sign = SysConst::$cxPluginSceneDeposit;
|
||
$orderdetail->goods_attr_info = $goods_info;
|
||
$orderdetail->created_at = time();
|
||
if (!$orderdetail->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误', $this->getModelError($orderdetail));
|
||
}
|
||
|
||
$t->commit();
|
||
return $this->apiReturnSuccess('ok', ['id' => $order->id]);
|
||
} catch (Exception $exception) {
|
||
$t->rollBack();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '创建订单失败' . $exception->getMessage(),
|
||
'data' => null
|
||
];
|
||
}
|
||
|
||
}
|
||
|
||
//押金订单列表
|
||
public function search_order_deposit()
|
||
{
|
||
|
||
$query = Order::find()->alias('o')
|
||
->select('o.id as order_id,o.is_sale,o.total_pay_price as total_price,rt.goods_id as ball_number,ball.cover_pic,
|
||
o.is_pay,o.store_id,o.plugin_sign,s.phone,o.confirm_time,o.created_at,o.updated_at,o.pay_time,o.cancel_status,o.status,s.name as store_name,o.store_id,o.is_confirm')
|
||
->groupBy('o.id')
|
||
->leftJoin(['rt' => OrderDetail::tableName()], 'o.id=rt.order_id')
|
||
->leftJoin(['ball' => BallCart::tableName()], 'rt.goods_id=ball.ball_number')
|
||
->leftJoin(['s' => Store::tableName()], 'o.store_id=s.id')
|
||
->where([
|
||
'o.is_delete' => 0,
|
||
'rt.is_delete' => 0,
|
||
'ball.is_delete' => 0,
|
||
's.is_delete' => 0,
|
||
'o.user_id' => $this->user_id,
|
||
'o.is_pay' => $this->is_pay,
|
||
'plugin_sign' => SysConst::$cxPluginSceneDeposit
|
||
]);
|
||
|
||
$count = $query->count();
|
||
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
|
||
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['ball.id' => SORT_DESC])->asArray()->all();
|
||
foreach ($list as $index => &$item) {
|
||
if ($item['status'] == 0) {
|
||
$order_status = 0; //球车订单已关闭。押金订单已触发还车按钮
|
||
if ($item['is_confirm'] == 0) { //判断退款
|
||
$deposit_title = '待退款'; //未发起
|
||
} elseif ($item['is_confirm'] == 1) {
|
||
$deposit_title = '退款成功';
|
||
} elseif ($item['is_confirm'] == 2) {
|
||
$deposit_title = '退款关闭';
|
||
} elseif ($item['is_confirm'] == 3) {
|
||
$deposit_title = '退款异常';
|
||
}
|
||
} elseif ($item['is_pay'] == 1) {
|
||
$order_status = 1; //已支付
|
||
}
|
||
|
||
$item['order_status'] = $order_status;
|
||
$item['deposit_title'] = $deposit_title;
|
||
$item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
|
||
$item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
|
||
}
|
||
$end_flag = $this->page >= $pagination->pageCount ? true : false;
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $list,
|
||
'count' => $count,
|
||
'page_size' => $this->limit,
|
||
'page_count' => $pagination->pageCount,
|
||
'page_no' => $this->page,
|
||
'end_flag' => $end_flag
|
||
];
|
||
}
|
||
|
||
//生成支付分订单
|
||
public function createOrderPoints($order_no, $amount, $ball_number)
|
||
{
|
||
try {
|
||
|
||
$url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/notify/payment/wxpay-points';
|
||
|
||
|
||
$time = time();
|
||
$start_time = date('YmdHis', $time);
|
||
|
||
$param = [
|
||
'out_order_no' => $order_no,//商户系统内部服务订单号
|
||
'service_id' => 'service_id',//服务ID
|
||
'service_introduction' => '球车租赁',//用于介绍本订单所提供的服务 ,当参数长度超过20个字符时,报错处理。示例值:某某酒店
|
||
'time_range' => [
|
||
'start_time' => $start_time,//服务开始时间
|
||
],
|
||
'risk_fund' => [
|
||
'name' => 'ESTIMATE_ORDER_COST',//风险金名称ESTIMATE_ORDER_COST预估订单费用
|
||
'amount' => $amount,//风险金额
|
||
'description' => '球车编号为' . $ball_number . '的租赁费用',//风险说明
|
||
],
|
||
'notify_url' => $url,//商户接收用户确认订单和付款成功回调通知的地址。
|
||
];
|
||
|
||
$plugin = new PluginService();
|
||
$wxmpService = $plugin->getWxmpService(0);
|
||
$res = $wxmpService->pay->serviceOrder($param, time());
|
||
|
||
|
||
} catch (\Exception $e) {
|
||
return $this->apiReturnError($e->getMessage());
|
||
}
|
||
|
||
}
|
||
|
||
//关闭订单-微信支付订单
|
||
public function closeOrder($order_id)
|
||
{
|
||
if (empty($order_id)) {
|
||
return $this->apiReturnError('参数异常');
|
||
}
|
||
$user_id = \Yii::$app->user->identity->id;
|
||
$paymentOrderUnion = PaymentOrderUnion::findOne(['id' => $order_id, 'user_id' => $user_id, 'is_pay' => 0]);
|
||
if ($paymentOrderUnion == null) {
|
||
return $this->apiReturnError('订单无效');
|
||
}
|
||
|
||
$paymentOrder = PaymentOrder::findOne(['payment_order_union_id' => $paymentOrderUnion->id]);
|
||
if ($paymentOrder == null) {
|
||
return $this->apiReturnError('订单无效');
|
||
}
|
||
|
||
$order = Order::find()->alias('o')
|
||
->select('o.id,od.goods_id')
|
||
->leftJoin(['od' => OrderDetail::tableName()], 'o.id=od.order_id')
|
||
->where(['o.order_no' => $paymentOrder->order_no, 'o.is_pay' => 0])
|
||
->asArray()
|
||
->one();
|
||
if (empty($order)) {
|
||
return $this->apiReturnError('订单无效');
|
||
}
|
||
|
||
$ball_cart = BallCart::find()->where(['ball_number' => $order['goods_id'], 'is_delete' => 0, 'status' => 3])->orderBy(['id' => SORT_DESC])->one();
|
||
if ($ball_cart == null) {
|
||
return $this->apiReturnSuccess('订单无需变更');
|
||
}
|
||
$ball_cart->status = 0;
|
||
$ball_cart->updated_at = time();
|
||
if (!$ball_cart->save()) {
|
||
return $this->getModelError($ball_cart);
|
||
}
|
||
return $this->apiReturnSuccess('ok');
|
||
}
|
||
} |