|
|
@@ -0,0 +1,325 @@
|
|
|
+<?php
|
|
|
+declare (strict_types = 1);
|
|
|
+
|
|
|
+namespace app\system\controller;
|
|
|
+
|
|
|
+use app\BaseController;
|
|
|
+use app\model\api\StoreCategory;
|
|
|
+use app\model\api\StoreBrand;
|
|
|
+use app\model\api\StoreProduct;
|
|
|
+use app\model\api\StoreProductAttrValue;
|
|
|
+use app\Request;
|
|
|
+use library\services\UtilService;
|
|
|
+use think\facade\Db;
|
|
|
+
|
|
|
+class Product extends BaseController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 商品分类列表
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function categoryList(Request $request)
|
|
|
+ {
|
|
|
+ $pageSize = 20;
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['page', 1],
|
|
|
+ ['pageSize', 20],
|
|
|
+ ['pid', 0],
|
|
|
+ ['keyword', ''],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if ($post['pid'] > 0) {
|
|
|
+ $where[] = ['pid', '=', $post['pid']];
|
|
|
+ }
|
|
|
+ if (!empty($post['keyword'])) {
|
|
|
+ $where[] = ['name', 'like', '%' . $post['keyword'] . '%'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = (new StoreCategory())->getList($where, $post['page'], $post['pageSize']);
|
|
|
+
|
|
|
+ return app('json')->success($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存商品分类
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function categorySave(Request $request)
|
|
|
+ {
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['id', 0],
|
|
|
+ ['name', '', 'empty', '请输入分类名称'],
|
|
|
+ ['pid', 0],
|
|
|
+ ['sort', 0],
|
|
|
+ ['status', 1],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'name' => $post['name'],
|
|
|
+ 'pid' => $post['pid'],
|
|
|
+ 'sort' => $post['sort'],
|
|
|
+ 'status' => $post['status'] == 1 ? 1 : 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($post['id'] > 0) {
|
|
|
+ $result = Db::name('store_category')->where('id', $post['id'])->update($data);
|
|
|
+ } else {
|
|
|
+ $result = Db::name('store_category')->insertGetId($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ return app('json')->success('保存成功');
|
|
|
+ } else {
|
|
|
+ return app('json')->fail('保存失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除商品分类
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function categoryDelete(Request $request)
|
|
|
+ {
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['id', '', 'empty', '参数错误'],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ // 检查是否有子分类
|
|
|
+ $hasChild = Db::name('store_category')->where('pid', $post['id'])->count();
|
|
|
+ if ($hasChild > 0) {
|
|
|
+ return app('json')->fail('该分类下有子分类,无法删除');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否有商品
|
|
|
+ $hasProduct = Db::name('store_product')->where('category_id', $post['id'])->count();
|
|
|
+ if ($hasProduct > 0) {
|
|
|
+ return app('json')->fail('该分类下有商品,无法删除');
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = Db::name('store_category')->where('id', $post['id'])->delete();
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ return app('json')->success('删除成功');
|
|
|
+ } else {
|
|
|
+ return app('json')->fail('删除失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 品牌列表
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function brandList(Request $request)
|
|
|
+ {
|
|
|
+ $pageSize = 20;
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['page', 1],
|
|
|
+ ['pageSize', 20],
|
|
|
+ ['keyword', ''],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if (!empty($post['keyword'])) {
|
|
|
+ $where[] = ['name', 'like', '%' . $post['keyword'] . '%'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = (new StoreBrand())->getList($where, $post['page'], $post['pageSize']);
|
|
|
+
|
|
|
+ return app('json')->success($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存品牌
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function brandSave(Request $request)
|
|
|
+ {
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['id', 0],
|
|
|
+ ['name', '', 'empty', '请输入品牌名称'],
|
|
|
+ ['image', ''],
|
|
|
+ ['sort', 0],
|
|
|
+ ['status', 1],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'name' => $post['name'],
|
|
|
+ 'image' => $post['image'],
|
|
|
+ 'sort' => $post['sort'],
|
|
|
+ 'status' => $post['status'] == 1 ? 1 : 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($post['id'] > 0) {
|
|
|
+ $result = Db::name('store_brand')->where('id', $post['id'])->update($data);
|
|
|
+ } else {
|
|
|
+ $result = Db::name('store_brand')->insertGetId($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ return app('json')->success('保存成功');
|
|
|
+ } else {
|
|
|
+ return app('json')->fail('保存失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除品牌
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function brandDelete(Request $request)
|
|
|
+ {
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['id', '', 'empty', '参数错误'],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $result = Db::name('store_brand')->where('id', $post['id'])->delete();
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ return app('json')->success('删除成功');
|
|
|
+ } else {
|
|
|
+ return app('json')->fail('删除失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商品列表
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function productList(Request $request)
|
|
|
+ {
|
|
|
+ $pageSize = 20;
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['page', 1],
|
|
|
+ ['pageSize', 20],
|
|
|
+ ['category_id', ''],
|
|
|
+ ['brand_id', ''],
|
|
|
+ ['keyword', ''],
|
|
|
+ ['status', ''],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if (!empty($post['category_id'])) {
|
|
|
+ $where[] = ['category_id', '=', $post['category_id']];
|
|
|
+ }
|
|
|
+ if (!empty($post['brand_id'])) {
|
|
|
+ $where[] = ['brand_id', '=', $post['brand_id']];
|
|
|
+ }
|
|
|
+ if (!empty($post['keyword'])) {
|
|
|
+ $where[] = ['title', 'like', '%' . $post['keyword'] . '%'];
|
|
|
+ }
|
|
|
+ if ($post['status'] !== '' && in_array((string)$post['status'], ["0", "1"])) {
|
|
|
+ $where[] = ['is_show', '=', (int)$post['status']];
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = (new StoreProduct())->getList($where, $post['page'], $post['pageSize']);
|
|
|
+
|
|
|
+ return app('json')->success($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存商品
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function productSave(Request $request)
|
|
|
+ {
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['id', 0],
|
|
|
+ ['category_id', '', 'empty', '请选择分类'],
|
|
|
+ ['brand_id', 0],
|
|
|
+ ['title', '', 'empty', '请输入商品标题'],
|
|
|
+ ['image', '', 'empty', '请上传商品图片'],
|
|
|
+ ['images', ''],
|
|
|
+ ['price', 0],
|
|
|
+ ['cost_price', 0],
|
|
|
+ ['ot_price', 0],
|
|
|
+ ['stock', 0],
|
|
|
+ ['sales', 0],
|
|
|
+ ['unit_name', '件'],
|
|
|
+ ['description', ''],
|
|
|
+ ['sort', 0],
|
|
|
+ ['status', 1],
|
|
|
+ ['sku_list', '[]'],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'category_id' => $post['category_id'],
|
|
|
+ 'brand_id' => $post['brand_id'],
|
|
|
+ 'title' => $post['title'],
|
|
|
+ 'image' => $post['image'],
|
|
|
+ 'images' => $post['images'],
|
|
|
+ 'price' => $post['price'],
|
|
|
+ 'cost_price' => $post['cost_price'],
|
|
|
+ 'ot_price' => $post['ot_price'],
|
|
|
+ 'stock' => $post['stock'],
|
|
|
+ 'sales' => $post['sales'],
|
|
|
+ 'unit_name' => $post['unit_name'],
|
|
|
+ 'description' => $post['description'],
|
|
|
+ 'sort' => $post['sort'],
|
|
|
+ 'is_show' => $post['status'] == 1 ? 1 : 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ if ($post['id'] > 0) {
|
|
|
+ // 更新商品
|
|
|
+ Db::name('store_product')->where('id', $post['id'])->update($data);
|
|
|
+ $productId = $post['id'];
|
|
|
+
|
|
|
+ // 删除原有SKU
|
|
|
+ Db::name('store_product_attr_value')->where('product_id', $productId)->delete();
|
|
|
+ } else {
|
|
|
+ // 新增商品
|
|
|
+ $data['time'] = time();
|
|
|
+ $productId = Db::name('store_product')->insertGetId($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存SKU
|
|
|
+ $skuList = json_decode($post['sku_list'], true);
|
|
|
+ if (!empty($skuList)) {
|
|
|
+ foreach ($skuList as $sku) {
|
|
|
+ $skuData = [
|
|
|
+ 'product_id' => $productId,
|
|
|
+ 'suk' => $sku['suk'] ?? '',
|
|
|
+ 'price' => $sku['price'] ?? $post['price'],
|
|
|
+ 'cost_price' => $sku['cost_price'] ?? $post['cost_price'],
|
|
|
+ 'stock' => $sku['stock'] ?? 0,
|
|
|
+ 'bar_code' => $sku['bar_code'] ?? '',
|
|
|
+ ];
|
|
|
+ Db::name('store_product_attr_value')->insert($skuData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ return app('json')->success('保存成功');
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ return app('json')->fail('保存失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除商品
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function productDelete(Request $request)
|
|
|
+ {
|
|
|
+ $post = UtilService::getMore([
|
|
|
+ ['id', '', 'empty', '参数错误'],
|
|
|
+ ], $request);
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ // 删除商品
|
|
|
+ Db::name('store_product')->where('id', $post['id'])->delete();
|
|
|
+ // 删除SKU
|
|
|
+ Db::name('store_product_attr_value')->where('product_id', $post['id'])->delete();
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ return app('json')->success('删除成功');
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ return app('json')->fail('删除失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|