Product.php 9.6 KB

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