Goods.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\store\controller;
  14. use library\Controller;
  15. use library\tools\Data;
  16. use think\Db;
  17. /**
  18. * 商品信息管理
  19. * Class Goods
  20. * @package app\store\controller
  21. */
  22. class Goods extends Controller
  23. {
  24. /**
  25. * 指定数据表
  26. * @var string
  27. */
  28. protected $table = 'StoreGoods';
  29. /**
  30. * 商品信息管理
  31. * @return mixed
  32. * @throws \think\Exception
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. * @throws \think\exception\PDOException
  37. */
  38. public function index()
  39. {
  40. $this->title = '商品信息管理';
  41. $this->_query($this->table)->equal('status,cate_id')->like('title')->where(['is_deleted' => '0'])->order('sort asc,id desc')->page();
  42. }
  43. /**
  44. * 数据列表处理
  45. * @param array $data
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. protected function _index_page_filter(&$data)
  51. {
  52. $this->clist = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->select();
  53. $list = Db::name('StoreGoodsList')->where('status', '1')->whereIn('goods_id', array_unique(array_column($data, 'id')))->select();
  54. foreach ($data as &$vo) {
  55. list($vo['list'], $vo['cate']) = [[], []];
  56. foreach ($list as $goods) if ($goods['goods_id'] === $vo['id']) array_push($vo['list'], $goods);
  57. foreach ($this->clist as $cate) if ($cate['id'] === $vo['cate_id']) $vo['cate'] = $cate;
  58. }
  59. }
  60. /**
  61. * 商品库存入库
  62. * @return mixed
  63. * @throws \think\Exception
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. * @throws \think\exception\PDOException
  68. */
  69. public function stock()
  70. {
  71. if ($this->request->isGet()) {
  72. $GoodsId = $this->request->get('id');
  73. $goods = Db::name('StoreGoods')->where(['id' => $GoodsId])->find();
  74. empty($goods) && $this->error('无效的商品信息,请稍候再试!');
  75. $goods['list'] = Db::name('StoreGoodsList')->where(['goods_id' => $GoodsId])->select();
  76. return $this->fetch('', ['vo' => $goods]);
  77. }
  78. list($post, $data) = [$this->request->post(), []];
  79. if (isset($post['id']) && isset($post['goods_id']) && is_array($post['goods_id'])) {
  80. foreach (array_keys($post['goods_id']) as $key) if ($post['goods_number'][$key] > 0) array_push($data, [
  81. 'goods_id' => $post['goods_id'][$key],
  82. 'goods_spec' => $post['goods_spec'][$key],
  83. 'number_stock' => $post['goods_number'][$key],
  84. ]);
  85. if (!empty($data)) {
  86. Db::name('StoreGoodsStock')->insertAll($data);
  87. \app\store\service\GoodsService::syncStock($post['id']);
  88. $this->success('商品信息入库成功!');
  89. }
  90. }
  91. $this->error('没有需要商品入库的数据!');
  92. }
  93. /**
  94. * 添加商品信息
  95. * @return mixed
  96. */
  97. public function add()
  98. {
  99. $this->title = '添加商品信息';
  100. $this->isAddMode = '1';
  101. return $this->_form($this->table, 'form');
  102. }
  103. /**
  104. * 编辑商品信息
  105. * @return mixed
  106. */
  107. public function edit()
  108. {
  109. $this->title = '编辑商品信息';
  110. $this->isAddMode = '0';
  111. return $this->_form($this->table, 'form');
  112. }
  113. /**
  114. * 表单数据处理
  115. * @param array $data
  116. * @throws \think\Exception
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. * @throws \think\exception\PDOException
  121. */
  122. protected function _form_filter(&$data)
  123. {
  124. // 生成商品ID
  125. if (empty($data['id'])) $data['id'] = Data::uniqidNumberCode(10);
  126. if ($this->request->isGet()) {
  127. $fields = 'goods_spec,goods_id,status,price_market market,price_selling selling,number_virtual `virtual`,number_express express';
  128. $defaultValues = Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->column($fields);
  129. $this->defaultValues = json_encode($defaultValues, JSON_UNESCAPED_UNICODE);
  130. $this->cates = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->order('sort asc,id desc')->select();
  131. } elseif ($this->request->isPost()) {
  132. Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->update(['status' => '0']);
  133. foreach (json_decode($data['lists'], true) as $vo) Data::save('StoreGoodsList', [
  134. 'goods_id' => $data['id'],
  135. 'goods_spec' => $vo[0]['key'],
  136. 'price_market' => $vo[0]['market'],
  137. 'price_selling' => $vo[0]['selling'],
  138. 'number_virtual' => $vo[0]['virtual'],
  139. 'number_express' => $vo[0]['express'],
  140. 'status' => $vo[0]['status'] ? 1 : 0,
  141. ], 'goods_spec', ['goods_id' => $data['id']]);
  142. }
  143. }
  144. /**
  145. * 表单结果处理
  146. * @param boolean $result
  147. */
  148. protected function _form_result($result)
  149. {
  150. if ($result && $this->request->isPost()) {
  151. $this->success('商品编辑成功!', 'javascript:history.back()');
  152. }
  153. }
  154. /**
  155. * 禁用商品信息
  156. */
  157. public function forbid()
  158. {
  159. $this->_save($this->table, ['status' => '0']);
  160. }
  161. /**
  162. * 启用商品信息
  163. */
  164. public function resume()
  165. {
  166. $this->_save($this->table, ['status' => '1']);
  167. }
  168. /**
  169. * 删除商品信息
  170. */
  171. public function remove()
  172. {
  173. $this->_delete($this->table);
  174. }
  175. }