|
@@ -68,4 +68,102 @@ class Orders extends Model
|
|
|
return 'kldd'. str_pad($id, 9, '0', STR_PAD_LEFT);;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 取消订单
|
|
|
+ *
|
|
|
+ * @param int $id 订单ID
|
|
|
+ * @param int $status 订单状态
|
|
|
+ * @param OrdersProduct $OrdersProduct 子订单模型
|
|
|
+ */
|
|
|
+ public function setOrderStatus($id,$status,$OrdersProduct){
|
|
|
+ // 查询数据
|
|
|
+ $result = $this->edit($id,['status'=>$status]);
|
|
|
+ // 提示新增失败
|
|
|
+ if( !$result ) {
|
|
|
+ // 提示信息
|
|
|
+ return ['error'=>'订单修改失败'];
|
|
|
+ }
|
|
|
+ // 查询数据
|
|
|
+ $result = $OrdersProduct->query()->where([['order_id','=',$id]])->update(['status'=>$status,'update_time'=>time()]);
|
|
|
+ // 提示新增失败
|
|
|
+ if( !$result ) {
|
|
|
+ // 提示信息
|
|
|
+ return ['error'=>'子订单修改失败'];json_send(['code'=>'error','msg'=>'取消失败','data'=>['error'=>'']]);
|
|
|
+ }
|
|
|
+ // 进行操作
|
|
|
+ return ['success'=>'成功'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消优惠券关联订单
|
|
|
+ *
|
|
|
+ * @param int $orderTime 订单时间
|
|
|
+ * @param int $uid 客户ID
|
|
|
+ * @param OrdersProduct $OrdersProduct 子订单模型
|
|
|
+ * @param CustomScore $CustomScore 客户积分
|
|
|
+ */
|
|
|
+ public function cancelRelate($orderTime,$uid,$OrdersProduct,$CustomScore){
|
|
|
+ // 模型实例
|
|
|
+ $CouponRewardRule = new \App\Models\CouponRewardRule();
|
|
|
+ $CustomCoupon = new \App\Models\CustomCoupon();
|
|
|
+ $CouponRewardProduct = new \App\Models\CouponRewardProduct();
|
|
|
+ // 获取配置列表
|
|
|
+ $ruleList = $CouponRewardRule->query()->where([['start_time','<=',$orderTime],['end_time','>=',$orderTime]])->get(['id','name','std_num','remove_custom','city_ids','status','start_time','end_time','coupon_id']);
|
|
|
+ // 如果有活动列表
|
|
|
+ foreach ($ruleList as $value) {
|
|
|
+ // 查询客户是否有该优惠券
|
|
|
+ $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['id','status','order_id']);
|
|
|
+ // 没有优惠券,跳过
|
|
|
+ if( !$havaCoupon ) continue;
|
|
|
+ // 通过配置ID获取对应的商品范围
|
|
|
+ $productList = $CouponRewardProduct->getListByRule($value['id']);
|
|
|
+ // 获取客户 规定时段内订单的商品ID以及购买数量
|
|
|
+ $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->get(['product_id','buy_num'])->toArray();
|
|
|
+ // 计算商品总量
|
|
|
+ $total = 0;
|
|
|
+ // 循环商品范围
|
|
|
+ foreach ($productList as $scope) {
|
|
|
+ // 循环订单产品
|
|
|
+ foreach ($orderList as $order) {
|
|
|
+ // 如果产品不相等
|
|
|
+ if( $scope['product_id'] != $order['product_id'] ) continue;
|
|
|
+ // 相等的计算总量
|
|
|
+ $total += $scope['product_units'] * $order['buy_num'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 判断总数还是达标,跳过
|
|
|
+ if( $total >= $value['std_num'] ) continue;
|
|
|
+ // 剩余的达不到条件,如果有优惠券,未使用的的作废
|
|
|
+ if( $havaCoupon['status'] == 0 ) {
|
|
|
+ // 作废
|
|
|
+ $CustomCoupon->edit($havaCoupon['id'],['status'=>4]);
|
|
|
+ // 作废成功
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 优惠券有订单信息
|
|
|
+ if( $havaCoupon['order_id'] ) {
|
|
|
+ // 获取优惠券关联的订单
|
|
|
+ $oldData = $this->query()->where([['id','=',$havaCoupon['order_id']],['custom_uid','=',$uid]])->first(['id','order_score','status','custom_uid','insert_time']);
|
|
|
+ // 如果订单不存在
|
|
|
+ if( !$oldData ) continue;
|
|
|
+ // 如果订单状态已取消
|
|
|
+ if( $oldData['status'] == 4 ) continue;
|
|
|
+ // 取消订单
|
|
|
+ $result = $this->setOrderStatus($havaCoupon['order_id'],4,$OrdersProduct);
|
|
|
+ // 取消失败
|
|
|
+ if( isset($result['error']) ) return ['error'=>$result['error']];
|
|
|
+ // 撤回订单
|
|
|
+ if( $oldData['order_score'] > 0 ) {
|
|
|
+ // 如果扣减失败
|
|
|
+ $result = $CustomScore->trade($oldData['custom_uid'],$oldData['id'],($oldData['order_score']*-1),6,1);
|
|
|
+ // 提示新增失败
|
|
|
+ if( isset($result['error']) ) return ['error'=>$result['error']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 默认成功
|
|
|
+ return ['success'=>'操作成功'];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|