| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?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('删除失败');
- }
- }
- }
|