diff --git a/app/Constants/Common/GoodCode.php b/app/Constants/Common/GoodCode.php new file mode 100644 index 0000000..9488351 --- /dev/null +++ b/app/Constants/Common/GoodCode.php @@ -0,0 +1,18 @@ +add(); + return (new SpuService)->add(); + } + + /** + * spu修改 + * @param GoodRequest $request + * @return array + */ + #[RequestMapping(path: "edit_spu", methods: "POST")] + #[Scene(scene: "edit_spu")] + public function editSpu(GoodRequest $request): array + { + return (new SpuService)->edit(); + } + + /** + * spu删除 + * @param GoodRequest $request + * @return array + * @throws Exception + */ + #[RequestMapping(path: "del_spu", methods: "POST")] + #[Scene(scene: "del_spu")] + public function delSpu(GoodRequest $request): array + { + return (new SpuService)->del(); + } + + /** + * spu详情 + * @param GoodRequest $request + * @return array + */ + #[RequestMapping(path: "spu", methods: "POST")] + #[Scene(scene: "spu")] + public function spu(GoodRequest $request): array + { + return (new SpuService)->view(); + } + + /** + * spu列表 + * @param GoodRequest $request + * @return array + */ + #[RequestMapping(path: "list_spu", methods: "POST")] + #[Scene(scene: "list_spu")] + public function spuList(GoodRequest $request): array + { + return (new SpuService)->handle(); } /** @@ -38,6 +88,6 @@ class GoodController #[Scene(scene: "add_sku")] public function add_sku(GoodRequest $request) { - + return (new SkuService)->add(); } } diff --git a/app/Model/Cycle.php b/app/Model/Cycle.php index 973cf37..e647c75 100644 --- a/app/Model/Cycle.php +++ b/app/Model/Cycle.php @@ -43,4 +43,13 @@ class Cycle extends Model { return $this->where('dates',$date)->first(); } + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id',$id)->first(); + } } diff --git a/app/Model/Sku.php b/app/Model/Sku.php index e8f13d5..44b1cd5 100644 --- a/app/Model/Sku.php +++ b/app/Model/Sku.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Common\GoodCode; +use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; /** @@ -21,7 +23,8 @@ use Hyperf\DbConnection\Model\Model; * @property int $cancel_num * @property int $ahead_refund_num * @property int $behind_refund_num - * @property int $saleable + * @property int $saleable + * @property int $is_del * @property string $create_time * @property string $update_time */ @@ -37,8 +40,22 @@ class Sku extends Model */ protected array $fillable = []; + protected array $guarded = []; + /** * The attributes that should be cast to native types. */ protected array $casts = ['id' => 'integer', 'spu_id' => 'integer', 'total_stock' => 'integer', 'surplus_stock' => 'integer', 'sales_num' => 'integer', 'order_num' => 'integer', 'cancel_num' => 'integer', 'ahead_refund_num' => 'integer', 'behind_refund_num' => 'integer', 'saleable' => 'integer']; + + const string CREATED_AT = 'create_time'; + const string UPDATED_AT = 'update_time'; + + /** + * @param int $id + * @return Builder|\Hyperf\Database\Model\Model|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id',$id)->where('is_del',GoodCode::SKU_IS_NO_DEL)->first(); + } } diff --git a/app/Model/Spu.php b/app/Model/Spu.php index 5768e58..d923c84 100644 --- a/app/Model/Spu.php +++ b/app/Model/Spu.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Common\GoodCode; use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; @@ -16,7 +17,8 @@ use Hyperf\DbConnection\Model\Model; * @property string $title * @property string $sub_title * @property int $category_id - * @property int $saleable + * @property int $saleable + * @property int $is_del * @property string $create_time * @property string $update_time */ @@ -45,11 +47,12 @@ class Spu extends Model /** * @param int $cityId * @param int $cycleId + * @param string $title * @return Builder|\Hyperf\Database\Model\Model|null */ - public function getInfoByCityIdAndCycleId(int $cityId, int $cycleId): \Hyperf\Database\Model\Model|Builder|null + public function getInfoByCityIdAndCycleId(int $cityId, int $cycleId,string $title): \Hyperf\Database\Model\Model|Builder|null { - return $this->where('city_id', $cityId)->where('cycle_id', $cycleId)->first(); + return $this->where('city_id', $cityId)->where('cycle_id', $cycleId)->where('title',$title)->where('is_del',GoodCode::SPU_IS_NO_DEL)->first(); } /** @@ -58,6 +61,6 @@ class Spu extends Model */ public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null { - return $this->where('id', $id)->first(); + return $this->where('id', $id)->where('is_del',GoodCode::SPU_IS_NO_DEL)->first(); } } diff --git a/app/Request/Admin/GoodRequest.php b/app/Request/Admin/GoodRequest.php index aeebbd0..e4fd8b8 100644 --- a/app/Request/Admin/GoodRequest.php +++ b/app/Request/Admin/GoodRequest.php @@ -20,9 +20,32 @@ class GoodRequest extends FormRequest * Get the validation rules that apply to the request. */ public function rules(): array + { + return [ + 'date' => 'required|date|after:today', + 'kitchen_id' => 'required|integer|exists:kitchen,id', + 'chef_id' => 'required|integer|exists:chef,id', + 'title' => 'required|string', + 'sub_title' => 'string', + 'category_id' => 'required|integer|exists:category,id', + 'saleable' => 'required|integer|in:1,2', + 'id' => 'required|integer', + 'cycle_id' => 'required|integer|exists:cycle,id', + ]; + } + + public function messages(): array { return [ ]; } + + protected array $scenes = [ + 'add_spu' => ['date', 'kitchen_id', 'chef_id', 'title', 'sub_title', 'category_id', 'saleable'], + 'edit_spu' => ['id','kitchen_id', 'chef_id', 'title', 'sub_title', 'category_id', 'saleable'], + 'del_spu' => ['id'], + 'spu' => ['id'], + 'list_spu' => ['cycle_id'] + ]; } diff --git a/app/Service/Admin/Good/SkuService.php b/app/Service/Admin/Good/SkuService.php index 9e3615d..4fe199a 100644 --- a/app/Service/Admin/Good/SkuService.php +++ b/app/Service/Admin/Good/SkuService.php @@ -10,12 +10,151 @@ declare(strict_types=1); namespace App\Service\Admin\Good; +use App\Constants\Common\GoodCode; +use App\Exception\ErrException; +use App\Model\Sku; use App\Service\Admin\BaseService; +use App\Service\ServiceTrait\Common\OssTrait; +use Hyperf\Di\Annotation\Inject; class SkuService extends BaseService { + use OssTrait; + + /** + * @var Sku $skuModel + */ + #[Inject] + protected Sku $skuModel; + public function handle() { //todo Write logic } + + /** + * @return array + */ + public function add(): array + { + $spuId = $this->request->input('spu_id'); + + $insertModel = new Sku(); + + $imageIds = $this->request->input('image_ids'); + + $this->updateOssObjects(explode(',',$imageIds)); + + $insertModel->spu_id = $spuId; + $insertModel->title = $this->request->input('title'); + $insertModel->price = $this->request->input('price'); + $insertModel->image_ids = $imageIds; + $insertModel->param = $this->request->input('param',''); + $insertModel->extra = $this->request->input('extra',''); + $insertModel->total_stock = $this->request->input('stock'); +// $insertModel->surplus_stock = $this->request->input('stock'); + $insertModel->sales_num = 0; + $insertModel->order_num = 0; + $insertModel->cancel_num = 0; + $insertModel->ahead_refund_num = 0; + $insertModel->behind_refund_num = 0; + $insertModel->saleable = $this->request->input('saleable'); + + if (!$insertModel->save()) throw new ErrException('添加失败'); + + //todo 生成缓存 + + return $this->return->success(); + } + + /** + * @return array + */ + public function edit(): array + { + $id = (int)$this->request->input('id'); + + $skuInfo = $this->skuModel->getInfoById($id); + + if (empty($skuInfo)) throw new ErrException('原数据不存在,无法修改'); + + $stock = $this->request->input('stock'); + $forceCleanStockCache = 0; + if ($skuInfo->order_num > 0) { //已产生订单 修改库存需要强制删除库存缓存 + $forceCleanStockCache = 1; + + if ($skuInfo->total_stock < $stock) $skuInfo->total_stock = $stock; + if ($skuInfo->total_stock > $stock) { + $skuInfo->total_stock = $stock; + if ($skuInfo->order_num >= $stock) throw new ErrException('库存不能小于已下单量'); + } + } + + if ($skuInfo->order_num > 0 && $skuInfo->price != $this->request->input('price')) throw new ErrException('已有订单不可改价,退单后即可操作'); + else $skuInfo->price = $this->request->input('price'); + + $requestOssIds = $this->request->input('oss_ids'); + + $updateOssIds = array_diff(explode(',',$requestOssIds), explode(',',$skuInfo->image_ids)); + $delOssIds = array_diff(explode(',',$skuInfo->image_ids), explode(',',$requestOssIds)); + + if (!empty($updateOssIds)) $this->updateOssObjects($updateOssIds); + if (!empty($delOssIds)) $this->updateOssObjectsDisable($delOssIds); + + if (!empty($updateOssIds) || !empty($delOssIds)) { + $skuInfo->image_ids = $requestOssIds; + } + + $skuInfo->param = $this->request->input('param',''); + $skuInfo->extra = $this->request->input('extra',''); + $skuInfo->total_stock = $this->request->input('stock'); + $skuInfo->saleable = $this->request->input('saleable'); + $skuInfo->title = $this->request->input('title'); + + if (!$skuInfo->save()) throw new ErrException('修改失败'); + + if ($forceCleanStockCache == 1) return $this->return->success();//todo 修改缓存 + + return $this->return->success(); + } + + /** + * @return array + */ + public function del(): array + { + $id = (int)$this->request->input('id'); + $skuInfo = $this->skuModel->getInfoById($id); + + if (empty($skuInfo)) throw new ErrException('数据已删除,无法修改'); + + $skuInfo->is_del = GoodCode::SKU_IS_DELETE; + + if (!$skuInfo->save()) throw new ErrException('删除失败'); + + //todo 删除缓存 + + return $this->return->success(); + } + + /** + * @return array + */ + public function view(): array + { + $id = (int)$this->request->input('id'); + $skuInfo = $this->skuModel->getInfoById($id); + + if (empty($skuInfo)) throw new ErrException('数据不存在'); + + $imageIds = explode(',',$skuInfo->image_ids); + + $imageList = $this->getOssObjects($imageIds); + + $res = $skuInfo->toArray(); + + $res['image_list'] = $imageList; + + return $this->return->success('success',$res); + } } \ No newline at end of file diff --git a/app/Service/Admin/Good/SpuService.php b/app/Service/Admin/Good/SpuService.php index 2f1d960..da4bc9a 100644 --- a/app/Service/Admin/Good/SpuService.php +++ b/app/Service/Admin/Good/SpuService.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace App\Service\Admin\Good; use App\Constants\Admin\UserCode; +use App\Constants\Common\GoodCode; use App\Constants\Common\SiteCode; use App\Exception\ErrException; use App\Model\AdminUser; @@ -21,6 +22,7 @@ use App\Model\Spu; use App\Service\Admin\BaseService; use App\Service\ServiceTrait\Admin\GetUserInfoTrait; use App\Service\ServiceTrait\Common\OssTrait; +use Exception; use Hyperf\Di\Annotation\Inject; class SpuService extends BaseService @@ -51,10 +53,17 @@ class SpuService extends BaseService $this->cityId = (int)$this->userInfo['city_id']; } - - public function handle() + /** + * @return array + */ + public function handle(): array { + $limit = (int)$this->request->input('limit', 10); + $cycleId = (int)$this->request->input('cycle_id'); + $list = $this->spuModel->where('cycle_id',$cycleId)->where('city_id',$this->cityId)->paginate($limit)->toArray(); + + return $this->return->success('success', ['list' => $list]); } /** @@ -75,7 +84,9 @@ class SpuService extends BaseService if (empty($cycleInfo)) throw new ErrException('没有该周期,请刷新后重新上传'); - $info = $this->spuModel->getInfoByCityIdAndCycleId($this->cityId, $cycleInfo->id); + $title = $this->request->input('title'); + + $info = $this->spuModel->getInfoByCityIdAndCycleId($this->cityId, $cycleInfo->id,$title); if (!empty($info)) throw new ErrException('该菜品在当前城市已存在'); @@ -87,7 +98,7 @@ class SpuService extends BaseService $insertModel->cycle_id = $cycleInfo->id; $insertModel->kitchen_id = $this->request->input('kitchen_id'); $insertModel->chef_id = $this->request->input('chef_id'); - $insertModel->title = $this->request->input('title'); + $insertModel->title = $title; $insertModel->sub_title = $this->request->input('sub_title',''); $insertModel->category_id = $this->request->input('category_id'); $insertModel->saleable = $this->request->input('saleable'); @@ -132,9 +143,9 @@ class SpuService extends BaseService */ public function edit(): array { - $editId = $this->request->input('edit_id'); + $id = (int)$this->request->input('id'); - $info = $this->spuModel->getInfoById($editId); + $info = $this->spuModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $this->checkInfo(); @@ -154,19 +165,23 @@ class SpuService extends BaseService /** * spu 删除 * @return array - * @throws \Exception + * @throws Exception */ public function del(): array { - $id = $this->request->input('id'); + $id = (int)$this->request->input('id'); $info = $this->spuModel->getInfoById($id); if (empty($info)) throw new ErrException('数据已删除'); //todo 需要联动删除sku 已经有用户下单sku该怎么办 + //删除所有 sku + $this->skuModel->where('spu_id',$info->id)->update(['is_del' => GoodCode::SKU_IS_DELETE]); + //没有直接删除菜 - $info->delete(); + $info->is_del = GoodCode::SPU_IS_DELETE; + $info->save(); return $this->return->success(); } @@ -177,7 +192,7 @@ class SpuService extends BaseService */ public function view(): array { - $id = $this->request->input('id'); + $id = (int)$this->request->input('id'); $info = $this->spuModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在');