Product.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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[] = ['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. ['name', '', 'empty', '请输入分类名称'],
  46. ['pid', 0],
  47. ['sort', 0],
  48. ['status', 1],
  49. ], $request);
  50. $data = [
  51. 'name' => $post['name'],
  52. 'pid' => $post['pid'],
  53. 'sort' => $post['sort'],
  54. 'status' => $post['status'] == 1 ? 1 : 0,
  55. ];
  56. if ($post['id'] > 0) {
  57. $result = Db::name('store_category')->where('id', $post['id'])->update($data);
  58. } else {
  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('category_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[] = ['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. ['name', '', 'empty', '请输入品牌名称'],
  121. ['image', ''],
  122. ['sort', 0],
  123. ['status', 1],
  124. ], $request);
  125. $data = [
  126. 'name' => $post['name'],
  127. 'image' => $post['image'],
  128. 'sort' => $post['sort'],
  129. 'status' => $post['status'] == 1 ? 1 : 0,
  130. ];
  131. if ($post['id'] > 0) {
  132. $result = Db::name('store_brand')->where('id', $post['id'])->update($data);
  133. } else {
  134. $result = Db::name('store_brand')->insertGetId($data);
  135. }
  136. if ($result) {
  137. return app('json')->success('保存成功');
  138. } else {
  139. return app('json')->fail('保存失败');
  140. }
  141. }
  142. /**
  143. * 删除品牌
  144. * @param Request $request
  145. */
  146. public function brandDelete(Request $request)
  147. {
  148. $post = UtilService::getMore([
  149. ['id', '', 'empty', '参数错误'],
  150. ], $request);
  151. $result = Db::name('store_brand')->where('id', $post['id'])->delete();
  152. if ($result) {
  153. return app('json')->success('删除成功');
  154. } else {
  155. return app('json')->fail('删除失败');
  156. }
  157. }
  158. /**
  159. * 商品列表
  160. * @param Request $request
  161. */
  162. public function productList(Request $request)
  163. {
  164. $pageSize = 20;
  165. $post = UtilService::getMore([
  166. ['page', 1],
  167. ['pageSize', 20],
  168. ['category_id', ''],
  169. ['brand_id', ''],
  170. ['keyword', ''],
  171. ['status', ''],
  172. ], $request);
  173. $where = [];
  174. if (!empty($post['category_id'])) {
  175. $where[] = ['category_id', '=', $post['category_id']];
  176. }
  177. if (!empty($post['brand_id'])) {
  178. $where[] = ['brand_id', '=', $post['brand_id']];
  179. }
  180. if (!empty($post['keyword'])) {
  181. $where[] = ['title', 'like', '%' . $post['keyword'] . '%'];
  182. }
  183. if ($post['status'] !== '' && in_array((string)$post['status'], ["0", "1"])) {
  184. $where[] = ['is_show', '=', (int)$post['status']];
  185. }
  186. $result = (new StoreProduct())->getList($where, $post['page'], $post['pageSize']);
  187. return app('json')->success($result);
  188. }
  189. /**
  190. * 保存商品
  191. * @param Request $request
  192. */
  193. public function productSave(Request $request)
  194. {
  195. $post = UtilService::getMore([
  196. ['id', 0],
  197. ['category_id', '', 'empty', '请选择分类'],
  198. ['brand_id', 0],
  199. ['title', '', 'empty', '请输入商品标题'],
  200. ['image', '', 'empty', '请上传商品图片'],
  201. ['images', ''],
  202. ['price', 0],
  203. ['cost_price', 0],
  204. ['ot_price', 0],
  205. ['stock', 0],
  206. ['sales', 0],
  207. ['unit_name', '件'],
  208. ['description', ''],
  209. ['sort', 0],
  210. ['status', 1],
  211. ['sku_list', '[]'],
  212. ], $request);
  213. $data = [
  214. 'category_id' => $post['category_id'],
  215. 'brand_id' => $post['brand_id'],
  216. 'title' => $post['title'],
  217. 'image' => $post['image'],
  218. 'images' => $post['images'],
  219. 'price' => $post['price'],
  220. 'cost_price' => $post['cost_price'],
  221. 'ot_price' => $post['ot_price'],
  222. 'stock' => $post['stock'],
  223. 'sales' => $post['sales'],
  224. 'unit_name' => $post['unit_name'],
  225. 'description' => $post['description'],
  226. 'sort' => $post['sort'],
  227. 'is_show' => $post['status'] == 1 ? 1 : 0,
  228. ];
  229. Db::startTrans();
  230. try {
  231. if ($post['id'] > 0) {
  232. // 更新商品
  233. Db::name('store_product')->where('id', $post['id'])->update($data);
  234. $productId = $post['id'];
  235. // 删除原有SKU
  236. Db::name('store_product_attr_value')->where('product_id', $productId)->delete();
  237. } else {
  238. // 新增商品
  239. $data['time'] = time();
  240. $productId = Db::name('store_product')->insertGetId($data);
  241. }
  242. // 保存SKU
  243. $skuList = json_decode($post['sku_list'], true);
  244. if (!empty($skuList)) {
  245. foreach ($skuList as $sku) {
  246. $skuData = [
  247. 'product_id' => $productId,
  248. 'suk' => $sku['suk'] ?? '',
  249. 'price' => $sku['price'] ?? $post['price'],
  250. 'cost_price' => $sku['cost_price'] ?? $post['cost_price'],
  251. 'stock' => $sku['stock'] ?? 0,
  252. 'bar_code' => $sku['bar_code'] ?? '',
  253. ];
  254. Db::name('store_product_attr_value')->insert($skuData);
  255. }
  256. }
  257. Db::commit();
  258. return app('json')->success('保存成功');
  259. } catch (\Exception $e) {
  260. Db::rollback();
  261. return app('json')->fail('保存失败');
  262. }
  263. }
  264. /**
  265. * 删除商品
  266. * @param Request $request
  267. */
  268. public function productDelete(Request $request)
  269. {
  270. $post = UtilService::getMore([
  271. ['id', '', 'empty', '参数错误'],
  272. ], $request);
  273. Db::startTrans();
  274. try {
  275. // 删除商品
  276. Db::name('store_product')->where('id', $post['id'])->delete();
  277. // 删除SKU
  278. Db::name('store_product_attr_value')->where('product_id', $post['id'])->delete();
  279. Db::commit();
  280. return app('json')->success('删除成功');
  281. } catch (\Exception $e) {
  282. Db::rollback();
  283. return app('json')->fail('删除失败');
  284. }
  285. }
  286. }