Goods.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. namespace app\admin\controller\shopro\goods;
  3. use app\admin\controller\shopro\Common;
  4. use app\admin\model\shopro\goods\Goods as GoodsModel;
  5. use app\admin\model\shopro\goods\Sku as SkuModel;
  6. use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
  7. use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
  8. use app\admin\model\shopro\activity\Activity as ActivityModel;
  9. use app\admin\controller\shopro\traits\SkuPrice as SkuPriceTrait;
  10. use addons\shopro\traits\StockWarning as StockWarningTrait;
  11. use addons\shopro\service\goods\GoodsService;
  12. use think\Db;
  13. /**
  14. * 商品管理
  15. */
  16. class Goods extends Common
  17. {
  18. use SkuPriceTrait, StockWarningTrait;
  19. protected $noNeedRight = ['getType', 'select', 'activitySelect'];
  20. /**
  21. * 商品模型对象
  22. * @var \app\admin\model\shopro\goods\Goods
  23. */
  24. protected $model = null;
  25. protected $activityModel = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new GoodsModel;
  30. $this->activityModel = new ActivityModel;
  31. }
  32. /**
  33. * 查看
  34. *
  35. * @return string|Json
  36. * @throws \think\Exception
  37. * @throws DbException
  38. */
  39. public function index()
  40. {
  41. if (!$this->request->isAjax()) {
  42. return $this->view->fetch();
  43. }
  44. $goodsTableName = $this->model->getQuery()->getTable();
  45. $goods = $this->model->sheepFilter()->with(['max_sku_price']);
  46. // 聚合库存 (包含下架的规格)
  47. $skuSql = SkuPriceModel::field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
  48. $goods = $goods->join([$skuSql => 'sp'], $goodsTableName . '.id = sp.sku_goods_id', 'left')
  49. ->field("$goodsTableName.*, sp.stock") // ,score.*
  50. ->paginate($this->request->param('list_rows', 10))->each(function ($goods) {
  51. // 获取活动信息
  52. $goods->activities = $goods->activities;
  53. $goods->promos = $goods->promos;
  54. $data_type = request()->param('data_type', ''); // 特殊 type 需要处理的数据
  55. if ($data_type == 'score_shop') {
  56. $goods->is_score_shop = $goods->is_score_shop;
  57. }
  58. });
  59. $this->success('获取成功', null, $goods);
  60. }
  61. // 获取数据类型
  62. public function getType()
  63. {
  64. $activityTypes = $this->activityModel->typeList();
  65. $statusList = $this->model->statusList();
  66. $result = [
  67. 'activity_type' => $activityTypes,
  68. 'status' => $statusList
  69. ];
  70. $data = [];
  71. foreach ($result as $key => $list) {
  72. $data[$key][] = ['name' => '全部', 'type' => 'all'];
  73. foreach ($list as $k => $v) {
  74. $data[$key][] = [
  75. 'name' => $v,
  76. 'type' => $k
  77. ];
  78. }
  79. }
  80. $this->success('获取成功', null, $data);
  81. }
  82. /**
  83. * 添加
  84. */
  85. public function add()
  86. {
  87. if (!$this->request->isAjax()) {
  88. return $this->view->fetch();
  89. }
  90. $params = $this->request->only([
  91. 'type', 'title', 'subtitle', 'category_ids', 'image', 'images', 'image_wh', 'params',
  92. 'original_price', 'price', 'is_sku', 'limit_type', 'limit_num', 'sales_show_type',
  93. 'stock_show_type', 'show_sales', 'service_ids', 'dispatch_type', 'dispatch_id', 'is_offline','yushou', 'status', 'weigh',
  94. ]); // likes, views, sales,
  95. $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
  96. $this->svalidate($params, ".add");
  97. if (!$params['is_sku']) {
  98. // 校验单规格属性
  99. $sku_params = $this->request->only(['stock', 'stock_warning', 'sn', 'weight', 'cost_price', 'original_price', 'price']);
  100. $this->svalidate($sku_params, '.sku_params');
  101. }
  102. $data = Db::transaction(function () use ($params) {
  103. $this->model->save($params);
  104. $this->editSku($this->model, 'add');
  105. });
  106. $this->success('保存成功', null, $data);
  107. }
  108. /**
  109. * 详情
  110. *
  111. * @param $id
  112. * @return \think\Response
  113. */
  114. public function detail($id)
  115. {
  116. $goods = $this->model->where('id', $id)->find();
  117. if (!$goods) {
  118. $this->error(__('No Results were found'));
  119. }
  120. $goods->category_ids_arr = $goods->category_ids_arr;
  121. if ($goods->is_sku) {
  122. $goods->skus = $goods->skus;
  123. $goods->sku_prices = $goods->sku_prices;
  124. } else {
  125. // 将单规格的部分数据直接放到 row 上
  126. $goodsSkuPrice = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find();
  127. $goods->stock = $goodsSkuPrice->stock;
  128. $goods->sn = $goodsSkuPrice->sn;
  129. $goods->weight = $goodsSkuPrice->weight;
  130. $goods->stock_warning = $goodsSkuPrice->stock_warning;
  131. $goods->cost_price = $goodsSkuPrice->cost_price;
  132. }
  133. $content = $goods['content'];
  134. $goods = $goods->toArray();
  135. $goods['content'] = $content;
  136. $this->success('保存成功', null, $goods);
  137. }
  138. /**
  139. * 编辑(支持批量)
  140. */
  141. public function edit($id = null)
  142. {
  143. if (!$this->request->isAjax()) {
  144. return $this->view->fetch('add');
  145. }
  146. $params = $this->request->only([
  147. 'type', 'title', 'subtitle', 'image', 'images', 'image_wh', 'params',
  148. 'original_price', 'price', 'is_sku', 'limit_type', 'limit_num', 'sales_show_type',
  149. 'stock_show_type', 'show_sales', 'service_ids', 'dispatch_type', 'dispatch_id', 'is_offline', 'yushou', 'status', 'weigh',
  150. ]); // likes, views, sales,
  151. $this->request->has('content') && $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
  152. $this->svalidate($params);
  153. isset($params['is_sku']) && $params['category_ids'] = $this->request->param('category_ids', ''); // 分类不判空
  154. isset($params['is_sku']) && $params['params'] = $this->request->param('params/a', []); // 编辑如果没有传 params 赋值为空
  155. if (isset($params['is_sku']) && !$params['is_sku']) {
  156. // 校验单规格属性
  157. $sku_params = $this->request->only(['stock_warning', 'sn', 'weight', 'cost_price', 'original_price', 'price']);
  158. $this->svalidate($sku_params, 'sku_params');
  159. }
  160. $id = explode(',', $id);
  161. $items = $this->model->whereIn('id', $id)->select();
  162. Db::transaction(function () use ($items, $params) {
  163. foreach ($items as $goods) {
  164. $goods->save($params);
  165. if (isset($params['is_sku'])) {
  166. // 编辑商品(如果没有 is_sku 就是批量编辑上下架等)
  167. $this->editSku($goods, 'edit');
  168. }
  169. }
  170. });
  171. $this->success('更新成功', null);
  172. }
  173. public function addStock($id)
  174. {
  175. if (!$this->request->isAjax()) {
  176. return $this->view->fetch();
  177. }
  178. $goods = $this->model->where('id', $id)->find();
  179. if (!$goods) {
  180. $this->error(__('No Results were found'));
  181. }
  182. if ($goods->is_sku) {
  183. // 多规格
  184. $skuPrices = $this->request->post('sku_prices/a', []);
  185. foreach ($skuPrices as $skuPrice) {
  186. if (isset($skuPrice['add_stock']) && $skuPrice['add_stock'] != 0 && $skuPrice['id']) {
  187. $skuPriceModel = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find($skuPrice['id']);
  188. if ($skuPriceModel) {
  189. Db::transaction(function () use ($skuPriceModel, $skuPrice) {
  190. $this->addStockToSkuPrice($skuPriceModel, $skuPrice['add_stock'], 'goods');
  191. });
  192. }
  193. }
  194. }
  195. } else {
  196. $add_stock = $this->request->param('add_stock', 0);
  197. $skuPriceModel = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find();
  198. if ($skuPriceModel) {
  199. Db::transaction(function () use ($skuPriceModel, $add_stock) {
  200. $this->addStockToSkuPrice($skuPriceModel, $add_stock, 'goods');
  201. });
  202. }
  203. }
  204. $this->success('补货成功');
  205. }
  206. public function select()
  207. {
  208. if (!$this->request->isAjax()) {
  209. return $this->view->fetch();
  210. }
  211. $type = $this->request->param('type', 'page');
  212. $goodsTableName = $this->model->getQuery()->getTable();
  213. $goods = $this->model->sheepFilter()->with(['max_sku_price']);
  214. // 聚合库存 (包含下架的规格)
  215. $skuSql = SkuPriceModel::field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
  216. $goods = $goods->join([$skuSql => 'sp'], $goodsTableName . '.id = sp.sku_goods_id', 'left')
  217. ->field("$goodsTableName.*, sp.stock"); // ,score.*
  218. if ($type == 'select') {
  219. // 普通结果
  220. $goods = collection($goods->select());
  221. } else {
  222. // 分页结果
  223. $goods = $goods->paginate($this->request->param('list_rows', 10));
  224. }
  225. $goods = $goods->each(function ($goods) {
  226. // 获取活动信息
  227. $goods->activities = $goods->activities;
  228. $goods->promos = $goods->promos;
  229. $data_type = $this->request->param('data_type', ''); // 特殊 type 需要处理的数据
  230. if ($data_type == 'score_shop') {
  231. $goods->is_score_shop = $goods->is_score_shop;
  232. }
  233. });
  234. $this->success('获取成功', null, $goods);
  235. }
  236. /**
  237. * 获取指定活动相关商品
  238. *
  239. * @param Request $request
  240. * @return void
  241. */
  242. public function activitySelect()
  243. {
  244. $activity_id = $this->request->param('activity_id');
  245. $need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
  246. $activity = $this->activityModel->where('id', $activity_id)->find();
  247. if (!$activity) {
  248. $this->error(__('No Results were found'));
  249. }
  250. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  251. // 存一下,获取器获取指定活动的时候会用到
  252. foreach ($goodsIds as $id) {
  253. session('goods-activity_id:' . $id, $activity_id);
  254. }
  255. $service = new GoodsService(function ($goods) use ($need_buyers) {
  256. if ($need_buyers) {
  257. $goods->buyers = $goods->buyers;
  258. }
  259. $goods->activity = $goods->activity;
  260. return $goods;
  261. });
  262. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->select();
  263. $goods = collection($goods)->toArray(); // 可以将里面的单个 model也转为数组
  264. foreach ($goods as &$gd) {
  265. unset($gd['new_sku_prices'], $gd['activity']);
  266. }
  267. $this->success('获取成功', null, $goods);
  268. }
  269. /**
  270. * 删除(支持批量)
  271. *
  272. * @param $id
  273. * @return \think\Response
  274. */
  275. public function delete($id)
  276. {
  277. if (empty($id)) {
  278. $this->error(__('Parameter %s can not be empty', 'id'));
  279. }
  280. $list = $this->model->where('id', 'in', $id)->select();
  281. $result = Db::transaction(function () use ($list) {
  282. $count = 0;
  283. foreach ($list as $item) {
  284. // 删除相关库存预警记录
  285. StockWarningModel::destroy(function ($query) use ($item) {
  286. $query->where('goods_id', $item->id);
  287. });
  288. $count += $item->delete();
  289. }
  290. return $count;
  291. });
  292. if ($result) {
  293. $this->success('删除成功', null, $result);
  294. } else {
  295. $this->error(__('No rows were deleted'));
  296. }
  297. }
  298. public function recyclebin()
  299. {
  300. if (!$this->request->isAjax()) {
  301. return $this->view->fetch();
  302. }
  303. $goods = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
  304. $this->success('获取成功', null, $goods);
  305. }
  306. /**
  307. * 还原(支持批量)
  308. *
  309. * @param $id
  310. * @return \think\Response
  311. */
  312. public function restore($id = null)
  313. {
  314. if (empty($id)) {
  315. $this->error(__('Parameter %s can not be empty', 'id'));
  316. }
  317. $items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
  318. $result = Db::transaction(function () use ($items) {
  319. $count = 0;
  320. foreach ($items as $item) {
  321. $count += $item->restore();
  322. }
  323. return $count;
  324. });
  325. if ($result) {
  326. $this->success('还原成功', null, $result);
  327. } else {
  328. $this->error(__('No rows were updated'));
  329. }
  330. }
  331. /**
  332. * 销毁(支持批量)
  333. *
  334. * @param $id
  335. * @return \think\Response
  336. */
  337. public function destroy($id = null)
  338. {
  339. if (empty($id)) {
  340. $this->error(__('Parameter %s can not be empty', 'id'));
  341. }
  342. if ($id !== 'all') {
  343. $items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
  344. } else {
  345. $items = $this->model->onlyTrashed()->select();
  346. }
  347. $result = Db::transaction(function () use ($items) {
  348. $count = 0;
  349. foreach ($items as $goods) {
  350. // 删除商品相关的规格,规格记录
  351. SkuModel::where('goods_id', $goods->id)->delete();
  352. SkuPriceModel::where('goods_id', $goods->id)->delete();
  353. // 删除商品
  354. $count += $goods->delete(true);
  355. }
  356. return $count;
  357. });
  358. if ($result) {
  359. $this->success('销毁成功', null, $result);
  360. }
  361. $this->error('销毁失败');
  362. }
  363. }