Product.php 9.3 KB

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