Channel.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\facade\Validate;
  5. use think\facade\Db;
  6. class Channel extends Base{
  7. public function __construct(App $app) {
  8. parent::__construct($app);
  9. }
  10. public function add(){
  11. $param =$this->request->only(["channel_name"=>"","companyNo"=>""],"post","trim");
  12. $valid =Validate::rule(["channel_name|渠道名称"=>"require|max:255|min:4","companyNo|业务公司"=>"require|max:255"]);
  13. if($valid->check($param)==false) return error_show(1006,$valid->getError());
  14. $ist=Db::name("channel")->where("channel_name",$param['channel_name'])->findOrEmpty();
  15. if(!empty($ist)) return error_show(1006,"渠道名称已存在");
  16. $company = \app\admin\common\User::getIns()->handle("getCodeAndName",["code"=>$param['companyNo']]);
  17. if(!isset($company['code'])|| $company['code']!=0) return error_show(1004,"未找到业务公司信息");
  18. $comp = $company['data']??[];
  19. $data=[
  20. "channel_name"=>$param["channel_name"],
  21. "companyName"=>$comp[$param['companyNo']]??"",
  22. "companyNo"=>$param['companyNo'],
  23. "apply_id"=>$this->uid,
  24. "apply_name"=>$this->uname,
  25. ];
  26. $int= Db::name("channel")->insert($data);
  27. if($int==false) return error_show(1004,"渠道新建失败");
  28. return app_show(0,"渠道新建成功");
  29. }
  30. public function edit(){
  31. $param =$this->request->only(["channel_name"=>"","companyNo"=>"","id"=>""],"post","trim");
  32. $valid =Validate::rule([
  33. "channel_name|渠道名称"=>"require|max:255|min:4",
  34. "id|渠道ID"=>"require|number|gt:0",
  35. "companyNo|业务公司"=>"require|max:255"
  36. ]);
  37. if($valid->check($param)==false) return error_show(1006,$valid->getError());
  38. $info=Db::name("channel")->where([["id","=",$param['id']]])->findOrEmpty();
  39. if(empty($info)) return error_show(1006,"渠道信息未找到");
  40. if($info['status']==1) return error_show(1006,"渠道启用状态不可编辑");
  41. $ist=Db::name("channel")->where([["channel_name","=",$param['channel_name']],["id","<>",$param['id']]])->findOrEmpty();
  42. if(!empty($ist)) return error_show(1006,"渠道名称已存在");
  43. $company = \app\admin\common\User::getIns()->handle("getCodeAndName",["code"=>$param['companyNo']]);
  44. if(!isset($company['code'])|| $company['code']!=0) return error_show(1004,"未找到业务公司信息");
  45. $comp = $company['data']??[];
  46. if($param['companyNo']!=$info['companyNo']){
  47. $isbind =Db::name("platform_channel")->where(["channel_id"=>$param['id'],"is_del"=>0])->findOrEmpty();
  48. if(!empty($isbind)) return error_show(1006,"渠道已绑定平台不可编辑业务公司");
  49. }
  50. $data=[
  51. "channel_name"=>$param["channel_name"],
  52. "companyName"=>$comp[$param['companyNo']]??"",
  53. "companyNo"=>$param['companyNo'],
  54. "updatetime"=>date("Y-m-d H:i:s")
  55. ];
  56. $int= Db::name("channel")->where($info)->update($data);
  57. if($int==false) return error_show(1004,"渠道编辑失败");
  58. return app_show(0,"渠道编辑成功");
  59. }
  60. /**
  61. * @return \think\response\Json|void
  62. * @throws \think\db\exception\DbException
  63. */
  64. public function status(){
  65. $param =$this->request->only(["status"=>"","id"=>""],"post","trim");
  66. $valid =Validate::rule([
  67. "id|渠道ID"=>"require|number|gt:0",
  68. "status|状态"=>"require|number|in:0,1"
  69. ]);
  70. if($valid->check($param)==false) return error_show(1006,$valid->getError());
  71. $info=Db::name("channel")->where([["id","=",$param['id']]])->findOrEmpty();
  72. if(empty($info)) return error_show(1006,"渠道信息未找到");
  73. $data=[
  74. "status"=>$param['status'],
  75. "updatetime"=>date("Y-m-d H:i:s")
  76. ];
  77. $int= Db::name("channel")->where($info)->update($data);
  78. if($int==false) return error_show(1004,"渠道编辑失败");
  79. return app_show(0,"渠道编辑成功");
  80. }
  81. public function list(){
  82. $param =$this->request->only([
  83. "channel_name"=>"",
  84. "companyNo"=>"",
  85. "companyName"=>"",
  86. "status"=>"",
  87. "apply_name"=>"",
  88. "page"=>1,
  89. "size"=>10
  90. ],"post","trim");
  91. $where=[["is_del","=",0]];
  92. if($param['channel_name']!=='')$where[]=["channel_name","like","%{$param['channel_name']}%"];
  93. if($param['companyName']!=='')$where[]=["companyName","like","%{$param['companyName']}%"];
  94. if($param['companyNo']!=='')$where[]=["companyNo","like","%{$param['companyNo']}%"];
  95. if($param['apply_name']!=='')$where[]=["apply_name","like","%{$param['apply_name']}%"];
  96. if($param['status']!=='')$where[]=["status","=",$param['status']];
  97. $count=Db::name("channel")->where($where)->count();
  98. $total=ceil($count/$param['size']);
  99. $page= $total>$param['page'] ? $param['page'] : $total;
  100. $list =Db::name("channel")->where($where)->page(intval($page),intval($param['size']))->order("id desc")->select()->toArray();
  101. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  102. }
  103. public function query(){
  104. $param =$this->request->only(["channel_name"=>"","companyNo"=>"","companyName"=>""],"post","trim");
  105. $where=[["is_del","=",0]];
  106. if($param['channel_name']!=='')$where[]=["channel_name","like","%{$param['channel_name']}%"];
  107. if($param['companyName']!=='')$where[]=["companyName","like","%{$param['companyName']}%"];
  108. if($param['companyNo']!=='')$where[]=["companyNo","=",$param['companyNo']];
  109. $list =Db::name("channel")->where($where)->field("*,LENGTH(channel_name) as weight")->order("weight desc")
  110. ->select()->toArray();
  111. return app_show(0,"获取成功",$list);
  112. }
  113. public function delete(){
  114. $param =$this->request->only(["id"=>""],"post","trim");
  115. $list =Db::name("channel")->where(["id"=>$param['id']])->findOrEmpty();
  116. if(empty($list))return error_show(1004,"未找到渠道信息");
  117. if($list["is_del"]==1)return error_show(1004,"渠道已删除");
  118. $int=Db::name("channel")->where(["id"=>$param['id']])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  119. if($int==false) return error_show(1004,"渠道删除失败");
  120. Db::name("platform_channel")->where(["channel_id"=>$param['id']])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  121. return app_show(0,"渠道删除成功");
  122. }
  123. public function channelCatList(){
  124. $param =$this->request->only(["channel_id"=>"0","cat_id"=>'',"cat_name"=>"","page"=>1,"size"=>15],"post","trim");
  125. $where=[["is_del","=",0],['pid',"=",0]];
  126. if($param['cat_id']!=='') $where[]=["cat_id","=",$param['cat_id']];
  127. if($param['cat_name']!=='') $where[]=["cat_name","=",$param['cat_name']];
  128. $list =Db::name("cat")
  129. ->field("id as cat_id,cat_name,{$param['channel_id']} channel_id,0 rate")
  130. ->where($where)
  131. ->withAttr("rate",
  132. function ($value,$data){
  133. return Db::name("channel_cat")->where(["channel_id"=>$data['channel_id'],"cat_id"=>$data['cat_id'],"is_del"=>0])->value("rate",0);
  134. })
  135. ->select();
  136. return app_show(0,"获取成功",$list);
  137. }
  138. public function channleCatSave(){
  139. $param =$this->request->only(["channel_id"=>"","cat_id"=>'',"rate"=>""],"post","trim");
  140. $valid =Validate::rule([
  141. "channel_id|渠道"=>"require|number|gt:0",
  142. "cat_id|分类"=>"require|number|gt:0",
  143. "rate|费率"=>"require|float|egt:0",
  144. ]);
  145. if($valid->check($param)==false) return error_show(1004,$valid->getError());
  146. $catifno = Db::name("channel_cat")->where(["channel_id"=>$param['channel_id'],"cat_id"=>$param['cat_id'],"is_del"=>0])->findOrEmpty();
  147. if(empty($catifno)){
  148. $catifno=[
  149. "channel_id"=>$param["channel_id"],
  150. "cat_id"=>$param["cat_id"],
  151. "rate"=>$param["rate"],
  152. "apply_id"=>$this->uid,
  153. "apply_name"=>$this->uname,
  154. ];
  155. }else{
  156. $catifno['rate'] = $param['rate'];
  157. $catifno['updatetime'] = date("Y-m-d H:i:s");
  158. }
  159. $up =Db::name("channel_cat")->save($catifno);
  160. if($up==false) return error_show(1004,"渠道分类费率编辑失败");
  161. return app_show(0,"渠道分类费率编辑成功");
  162. }
  163. }