191 lines
5.8 KiB
PHP
191 lines
5.8 KiB
PHP
<?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();
|
||
}
|
||
} |