Files
hyperf_service/app/Service/ServiceTrait/Admin/CouponDispenseTrait.php
2025-03-12 14:33:53 +08:00

215 lines
6.4 KiB
PHP

<?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;
$appointValue = $this->request->input('appoint_value');
// if ($this->groupType == CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS) {
// $this->appointValue = [
// 'site' => explode(',', $appointValue['site']),
// 'sku' => explode(',', $appointValue['goods'])
// ];
// } else {
// $this->appointValue = explode(',', $this->request->input('appoint_value'));
// }
//
match ($this->groupType){
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_USERS => $this->appointValue = explode(',', $appointValue['user_ids']),
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES => $this->appointValue = explode(',', $appointValue['site']),
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_GOODS => $this->appointValue = explode(',', $appointValue['goods']),
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS => $this->appointValue = [
'site' => explode(',', $appointValue['site']),
'sku' => explode(',', $appointValue['goods'])
]
};
unset($cycleInfo);
}
// protected function getSiteAndGoodsValueAndCheckDate()
// {
//
// }
/**
* @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['site'])
->where('status',OrderCode::FINISH)
->pluck('user_id','id')
->toArray();
if (empty($orderIds)) throw new Exception('未找到该周期该地点的订单');
$skuOrderIds = $this->orderGoodModel
->whereIn('order_id',array_keys($orderIds))
->whereIn('sku_id',$this->appointValue['sku'])
->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 array
* @throws Exception
*/
protected function handleDesignatedUsers(): array
{
$this->checkAppointValue();
$appointValue = $this->request->input('appoint_value');
return explode(',', $appointValue['user_ids']);
}
/**
* @return array
* @throws Exception
*/
protected function handleDesignatedSitesAndGoods(): array
{
$this->checkAppointValue();
$this->getValueAndCheckDate();
$this->checkSite();
$this->checkSku();
return $this->getUserData();
}
/**
* @return array
* @throws Exception
*/
protected function handleDesignatedSites(): array
{
$this->checkAppointValue();
$this->getValueAndCheckDate();
$this->checkSite();
return $this->getUserData();
}
/**
* @return array
* @throws Exception
*/
protected function handleDesignatedGoods(): array
{
$this->checkAppointValue();
$this->getValueAndCheckDate();
$this->checkSku();
return $this->getUserData();
}
/**
* @return void
* @throws Exception
*/
protected function checkAppointValue(): void
{
if (empty($this->request->input('appoint_value'))) throw new Exception('请选择指定值');
}
}