StoreGoods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\mall\controller;
  3. use app\common\model\GoodsSeason;
  4. use app\common\model\GoodsTerritory;
  5. use library\Controller;
  6. use think\Db;
  7. use app\common\model\GoodsCate;
  8. use app\common\model\StoreGoodsItem;
  9. use library\tools\Data;
  10. /**
  11. * 商品管理
  12. * Class StoreGoods
  13. * @package app\mall\controller
  14. */
  15. class StoreGoods extends Controller
  16. {
  17. /**
  18. * 绑定数据表
  19. * @var string
  20. */
  21. protected $table = 'StoreGoods';
  22. /**
  23. * 商品列表
  24. * @auth true
  25. * @menu true
  26. * @throws \think\Exception
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. * @throws \think\exception\PDOException
  31. */
  32. public function index()
  33. {
  34. $this->title = '商品管理';
  35. $where = [];
  36. $where['is_deleted'] = 0;
  37. $query = $this->_query($this->table)->where($where)->like('name');
  38. $query->dateBetween('create_at')->order('sort desc , id desc')->page();
  39. }
  40. /**
  41. * 数据列表处理
  42. * @auth true
  43. * @menu true
  44. * @param array $data
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. protected function _index_page_filter(&$data)
  50. {
  51. $this->clist = GoodsCate::getCateTree();
  52. $list = Db::name('StoreGoodsItem')->where('status', '1')->whereIn('goods_id', array_unique(array_column($data, 'id')))->select();
  53. foreach ($data as &$vo) {
  54. list($vo['list'], $vo['cate']) = [[], []];
  55. foreach ($list as $goods){
  56. if ($goods['goods_id'] === $vo['id']) array_push($vo['list'], $goods);
  57. }
  58. }
  59. }
  60. /**
  61. * 添加商品
  62. * @auth true
  63. * @menu true
  64. * @throws \think\Exception
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. * @throws \think\exception\PDOException
  69. */
  70. public function add()
  71. {
  72. $this->title = '添加商品';
  73. $this->isAddMode = '1';
  74. $this->_form($this->table, 'form');
  75. }
  76. /**
  77. * 编辑商品
  78. * @auth true
  79. * @menu true
  80. * @throws \think\Exception
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. * @throws \think\exception\PDOException
  85. */
  86. function edit()
  87. {
  88. $this->title = '编辑商品';
  89. $this->isAddMode = '0';
  90. $this->_form($this->table, 'form');
  91. }
  92. /**
  93. * 表单数据处理
  94. * @param array $data
  95. */
  96. protected function _form_filter(&$data)
  97. {
  98. if($this->request->isGet()){
  99. $fields = 'goods_spec,goods_id,status,original_price,sell_price,virtual,cover pic,goods_no sku,weight';
  100. $defaultValues = [];
  101. if(isset_full($data,'id')) $defaultValues = Db::name('StoreGoodsItem')->where(['goods_id' => $data['id']])->column($fields);
  102. $this->defaultValues = json_encode($defaultValues, JSON_UNESCAPED_UNICODE);
  103. $this->goods_cate = GoodsCate::getCateTree();
  104. $this->goods_season = GoodsSeason::getSeasonTree();
  105. $this->goods_column = \app\common\model\GoodsColumn::where(['is_deleted'=>0,'status'=>1])->order('sort desc ,id desc')->column('id,title,desc','id');
  106. $this->goods_territory = GoodsTerritory::getTerritoryTree();
  107. $this->express= Db::name('freight_template')->select();
  108. $this->remark_item = \app\common\model\RemarkLabel::column('title','id');
  109. $this->serve_item = \app\common\model\GoodsServe::column('title','id');
  110. //选中的服务标签
  111. $serve_id_arr = isset($data['serve_label']) ? explode(',',$data['serve_label']) : [];
  112. $this->serve_id_arr = $serve_id_arr;
  113. //选中的评论标签
  114. $remark_id_arr = isset($data['remark_label']) ? explode(',',$data['remark_label']) : [];
  115. $this->remark_id_arr = $remark_id_arr;
  116. // 等级设置
  117. $this->level = Db::name('user_level')->column('name','id');
  118. $this->level_set = isset($data['level_set']) ? json_decode($data['level_set'],true): [];
  119. // 分销设置
  120. $this->partner_lev = Db::name('partner_level')->column('name','id');
  121. $goods_dist = isset_full($data,'id') ? Db::name('goods_dist')->where(['goods_id'=>$data['id']])->find():'';
  122. $this->partner_set = $goods_dist ? json_decode($goods_dist['dist_set'],true):[];
  123. }
  124. // 添加或编辑商品
  125. if($this->request->isPost() && in_array($this->request->action(),['add','edit'])){
  126. if(!isset_full($data,'first_classify')) $data['first_classify'] = 0;
  127. if(!isset_full($data,'second_classify')) $data['second_classify'] = 0;
  128. if(!isset_full($data,'third_classify')) $data['third_classify'] = 0;
  129. if(!isset_full($data,'fourth_classify')) $data['fourth_classify'] = 0;
  130. // 服务标签
  131. $select_sever = [];
  132. if(isset($data['serve_label']) && !empty($data['serve_label'])){
  133. foreach ($data['serve_label'] as $key=>$value){
  134. if($value) $select_sever[] = $key;
  135. }
  136. }
  137. $data['serve_label'] = implode(',',$select_sever);
  138. // 商品评论标签
  139. $select_remark = [];
  140. if(isset($data['remark_label']) && !empty($data['remark_label'])){
  141. foreach ($data['remark_label'] as $key=>$value){
  142. if($value) $select_remark[] = $key;
  143. }
  144. }
  145. $data['remark_label'] = implode(',',$select_remark);
  146. // level_set
  147. $level_set = [];
  148. if(isset($data['level_id']) && !empty($data['level_id'])){
  149. foreach ($data['level_id'] as $key=>$value){
  150. $level_set[$value] = [
  151. 'level_id'=> $value ,
  152. 'integral_cash'=> $data['integral_cash'][$key] ? $data['integral_cash'][$key]:0,
  153. 'integral_freight'=> $data['integral_freight'][$key] ? $data['integral_freight'][$key]:0 ,
  154. ];
  155. }
  156. }
  157. $data['level_set']=json_encode($level_set);
  158. }
  159. }
  160. /**
  161. * 商品上架
  162. * @auth true
  163. * @menu true
  164. * @throws \think\Exception
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. * @throws \think\exception\PDOException
  169. */
  170. public function up()
  171. {
  172. $this->_save($this->table, ['status' => '1','is_edit'=>0]);
  173. }
  174. /**
  175. * 商品下架
  176. * @auth true
  177. * @menu true
  178. * @throws \think\Exception
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. * @throws \think\exception\DbException
  182. * @throws \think\exception\PDOException
  183. */
  184. public function down()
  185. {
  186. $this->_save($this->table, ['status' => '0']);
  187. }
  188. /**
  189. * 商品删除
  190. * @auth true
  191. * @menu true
  192. * @throws \think\Exception
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. * @throws \think\exception\DbException
  196. * @throws \think\exception\PDOException
  197. */
  198. public function remove()
  199. {
  200. $this->_save($this->table, ['is_deleted' => '1']);
  201. }
  202. /**
  203. * 商品库存入库
  204. * @auth true
  205. * @throws \think\Exception
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. * @throws \think\exception\DbException
  209. * @throws \think\exception\PDOException
  210. */
  211. public function stock()
  212. {
  213. if ($this->request->isGet()) {
  214. $goods_id = $this->request->get('id');
  215. $goods_info = Db::name('StoreGoods')->where(['id' => $goods_id])->find();
  216. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  217. $goods_info['list'] = Db::name('StoreGoodsItem')->where(['goods_id' => $goods_id])->select();
  218. $this->fetch('', ['vo' => $goods_info]);
  219. } else {
  220. list($post, $data) = [$this->request->post(), []];
  221. if (isset($post['id']) && isset($post['goods_id']) && is_array($post['goods_id'])) {
  222. foreach (array_keys($post['goods_id']) as $key) {
  223. if ($post['goods_number'][$key] > 0) array_push($data, [
  224. 'goods_id' => $post['goods_id'][$key],
  225. 'goods_spec' => $post['goods_spec'][$key],
  226. 'number_stock' => $post['goods_number'][$key],
  227. ]);
  228. }
  229. if (!empty($data)) {
  230. Db::name('GoodsStock')->insertAll($data);
  231. foreach ($data as $dv) {
  232. Db::name('StoreGoodsItem')->where(['goods_id'=>$dv['goods_id'],'goods_spec'=>$dv['goods_spec']])->setInc('stock',$dv['number_stock']);
  233. Db::name('StoreGoodsItem')->where(['goods_id'=>$dv['goods_id'],'goods_spec'=>$dv['goods_spec']])->setInc('base_stock',$dv['number_stock']);
  234. }
  235. Db::name('StoreGoods')->where('id',$post['id'])->setInc('stock',array_sum(array_column($data,'number_stock')));
  236. Db::name('StoreGoods')->where('id',$post['id'])->setInc('base_stock',array_sum(array_column($data,'number_stock')));
  237. $this->success('商品信息入库成功!');
  238. }
  239. }
  240. $this->error('没有需要商品入库的数据!');
  241. }
  242. }
  243. /**
  244. * 商品参数设置
  245. * @auth true
  246. * @throws \think\Exception
  247. * @throws \think\db\exception\DataNotFoundException
  248. * @throws \think\db\exception\ModelNotFoundException
  249. * @throws \think\exception\DbException
  250. * @throws \think\exception\PDOException
  251. */
  252. public function param()
  253. {
  254. if ($this->request->isGet()) {
  255. $goods_id = $this->request->get('id');
  256. $goods_info = Db::name('StoreGoods')->where(['id' => $goods_id])->find();
  257. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  258. $param = Db::name('goods_param')->where('goods_id',$goods_id)->find();
  259. $param_set = !$param ? [['title'=>'','value'=>'']]:json_decode($param['goods_param'],true);
  260. $this->fetch('', ['goods_info' => $goods_info,'param_set'=>$param_set]);
  261. }else{
  262. $goods_id = input('post.goods_id');
  263. $title_arr= input('post.title');
  264. $value_arr= input('post.value');
  265. if(empty($title_arr)) $this->error('请设置商品参数');
  266. $param_post = [];
  267. foreach ($title_arr as $k=>$t){
  268. $param_post[] = ['title'=>$t,'value'=>$value_arr[$k]];
  269. }
  270. Data::save('GoodsParam',['goods_id'=>$goods_id,'goods_param'=>json_encode($param_post)],'goods_id',['goods_id'=>$goods_id]);
  271. $this->success('保存成功!');
  272. }
  273. }
  274. /**
  275. * 添加完成商品之后逻辑处理
  276. * @param $result
  277. */
  278. protected function _form_result($result)
  279. {
  280. if ($result && $this->request->isPost() && in_array($this->request->action(),['add','edit'])) {
  281. list($data) = [$this->request->post()];
  282. $data['id'] = $result;
  283. $low_price = 0;
  284. foreach (json_decode($data['lists'], true) as $vo){
  285. if($vo[0]['sell_price'] <= 0) $this->error('商品销售价格需大于0元');
  286. if($low_price == 0 || $vo[0]['sell_price'] < $low_price )$low_price = $vo[0]['sell_price'];
  287. Data::save('StoreGoodsItem', [
  288. 'goods_id' => $data['id'],
  289. 'goods_spec' => $vo[0]['key'],
  290. 'goods_no' => $vo[0]['sku'],
  291. 'original_price' => $vo[0]['original_price'],
  292. 'sell_price' => $vo[0]['sell_price'],
  293. 'virtual' => $vo[0]['virtual'],
  294. 'status' => $vo[0]['status'] ? 1 : 0,
  295. 'weight' => $vo[0]['weight'] ? $vo[0]['weight'] : 0,
  296. ], 'goods_spec', ['goods_id' => $data['id']]);
  297. }
  298. Db::name('StoreGoods')->where(['id'=>$data['id']])->update(['low_price'=>$low_price]);
  299. if($data['is_dist'])
  300. {
  301. $dist_set = [];
  302. if(isset($data['par_level_id']) && !empty($data['par_level_id'])){
  303. foreach ($data['par_level_id'] as $key=>$value){
  304. $dist_set[$value] = [
  305. 'level_id'=> $value ,
  306. 'rebate'=> $data['rebate'][$key] ? $data['rebate'][$key]:0,
  307. ];
  308. }
  309. }
  310. Data::save('GoodsDist',['goods_id'=>$data['id'],'dist_set'=>json_encode($dist_set)],'goods_id',['goods_id'=>$data['id']]);
  311. }
  312. $this->success('商品编辑成功!', 'javascript:history.back()');
  313. }
  314. }
  315. }