diff --git a/app/Command/GrantUserCouponCommand.php b/app/Command/GrantUserCouponCommand.php new file mode 100644 index 0000000..db4cbbc --- /dev/null +++ b/app/Command/GrantUserCouponCommand.php @@ -0,0 +1,87 @@ +setDescription('发放用户优惠卷'); + } + + public function handle() + { + $this->line("开始"); + + // 获取所有状态正常用户 + $params = array(); + $params['user_type'] = 1; + $params['user_status'] = 1; + $users = User::getList($params); + if (empty($users)){ + $this->line("结束:无可执行用户"); + return; + } + + // 获取需要发放的优惠卷数据 + $params = array(); + $params['coupon_id'] = 2; + $coupon = Coupon::getOne($params); + if (empty($coupon)){ + $this->line("结束:无可执行优惠卷"); + return; + } + + foreach ($users as $user){ + // 获取用户优惠卷 + $params = array(); + $params['coupon_id'] = $coupon['coupon_id']; + $params['user_id'] = $user['user_id']; + $user_coupon = UserCoupon::getOne($params); + if (!empty($user_coupon)){ + $this->line("用户已拥有该优惠卷"); + continue; + } + + Db::beginTransaction(); + + try { + // 发放优惠卷 + $couponService = new CouponService(); + $res = $couponService->GrantUserCoupon($coupon['coupon_id'],$user['user_id']); + if (!$res){ + Db::rollBack(); + $this->line("优惠卷发放失败"); + continue; + } + }catch (\Throwable $e){ + Db::rollBack(); + $this->line($e->getMessage()); + } + + Db::commit(); + + $this->line("用户" . $user['user_name'] . "执行完毕"); + } + + $this->line("发放完毕"); + } +} diff --git a/app/Model/User.php b/app/Model/User.php index 9a19c85..40bc14d 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -6,6 +6,7 @@ namespace App\Model; use Hyperf\Database\Model\Builder; +use Hyperf\Database\Model\Collection; use Hyperf\Snowflake\Concern\Snowflake; /** @@ -58,6 +59,17 @@ class User extends Model return $result; } + /** + * 获取用户信息-多条 + * @param array $params + * @param array $fields + * @return Collection|array + */ + public static function getList(array $params, array $fields = ['*']): Collection|array + { + return self::where($params)->get($fields); + } + /** * 新增用户-批量 * @param array $data 新增数据 diff --git a/app/Model/UserCoupon.php b/app/Model/UserCoupon.php index 58ecb9e..4fcd438 100644 --- a/app/Model/UserCoupon.php +++ b/app/Model/UserCoupon.php @@ -155,4 +155,33 @@ class UserCoupon extends Model ->groupBy("coupon_id") ->get($fields); } + + /** + * 获取患者某一类型下的可用优惠卷 + * @param string|int $user_id + * @param int $distribution_object + * @param array $fields + * @return Collection|array + */ + public static function getUserObjectTypeCoupon(string|int $user_id,int $distribution_object,array $fields = ['*']): Collection|array + { + $params = array(); + $params[] = ['user_id', '=', $user_id]; + $params[] = ['user_coupon_status', '=', 0];// 状态(0:未使用 1:已使用 3:已过期) + $params[] = ['valid_start_time', '<', date('Y-m-d H:i:s', time())]; // 有效使用时间 + $params[] = ['valid_end_time', '>', date('Y-m-d H:i:s', time())]; // 过期使用时间 + + return self::with(['Coupon']) + ->whereHas('Coupon', function ($query) use ($distribution_object) { + $query->where("coupon_client",1) + ->where("coupon_status",1) + ->where(function ($query) use ($distribution_object) { + $query->orwhere("distribution_object",$distribution_object); + }); + }) + ->where($params) + ->groupBy("coupon_id") + ->get($fields); + } + } diff --git a/app/Services/OrderProductService.php b/app/Services/OrderProductService.php index 72cd6ed..0146835 100644 --- a/app/Services/OrderProductService.php +++ b/app/Services/OrderProductService.php @@ -279,70 +279,47 @@ class OrderProductService extends BaseService $product_refund_no = $generator->generate(); // 检测订单金额 - if ($order_product['payment_amount_total'] > 0){ - // 发起退款 - $WechatPay = new WechatPay(1, 2); - - $options = array(); - $options['transaction_id'] = $order_product['escrow_trade_no']; - $options['out_refund_no'] = (string)$product_refund_no; - $options['reason'] = $refund_reason; - $options['amount'] = [ - 'refund' => (int)($order_product['payment_amount_total'] * 100), - 'total' => (int)($order_product['payment_amount_total'] * 100), - 'currency' => "CNY", - ]; - - $result = $WechatPay->refund($options); - - // 处理订单退款状态 - // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) - $success_time = ""; - if ($result['status'] == "SUCCESS") { - // 退款成功 - $refund_status = 3; - $success_time = $result['success_time']; - } elseif ($result['status'] == "CLOSED") { - // 退款关闭 - $refund_status = 5; - } elseif ($result['status'] == "PROCESSING") { - // 退款处理中 - $refund_status = 2; - } elseif ($result['status'] == "ABNORMAL") { - // 退款异常,此情况不处理,进行短信通知 - throw new BusinessException("订单退款状态异常"); - } else { - throw new BusinessException("订单退款状态错误"); - } - - $refund_id = $result['refund_id']; - - }else{ - // 模拟退款 - $refund_status = 3; - $refund_id = "模拟退款:" . $generator->generate(); - $success_time = date("Y-m-d H:i:s", time()); - - // 模拟退款时手动退还优惠卷 - if (!empty($order_product['coupon_amount_total']) && $order_product['coupon_amount_total'] > 0) { - // 获取该订单全部优惠卷数据 - $params = array(); - $params['order_product_id'] = $order_product['order_product_id']; - $order_product_coupons = OrderProductCoupon::getList($params); - if (!empty($order_product_coupons)){ - $userCouponService = new UserCouponService(); - foreach ($order_product_coupons as $order_product_coupon){ - // 退还优惠卷 - $userCouponService->returnUserCoupon($order_product_coupon['user_coupon_id']); - - // 发送站内消息-优惠卷退还 - $MessagePush = new MessagePush($user_patient['user_id']); - $MessagePush->patientRefundCoupon($order_product_coupon['coupon_name']); - } - } - } + if ($order_product['payment_amount_total'] <= 0){ + throw new BusinessException("订单金额错误"); } + // 发起退款 + $WechatPay = new WechatPay(1, 2); + + $options = array(); + $options['transaction_id'] = $order_product['escrow_trade_no']; + $options['out_refund_no'] = (string)$product_refund_no; + $options['reason'] = $refund_reason; + $options['amount'] = [ + 'refund' => (int)($order_product['payment_amount_total'] * 100), + 'total' => (int)($order_product['payment_amount_total'] * 100), + 'currency' => "CNY", + ]; + + $result = $WechatPay->refund($options); + + // 处理订单退款状态 + // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + $success_time = ""; + if ($result['status'] == "SUCCESS") { + // 退款成功 + $refund_status = 3; + $success_time = $result['success_time']; + } elseif ($result['status'] == "CLOSED") { + // 退款关闭 + $refund_status = 5; + } elseif ($result['status'] == "PROCESSING") { + // 退款处理中 + $refund_status = 2; + } elseif ($result['status'] == "ABNORMAL") { + // 退款异常,此情况不处理,进行短信通知 + throw new BusinessException("订单退款状态异常"); + } else { + throw new BusinessException("订单退款状态错误"); + } + + $refund_id = $result['refund_id']; + // 新增退款表 $data = array(); $data['patient_id'] = $order_product['patient_id']; diff --git a/app/Services/UserPatientService.php b/app/Services/UserPatientService.php index 6363d9f..8fb9e27 100644 --- a/app/Services/UserPatientService.php +++ b/app/Services/UserPatientService.php @@ -45,7 +45,7 @@ class UserPatientService extends BaseService } // 获取福利数据 - $coupon = $this->getPatientAvailableCouponList($user_patient['user_id'],1); + $coupon = UserCoupon::getUserObjectTypeCoupon($user_patient['user_id'],2); // 获取问诊数量-待支付 $InquiryService = new InquiryService();