GroupGoods.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\mall\controller;
  3. use app\common\model\GroupGoodsItem;
  4. use app\common\model\StoreGoodsItem;
  5. use library\Controller;
  6. use library\tools\Data;
  7. use think\cache\driver\Redis;
  8. use think\Db;
  9. /**
  10. * 拼团商品
  11. * Class GroupGoods
  12. * @package app\mall\controller
  13. */
  14. class GroupGoods extends Controller
  15. {
  16. /**
  17. * 绑定数据表
  18. * @var string
  19. */
  20. protected $table = 'GroupGoods';
  21. /**
  22. * 列表
  23. * @auth true
  24. * @menu true
  25. * @throws \think\Exception
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. * @throws \think\exception\PDOException
  30. */
  31. public function index()
  32. {
  33. $lid = input('id');
  34. $search_name = input('search_name','');
  35. $this->title = Db::name('GroupActivity')->where('id',$lid)->value('title');
  36. $this->lid = $lid;
  37. $where = [];
  38. $where[]= ['v.act_id','=',$lid];
  39. if($search_name) $where[] = ['g.name','like',"%".$search_name."%"];
  40. $list = $this->_query($this->table)->alias('v')
  41. ->field('v.create_at,v.sort,v.id,v.goods_id,v.act_id,g.name as goods_name,g.cover,g.name,g.low_price,g.status,g.is_deleted')
  42. ->where($where)
  43. ->join('StoreGoods g','v.goods_id = g.id','LEFT')
  44. ->order('v.sort desc ,v.id desc')
  45. ->page();
  46. }
  47. /**
  48. * 数据列表处理
  49. * @auth true
  50. * @menu true
  51. * @param array $data
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @throws \think\exception\DbException
  55. */
  56. protected function _index_page_filter(&$data)
  57. {
  58. foreach ($data as &$v){
  59. $v['goods_num'] = Db::name('GroupGoods')->where('act_id',$v['id'])->count();
  60. }
  61. }
  62. /**
  63. * 可添加商品列表
  64. * @auth true
  65. * @menu true
  66. * @param array $data
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. */
  71. public function goods_list()
  72. {
  73. $lid = input('id');
  74. $this->title = Db::name('GroupActivity')->where('id',$lid)->value('title');
  75. $this->lid = $lid;
  76. $name = input('name');
  77. $sel_ids = Db::name('GroupGoods')->where(['act_id'=>$lid])->column('goods_id');
  78. $where = [];
  79. $where[]= ['is_deleted','=',0];
  80. $where[]= ['status','=',1];
  81. if(!empty($sel_ids)) $where[] = ['id','not in',$sel_ids];
  82. if($name) $where[] = ['name','like','%'.$name.'%'];
  83. $list = $this->_query('StoreGoods')
  84. ->field('id,name,cover,low_price')
  85. ->where($where)
  86. ->order('sort desc ,id desc')->page();
  87. return $this->fetch('goods_list');
  88. }
  89. /**
  90. * ajax添加商品
  91. * @auth true
  92. * @menu true
  93. * @param array $data
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public function ajax_add()
  99. {
  100. $sel_goods = input('post.ids');
  101. $act_id = input('post.act_id');
  102. foreach ($sel_goods as $goods_id){
  103. $int_data[]=[
  104. 'act_id' => $act_id,
  105. 'goods_id' => $goods_id,
  106. 'sort' => 0,
  107. 'create_at' => date("Y-m-d H:i:s")
  108. ];
  109. $goods_item = StoreGoodsItem::where('goods_id',$goods_id)->field("goods_id,id as spec_id,sell_price ,is_combo,original_price,sell_price")->select()->toArray();
  110. array_walk($goods_item,function (&$v,$k)use($act_id){
  111. $v['act_id'] = $act_id;
  112. $v['group_price'] = $v['sell_price'];
  113. });
  114. (new GroupGoodsItem())->saveAll($goods_item);
  115. }
  116. Db::name('GroupGoods')->insertAll($int_data);
  117. return json_encode(['code'=>200]);
  118. }
  119. /**
  120. * 拼团设置
  121. * @auth true
  122. * @menu true
  123. * @param array $data
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @throws \think\exception\DbException
  127. */
  128. public function price_set()
  129. {
  130. if ($this->request->isGet()) {
  131. $act_goods = $this->request->get('id');
  132. $act_info = \app\common\model\GroupGoods::where('id',$act_goods)->find()->toArray();// 活动商品记录
  133. $goods_info = Db::name('StoreGoods')->field('id,name')->where(['id' => $act_info['goods_id']])->find();// 商品详情
  134. empty($goods_info) && $this->error('无效的商品信息,请稍候再试!');
  135. $group_item = GroupGoodsItem::field('s.*,i.goods_spec ,i.stock istock,i.base_stock ibase_stock')->alias('s')
  136. ->leftJoin('StoreGoodsItem i','i.id = s.spec_id')
  137. ->where(['s.act_id'=>$act_info['act_id'],'s.goods_id'=>$act_info['goods_id']])
  138. ->select()->toArray();// 拼团设置
  139. $this->fetch('', ['goods_info' => $goods_info,'group_item'=>$group_item]);
  140. }else{
  141. $post = input('post.');
  142. if(empty($post)) $this->error('数据错误');
  143. $group_set = [];
  144. //$redis = new Redis();
  145. // GROUP_活动id_GroupGoodsItem主键 :剩余库存
  146. // $redis_prefix = 'GROUP_'.$this->request->get('act_id').'_';
  147. foreach ($post['id'] as $k=>$id) {
  148. $group_set[$k]['id'] = $id;
  149. $group_set[$k]['original_price'] = $post['original_price'][$k];
  150. $group_set[$k]['sell_price'] = $post['sell_price'][$k];
  151. $group_set[$k]['group_price'] = $post['group_price'][$k];
  152. /* if($post['add_stock'][$k] != 0){
  153. //$redis->inc($redis_prefix.'_'.$id,$post['add_stock'][$k]);
  154. GroupGoodsItem::where('id',$id)->setInc('stock',$post['add_stock'][$k]);
  155. GroupGoodsItem::where('id',$id)->setInc('base_stock',$post['add_stock'][$k]);
  156. }*/
  157. }
  158. (new GroupGoodsItem())->saveAll($group_set);
  159. $this->success('保存成功!');
  160. }
  161. }
  162. /**
  163. * 删除关联商品
  164. * @auth true
  165. * @menu true
  166. * @throws \think\Exception
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. * @throws \think\exception\DbException
  170. * @throws \think\exception\PDOException
  171. */
  172. public function del_goods()
  173. {
  174. $id = input('id');
  175. $detail = \app\common\model\GroupGoods::where('id',$id)->find()->toArray();
  176. GroupGoodsItem::where(['act_id'=>$detail['act_id'],'goods_id'=>$detail['goods_id']])->delete();
  177. $this->_delete($this->table);
  178. }
  179. /**
  180. * 表单数据处理
  181. * @auth true
  182. * @menu true
  183. * @param array $data
  184. */
  185. protected function _form_filter(&$data)
  186. {
  187. }
  188. protected function _form_result($result){
  189. }
  190. }