修改优惠卷表。重写注册发放优惠卷逻辑。新增品牌model。

This commit is contained in:
wucongxing 2023-11-07 16:46:24 +08:00
parent 361cd4f33f
commit 3aed48b9a6
8 changed files with 261 additions and 143 deletions

View File

@ -87,8 +87,7 @@ class UserCouponExpiredNoticeDelayDirectConsumer extends ConsumerMessage
// 患者-优惠卷即将过期
$MessagePush = new MessagePush($user_coupon['user_id']);
$MessagePush->patientExpireCoupon($coupon['coupon_name']);
}catch (\Exception $e){
}catch (\Throwable $e){
Log::getInstance("queue-UserCouponExpiredNotice")->error("错误:" . $e->getMessage());
return Result::DROP;
}

55
app/Model/BasicBrand.php Normal file
View File

@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake;
/**
* @property int $brand_id 主键id
* @property string $brand_name 品牌名称
* @property \Carbon\Carbon $created_at 创建时间
* @property \Carbon\Carbon $updated_at 修改时间
*/
class BasicBrand extends Model
{
use Snowflake;
/**
* The table associated with the model.
*/
protected ?string $table = 'basic_brand';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['brand_id', 'brand_name', 'created_at', 'updated_at'];
protected string $primaryKey = "bank_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);
}
}

View File

@ -6,28 +6,37 @@ namespace App\Model;
use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake;
/**
* @property int $coupon_id 主键id
* @property string $coupon_id 主键id
* @property string $coupon_name 优惠卷名称
* @property string $coupon_icon 优惠卷图片
* @property int $coupon_client 使用平台1:小程序)
* @property int $coupon_type 优惠卷类型1:无门槛 2:满减)
* @property int $coupon_status 状态1:正常 2:强制失效 3:结束)
* @property int $distribution_object 发放对象1:新注册用户 2:会员 3:近期消费 4:近期购药)
* @property int $application_scope 适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
* @property int $coupon_type 优惠卷类型1:无门槛 2:满减 3:数量)
* @property int $coupon_status 状态1:正常 2:强制失效 3:结束 4:删除)
* @property int $distribution_object 发放对象1:全部用户 2:新注册用户 3:会员 4:近期消费 5:近期购药 6:存量用户)
* @property int $application_scope 适用范围1:全场通用 2:问诊 3:按品牌适用 4:按类别类别适用 5:单品使用)
* @property int $inquiry_type 关联问诊类型适用范围为问诊时存在生效1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测)
* @property int $brand_id 关联品牌id如不限制品牌此项为空
* @property int $is_mutex 是否互斥0: 1:是)互斥情况下无法和其他优惠卷同时使用
* @property int $is_display 是否展示0: 1:是)
* @property int $distribution_with_day 发放关联时间(发放对象为近期消费等类型时规定天数)
* @property int $distribution_with_day 发放关联天数(发放对象为近期消费等类型时规定天数)
* @property int $min_usable_number 单商品最小可使用数量默认为1类型为数量时使用如需限制优惠卷使用数量请填写此处
* @property int $coupon_count 发放数量
* @property int $coupon_take_count 领取数量
* @property int $coupon_ used_count 使用数量
* @property int $coupon_take_count 领取数量
* @property int $coupon_used_count 使用数量
* @property string $coupon_price 优惠卷金额
* @property string $with_amount 符合满减标准金额(优惠卷类型为满减时使用)
* @property int $valid_type 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
* @property int $valid_days 自领取之日起有效天数
* @property string $valid_start_time 开始使用时间
* @property string $valid_end_time 结束使用时间
* @property string $product_id 关联商品id逗号分隔指定商品时填入此项。
* @property int $reissue_interval_days 确认收货后的再次发放间隔天数(如果设置为 0,则表示不再次发放。当适用范围为商品时生效)
* @property int $is_reissuable_after_expire 过期之后是否允许再次发放(0: 1:)
* @property int $is_popup 是否首页弹窗0: 1:是)
* @property \Carbon\Carbon $created_at 创建时间
* @property \Carbon\Carbon $updated_at 修改时间
*/
@ -43,12 +52,12 @@ class Coupon extends Model
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['coupon_id', 'coupon_name', 'coupon_icon', 'coupon_client', 'coupon_type', 'coupon_status', 'distribution_object', 'application_scope', 'is_display', 'distribution_with_day', 'coupon_count', 'coupon_take_count', 'coupon_used_count', 'coupon_price', 'with_amount', 'valid_type', 'valid_days', 'valid_start_time', 'valid_end_time', 'created_at', 'updated_at'];
protected array $fillable = ['coupon_id', 'coupon_name', 'coupon_icon', 'coupon_client', 'coupon_type', 'coupon_status', 'distribution_object', 'application_scope', 'inquiry_type', 'brand_id', 'is_mutex', 'is_display', 'distribution_with_day', 'min_usable_number', 'coupon_count', 'coupon_take_count', 'coupon_used_count', 'coupon_price', 'with_amount', 'valid_type', 'valid_days', 'valid_start_time', 'valid_end_time', 'product_id', 'reissue_interval_days', 'is_reissuable_after_expire', 'is_popup', 'created_at', 'updated_at'];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['coupon_id' => 'string', 'coupon_client' => 'integer', 'coupon_type' => 'integer', 'coupon_status' => 'integer', 'distribution_object' => 'integer', 'application_scope' => 'integer', 'is_display' => 'integer', 'distribution_with_day' => 'integer', 'coupon_count' => 'integer', 'coupon_take_count' => 'integer', 'coupon_used_count' => 'integer', 'valid_type' => 'integer', 'valid_days' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
protected array $casts = ['coupon_id' => 'string', 'coupon_client' => 'integer', 'coupon_type' => 'integer', 'coupon_status' => 'integer', 'distribution_object' => 'integer', 'application_scope' => 'integer', 'is_display' => 'integer', 'distribution_with_day' => 'integer', 'coupon_count' => 'integer', 'coupon_take_count' => 'integer', 'coupon_used_count' => 'integer', 'valid_type' => 'integer', 'valid_days' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'is_mutex' => 'integer', 'brand_id' => 'integer', 'min_usable_number' => 'integer', 'reissue_interval_days' => 'integer', 'is_reissuable_after_expire' => 'integer', 'is_popup' => 'integer', 'inquiry_type' => 'integer'];
protected string $primaryKey = "coupon_id";
@ -67,9 +76,9 @@ class Coupon extends Model
* 获取信息-多条
* @param array $params
* @param array $fields
* @return object|null
* @return Collection|array
*/
public static function getList(array $params, array $fields = ['*']): object|null
public static function getList(array $params, array $fields = ['*']): Collection|array
{
return self::where($params)->get($fields);
}
@ -97,4 +106,13 @@ class Coupon extends Model
{
return self::where($params)->decrement($field,$numeral);
}
// 获取注册用户可领取的优惠卷列表
public static function getRegisterCouponList(): Collection|array
{
return self::where("coupon_client",1)
->where("coupon_status",1)
->whereIn("distribution_object",[1,2])
->get();
}
}

