StoreGoods.php 10 KB

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