feat : sts
This commit is contained in:
31
app/Service/Admin/Good/FavorableService.php
Normal file
31
app/Service/Admin/Good/FavorableService.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Good;
|
||||
|
||||
use App\Model\Sku;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class FavorableService extends BaseService
|
||||
{
|
||||
/**
|
||||
* @var Sku
|
||||
*/
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
//todo
|
||||
$skuId = $this->request->input('sku_id');
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
191
app/Service/Admin/Good/PurchaseService.php
Normal file
191
app/Service/Admin/Good/PurchaseService.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Good;
|
||||
|
||||
use App\Cache\Redis\Api\GoodCache;
|
||||
use App\Constants\Common\GoodCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Purchase;
|
||||
use App\Model\Sku;
|
||||
use App\Model\Spu;
|
||||
use App\Service\Admin\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class PurchaseService extends BaseService
|
||||
{
|
||||
use OssTrait;
|
||||
|
||||
/**
|
||||
* @var Sku
|
||||
*/
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
/**
|
||||
* @var Spu
|
||||
*/
|
||||
#[Inject]
|
||||
protected Spu $spuModel;
|
||||
|
||||
/**
|
||||
* @var GoodCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected GoodCache $goodCache;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$limit = (int)$this->request->input('limit', 10);
|
||||
$cycleId = (int)$this->request->input('cycle_id');
|
||||
$kitchenId = (int)$this->request->input('search_kitchen_id');
|
||||
$cityId = (int)$this->request->input('search_city_id');
|
||||
|
||||
$list = $this->spuModel
|
||||
->where('cycle_id',$cycleId)
|
||||
->where('city_id',$cityId)
|
||||
->where('kitchen_id',$kitchenId)
|
||||
->paginate($limit)
|
||||
->toArray();
|
||||
|
||||
if (empty($list['data'])) return $this->return->success('success', ['list' => $list]);
|
||||
|
||||
$skuIds = array_unique(explode(',',implode(',',array_column($list['data'],'sku_ids'))));
|
||||
$skuNameList = $this->skuModel->whereIn('id',$skuIds)->pluck('title','id')->toArray();
|
||||
|
||||
$imageIds = array_column($list['data'],'image_ids');
|
||||
$imageList = $this->getOssObjects($imageIds);
|
||||
|
||||
foreach ($list['data'] as &$one) {
|
||||
$oneSkuName = [];
|
||||
foreach (explode(',',$one['sku_ids']) as $skuId) {
|
||||
$oneSkuName[] = $skuNameList[$skuId] ?? '';
|
||||
}
|
||||
|
||||
$one['url'] = $imageList[$one['image_id']]['url'] ?? '';
|
||||
$one['sku_name'] = $oneSkuName;
|
||||
}
|
||||
|
||||
return $this->return->success('success', ['list' => $list]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function add(): array
|
||||
{
|
||||
$cycleId = (int)$this->request->input('cycle_id');
|
||||
$kitchenId = (int)$this->request->input('kitchen_id');
|
||||
$ids = explode(',', $this->request->input('ids'));
|
||||
|
||||
sort($ids);
|
||||
|
||||
$spuList = $this->spuModel->getListByCycleIdAndType($cycleId, $kitchenId,GoodCode::SPU_TYPE_OPTIONAL);
|
||||
if ($spuList->isEmpty()) throw new ErrException('今日无菜品数据');
|
||||
$spuIds = array_column($spuList->toArray(), 'id');
|
||||
$skuList = $this->skuModel->getListBySpuIds($spuIds);
|
||||
if ($skuList->isEmpty()) throw new ErrException('今日无菜品数据');
|
||||
$occupied = array_column($skuList->toArray(), 'occupied','id');
|
||||
$skuToSpu = array_column($skuList->toArray(), 'spu_id','id');
|
||||
$skuIds = array_keys($skuToSpu);
|
||||
|
||||
$this->check($ids, $skuIds,$skuToSpu,$occupied);
|
||||
|
||||
Db::transaction(function () use ($ids, $cycleId, $kitchenId){
|
||||
$insertModel = new Purchase();
|
||||
|
||||
$insertModel->cycle_id = $cycleId;
|
||||
$insertModel->kitchen_id = $kitchenId;
|
||||
$insertModel->city_id = $this->request->input('city_id');
|
||||
$insertModel->sku_ids = implode(',', $ids);
|
||||
$insertModel->image_id = $this->request->input('image_id');
|
||||
$insertModel->name = $this->request->input('name');
|
||||
$insertModel->remark = $this->request->input('remark');
|
||||
|
||||
if (!$insertModel->save()) throw new ErrException('添加失败');
|
||||
|
||||
$this->updateOssObjects([$this->request->input('image_id')]);
|
||||
});
|
||||
|
||||
$this->goodCache->delPurchaseCache($kitchenId,$cycleId);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ids
|
||||
* @param array $skuIds
|
||||
* @param array $skuToSpu
|
||||
* @param array $occupied
|
||||
* @return void
|
||||
*/
|
||||
private function check(array $ids, array $skuIds, array $skuToSpu, array $occupied): void
|
||||
{
|
||||
if (empty($ids) || empty($skuIds) || empty($skuToSpu) || empty($occupied)) throw new ErrException('数据异常');
|
||||
|
||||
$spuArr = [];
|
||||
$occupiedTwo = 0;
|
||||
$occupiedOne = 0;
|
||||
foreach ($ids as $one) {
|
||||
if (!in_array($one, $skuIds)) throw new ErrException('菜品数据异常-01,添加失败');
|
||||
|
||||
if (in_array($skuToSpu[$one], $spuArr)) throw new ErrException('不符合四选一的规则,不能重复添加');
|
||||
|
||||
if (!isset($occupied[$one])) throw new ErrException('菜品数据异常-02,添加失败');
|
||||
if ($occupied[$one] == 2) {
|
||||
$occupiedTwo++;
|
||||
|
||||
if ($occupied > 3) throw new ErrException('不符合目前盒子的规则01,添加失败');
|
||||
} elseif ($occupied[$one] == 1) {
|
||||
$occupiedOne++;
|
||||
}
|
||||
|
||||
$spuArr[] = $skuToSpu[$one];
|
||||
}
|
||||
|
||||
if ($occupiedOne >= (9 - 2 * $occupiedTwo)) throw new ErrException('不符合目前盒子的规则02,添加失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Purchase
|
||||
*/
|
||||
#[Inject]
|
||||
protected Purchase $purchaseModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function del(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
|
||||
$info = $this->purchaseModel->find($id);
|
||||
|
||||
if (empty($info)) throw new ErrException('数据不存在');
|
||||
|
||||
$info->delete();
|
||||
if (!$info->save()) throw new ErrException('删除失败');
|
||||
|
||||
$this->goodCache->delPurchaseCache($info->kitchen_id,$info->cycle_id);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,12 @@ use App\Constants\Common\GoodCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\AdminUser;
|
||||
use App\Model\Chef;
|
||||
use App\Model\Purchase;
|
||||
use App\Model\Sku;
|
||||
use App\Model\Spu;
|
||||
use App\Service\Admin\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -215,10 +217,14 @@ class SkuService extends BaseService
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Purchase
|
||||
*/
|
||||
#[Inject]
|
||||
protected Purchase $purchaseModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function del(): array
|
||||
{
|
||||
@@ -229,12 +235,28 @@ class SkuService extends BaseService
|
||||
$spuInfo = $this->spuModel->getInfoById($skuInfo->spu_id);
|
||||
if (empty($spuInfo)) throw new ErrException('数据出错');
|
||||
|
||||
$skuInfo->is_del = GoodCode::SKU_IS_DELETE;
|
||||
$purchaseInfo = $this->purchaseModel->getListByCycleIdAndKitchenId($skuInfo->cycle_id,$spuInfo->kitchen_id);
|
||||
$purchaseIds = [];
|
||||
if (!empty($purchaseInfo)) {
|
||||
foreach ($purchaseInfo->toArray() as $one) {
|
||||
$oneSkuArr = explode(',',$one['sku_ids']);
|
||||
if (!in_array($skuInfo->id,$oneSkuArr)) continue;
|
||||
|
||||
if (!$skuInfo->save()) throw new ErrException('删除失败');
|
||||
$purchaseIds = $one['id'];
|
||||
}
|
||||
}
|
||||
|
||||
//删除缓存
|
||||
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
|
||||
Db::transaction(function () use ($id,$purchaseIds,$spuInfo,$skuInfo) {
|
||||
$skuInfo->is_del = GoodCode::SKU_IS_DELETE;
|
||||
|
||||
if (!$skuInfo->save()) throw new ErrException('删除失败');
|
||||
|
||||
if (!empty($purchaseIds) && !$this->purchaseModel->whereIn('id',$purchaseIds)->delete()) throw new ErrException('删除联动数据失败');
|
||||
|
||||
//删除缓存
|
||||
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
|
||||
$this->goodCache->delPurchaseCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
|
||||
});
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use App\Cache\Redis\Api\ApiRedisKey;
|
||||
use App\Cache\Redis\Api\GoodCache;
|
||||
use App\Cache\Redis\Api\SiteCache;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Constants\ConfigCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Category;
|
||||
use App\Service\Api\BaseService;
|
||||
@@ -87,6 +88,8 @@ class OptionalListService extends BaseService
|
||||
*/
|
||||
private function buildData($data): array
|
||||
{
|
||||
// 一键选购
|
||||
$purchaseData = $this->goodCache->getPurchaseGoodList();
|
||||
|
||||
$categoryIds = $this->categoryModel->whereIn('id',array_column($data,'category_id'))->get();
|
||||
if (empty($categoryIds)) throw new ErrException('数据错误');
|
||||
@@ -111,6 +114,14 @@ class OptionalListService extends BaseService
|
||||
$res[$item['category_id']]['spu_list'][] = $item;
|
||||
}
|
||||
|
||||
if (!empty($purchaseData)) {
|
||||
array_unshift($res,[
|
||||
'category_name' => $this->configCache->getConfigValueByKey(ConfigCode::ONE_CLICK_SHOPPING_FOR_CATEGORY_NAME),
|
||||
'category_image' => $this->getOssObjectById((int)$this->configCache->getConfigValueByKey(ConfigCode::ONE_CLICK_SHOPPING_FOR_CATEGORY_IMAGE)),
|
||||
'spu_list' => $purchaseData
|
||||
]);
|
||||
}
|
||||
|
||||
return array_values($res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user