feat : coupon template
This commit is contained in:
21
app/Service/Admin/Coupon/DispenseAddService.php
Normal file
21
app/Service/Admin/Coupon/DispenseAddService.php
Normal 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
|
||||
}
|
||||
}
|
||||
153
app/Service/Admin/Coupon/DispenseConfirmService.php
Normal file
153
app/Service/Admin/Coupon/DispenseConfirmService.php
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
21
app/Service/Admin/Coupon/DispenseService.php
Normal file
21
app/Service/Admin/Coupon/DispenseService.php
Normal 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
|
||||
}
|
||||
}
|
||||
118
app/Service/Admin/Coupon/TemplateService.php
Normal file
118
app/Service/Admin/Coupon/TemplateService.php
Normal 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());
|
||||
}
|
||||
}
|
||||
128
app/Service/ServiceTrait/Admin/CouponDispenseTrait.php
Normal file
128
app/Service/ServiceTrait/Admin/CouponDispenseTrait.php
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user