feat : auto coupon
This commit is contained in:
@@ -44,4 +44,11 @@ class CouponCode
|
||||
CONST INT COUPON_STATUS_UNUSED = 1;
|
||||
CONST INT COUPON_STATUS_USED = 2;
|
||||
CONST INT COUPON_STATUS_NOT_EXPIRE = 3;
|
||||
|
||||
/**
|
||||
* @var int 优惠券发放状态 1 未领取 2 已领取
|
||||
*/
|
||||
CONST INT DISPENSE_STATUS_IS_NO_RECEIVED = 1;
|
||||
CONST INT DISPENSE_STATUS_IS_RECEIVED = 2;
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Controller\Admin;
|
||||
use App\Middleware\Admin\JwtAuthMiddleware;
|
||||
use App\Service\Admin\Coupon\DispenseAddService;
|
||||
use App\Service\Admin\Coupon\DispenseConfirmService;
|
||||
use App\Service\Admin\Coupon\DispenseService;
|
||||
use App\Service\Admin\Coupon\TemplateService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
@@ -73,7 +74,7 @@ class CouponController
|
||||
#[Scene(scene: "dispense_list")]
|
||||
public function dispenseList()
|
||||
{
|
||||
|
||||
return (new DispenseService)->list();
|
||||
}
|
||||
|
||||
#[RequestMapping(path: "dispense_add", methods: "POST")]
|
||||
@@ -94,6 +95,6 @@ class CouponController
|
||||
#[Scene(scene: "dispense_info")]
|
||||
public function dispenseInfo()
|
||||
{
|
||||
|
||||
return (new DispenseService)->handle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Constants\Common\CouponCode;
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
@@ -66,6 +67,9 @@ class CouponDispenseUser extends Model
|
||||
{
|
||||
return $this
|
||||
->whereIn('user_id', $userIds)
|
||||
->increment('receive_count', $count);
|
||||
->update([
|
||||
'receive_count' => $count,
|
||||
'is_receive' => CouponCode::DISPENSE_STATUS_IS_RECEIVED
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,4 +46,26 @@ class CouponTemplate extends Model
|
||||
{
|
||||
return $this->find($id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有数据
|
||||
* @param array $ids
|
||||
* @return array
|
||||
*/
|
||||
public function getDataByIds(array $ids): array
|
||||
{
|
||||
$data = $this->whereIn('id',$ids)->get();
|
||||
if (empty($data)){
|
||||
return [];
|
||||
}
|
||||
|
||||
$res = [];
|
||||
foreach ($data->toArray() as $one)
|
||||
{
|
||||
$res[$one['id']] = $one;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Coupon;
|
||||
|
||||
use App\Amqp\Producer\CouponAutoDispenseProducer;
|
||||
use App\Constants\Common\CouponCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\CouponDispenseLog;
|
||||
@@ -23,8 +24,12 @@ use App\Model\Sku;
|
||||
use App\Service\Admin\BaseService;
|
||||
use App\Service\ServiceTrait\Admin\CouponDispenseTrait;
|
||||
use Exception;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class DispenseAddService extends BaseService
|
||||
{
|
||||
@@ -96,8 +101,15 @@ class DispenseAddService extends BaseService
|
||||
*/
|
||||
private int $claimRule;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $dispenseId;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
@@ -109,14 +121,18 @@ class DispenseAddService extends BaseService
|
||||
$this->checkClaimRuleData();
|
||||
|
||||
Db::transaction(function () {
|
||||
$dispenseId = $this->addMasterTableInformation();
|
||||
$this->dispenseId = $this->addMasterTableInformation();
|
||||
|
||||
$this->addSubTableInformation($dispenseId);
|
||||
$this->addSubTableInformation($this->dispenseId);
|
||||
});
|
||||
|
||||
if ($this->claimRule == CouponCode::DISPENSE_CLAIM_RULE_POST_ACCOUNT) {
|
||||
//todo mq发放优惠券
|
||||
echo 1;
|
||||
//mq发放优惠券
|
||||
$message = new CouponAutoDispenseProducer([
|
||||
'coupon_dispense_id' => $this->dispenseId,
|
||||
]);
|
||||
$producer = ApplicationContext::getContainer()->get(Producer::class);
|
||||
$producer->produce($message);
|
||||
}
|
||||
|
||||
return $this->return->success();
|
||||
|
||||
@@ -10,12 +10,89 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Coupon;
|
||||
|
||||
use App\Constants\Common\CouponCode;
|
||||
use App\Model\CouponDispenseLog;
|
||||
use App\Model\CouponDispenseUser;
|
||||
use App\Model\CouponTemplate;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class DispenseService extends BaseService
|
||||
{
|
||||
public function handle()
|
||||
/**
|
||||
* @var CouponDispenseLog
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseLog $couponDispenseLogModel;
|
||||
|
||||
/**
|
||||
* @var CouponDispenseUser
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseUser $couponDispenseUserModel;
|
||||
|
||||
/**
|
||||
* @var CouponTemplate
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponTemplate $couponTemplateModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
//todo Write logic
|
||||
$info = $this->couponDispenseLogModel->getInfoById((int)$this->request->input('id'));
|
||||
|
||||
if (empty($info)) throw new Exception('数据不存在');
|
||||
|
||||
$templateInfo = $this->couponTemplateModel->getInfoById($info->coupon_template_id);
|
||||
|
||||
$subData = [];
|
||||
if ($info->appoint_group != CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE) {
|
||||
$userInfo = $this->couponDispenseUserModel->where('coupon_dispense_id',$info->id)->select(['receive_count','is_receive','user_id'])->get();
|
||||
|
||||
if (!empty($userInfo)) $subData = $userInfo->toArray();
|
||||
}
|
||||
|
||||
return $this->return->success('success',[
|
||||
'info' => $info,
|
||||
'template_info' => $templateInfo,
|
||||
'sub_data' => $subData,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
$limit = (int)$this->request->input('limit', 10);
|
||||
|
||||
$list = $this
|
||||
->couponDispenseLogModel
|
||||
->when($userId = $this->request->input('query_user_id'), function ($query) use ($userId) {
|
||||
$query->whereIn('id', $this->couponDispenseUserModel->where('user_id', $userId)->pluck('coupon_dispense_id')->toArray());
|
||||
})
|
||||
->orderBy('id', 'desc')
|
||||
->paginate($limit,[
|
||||
'title','coupon_name','coupon_template_id','total_count','receive_count','item_count','appoint_group','claim_rule','claim_value','appoint_value','validity_time_type','validity_time_value'
|
||||
])->toArray();
|
||||
|
||||
if (empty($list['data'])) return $this->return->success('success', $list);
|
||||
|
||||
|
||||
$templateIds = array_column($list['data'],'coupon_template_id');
|
||||
$templateList = $this->couponTemplateModel->getDataByIds($templateIds);
|
||||
|
||||
foreach ($list['data'] as &$item) {
|
||||
$item['template_info'] = $templateList[$item['coupon_template_id']];
|
||||
$item['appoint_value'] = json_decode($item['appoint_value'],true);
|
||||
$item['validity_time_value'] = json_decode($item['validity_time_value'],true);
|
||||
}
|
||||
|
||||
return $this->return->success('success', $list);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user