feat : coupon template
This commit is contained in:
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