This commit is contained in:
wucongxing 2023-11-09 16:07:23 +08:00
parent 39f88f3e1b
commit 6c3970ec5c
5 changed files with 168 additions and 63 deletions

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Model\Coupon;
use App\Model\User;
use App\Model\UserCoupon;
use App\Services\CouponService;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerInterface;
#[Command]
class GrantUserCouponCommand extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('GrantUserCoupon:command');
}
public function configure()
{
parent::configure();
$this->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("发放完毕");
}
}

View File

@ -6,6 +6,7 @@ namespace App\Model;
use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake; use Hyperf\Snowflake\Concern\Snowflake;
/** /**
@ -58,6 +59,17 @@ class User extends Model
return $result; 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 新增数据 * @param array $data 新增数据

View File

@ -155,4 +155,33 @@ class UserCoupon extends Model
->groupBy("coupon_id") ->groupBy("coupon_id")
->get($fields); ->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);
}
} }

View File

@ -279,70 +279,47 @@ class OrderProductService extends BaseService
$product_refund_no = $generator->generate(); $product_refund_no = $generator->generate();
// 检测订单金额 // 检测订单金额
if ($order_product['payment_amount_total'] > 0){ 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'];
}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']);
}
}
}
} }
// 发起退款
$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 = array();
$data['patient_id'] = $order_product['patient_id']; $data['patient_id'] = $order_product['patient_id'];

View File

@ -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(); $InquiryService = new InquiryService();