Product.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\system\controller;
  4. use app\BaseController;
  5. use app\model\api\StoreCategory;
  6. use app\model\api\StoreBrand;
  7. use app\model\api\StoreProduct;
  8. use app\model\api\StoreProductAttrValue;
  9. use app\Request;
  10. use library\services\UtilService;
  11. use think\facade\Db;
  12. class Product extends BaseController
  13. {
  14. /**
  15. * 商品分类列表
  16. * @param Request $request
  17. */
  18. public function categoryList(Request $request)
  19. {
  20. $pageSize = 20;
  21. $post = UtilService::getMore([
  22. ['page', 1],
  23. ['pageSize', 20],
  24. ['pid', 0],
  25. ['keyword', ''],
  26. ], $request);
  27. $where = [];
  28. if ($post['pid'] > 0) {
  29. $where[] = ['pid', '=', $post['pid']];
  30. }
  31. if (!empty($post['keyword'])) {
  32. $where[] = ['cate_name', 'like', '%' . $post['keyword'] . '%'];
  33. }
  34. $result = (new StoreCategory())->getList($where, $post['page'], $post['pageSize']);
  35. return app('json')->success($result);
  36. }
  37. /**
  38. * 保存商品分类
  39. * @param Request $request
  40. */
  41. public function categorySave(Request $request)
  42. {
  43. $post = UtilService::getMore([
  44. ['id', 0],
  45. ['cate_name', '', 'empty', '请输入分类名称'],
  46. ['pid', 0],
  47. ['sort', 0],
  48. ], $request);
  49. $data = [
  50. 'cate_name' => $post['cate_name'],
  51. 'pid' => $post['pid'],
  52. 'sort' => $post['sort'],
  53. ];
  54. if ($post['id'] > 0) {
  55. $data['update_time'] = time();
  56. $result = Db::name('store_category')->where('id', $post['id'])->update($data);
  57. } else {
  58. $data['create_time'] = time();
  59. $result = Db::name('store_category')->insertGetId($data);
  60. }
  61. if ($result) {
  62. return app('json')->success('保存成功');
  63. } else {
  64. return app('json')->fail('保存失败');
  65. }
  66. }
  67. /**
  68. * 删除商品分类
  69. * @param Request $request
  70. */
  71. public function categoryDelete(Request $request)
  72. {
  73. $post = UtilService::getMore([
  74. ['id', '', 'empty', '参数错误'],
  75. ], $request);
  76. // 检查是否有子分类
  77. $hasChild = Db::name('store_category')->where('pid', $post['id'])->count();
  78. if ($hasChild > 0) {
  79. return app('json')->fail('该分类下有子分类,无法删除');
  80. }
  81. // 检查是否有商品
  82. $hasProduct = Db::name('store_product')->where('cate_id', $post['id'])->count();
  83. if ($hasProduct > 0) {
  84. return app('json')->fail('该分类下有商品,无法删除');
  85. }
  86. $result = Db::name('store_category')->where('id', $post['id'])->delete();
  87. if ($result) {
  88. return app('json')->success('删除成功');
  89. } else {
  90. return app('json')->fail('删除失败');
  91. }
  92. }
  93. /**
  94. * 品牌列表
  95. * @param Request $request
  96. */
  97. public function brandList(Request $request)
  98. {
  99. $pageSize = 20;
  100. $post = UtilService::getMore([
  101. ['page', 1],
  102. ['pageSize', 20],
  103. ['keyword', ''],
  104. ], $request);
  105. $where = [];
  106. if (!empty($post['keyword'])) {
  107. $where[] = ['brand_name', 'like', '%' . $post['keyword'] . '%'];
  108. }
  109. $result = (new StoreBrand())->getList($where, $post['page'], $post['pageSize']);
  110. return app('json')->success($result);
  111. }
  112. /**
  113. * 保存品牌
  114. * @param Request $request
  115. */
  116. public function brandSave(Request $request)
  117. {
  118. $post = UtilService::getMore([
  119. ['id', 0],
  120. ['brand_name', '', 'empty', '请输入品牌名称'],
  121. ['brand_logo', ''],
  122. ['sort', 0],
  123. ], $request);
  124. $data = [
  125. 'brand_name' => $post['brand_name'],
  126. 'brand_logo' => $post['brand_logo'],
  127. 'sort' => $post['sort'],
  128. ];
  129. if ($post['id'] > 0) {
  130. $result = Db::name('store_brand')->where('id', $post['id'])->update($data);
  131. } else {
  132. $data['create_time'] = time();
  133. $result = Db::name('store_brand')->insertGetId($data);
  134. }
  135. if ($result) {
  136. return app('json')->success('保存成功');
  137. } else {
  138. return app('json')->fail('保存失败');
  139. }
  140. }
  141. /**
  142. * 删除品牌
  143. * @param Request $request
  144. */
  145. public function brandDelete(Request $request)
  146. {
  147. $post = UtilService::getMore([
  148. ['id', '', 'empty', '参数错误'],
  149. ], $request);
  150. $result = Db::name('store_brand')->where('id', $post['id'])->delete();
  151. if ($result) {
  152. return app('json')->success('删除成功');
  153. } else {
  154. return app('json')->fail('删除失败');
  155. }
  156. }
  157. /**
  158. * 商品列表
  159. * @param Request $request
  160. */
  161. public function productList(Request $request)
  162. {
  163. $pageSize = 20;
  164. $post = UtilService::getMore([
  165. ['page', 1],
  166. ['pageSize', 20],
  167. ['cate_id', ''],
  168. ['brand_id', ''],
  169. ['keyword', ''],
  170. ['status', ''],
  171. ], $request);
  172. $where = [];
  173. if (!empty($post['cate_id'])) {
  174. $where[] = ['cate_id', '=', $post['cate_id']];
  175. }
  176. if (!empty($post['brand_id'])) {
  177. $where[] = ['brand_id', '=', $post['brand_id']];
  178. }
  179. if (!empty($post['keyword'])) {
  180. $where[] = ['store_name', 'like', '%' . $post['keyword'] . '%'];
  181. }
  182. if ($post['status'] !== '' && in_array((string)$post['status'], ["0", "1"])) {
  183. $where[] = ['is_show', '=', (int)$post['status']];
  184. }
  185. $result = (new StoreProduct())->getList($where, $post['page'], $post['pageSize']);
  186. return app('json')->success($result);
  187. }
  188. /**
  189. * 保存商品
  190. * @param Request $request
  191. */
  192. public function productSave(Request $request)
  193. {
  194. $post = UtilService::getMore([
  195. ['id', 0],
  196. ['cate_id', '', 'empty', '请选择分类'],
  197. ['brand_id', 0],
  198. ['store_name', '', 'empty', '请输入商品标题'],
  199. ['image', '', 'empty', '请上传商品图片'],
  200. ['slider_image', ''],
  201. ['price', 0],
  202. ['cost', 0],
  203. ['ot_price', 0],
  204. ['stock', 0],
  205. ['sales', 0],
  206. ['unit_name', '件'],
  207. ['content', ''],
  208. ['sort', 0],
  209. ['status', 1],
  210. ['sku_list', '[]'],
  211. ], $request);
  212. $data = [
  213. 'cate_id' => $post['cate_id'],
  214. 'brand_id' => $post['brand_id'],
  215. 'store_name' => $post['store_name'],
  216. 'image' => $post['image'],
  217. 'slider_image' => $post['slider_image'],
  218. 'price' => $post['price'],
  219. 'cost' => $post['cost'],
  220. 'ot_price' => $post['ot_price'],
  221. 'stock' => $post['stock'],
  222. 'sales' => $post['sales'],
  223. 'unit_name' => $post['unit_name'],
  224. 'content' => $post['content'],
  225. 'sort' => $post['sort'],
  226. 'is_show' => $post['status'] == 1 ? 1 : 0,
  227. ];
  228. Db::startTrans();
  229. try {
  230. if ($post['id'] > 0) {
  231. // 更新商品
  232. Db::name('store_product')->where('id', $post['id'])->update($data);
  233. $productId = $post['id'];
  234. // 删除原有SKU
  235. Db::name('store_product_attr_value')->where('product_id', $productId)->delete();
  236. } else {
  237. // 新增商品
  238. $data['time'] = time();
  239. $productId = Db::name('store_product')->insertGetId($data);
  240. }
  241. // 保存SKU
  242. $skuList = json_decode($post['sku_list'], true);
  243. if (!empty($skuList)) {
  244. foreach ($skuList as $sku) {
  245. $skuData = [
  246. 'product_id' => $productId,
  247. 'suk' => $sku['suk'] ?? '',
  248. 'price' => $sku['price'] ?? $post['price'],
  249. 'cost' => $sku['cost'] ?? $post['cost'],
  250. 'stock' => $sku['stock'] ?? 0,
  251. 'bar_code' => $sku['bar_code'] ?? '',
  252. ];
  253. Db::name('store_product_attr_value')->insert($skuData);
  254. }
  255. }
  256. Db::commit();
  257. return app('json')->success('保存成功');
  258. } catch (\Exception $e) {
  259. Db::rollback();
  260. return app('json')->fail('保存失败');
  261. }
  262. }
  263. /**
  264. * 删除商品
  265. * @param Request $request
  266. */
  267. public function productDelete(Request $request)
  268. {
  269. $post = UtilService::getMore([
  270. ['id', '', 'empty', '参数错误'],
  271. ], $request);
  272. Db::startTrans();
  273. try {
  274. // 删除商品
  275. Db::name('store_product')->where('id', $post['id'])->delete();
  276. // 删除SKU
  277. Db::name('store_product_attr_value')->where('product_id', $post['id'])->delete();
  278. Db::commit();
  279. return app('json')->success('删除成功');
  280. } catch (\Exception $e) {
  281. Db::rollback();
  282. return app('json')->fail('删除失败');
  283. }
  284. }
  285. }