InformationGoods.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use library\Controller;
  4. use think\Db;
  5. /**
  6. * 资讯商品
  7. * Class InformationGoods
  8. * @package app\Nutrition\controller
  9. */
  10. class InformationGoods extends Controller
  11. {
  12. /**
  13. * 绑定数据表
  14. * @var string
  15. */
  16. protected $table = 'InformationGoods';
  17. /**
  18. * 列表
  19. * @auth true
  20. * @menu true
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index()
  28. {
  29. $lid = input('id');
  30. $search_name = input('search_name','');
  31. $information_info = Db::name('information_article')->find($lid);
  32. $this->title = $information_info['title'];
  33. $this->lid = $lid;
  34. $where = [];
  35. $where[]= ['v.news_id','=',$lid];
  36. if($search_name) $where[] = ['g.name','like',"%".$search_name."%"];
  37. $list = $this->_query($this->table)->alias('v')
  38. ->field('v.create_at,v.sort,v.id,v.goods_id,g.name as goods_name,g.cover,g.name,g.low_price,g.status,g.is_deleted')
  39. ->where($where)
  40. ->join('StoreGoods g','v.goods_id = g.id','LEFT')
  41. ->order('v.sort desc ,v.id desc')
  42. ->page();
  43. }
  44. /**
  45. * 数据列表处理
  46. * @auth true
  47. * @menu true
  48. * @param array $data
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. protected function _index_page_filter(&$data)
  54. {
  55. foreach ($data as &$v){
  56. $v['goods_num'] = Db::name('InformationGoods')->where('news_id',$v['id'])->count();
  57. }
  58. }
  59. /**
  60. * 可添加商品列表
  61. * @auth true
  62. * @menu true
  63. * @param array $data
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public function goods_list()
  69. {
  70. $lid = input('id');
  71. $live_info = Db::name('InformationArticle')->find($lid);
  72. $this->title = $live_info['title'];
  73. $this->lid = $lid;
  74. $name = input('name');
  75. $sel_ids = Db::name('InformationGoods')->where(['news_id'=>$lid])->column('goods_id');
  76. $where = [];
  77. $where[]= ['is_deleted','=',0];
  78. $where[]= ['status','=',1];
  79. if(!empty($sel_ids)) $where[] = ['id','not in',$sel_ids];
  80. if($name) $where[] = ['name','like','%'.$name.'%'];
  81. $list = $this->_query('StoreGoods')
  82. ->field('id,name,cover,low_price')
  83. ->where($where)
  84. ->order('sort desc ,id desc')->page();
  85. return $this->fetch('goods_list');
  86. }
  87. /**
  88. * ajax添加商品
  89. * @auth true
  90. * @menu true
  91. * @param array $data
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @throws \think\exception\DbException
  95. */
  96. public function ajax_add()
  97. {
  98. $sel_goods = input('post.ids');
  99. $news_id = input('post.news_id');
  100. foreach ($sel_goods as $goods_id){
  101. $int_data[]=[
  102. 'news_id' => $news_id,
  103. 'goods_id' => $goods_id,
  104. 'sort' => 0,
  105. 'create_at' => date("Y-m-d H:i:s")
  106. ];
  107. }
  108. Db::name('InformationGoods')->insertAll($int_data);
  109. return json_encode(['code'=>200]);
  110. }
  111. /**
  112. * 删除关联商品
  113. * @auth true
  114. * @menu true
  115. * @throws \think\Exception
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. * @throws \think\exception\PDOException
  120. */
  121. public function del_goods()
  122. {
  123. $this->_delete($this->table);
  124. }
  125. /**
  126. * 表单数据处理
  127. * @auth true
  128. * @menu true
  129. * @param array $data
  130. */
  131. protected function _form_filter(&$data)
  132. {
  133. }
  134. protected function _form_result($result){
  135. }
  136. }