GoodsInstall.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\GoodsInstallLink;
  5. /**
  6. * 安装费管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class GoodsInstall extends Backend
  11. {
  12. protected $noNeedRight=['goods'];
  13. /**
  14. * GoodsInstall模型对象
  15. * @var \app\admin\model\GoodsInstall
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\GoodsInstall;
  22. }
  23. public function import()
  24. {
  25. parent::import();
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = false;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->visible(['id','name','create_time','update_time']);
  53. }
  54. $result = array("total" => $list->total(), "rows" => $list->items());
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. public function goods(){
  60. $id=input('id/d');
  61. $skus=[];
  62. if($id){
  63. $skus=GoodsInstallLink::where('goods_install_id',$id)->column('goods_sku_id');
  64. }
  65. $category=\app\common\model\Category::mall(['goods','goods.sku']);
  66. $data=[];
  67. foreach ($category as $cate){
  68. $data[]=[
  69. 'id'=>$cate['id'],
  70. 'parent'=>'#',
  71. 'state'=>[
  72. 'selected'=>false,
  73. ],
  74. 'text'=>$cate['name'],
  75. 'type'=>'menu',
  76. ];
  77. foreach ($cate['goods'] as $goods){
  78. $data[]=[
  79. 'id'=>"goods-".$goods['id'],
  80. 'parent'=>$cate['id'],
  81. 'state'=>[
  82. 'selected'=>false,
  83. ],
  84. 'text'=>$goods['name'],
  85. 'type'=>'menu',
  86. ];
  87. foreach ($goods['sku'] as $sku){
  88. $data[]=[
  89. 'id'=>"goods-{$goods['id']}|"."sku-".$sku['id'],
  90. 'parent'=>"goods-".$goods['id'],
  91. 'state'=>[
  92. 'selected'=>in_array($sku['id'],$skus),
  93. ],
  94. 'text'=>$sku['name'],
  95. 'type'=>'menu',
  96. ];
  97. }
  98. }
  99. }
  100. $this->success('',null,$data);
  101. }
  102. public function add()
  103. {
  104. return $this->makeAdd();
  105. }
  106. public function edit($ids = null)
  107. {
  108. return $this->makeAdd($ids);
  109. }
  110. protected function makeAdd($id=null){
  111. if($this->request->isGet()){
  112. if($id){
  113. $row=$this->model->find($id);
  114. $this->assign('row',$row);
  115. }
  116. return $this->fetch('add');
  117. }else{
  118. $data=input('row/a');
  119. $this->validate($data,[
  120. 'name|名称'=>['require'],
  121. 'goods|商品'=>['require'],
  122. 'fee'=>['require','array','min:1']
  123. ]);
  124. $fee=$data['fee'];
  125. foreach ($fee as $feeItem){
  126. $this->validate($feeItem,[
  127. 'num_min|最小数量'=>['require','egt:0'],
  128. 'num_max|最大数量'=>['require','egt:0'],
  129. 'amount|安装费'=>['require','egt:0'],
  130. ]);
  131. }
  132. $data['goods']=array_filter(explode(',',$data['goods']));
  133. if($id) {
  134. $install = $this->model->find($id);
  135. }else{
  136. $install = new $this->model;
  137. }
  138. $install['name']=$data['name'];
  139. $install['fee']=array_values($data['fee']);
  140. $install->save();
  141. $ids=[];
  142. foreach ($data['goods'] as $good){
  143. if(strpos($good,'|')){
  144. list($goodsStr,$skuStr)=explode('|',$good);
  145. $goods_id=str_replace('goods-','',$goodsStr);
  146. $sku_id=str_replace('sku-','',$skuStr);
  147. $link=$install->link()->sku($sku_id,$goods_id)->find();
  148. if(!$link){
  149. $link=$install->link()->save([
  150. 'goods_id'=>$goods_id,
  151. 'goods_sku_id'=>$sku_id ,
  152. ]);
  153. }
  154. $ids[]=$link['id'];
  155. }
  156. }
  157. $install->link()->whereNotIn('id',$ids)->delete();
  158. $this->success();
  159. }
  160. }
  161. }