290 lines
10 KiB
PHP
290 lines
10 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Amqp\Producer\AutoCompleteInquiryDelayDirectProducer;
|
||
use App\Amqp\Producer\GrantUserCouponDelayDirectProducer;
|
||
use App\Amqp\Producer\UserCouponExpiredDelayDirectProducer;
|
||
use App\Amqp\Producer\UserCouponExpiredNoticeDelayDirectProducer;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\Coupon;
|
||
use App\Model\OrderCoupon;
|
||
use App\Model\Popup;
|
||
use App\Model\UserCoupon;
|
||
use App\Model\UserPatient;
|
||
use App\Utils\Log;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\DbConnection\Db;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
/**
|
||
* 优惠卷
|
||
*/
|
||
class CouponService extends BaseService
|
||
{
|
||
/**
|
||
* 发放优惠卷
|
||
* @param string $coupon_id 优惠卷id
|
||
* @param string $user_id 用户id
|
||
* @param int $grant_quantity 发放数量
|
||
* @return bool
|
||
*/
|
||
public function GrantUserCoupon(string $coupon_id,string $user_id,int $grant_quantity = 1): bool
|
||
{
|
||
// 获取患者数据
|
||
$params = array();
|
||
$params['user_id'] = $user_id;
|
||
$user_patient = UserPatient::getOne($params);
|
||
if (empty($user_patient)){
|
||
return false;
|
||
}
|
||
|
||
// 获取优惠卷数据
|
||
$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 false;
|
||
}
|
||
|
||
// 优惠卷剩余数量
|
||
$remaining_quantity = $coupon['coupon_count'] - $coupon['coupon_take_count'];
|
||
if ($remaining_quantity <= $grant_quantity){
|
||
return false;
|
||
}
|
||
|
||
// // 判断用户是否已有该优惠卷
|
||
// $params = array();
|
||
// $params['user_id'] = $user_id;
|
||
// $params['coupon_id'] = $coupon['coupon_id'];
|
||
// $user_coupon = UserCoupon::getOne($params);
|
||
// if (!empty($user_coupon)){
|
||
// if ($user_coupon['user_coupon_status'] == 0){
|
||
// return true;
|
||
// }
|
||
// }
|
||
|
||
// 判断该优惠卷状态
|
||
if ($coupon['coupon_status'] != 1){
|
||
// 优惠卷状态异常
|
||
return true;
|
||
}
|
||
|
||
// 判断该优惠卷过期时间
|
||
if (!empty($coupon['valid_type'])){
|
||
if ($coupon['valid_type'] == 1){
|
||
// 绝对时效
|
||
$valid_end_time = strtotime($coupon['valid_end_time']);
|
||
|
||
$date = time() + 60 * 10;
|
||
if ($valid_end_time < $date){
|
||
// 超出结束使用时间
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
|
||
try {
|
||
// 添加用户优惠卷表
|
||
$data = array();
|
||
$data['user_id'] = $user_id;
|
||
$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'] = $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($coupon['valid_days'] . " day"));
|
||
}
|
||
$user_coupon = UserCoupon::addUserCoupon($data);
|
||
if (empty($user_coupon)) {
|
||
return false;
|
||
}
|
||
|
||
// 增加优惠卷发放数量
|
||
$params = array();
|
||
$params['coupon_id'] = $coupon['coupon_id'];
|
||
Coupon::inc($params,'coupon_take_count');
|
||
|
||
// 添加弹窗表
|
||
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' => $coupon['coupon_price'], // 优惠卷金额
|
||
'application_scope' => $coupon['application_scope'], // 适用范围(1:全场通用 2:问诊 3:按品牌适用 4:按类别类别适用 5:单品使用)
|
||
'inquiry_type' => $coupon['inquiry_type'], // 关联问诊类型,application_scope=问诊时存在生效,逗号分隔(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']);
|
||
|
||
// 当天结束时间
|
||
$day_end_time = strtotime(date('Y-m-d 23:59:59',time()));
|
||
|
||
if ($valid_end_time <= $day_end_time){
|
||
$data = array();
|
||
$data['user_coupon_id'] = $user_coupon['user_coupon_id'];
|
||
|
||
$time = $valid_end_time - time();
|
||
$message = new UserCouponExpiredDelayDirectProducer($data);
|
||
$message->setDelayMs(1000 * $time);
|
||
$producer = $this->container->get(Producer::class);
|
||
$res = $producer->produce($message);
|
||
if (!$res) {
|
||
return false;
|
||
}
|
||
}
|
||
}catch (\Throwable $e){
|
||
Log::getInstance("CouponService-GrantUserCoupon")->error($e->getMessage());
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
// 通知-患者-优惠卷发放
|
||
$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("发放注册用户优惠卷");
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 发放购买服务包的关联优惠卷
|
||
* @param string $user_id 用户id
|
||
* @return bool
|
||
*/
|
||
public function GrantBuyOrderServicePackageCoupon(string $user_id): bool
|
||
{
|
||
// 获取购买服务包的用户可领取的优惠卷列表
|
||
$coupons = Coupon::getOrderServicePackageCouponList();
|
||
if (empty($coupons)) {
|
||
return true;
|
||
}
|
||
|
||
foreach ($coupons as $coupon){
|
||
// 发放优惠卷
|
||
$res = $this->GrantUserCoupon($coupon['coupon_id'],$user_id);
|
||
if (!$res){
|
||
// 发放失败
|
||
return false;
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
} |