StoreGoods.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $this->express= Db::name('freight_template')->select();
  106. }
  107. // 添加或编辑商品
  108. if($this->request->isPost() && in_array($this->request->action(),['add','edit'])){
  109. list($data) = [$this->request->post()];
  110. $check_price = true;
  111. foreach (json_decode($data['lists'], true) as $vo){
  112. if( $vo[0]['sell_price'] <=0 )$check_price = false;
  113. }
  114. if(!$check_price) $this->error('价格设置有误');
  115. }
  116. }
  117. /**
  118. * 商品上架
  119. * @auth true
  120. * @menu true
  121. * @throws \think\Exception
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. * @throws \think\exception\PDOException
  126. */
  127. public function up()
  128. {
  129. $this->_save($this->table, ['status' => '1','is_edit'=>0]);
  130. }
  131. /**
  132. * 商品下架
  133. * @auth true
  134. * @menu true
  135. * @throws \think\Exception
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. * @throws \think\exception\DbException
  139. * @throws \think\exception\PDOException
  140. */
  141. public function down()
  142. {
  143. $this->_save($this->table, ['status' => '0']);
  144. }
  145. /**
  146. * 商品删除
  147. * @auth true
  148. * @menu true
  149. * @throws \think\Exception
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * @throws \think\exception\DbException
  153. * @throws \think\exception\PDOException
  154. */
  155. public function remove()
  156. {
  157. $this->_save($this->table, ['is_deleted' => '1']);
  158. }
  159. /**
  160. * 商品库存入库
  161. * @auth true
  162. * @throws \think\Exception
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. * @throws \think\exception\PDOException
  167. */
  168. public function stock()
  169. {
  170. if ($this->request->isGet()) {
  171. $goods_id = $this->request->get('id');
  172. $goods_info = Db::name('StoreGoods')->where(['id' => $goods_id])->find();
  173. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  174. $goods_info['list'] = Db::name('StoreGoodsItem')->where(['goods_id' => $goods_id])->select();
  175. $this->fetch('', ['vo' => $goods_info]);
  176. } else {
  177. list($post, $data) = [$this->request->post(), []];
  178. if (isset($post['id']) && isset($post['goods_id']) && is_array($post['goods_id'])) {
  179. foreach (array_keys($post['goods_id']) as $key) {
  180. if ($post['goods_number'][$key] > 0) array_push($data, [
  181. 'goods_id' => $post['goods_id'][$key],
  182. 'goods_spec' => $post['goods_spec'][$key],
  183. 'number_stock' => $post['goods_number'][$key],
  184. ]);
  185. }
  186. if (!empty($data)) {
  187. Db::name('GoodsStock')->insertAll($data);
  188. foreach ($data as $dv) {
  189. Db::name('StoreGoodsItem')->where(['goods_id'=>$dv['goods_id'],'goods_spec'=>$dv['goods_spec']])->setInc('stock',$dv['number_stock']);
  190. Db::name('StoreGoodsItem')->where(['goods_id'=>$dv['goods_id'],'goods_spec'=>$dv['goods_spec']])->setInc('base_stock',$dv['number_stock']);
  191. }
  192. Db::name('StoreGoods')->where('id',$post['id'])->setInc('stock',array_sum(array_column($data,'number_stock')));
  193. Db::name('StoreGoods')->where('id',$post['id'])->setInc('base_stock',array_sum(array_column($data,'number_stock')));
  194. $this->success('商品信息入库成功!');
  195. }
  196. }
  197. $this->error('没有需要商品入库的数据!');
  198. }
  199. }
  200. /**
  201. * 商品参数设置
  202. * @auth true
  203. * @throws \think\Exception
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. * @throws \think\exception\DbException
  207. * @throws \think\exception\PDOException
  208. */
  209. public function param()
  210. {
  211. if ($this->request->isGet()) {
  212. $goods_id = $this->request->get('id');
  213. $goods_info = Db::name('StoreGoods')->where(['id' => $goods_id])->find();
  214. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  215. $param = Db::name('goods_param')->where('goods_id',$goods_id)->find();
  216. $param_set = !$param ? [['title'=>'','value'=>'']]:json_decode($param['goods_param'],true);
  217. $this->fetch('', ['goods_info' => $goods_info,'param_set'=>$param_set]);
  218. }else{
  219. $goods_id = input('post.goods_id');
  220. $title_arr= input('post.title');
  221. $value_arr= input('post.value');
  222. if(empty($title_arr)) $this->error('请设置商品参数');
  223. $param_post = [];
  224. foreach ($title_arr as $k=>$t){
  225. $param_post[] = ['title'=>$t,'value'=>$value_arr[$k]];
  226. }
  227. Data::save('GoodsParam',['goods_id'=>$goods_id,'goods_param'=>json_encode($param_post)],'goods_id',['goods_id'=>$goods_id]);
  228. $this->success('保存成功!');
  229. }
  230. }
  231. /**
  232. * 添加完成商品之后逻辑处理
  233. * @param $result
  234. */
  235. protected function _form_result($result)
  236. {
  237. if ($result && $this->request->isPost() && in_array($this->request->action(),['add','edit'])) {
  238. list($data) = [$this->request->post()];
  239. $data['id'] = $result;
  240. $low_price = 0;
  241. foreach (json_decode($data['lists'], true) as $vo){
  242. if($low_price == 0 || $vo[0]['sell_price'] < $low_price )$low_price = $vo[0]['sell_price'];
  243. Data::save('StoreGoodsItem', [
  244. 'goods_id' => $data['id'],
  245. 'goods_spec' => $vo[0]['key'],
  246. 'goods_no' => $vo[0]['sku'],
  247. 'original_price' => $vo[0]['original_price'],
  248. 'sell_price' => $vo[0]['sell_price'],
  249. 'virtual' => $vo[0]['virtual'],
  250. 'status' => $vo[0]['status'] ? 1 : 0,
  251. 'weight' => $vo[0]['weight'] ? $vo[0]['weight'] : 0,
  252. ], 'goods_spec', ['goods_id' => $data['id']]);
  253. }
  254. Db::name('StoreGoods')->where(['id'=>$data['id']])->update(['low_price'=>$low_price]);
  255. $this->success('商品编辑成功!', 'javascript:history.back()');
  256. }
  257. }
  258. }