简化了患者端药品微信支付回调的再次发放优惠卷的方法调用、增加了发放优惠卷时数量的判断、增加了再次发放订单优惠卷的方法
This commit is contained in:
parent
80e2071efc
commit
7a793a61e7
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Amqp\Consumer;
|
namespace App\Amqp\Consumer;
|
||||||
|
|
||||||
use App\Model\Coupon;
|
use App\Model\Coupon;
|
||||||
|
use App\Model\CouponGrant;
|
||||||
use App\Services\CouponService;
|
use App\Services\CouponService;
|
||||||
use App\Utils\Log;
|
use App\Utils\Log;
|
||||||
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
||||||
@ -17,7 +18,7 @@ use Hyperf\DbConnection\Db;
|
|||||||
use PhpAmqpLib\Message\AMQPMessage;
|
use PhpAmqpLib\Message\AMQPMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 再次发放用户优惠卷
|
* 发放用户优惠卷
|
||||||
*/
|
*/
|
||||||
#[Consumer(nums: 1)]
|
#[Consumer(nums: 1)]
|
||||||
class GrantUserCouponDelayDirectConsumer extends ConsumerMessage
|
class GrantUserCouponDelayDirectConsumer extends ConsumerMessage
|
||||||
@ -37,20 +38,28 @@ class GrantUserCouponDelayDirectConsumer extends ConsumerMessage
|
|||||||
{
|
{
|
||||||
Log::getInstance("queue-GrantUserCoupon")->info("开始执行 延迟发放用户优惠卷 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
Log::getInstance("queue-GrantUserCoupon")->info("开始执行 延迟发放用户优惠卷 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
// 获取优惠卷数据
|
// 检测入参参数
|
||||||
$params = array();
|
if (empty($data['coupon_id'])) {
|
||||||
$params['coupon_id'] = $data["coupon_id"];
|
Db::rollBack();
|
||||||
$coupon = Coupon::getOne($params);
|
Log::getInstance("queue-GrantUserCoupon")->error("参数错误");
|
||||||
if (empty($coupon)){
|
|
||||||
Log::getInstance("queue-GrantUserCoupon")->error("无优惠卷数据");
|
|
||||||
return Result::DROP;
|
return Result::DROP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Db::beginTransaction();
|
if (empty($data['user_id'])) {
|
||||||
|
Db::rollBack();
|
||||||
|
Log::getInstance("queue-GrantUserCoupon")->error("参数错误");
|
||||||
|
return Result::DROP;
|
||||||
|
}
|
||||||
|
|
||||||
|
$grant_quantity = 1;
|
||||||
|
if (!empty($data['grant_quantity'])) {
|
||||||
|
$grant_quantity = $data['grant_quantity'];
|
||||||
|
}
|
||||||
|
|
||||||
|
Db::beginTransaction();
|
||||||
try {
|
try {
|
||||||
$couponService = new CouponService();
|
$couponService = new CouponService();
|
||||||
$res = $couponService->GrantUserCoupon($data["coupon_id"],$data['user_id']);
|
$res = $couponService->GrantUserCoupon($data["coupon_id"],$data['user_id'],$grant_quantity);
|
||||||
if (!$res){
|
if (!$res){
|
||||||
// 发放失败
|
// 发放失败
|
||||||
Db::rollBack();
|
Db::rollBack();
|
||||||
|
|||||||
@ -10,7 +10,7 @@ use Hyperf\Amqp\Message\ProducerMessage;
|
|||||||
use Hyperf\Amqp\Message\Type;
|
use Hyperf\Amqp\Message\Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 再次发放用户优惠卷
|
* 发放用户优惠卷
|
||||||
*/
|
*/
|
||||||
#[Producer]
|
#[Producer]
|
||||||
class GrantUserCouponDelayDirectProducer extends ProducerMessage
|
class GrantUserCouponDelayDirectProducer extends ProducerMessage
|
||||||
|
|||||||
@ -509,41 +509,10 @@ class CallBackController extends AbstractController
|
|||||||
try {
|
try {
|
||||||
// 再次发放优惠卷数据
|
// 再次发放优惠卷数据
|
||||||
if ($order_product['coupon_amount_total'] > 0){
|
if ($order_product['coupon_amount_total'] > 0){
|
||||||
// 获取药品订单优惠卷数据
|
$couponService = new CouponService();
|
||||||
$params = array();
|
$res = $couponService->againGrantOrderCoupon($order_product['order_id']);
|
||||||
$params['order_product_id'] = $order_product['order_product_id'];
|
if (!$res){
|
||||||
$order_product_coupons = OrderProductCoupon::getList($params);
|
Log::getInstance("CallBack-wxPayProductSuccess")->error("再次发放优惠卷失败");
|
||||||
if (!empty($order_product_coupons)){
|
|
||||||
foreach ($order_product_coupons as $order_product_coupon){
|
|
||||||
// 获取用户优惠卷数据
|
|
||||||
$params = array();
|
|
||||||
$params['user_coupon_id'] = $order_product_coupon['user_coupon_id'];
|
|
||||||
$user_coupon = UserCoupon::getOne($params);
|
|
||||||
if (!empty($user_coupon)){
|
|
||||||
// 获取优惠卷数据
|
|
||||||
$params = array();
|
|
||||||
$params['coupon_id'] = $user_coupon['coupon_id'];
|
|
||||||
$coupon = Coupon::getOne($params);
|
|
||||||
if (!empty($coupon)){
|
|
||||||
if ($coupon['reissue_interval_days'] > 0){
|
|
||||||
$data = array();
|
|
||||||
$data['coupon_id'] = $coupon['coupon_id'];
|
|
||||||
$data['user_id'] = $user_coupon['user_id'];
|
|
||||||
|
|
||||||
$time = $coupon['reissue_interval_days'] * 24 * 60 * 60 * 1000;
|
|
||||||
$message = new GrantUserCouponDelayDirectProducer($data);
|
|
||||||
$message->setDelayMs($time);
|
|
||||||
$producer = $this->container->get(Producer::class);
|
|
||||||
$res = $producer->produce($message);
|
|
||||||
if (!$res) {
|
|
||||||
Log::getInstance("CallBack-wxPayProductSuccess")->error("再次发放优惠卷失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
Log::getInstance("CallBack-wxPayProductSuccess")->info("再次发放优惠卷成功");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (\Throwable $e){
|
}catch (\Throwable $e){
|
||||||
|
|||||||
93
app/Model/CouponGrant.php
Normal file
93
app/Model/CouponGrant.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Model;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Hyperf\Database\Model\Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $grant_id 主键id
|
||||||
|
* @property int $coupon_id 优惠卷id
|
||||||
|
* @property int $grant_type 发放类型(1:具体用户 2:未拥有用户)
|
||||||
|
* @property int $user_id 用户id(发放类型为具体用户时存在)
|
||||||
|
* @property int $total_quantity 目标发放数量
|
||||||
|
* @property int $grant_quantity 已发放数量
|
||||||
|
* @property int $grant_result 发放结果(1:成功 2:发放中 3:失败)
|
||||||
|
* @property string $stop_reason 停止原因
|
||||||
|
* @property int $admin_user_id 后台操作用户id
|
||||||
|
* @property Carbon $created_at 创建时间
|
||||||
|
* @property Carbon $updated_at 修改时间
|
||||||
|
*/
|
||||||
|
class CouponGrant extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*/
|
||||||
|
protected ?string $table = 'coupon_grant';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*/
|
||||||
|
protected array $fillable = ['grant_id', 'coupon_id', 'grant_type', 'user_id', 'total_quantity', 'grant_quantity', 'grant_result', 'stop_reason', 'admin_user_id', 'created_at', 'updated_at'];
|
||||||
|
|
||||||
|
protected string $primaryKey = "grant_id";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取信息-单条
|
||||||
|
* @param array $params
|
||||||
|
* @param array $fields
|
||||||
|
* @return object|null
|
||||||
|
*/
|
||||||
|
public static function getOne(array $params, array $fields = ['*']): object|null
|
||||||
|
{
|
||||||
|
return self::where($params)->first($fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据-多
|
||||||
|
* @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 新增数据
|
||||||
|
* @return \Hyperf\Database\Model\Model|CouponGrant
|
||||||
|
*/
|
||||||
|
public static function addCouponGrant(array $data): \Hyperf\Database\Model\Model|CouponGrant
|
||||||
|
{
|
||||||
|
return self::create($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改-批量
|
||||||
|
* @param array $params
|
||||||
|
* @param array $data
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function editCouponGrant(array $params = [], array $data = []): int
|
||||||
|
{
|
||||||
|
return self::where($params)->update($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自增
|
||||||
|
* @param array $params
|
||||||
|
* @param string $field
|
||||||
|
* @param float $numeral
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function inc(array $params,string $field,float $numeral = 1): int
|
||||||
|
{
|
||||||
|
return self::where($params)->increment($field,$numeral);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,10 +3,12 @@
|
|||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use App\Amqp\Producer\AutoCompleteInquiryDelayDirectProducer;
|
use App\Amqp\Producer\AutoCompleteInquiryDelayDirectProducer;
|
||||||
|
use App\Amqp\Producer\GrantUserCouponDelayDirectProducer;
|
||||||
use App\Amqp\Producer\UserCouponExpiredDelayDirectProducer;
|
use App\Amqp\Producer\UserCouponExpiredDelayDirectProducer;
|
||||||
use App\Amqp\Producer\UserCouponExpiredNoticeDelayDirectProducer;
|
use App\Amqp\Producer\UserCouponExpiredNoticeDelayDirectProducer;
|
||||||
use App\Constants\HttpEnumCode;
|
use App\Constants\HttpEnumCode;
|
||||||
use App\Model\Coupon;
|
use App\Model\Coupon;
|
||||||
|
use App\Model\OrderCoupon;
|
||||||
use App\Model\Popup;
|
use App\Model\Popup;
|
||||||
use App\Model\UserCoupon;
|
use App\Model\UserCoupon;
|
||||||
use App\Model\UserPatient;
|
use App\Model\UserPatient;
|
||||||
@ -25,9 +27,10 @@ class CouponService extends BaseService
|
|||||||
* 发放优惠卷
|
* 发放优惠卷
|
||||||
* @param string $coupon_id 优惠卷id
|
* @param string $coupon_id 优惠卷id
|
||||||
* @param string $user_id 用户id
|
* @param string $user_id 用户id
|
||||||
|
* @param int $grant_quantity 发放数量
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function GrantUserCoupon(string $coupon_id,string $user_id): bool
|
public function GrantUserCoupon(string $coupon_id,string $user_id,int $grant_quantity = 1): bool
|
||||||
{
|
{
|
||||||
// 获取患者数据
|
// 获取患者数据
|
||||||
$params = array();
|
$params = array();
|
||||||
@ -47,7 +50,13 @@ class CouponService extends BaseService
|
|||||||
|
|
||||||
// 判断优惠卷数量是否充足
|
// 判断优惠卷数量是否充足
|
||||||
if ($coupon['coupon_count'] <= $coupon['coupon_take_count']){
|
if ($coupon['coupon_count'] <= $coupon['coupon_take_count']){
|
||||||
return true;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 优惠卷剩余数量
|
||||||
|
$remaining_quantity = $coupon['coupon_count'] - $coupon['coupon_take_count'];
|
||||||
|
if ($remaining_quantity <= $grant_quantity){
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断用户是否已有该优惠卷
|
// 判断用户是否已有该优惠卷
|
||||||
@ -216,4 +225,66 @@ class CouponService extends BaseService
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 再次发放订单优惠卷
|
||||||
|
* @param string $order_id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function againGrantOrderCoupon(string $order_id): bool
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// 获取订单优惠卷数据
|
||||||
|
$params = array();
|
||||||
|
$params['order_id'] = $order_id;
|
||||||
|
$order_coupons = OrderCoupon::getList($params);
|
||||||
|
if (empty($order_coupons)){
|
||||||
|
// 无订单优惠卷。无需再次发放
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($order_coupons as $order_coupon) {
|
||||||
|
// 获取用户优惠卷数据
|
||||||
|
$params = array();
|
||||||
|
$params['user_coupon_id'] = $order_coupon['user_coupon_id'];
|
||||||
|
$user_coupon = UserCoupon::getOne($params);
|
||||||
|
if (empty($user_coupon)){
|
||||||
|
// 未查到患者历史优惠卷数据。无需再次发放
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取优惠卷数据
|
||||||
|
$params = array();
|
||||||
|
$params['coupon_id'] = $user_coupon['coupon_id'];
|
||||||
|
$coupon = Coupon::getOne($params);
|
||||||
|
if (empty($coupon)){
|
||||||
|
// 未查到优惠卷数据。无需再次发放
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确认收货后的再次发放间隔天数
|
||||||
|
if ($coupon['reissue_interval_days'] == 0){
|
||||||
|
// 无需再次发放
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = array();
|
||||||
|
$data['coupon_id'] = $coupon['coupon_id'];
|
||||||
|
$data['user_id'] = $user_coupon['user_id'];
|
||||||
|
|
||||||
|
$time = $coupon['reissue_interval_days'] * 24 * 60 * 60 * 1000;
|
||||||
|
$message = new GrantUserCouponDelayDirectProducer($data);
|
||||||
|
$message->setDelayMs($time);
|
||||||
|
$producer = $this->container->get(Producer::class);
|
||||||
|
$res = $producer->produce($message);
|
||||||
|
if (!$res) {
|
||||||
|
Log::getInstance("CouponService-againGrantOrderCoupon")->error("再次发放优惠卷失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (\Throwable $e){
|
||||||
|
Log::getInstance("CouponService-againGrantOrderCoupon")->error($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1181,6 +1181,15 @@ class PatientOrderService extends BaseService
|
|||||||
$order_type = $this->request->input('order_type');
|
$order_type = $this->request->input('order_type');
|
||||||
$order_no = $this->request->input('order_no');
|
$order_no = $this->request->input('order_no');
|
||||||
|
|
||||||
|
// 获取订单数据
|
||||||
|
$params = array();
|
||||||
|
$params['order_no'] = $order_no;
|
||||||
|
$order = Order::getOne($params);
|
||||||
|
if (empty($order))
|
||||||
|
{
|
||||||
|
return fail(HttpEnumCode::HTTP_ERROR, "非法订单");
|
||||||
|
}
|
||||||
|
|
||||||
Db::beginTransaction();
|
Db::beginTransaction();
|
||||||
try {
|
try {
|
||||||
switch ($order_type) {
|
switch ($order_type) {
|
||||||
@ -1347,50 +1356,6 @@ class PatientOrderService extends BaseService
|
|||||||
$params['order_no'] = $order_product['order_product_no'];
|
$params['order_no'] = $order_product['order_product_no'];
|
||||||
Order::edit($params, $data);
|
Order::edit($params, $data);
|
||||||
|
|
||||||
try {
|
|
||||||
if ($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)) {
|
|
||||||
foreach ($order_product_coupons as $order_product_coupon) {
|
|
||||||
// 获取用户优惠卷数据
|
|
||||||
$params = array();
|
|
||||||
$params['user_coupon_id'] = $order_product_coupon['user_coupon_id'];
|
|
||||||
$user_coupon = UserCoupon::getOne($params);
|
|
||||||
if (!empty($user_coupon)) {
|
|
||||||
// 获取优惠卷数据
|
|
||||||
$params = array();
|
|
||||||
$params['coupon_id'] = $user_coupon['coupon_id'];
|
|
||||||
$coupon = Coupon::getOne($params);
|
|
||||||
if (!empty($coupon)) {
|
|
||||||
if ($coupon['reissue_interval_days'] > 0) {
|
|
||||||
$data = array();
|
|
||||||
$data['coupon_id'] = $coupon['coupon_id'];
|
|
||||||
$data['user_id'] = $user_coupon['user_id'];
|
|
||||||
|
|
||||||
$time = $coupon['reissue_interval_days'] * 24 * 60 * 60 * 1000;
|
|
||||||
$message = new GrantUserCouponDelayDirectProducer($data);
|
|
||||||
$message->setDelayMs($time);
|
|
||||||
$producer = $this->container->get(Producer::class);
|
|
||||||
$res = $producer->produce($message);
|
|
||||||
if (!$res) {
|
|
||||||
Log::getInstance("PatientOrderService-addPatientOrderPay")->error("再次发放优惠卷失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
Log::getInstance("PatientOrderService-addPatientOrderPay")->info("再次发放优惠卷成功");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
// 此处异常不做处理
|
|
||||||
Log::getInstance("PatientOrderService-addPatientOrderPay")->error($e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 3: // 检测订单
|
case 3: // 检测订单
|
||||||
Db::rollBack();
|
Db::rollBack();
|
||||||
@ -1414,6 +1379,19 @@ class PatientOrderService extends BaseService
|
|||||||
return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
|
return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if ($order['coupon_amount_total'] > 0) {
|
||||||
|
$couponService = new CouponService();
|
||||||
|
$res = $couponService->againGrantOrderCoupon($order['order_id']);
|
||||||
|
if (!$res){
|
||||||
|
Log::getInstance("PatientOrderService-addPatientOrderPay")->error("再次发放优惠卷失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// 此处异常不做处理
|
||||||
|
Log::getInstance("PatientOrderService-addPatientOrderPay")->error($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user