feat : coupon template

This commit is contained in:
2025-02-25 18:01:14 +08:00
parent ebecd14a30
commit af23f61991
9 changed files with 675 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Constants\Common;
class CouponCode
{
/**
* @var int 优惠券类型 1 立减 2 折扣
*/
CONST INT COUPON_TYPE_INSTANT_REDUCTION = 1;
CONST INT COUPON_TYPE_DISCOUNT = 2;
/**
* @var int 优惠券是否可以后台用户修改 1 可以 2 不可以
*/
CONST INT IS_ADMIN_EDIT = 1;
CONST INT IS_ADMIN_NO_EDIT = 2;
/**
* @var int 优惠券模板是否启用 1 启用 2 不启用
*/
CONST INT COUPON_TEMPLATE_STATUS_NORMAL = 1;
CONST INT COUPON_TEMPLATE_STATUS_ENABLE = 2;
/**
* @var int 优惠券发放群体 1 所有人 2 指定用户 3 指定某一个套餐日的地点(下单用户) 4 指定某一个套餐日的商品(下单用户)
*/
CONST INT DISPENSE_APPOINT_GROUP_ALL_PEOPLE = 1;
CONST INT DISPENSE_APPOINT_GROUP_DESIGNATED_USERS = 2;
CONST INT DISPENSE_APPOINT_GROUP_DESIGNATED_SITES = 3;
CONST INT DISPENSE_APPOINT_GROUP_DESIGNATED_GOODS = 4;
CONST INT DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS = 5;
/**
* @var int 优惠券发放规则 1 首页弹窗 2 下单账户 3 单页
*/
CONST INT DISPENSE_CLAIM_RULE_HOME_POPUPS = 1;
CONST INT DISPENSE_CLAIM_RULE_POST_ACCOUNT = 2;
CONST INT DISPENSE_CLAIM_RULE_SINGLE_PAGE = 3;
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
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\TemplateService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: "admin/coupon")]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class CouponController
{
/**
* @return array|null
*/
#[RequestMapping(path: "template_add", methods: "POST")]
#[Scene(scene: "template_add")]
public function templateAdd()
{
return (new TemplateService)->add();
}
/**
* @return array|null
*/
#[RequestMapping(path: "template_list", methods: "GET")]
#[Scene(scene: "template_list")]
public function templateList()
{
return (new TemplateService)->handle();
}
/**
* @return array
*/
#[RequestMapping(path: "template_edit", methods: "POST")]
#[Scene(scene: "template_edit")]
public function templateEdit()
{
return (new TemplateService)->edit();
}
/**
* @return array
*/
#[RequestMapping(path: "template_change_status", methods: "GET")]
#[Scene(scene: "template_change_status")]
public function templateChangeStatus()
{
return (new TemplateService)->changeStatus();
}
/**
* @return array
*/
#[RequestMapping(path: "template_info", methods: "GET")]
#[Scene(scene: "template_info")]
public function templateInfo()
{
return (new TemplateService)->info();
}
#[RequestMapping(path: "dispense_list", methods: "GET")]
#[Scene(scene: "dispense_list")]
public function dispenseList()
{
}
#[RequestMapping(path: "dispense_add", methods: "POST")]
#[Scene(scene: "dispense_add")]
public function dispenseAdd()
{
return (new DispenseAddService)->handle();
}
#[RequestMapping(path: "dispense_confirm", methods: "POST")]
#[Scene(scene: "dispense_confirm")]
public function dispenseConfirm()
{
return (new DispenseConfirmService)->handle();
}
#[RequestMapping(path: "dispense_info", methods: "GET")]
#[Scene(scene: "dispense_info")]
public function dispenseInfo()
{
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $title
* @property string $coupon_name
* @property int $coupon_template_id
* @property int $send_count
* @property int $receive_count
* @property int $left_count
* @property int $item_count
* @property int $appoint_group
* @property int $claim_rule
* @property string $claim_value
* @property int $appoint_city_id
* @property string $appoint_value
* @property string $use_scen_ids
* @property int $validity_time_type
* @property string $validity_time_value
* @property string $remark
* @property string $create_time
* @property string $update_time
*/
class CouponDispenseLog extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'coupon_dispense_log';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'coupon_template_id' => 'integer', 'send_count' => 'integer', 'receive_count' => 'integer', 'left_count' => 'integer', 'item_count' => 'integer', 'appoint_group' => 'integer', 'claim_rule' => 'integer', 'appoint_city_id' => 'integer', 'validity_time_type' => 'integer'];
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $name
* @property int $coupon_type
* @property string $amount
* @property string $ratio
* @property int $is_admin_edit
* @property int $status
* @property string $create_time
* @property string $update_time
*/
class CouponTemplate extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'coupon_template';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'coupon_type' => 'integer', 'is_admin_edit' => 'integer', 'status' => 'integer'];
const string CREATED_AT = 'created_time';
const string UPDATED_AT = 'updated_time';
/**
* @param int $id
* @return CouponTemplate|\Hyperf\Database\Model\Model|array|null
*/
public function getInfoById(int $id): CouponTemplate|\Hyperf\Database\Model\Model|array|null
{
return $this->find($id);
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Coupon;
use App\Service\Admin\BaseService;
class DispenseAddService extends BaseService
{
public function handle()
{
//todo Write logic
}
}

View File

@@ -0,0 +1,153 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Coupon;
use App\Constants\Common\CouponCode;
use App\Exception\ErrException;
use App\Model\CouponDispenseLog;
use App\Model\Cycle;
use App\Model\Order;
use App\Model\OrderGood;
use App\Model\Site;
use App\Model\Sku;
use App\Model\User;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Admin\CouponDispenseTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
class DispenseConfirmService extends BaseService
{
use CouponDispenseTrait;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var Site
*/
#[Inject]
protected Site $siteModel;
/**
* @var User
*/
#[Inject]
protected User $userModel;
/**
* @var CouponDispenseLog
*/
#[Inject]
protected CouponDispenseLog $couponDispenseLogModel;
/**
* @var Cycle
*/
#[Inject]
protected Cycle $cycleModel;
/**
* @var array
*/
private array $res;
/**
* @var int
*/
private int $cycleId;
/**
* @var array
*/
private array $appointValue;
/**
* @var int
*/
private int $groupType;
/**
* @return array
* @throws Exception
*/
public function handle(): array
{
try {
$this->res = [];
if (empty($this->request->input('appoint_value'))) throw new Exception('请选择指定值');
match ($this->groupType = (int)$this->request->input('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE))
{
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_USERS => $this->handleDesignatedUsers(),
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES => $this->handleDesignatedSites(),
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_GOODS => $this->handleDesignatedGoods(),
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS => $this->handleDesignatedSitesAndGoods(),
default => throw new Exception('不需要渲染用户数据')
};
return $this->return->success('success',['list' => $this->res]);
}catch (Exception $e) {
throw new ErrException($e->getMessage());
}
}
private function handleDesignatedUsers()
{
}
private function handleDesignatedSitesAndGoods()
{
$this->getValueAndCheckDate();
$this->checkSite();
$this->checkSku();
$this->getUserData();
}
private function handleDesignatedSites()
{
$this->getValueAndCheckDate();
$this->checkSite();
$this->getUserData();
}
private function handleDesignatedGoods()
{
$this->getValueAndCheckDate();
$this->checkSku();
$this->getUserData();
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Coupon;
use App\Service\Admin\BaseService;
class DispenseService extends BaseService
{
public function handle()
{
//todo Write logic
}
}

View File

@@ -0,0 +1,118 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Coupon;
use App\Constants\Common\CouponCode;
use App\Exception\ErrException;
use App\Model\CouponTemplate;
use App\Service\Admin\BaseService;
use Hyperf\Di\Annotation\Inject;
class TemplateService extends BaseService
{
/**
* @var CouponTemplate
*/
#[Inject]
protected CouponTemplate $couponTemplateModel;
/**
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$list = $this
->couponTemplateModel
->when($searchName = $this->request->input('query_name'), function ($query) use ($searchName) {
$query->where('name', 'like', "$searchName%");
})
->when($status = $this->request->input('query_status'), function ($query) use ($status) {
$query->where('id', $status);
})
->paginate($limit,['chinese_name','id','mobile','status'])->toArray();
return $this->return->success('success',$list);
}
/**
* @return array
* @throws ErrException
*/
public function add(): array
{
$insertModel = new CouponTemplate();
$insertModel->name = $this->request->input('name');
$insertModel->coupon_type = $this->request->input('coupon_type');
$insertModel->amount = $this->request->input('amount','0.00');
$insertModel->ratio = $this->request->input('ratio','0.00');
$insertModel->is_admin_edit = CouponCode::IS_ADMIN_EDIT;
$insertModel->status = CouponCode::COUPON_TEMPLATE_STATUS_NORMAL;
if (!$insertModel->save()) throw new ErrException('保存优惠券模板失败');
return $this->return->success();
}
/**
* @return array
* @throws ErrException
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$info = $this->couponTemplateModel->getInfoById($id);
if (!$info) throw new ErrException('优惠券模板不存在');
if ($info->is_admin_edit != CouponCode::IS_ADMIN_EDIT) throw new ErrException('该优惠券模板不允许修改,请联系运维或者后端工程师在不影响程序的情况下进行修改');
$info->name = $this->request->input('name');
$info->coupon_type = $this->request->input('coupon_type');
$info->amount = $this->request->input('amount','0.00');
$info->ratio = $this->request->input('ratio','0.00');
if (!$info->save()) throw new ErrException('保存优惠券模板失败');
return $this->return->success();
}
/**
* @return array
*/
public function changeStatus(): array
{
$id = (int)$this->request->input('id');
$info = $this->couponTemplateModel->getInfoById($id);
if (!$info) throw new ErrException('优惠券模板不存在');
$info->status = $info->status == CouponCode::COUPON_TEMPLATE_STATUS_NORMAL ? CouponCode::COUPON_TEMPLATE_STATUS_ENABLE : CouponCode::COUPON_TEMPLATE_STATUS_NORMAL;
if (!$info->save()) throw new ErrException('保存优惠券模板失败');
return $this->return->success();
}
/**
* @return array
*/
public function info(): array
{
$id = (int)$this->request->input('id');
$info = $this->couponTemplateModel->getInfoById($id);
return $this->return->success('success',$info->toArray());
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace App\Service\ServiceTrait\Admin;
use App\Constants\Common\CouponCode;
use App\Constants\Common\OrderCode;
use Exception;
trait CouponDispenseTrait
{
/**
* @return void
* @throws Exception
*/
protected function getValueAndCheckDate(): void
{
if (empty($cycleId = $this->request->input('appoint_cycle_id'))) {
throw new Exception('未选中点餐周期');
}
$cycleInfo = $this->cycleModel->getInfoById((int)$cycleId);
if (empty($cycleInfo)) throw new Exception('未找到该点餐周期');
$this->cycleId = $cycleInfo->id;
$this->appointValue = explode(',', $this->request->input('appoint_value'));
unset($cycleInfo);
}
/**
* @return void
* @throws Exception
*/
protected function checkSite(): void
{
$siteCount = $this->siteModel->whereIn('id',$this->appointValue)->count();
if ($siteCount != count($this->appointValue)) throw new Exception('请选择正确的站点');
}
/**
* @return void
* @throws Exception
*/
protected function checkSku(): void
{
$skuCount = $this->skuModel->whereIn('id',$this->appointValue)->count();
if ($skuCount!= count($this->appointValue)) throw new Exception('请选择正确的商品');
}
/**
* @return array
* @throws Exception
*/
protected function getUserData(): array
{
$userIds = [];
switch ($this->groupType) {
case CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES:
$userIds = $this->orderModel
->where('cycle_id',$this->cycleId)
->whereIn('site_id',$this->appointValue)
->where('status',OrderCode::FINISH)
->pluck('user_id')
->toArray();
break;
case CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_GOODS:
$orderIds = $this->orderModel
->where('cycle_id',$this->cycleId)
->where('status',OrderCode::FINISH)
->pluck('user_id','order_id')
->toArray();
if (empty($orderIds)) throw new Exception('未找到该周期的订单');
$skuOrderIds = $this->orderGoodModel
->whereIn('order_id',array_keys($orderIds))
->whereIn('sku_id',$this->appointValue)
->pluck('order_id')
->toArray();
$skuOrderIds = array_unique($skuOrderIds);
// 将 $b 转为键数组,值无所谓(可以用 null 或任意值)
$skuOrderIds_keys = array_flip($skuOrderIds);
// 获取 $a 和 $b_keys 的交集
$intersect = array_intersect_key($orderIds, $skuOrderIds_keys);
// 提取值
$userIds = array_values($intersect);
break;
case CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS:
$orderIds = $this->orderModel
->where('cycle_id',$this->cycleId)
->whereIn('site_id',$this->appointValue)
->where('status',OrderCode::FINISH)
->pluck('user_id','order_id')
->toArray();
if (empty($orderIds)) throw new Exception('未找到该周期的订单');
$skuOrderIds = $this->orderGoodModel
->whereIn('order_id',array_keys($orderIds))
->whereIn('sku_id',$this->appointValue)
->pluck('order_id')
->toArray();
$skuOrderIds = array_unique($skuOrderIds);
$skuOrderIds_keys = array_flip($skuOrderIds);
$intersect = array_intersect_key($orderIds, $skuOrderIds_keys);
$userIds = array_values($intersect);
break;
}
return $userIds;
}
/**
* @return void
*/
protected function getUserInfoByUserIds(array $userIds)
{
}
}