View File

@ -9,6 +9,7 @@ use App\Constants\HttpEnumCode;
use App\Model\Coupon;
use App\Model\Popup;
use App\Model\UserCoupon;
use App\Model\UserPatient;
use App\Utils\Log;
use Hyperf\Amqp\Producer;
use Hyperf\DbConnection\Db;
@ -21,110 +22,58 @@ use Psr\Container\NotFoundExceptionInterface;
class CouponService extends BaseService
{
/**
* 获取用户某状态全部优惠卷-列表
* @param string|int $user_id 用户id
* @param array $inquiry_type [1,2,3,4,5]适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
* @param int $user_coupon_status 状态0:未使用 1:已使用 3:已过期)
* @return array
*/
public function getUserCouponStatusList(string|int $user_id, array $inquiry_type, int $user_coupon_status): array
{
$params = array();
$params[] = ['user_id', '=', $user_id];
$params[] = ['user_coupon_status', '=', $user_coupon_status];// 状态0:未使用 1:已使用 3:已过期)
$coupon_params = array();
$coupon_params[] = ['coupon_client', '=', 1];
$coupon_params[] = ['coupon_status', '=', 1]; // 状态1:正常 2:强制失效 3:结束 4:删除)
$application_scope_params = $inquiry_type; // 适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
$user_coupon = UserCoupon::getWithCouponList($params, $coupon_params, $application_scope_params);
if (empty($user_coupon)) {
return array();
}
return $user_coupon->toArray();
}
/**
* 获取用户可用优惠卷-单条
* @param string|int $user_id 用户id
* @param string|int $inquiry_type 订单类型1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药)
* @param int $coupon_client 使用平台1:小程序)
* @return array
*/
public function getUserUsableCouponOne(string|int $user_id, string|int $inquiry_type, int $coupon_client = 1): array
{
// 问诊类型需加1
$inquiry_type = $inquiry_type + 1;
$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())]; // 过期使用时间
$coupon_params = array();
$coupon_params[] = ['coupon_client', '=', $coupon_client];
$coupon_params[] = ['coupon_status', '=', 1]; // 状态1:正常 2:强制失效 3:结束 4:删除)
$application_scope_params = [1, $inquiry_type]; // 适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
$user_coupon = UserCoupon::getDateWithCouponOne($params, $coupon_params, $application_scope_params);
if (empty($user_coupon)) {
return array();
}
return $user_coupon->toArray();
}
/**
* 发放用户优惠卷
* @param int $distribution_object 发放对象1:新注册用户 2:会员 3:近期消费 4:近期购药)
* @param string $user_id
* @param string $patient_id
* @param int $coupon_client 使用平台1:小程序)
* 发放优惠卷
* @param string $coupon_id 优惠卷id
* @param string $user_id 用户id
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function DistributeCoupon(int $distribution_object, string $user_id, string $patient_id, int $coupon_client = 1): bool
protected function GrantUserCoupon(string $coupon_id,string $user_id): bool
{
// 获取患者数据
$params = array();
$params['coupon_client'] = $coupon_client;
$params['coupon_status'] = 1;
$params['distribution_object'] = $distribution_object; // 发放对象1:新注册用户 2:会员 3:近期消费 4:近期购药)
$params['user_id'] = $user_id;
$user_patient = UserPatient::getOne($params);
if (empty($user_patient)){
return false;
}
$coupon = Coupon::getList($params);
if (empty($coupon)) {
// 获取优惠卷数据
$params = array();
$params['coupon_id'] = $coupon_id;
$coupon = Coupon::getOne($params);
if (empty($coupon)){
return false;
}
// 判断优惠卷数量是否充足
if ($coupon['coupon_count'] <= $coupon['coupon_take_count']){
return true;
}
foreach ($coupon as $key => $value) {
// 判断发放数量
if ($value['coupon_count'] == $value['coupon_take_count']) {
continue;
}
// 判断用户是否已有该优惠卷
$params = array();
$params['user_id'] = $user_id;
$params['coupon_id'] = $coupon['coupon_id'];
$user_coupon = UserCoupon::getOne($params);
if (!empty($user_coupon)){
return true;
}
try {
// 添加用户优惠卷表
$data = array();
$data['user_id'] = $user_id;
$data['patient_id'] = $patient_id;
$data['coupon_id'] = $value['coupon_id'];
if ($value['valid_type'] == 1) {
$data['patient_id'] = $user_patient['patient_id'];
$data['coupon_id'] = $coupon_id;
if ($coupon['valid_type'] == 1) {
// 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
$data['valid_start_time'] = $value['valid_start_time']; // 有效使用时间
$data['valid_end_time'] = $value['valid_end_time']; // 过期使用时间
} elseif ($value['valid_type'] == 2) {
$data['valid_start_time'] = $coupon['valid_start_time']; // 有效使用时间
$data['valid_end_time'] = $coupon['valid_end_time']; // 过期使用时间
} elseif ($coupon['valid_type'] == 2) {
// 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
$data['valid_start_time'] = date('Y-m-d H:i:s', time()); // 有效使用时间
$data['valid_end_time'] = date("Y-m-d H:i:s", strtotime($value['valid_days'] . " day"));
} else {
return false;
$data['valid_end_time'] = date("Y-m-d H:i:s", strtotime($coupon['valid_days'] . " day"));
}
$user_coupon = UserCoupon::addUserCoupon($data);
if (empty($user_coupon)) {
return false;
@ -132,33 +81,35 @@ class CouponService extends BaseService
// 增加优惠卷发放数量
$params = array();
$params['coupon_id'] = $value['coupon_id'];
$params['coupon_id'] = $coupon['coupon_id'];
Coupon::inc($params,'coupon_take_count');
// 添加弹窗表
$data = array();
$data['user_id'] = $user_id;
$data['app_type'] = 1;
$data['client_type'] = 1;// 客户端类型1:患者端 2:医生端 3:药师端)
$data['popup_type'] = 2;// 弹窗类型1:结算费用规则 2:新优惠卷弹窗)
$data['popup_title'] = "新人红包福利";
if ($coupon['is_popup'] == 1){
$data = array();
$data['user_id'] = $user_id;
$data['app_type'] = 1;
$data['client_type'] = 1;// 客户端类型1:患者端 2:医生端 3:药师端)
$data['popup_type'] = 2;// 弹窗类型1:结算费用规则 2:新优惠卷弹窗)
$data['popup_title'] = "新人红包福利";
$popup_content = [
'user_coupon_id' => (string)$user_coupon->user_coupon_id, // 优惠卷金额
'coupon_price' => $value['coupon_price'], // 优惠卷金额
'application_scope' => $value['application_scope'], // 适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
'valid_type' => $value['valid_type'], // 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
'valid_days' => $value['valid_days'], // 自领取之日起有效天数
'valid_start_time' => $value['valid_start_time'], // 开始使用时间
'valid_end_time' => $value['valid_end_time'], // 结束使用时间
];
$data['popup_content'] = json_encode($popup_content, JSON_UNESCAPED_UNICODE);
$popup = Popup::addPopup($data);
if (empty($popup)) {
return false;
$popup_content = [
'user_coupon_id' => (string)$user_coupon->user_coupon_id, // 优惠卷金额
'coupon_price' => $coupon['coupon_price'], // 优惠卷金额
'application_scope' => $coupon['application_scope'], // 适用范围1:全场通用 2:问诊 3:按品牌适用 4:按类别类别适用 5:单品使用)
'inquiry_type' => $coupon['inquiry_type'], // 关联问诊类型逗号分隔适用范围为问诊时存在生效1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测)
'valid_type' => $coupon['valid_type'], // 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
'valid_days' => $coupon['valid_days'], // 自领取之日起有效天数
'valid_start_time' => $coupon['valid_start_time'], // 开始使用时间
'valid_end_time' => $coupon['valid_end_time'], // 结束使用时间
];
$data['popup_content'] = json_encode($popup_content, JSON_UNESCAPED_UNICODE);
$popup = Popup::addPopup($data);
if (empty($popup)) {
return false;
}
}
// 添加用户优惠卷自动过期队列
// 添加优惠卷过期队列
$valid_end_time = strtotime($user_coupon->valid_end_time);
@ -173,7 +124,12 @@ class CouponService extends BaseService
if (!$res) {
return false;
}
}catch (\Throwable $e){
Log::getInstance("CouponService-GrantUserCoupon")->error($e->getMessage());
return false;
}
try {
// 添加优惠卷即将过期提醒队列
if ($time > 60 * 60 * 24 * 2) {
$time = 60 * 60 * 24 * 2;
@ -187,16 +143,38 @@ class CouponService extends BaseService
$producer = $this->container->get(Producer::class);
$res = $producer->produce($message);
if (!$res) {
return false;
Log::getInstance("CouponService-GrantUserCoupon")->error("添加优惠卷即将过期提醒队列");
}
try {
// 患者-优惠卷发放
$MessagePush = new MessagePush($user_id);
$MessagePush->patientDistributeCoupon($value['coupon_name']);
} catch (\Exception $e) {
// 不做处理
Log::getInstance()->error($e->getMessage());
// 通知-患者-优惠卷发放
$MessagePush = new MessagePush($user_id);
$MessagePush->patientDistributeCoupon($coupon['coupon_name']);
}catch (\Throwable $e){
Log::getInstance("CouponService-GrantUserCoupon")->error($e->getMessage());
}
return true;
}
/**
* 发放注册用户优惠卷
* @param string $user_id 用户id
* @return bool
*/
public function GrantRegisterCoupon(string $user_id): bool
{
// 获取注册用户可领取的优惠卷列表
$coupon = Coupon::getRegisterCouponList();
if (empty($coupon)) {
return true;
}
foreach ($coupon as $value){
// 发放优惠卷
$res = $this->GrantUserCoupon($value['coupon_id'],$user_id);
if (!$res){
// 发放失败
Log::getInstance("CouponService-GrantRegisterCoupon")->error("发放注册用户优惠卷");
}
}

View File

@ -155,9 +155,9 @@ class InquiryService extends BaseService
$inquiry_price = $DoctorInquiryService->getDoctorInquiryPrice($request_params['inquiry_type'], $request_params['inquiry_mode'], $request_params['doctor_id'] ?: "");
if ($inquiry_price > 0) {
// 获取可用优惠卷
$CouponService = new CouponService();
$user_coupon = $CouponService->getUserUsableCouponOne($user_info['user_id'], $request_params['inquiry_type']);
// 获取用户可用优惠卷
$userCouponService = new UserCouponService();
$user_coupon = $userCouponService->getUserUsableCouponOne($user_info['user_id'], $request_params['inquiry_type']);
}
// 确定支付渠道

View File

@ -145,10 +145,9 @@ class LoginService extends BaseService
$account->createAccount($user->user_id, $user->user_name, addAliyunOssWebsite($avatar));
if ($user['user_type'] == 1) {
// 发放用户优惠卷
// 发放注册用户优惠卷
$CouponService = new CouponService();
$res = $CouponService->DistributeCoupon(1, (string)$user->user_id, $user_patient['patient_id']);
$res = $CouponService->GrantRegisterCoupon($user->user_id);
if (!$res) {
Db::rollBack();
return fail(HttpEnumCode::SERVER_ERROR);
@ -411,10 +410,9 @@ class LoginService extends BaseService
$account->createAccount($user->user_id, $user->user_name, addAliyunOssWebsite($avatar));
if ($user['user_type'] == 1) {
// 发放用户优惠卷
// 发放注册用户优惠卷
$CouponService = new CouponService();
$res = $CouponService->DistributeCoupon(1, (string)$user->user_id, $user_patient['patient_id']);
$res = $CouponService->GrantRegisterCoupon($user->user_id);
if (!$res) {
Db::rollBack();
return fail(HttpEnumCode::SERVER_ERROR);
@ -545,7 +543,7 @@ class LoginService extends BaseService
Db::commit();
return success($data);
} catch (\Exception $e) {
} catch (\Throwable $e) {
Db::rollBack();
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
}

View File

@ -0,0 +1,70 @@
<?php
namespace App\Services;
use App\Model\UserCoupon;
/**
* 用户优惠卷
*/
class UserCouponService extends BaseService
{
/**
* 获取用户可用优惠卷-单条
* @param string|int $user_id 用户id
* @param string|int $inquiry_type 订单类型1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药)
* @param int $coupon_client 使用平台1:小程序)
* @return array
*/
public function getUserUsableCouponOne(string|int $user_id, string|int $inquiry_type, int $coupon_client = 1): array
{
// 问诊类型需加1
$inquiry_type = $inquiry_type + 1;
$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())]; // 过期使用时间
$coupon_params = array();
$coupon_params[] = ['coupon_client', '=', $coupon_client];
$coupon_params[] = ['coupon_status', '=', 1]; // 状态1:正常 2:强制失效 3:结束 4:删除)
$application_scope_params = [1, $inquiry_type]; // 适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
$user_coupon = \App\Model\UserCoupon::getDateWithCouponOne($params, $coupon_params, $application_scope_params);
if (empty($user_coupon)) {
return array();
}
return $user_coupon->toArray();
}
/**
* 获取用户某状态全部优惠卷-列表
* @param string|int $user_id 用户id
* @param array $inquiry_type [1,2,3,4,5]适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
* @param int $user_coupon_status 状态0:未使用 1:已使用 3:已过期)
* @return array
*/
public function getUserCouponStatusList(string|int $user_id, array $inquiry_type, int $user_coupon_status): array
{
$params = array();
$params[] = ['user_id', '=', $user_id];
$params[] = ['user_coupon_status', '=', $user_coupon_status];// 状态0:未使用 1:已使用 3:已过期)
$coupon_params = array();
$coupon_params[] = ['coupon_client', '=', 1];
$coupon_params[] = ['coupon_status', '=', 1]; // 状态1:正常 2:强制失效 3:结束 4:删除)
$application_scope_params = $inquiry_type; // 适用范围1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药)
$user_coupon = UserCoupon::getWithCouponList($params, $coupon_params, $application_scope_params);
if (empty($user_coupon)) {
return array();
}
return $user_coupon->toArray();
}
}

View File

@ -140,9 +140,9 @@ class UserPatientService extends BaseService
return fail(HttpEnumCode::HTTP_ERROR, "非法用户");
}
$CouponService = new CouponService();
$userCouponService = new UserCouponService();
$user_coupon = $CouponService->getUserCouponStatusList($user_patient['user_id'],[1,2,3,4,5],$user_coupon_status);
$user_coupon = $userCouponService->getUserCouponStatusList($user_patient['user_id'],[1,2,3,4,5],$user_coupon_status);
$result = [];
foreach ($user_coupon as $item){