StoreGoods.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\mall\controller;
  3. use app\common\model\GoodsSeason;
  4. use app\common\model\GoodsTerritory;
  5. use app\common\model\User;
  6. use library\Controller;
  7. use think\Db;
  8. use app\common\model\GoodsCate;
  9. use app\common\model\StoreGoodsItem;
  10. use library\tools\Data;
  11. /**
  12. * 商品管理
  13. * Class StoreGoods
  14. * @package app\mall\controller
  15. */
  16. class StoreGoods extends Controller
  17. {
  18. /**
  19. * 绑定数据表
  20. * @var string
  21. */
  22. protected $table = 'StoreGoods';
  23. /**
  24. * 商品列表
  25. * @auth true
  26. * @menu true
  27. * @throws \think\Exception
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. * @throws \think\exception\PDOException
  32. */
  33. public function index()
  34. {
  35. $this->title = '商品管理';
  36. $where = [];
  37. $where['is_deleted'] = 0;
  38. $query = $this->_query($this->table)->where($where)->like('name');
  39. $query->dateBetween('create_at')->order('sort desc , id desc')->page();
  40. }
  41. /**
  42. * 数据列表处理
  43. * @auth true
  44. * @menu true
  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 = GoodsCate::getCateTree();
  53. $list = Db::name('StoreGoodsItem')->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){
  57. if ($goods['goods_id'] === $vo['id']) array_push($vo['list'], $goods);
  58. }
  59. }
  60. }
  61. /**
  62. * 添加商品
  63. * @auth true
  64. * @menu true
  65. * @throws \think\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. * @throws \think\exception\PDOException
  70. */
  71. public function add()
  72. {
  73. $this->title = '添加商品';
  74. $this->isAddMode = '1';
  75. $this->_form($this->table, 'form');
  76. }
  77. /**
  78. * 编辑商品
  79. * @auth true
  80. * @menu true
  81. * @throws \think\Exception
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. * @throws \think\exception\PDOException
  86. */
  87. function edit()
  88. {
  89. $this->title = '编辑商品';
  90. $this->isAddMode = '0';
  91. $this->_form($this->table, 'form');
  92. }
  93. /**
  94. * 表单数据处理
  95. * @param array $data
  96. */
  97. protected function _form_filter(&$data)
  98. {
  99. if($this->request->isGet()){
  100. $fields = 'goods_spec,goods_id,status,original_price,sell_price,virtual,cover pic,goods_no sku,weight';
  101. $defaultValues = [];
  102. if(isset_full($data,'id')) $defaultValues = Db::name('StoreGoodsItem')->where(['goods_id' => $data['id']])->column($fields);
  103. $this->defaultValues = json_encode($defaultValues, JSON_UNESCAPED_UNICODE);
  104. $this->goods_cate = GoodsCate::getCateTree();
  105. }
  106. // 添加或编辑商品
  107. if($this->request->isPost() && in_array($this->request->action(),['add','edit'])){
  108. list($data) = [$this->request->post()];
  109. $check_price = true;
  110. foreach (json_decode($data['lists'], true) as $vo){
  111. if( $vo[0]['sell_price'] <=0 )$check_price = false;
  112. }
  113. if(!$check_price) $this->error('价格设置有误');
  114. }
  115. }
  116. /**
  117. * 商品上架
  118. * @auth true
  119. * @menu true
  120. * @throws \think\Exception
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @throws \think\exception\DbException
  124. * @throws \think\exception\PDOException
  125. */
  126. public function up()
  127. {
  128. $this->_save($this->table, ['status' => '1','is_edit'=>0]);
  129. }
  130. /**
  131. * 商品下架
  132. * @auth true
  133. * @menu true
  134. * @throws \think\Exception
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. * @throws \think\exception\DbException
  138. * @throws \think\exception\PDOException
  139. */
  140. public function down()
  141. {
  142. $this->_save($this->table, ['status' => '0']);
  143. }
  144. /**
  145. * 商品删除
  146. * @auth true
  147. * @menu true
  148. * @throws \think\Exception
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. * @throws \think\exception\PDOException
  153. */
  154. public function remove()
  155. {
  156. $this->_save($this->table, ['is_deleted' => '1']);
  157. }
  158. /**
  159. * 商品库存入库
  160. * @auth true
  161. * @throws \think\Exception
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. * @throws \think\exception\DbException
  165. * @throws \think\exception\PDOException
  166. */
  167. public function stock()
  168. {
  169. if ($this->request->isGet()) {
  170. $goods_id = $this->request->get('id');
  171. $goods_info = Db::name('StoreGoods')->where(['id' => $goods_id])->find();
  172. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  173. $goods_info['list'] = Db::name('StoreGoodsItem')->where(['goods_id' => $goods_id])->select();
  174. $this->fetch('', ['vo' => $goods_info]);
  175. } else {
  176. list($post, $data) = [$this->request->post(), []];
  177. if (isset($post['id']) && isset($post['goods_id']) && is_array($post['goods_id'])) {
  178. foreach (array_keys($post['goods_id']) as $key) {
  179. if ($post['goods_number'][$key] > 0) array_push($data, [
  180. 'goods_id' => $post['goods_id'][$key],
  181. 'goods_spec' => $post['goods_spec'][$key],
  182. 'number_stock' => $post['goods_number'][$key],
  183. ]);
  184. }
  185. if (!empty($data)) {
  186. Db::name('GoodsStock')->insertAll($data);
  187. foreach ($data as $dv) {
  188. Db::name('StoreGoodsItem')->where(['goods_id'=>$dv['goods_id'],'goods_spec'=>$dv['goods_spec']])->setInc('stock',$dv['number_stock']);
  189. Db::name('StoreGoodsItem')->where(['goods_id'=>$dv['goods_id'],'goods_spec'=>$dv['goods_spec']])->setInc('base_stock',$dv['number_stock']);
  190. }
  191. Db::name('StoreGoods')->where('id',$post['id'])->setInc('stock',array_sum(array_column($data,'number_stock')));
  192. Db::name('StoreGoods')->where('id',$post['id'])->setInc('base_stock',array_sum(array_column($data,'number_stock')));
  193. $this->success('商品信息入库成功!');
  194. }
  195. }
  196. $this->error('没有需要商品入库的数据!');
  197. }
  198. }
  199. /**
  200. * 商品参数设置
  201. * @auth true
  202. * @throws \think\Exception
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\ModelNotFoundException
  205. * @throws \think\exception\DbException
  206. * @throws \think\exception\PDOException
  207. */
  208. public function param()
  209. {
  210. if ($this->request->isGet()) {
  211. $goods_id = $this->request->get('id');
  212. $goods_info = Db::name('StoreGoods')->where(['id' => $goods_id])->find();
  213. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  214. $param = Db::name('goods_param')->where('goods_id',$goods_id)->find();
  215. $param_set = !$param ? [['title'=>'','value'=>'']]:json_decode($param['goods_param'],true);
  216. $this->fetch('', ['goods_info' => $goods_info,'param_set'=>$param_set]);
  217. }else{
  218. $goods_id = input('post.goods_id');
  219. $title_arr= input('post.title');
  220. $value_arr= input('post.value');
  221. if(empty($title_arr)) $this->error('请设置商品参数');
  222. $param_post = [];
  223. foreach ($title_arr as $k=>$t){
  224. $param_post[] = ['title'=>$t,'value'=>$value_arr[$k]];
  225. }
  226. Data::save('GoodsParam',['goods_id'=>$goods_id,'goods_param'=>json_encode($param_post)],'goods_id',['goods_id'=>$goods_id]);
  227. $this->success('保存成功!');
  228. }
  229. }
  230. /**
  231. * 添加完成商品之后逻辑处理
  232. * @param $result
  233. */
  234. protected function _form_result($result)
  235. {
  236. if ($result && $this->request->isPost() && in_array($this->request->action(),['add','edit'])) {
  237. list($data) = [$this->request->post()];
  238. $data['id'] = $result;
  239. $low_price = 0;
  240. foreach (json_decode($data['lists'], true) as $vo){
  241. if($low_price == 0 || $vo[0]['sell_price'] < $low_price )$low_price = $vo[0]['sell_price'];
  242. Data::save('StoreGoodsItem', [
  243. 'goods_id' => $data['id'],
  244. 'goods_spec' => $vo[0]['key'],
  245. 'goods_no' => $vo[0]['sku'],
  246. 'original_price' => $vo[0]['original_price'],
  247. 'sell_price' => $vo[0]['sell_price'],
  248. 'virtual' => $vo[0]['virtual'],
  249. 'status' => $vo[0]['status'] ? 1 : 0,
  250. 'weight' => $vo[0]['weight'] ? $vo[0]['weight'] : 0,
  251. ], 'goods_spec', ['goods_id' => $data['id']]);
  252. }
  253. Db::name('StoreGoods')->where(['id'=>$data['id']])->update(['low_price'=>$low_price]);
  254. $this->success('商品编辑成功!', 'javascript:history.back()');
  255. }
  256. }
  257. }