123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\facade\Validate;
- use think\facade\Db;
- class Channel extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- }
- public function add(){
- $param =$this->request->only(["channel_name"=>"","companyNo"=>""],"post","trim");
- $valid =Validate::rule(["channel_name|渠道名称"=>"require|max:255|min:4","companyNo|业务公司"=>"require|max:255"]);
- if($valid->check($param)==false) return error_show(1006,$valid->getError());
- $ist=Db::name("channel")->where("channel_name",$param['channel_name'])->findOrEmpty();
- if(!empty($ist)) return error_show(1006,"渠道名称已存在");
- $company = \app\admin\common\User::getIns()->handle("getCodeAndName",["code"=>$param['companyNo']]);
- if(!isset($company['code'])|| $company['code']!=0) return error_show(1004,"未找到业务公司信息");
- $comp = $company['data']??[];
- $data=[
- "channel_name"=>$param["channel_name"],
- "companyName"=>$comp[$param['companyNo']]??"",
- "companyNo"=>$param['companyNo'],
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- ];
- $int= Db::name("channel")->insert($data);
- if($int==false) return error_show(1004,"渠道新建失败");
- return app_show(0,"渠道新建成功");
- }
- public function edit(){
- $param =$this->request->only(["channel_name"=>"","companyNo"=>"","id"=>""],"post","trim");
- $valid =Validate::rule([
- "channel_name|渠道名称"=>"require|max:255|min:4",
- "id|渠道ID"=>"require|number|gt:0",
- "companyNo|业务公司"=>"require|max:255"
- ]);
- if($valid->check($param)==false) return error_show(1006,$valid->getError());
- $info=Db::name("channel")->where([["id","=",$param['id']]])->findOrEmpty();
- if(empty($info)) return error_show(1006,"渠道信息未找到");
- if($info['status']==1) return error_show(1006,"渠道启用状态不可编辑");
- $ist=Db::name("channel")->where([["channel_name","=",$param['channel_name']],["id","<>",$param['id']]])->findOrEmpty();
- if(!empty($ist)) return error_show(1006,"渠道名称已存在");
- $company = \app\admin\common\User::getIns()->handle("getCodeAndName",["code"=>$param['companyNo']]);
- if(!isset($company['code'])|| $company['code']!=0) return error_show(1004,"未找到业务公司信息");
- $comp = $company['data']??[];
- if($param['companyNo']!=$info['companyNo']){
- $isbind =Db::name("platform_channel")->where(["channel_id"=>$param['id'],"is_del"=>0])->findOrEmpty();
- if(!empty($isbind)) return error_show(1006,"渠道已绑定平台不可编辑业务公司");
- }
- $data=[
- "channel_name"=>$param["channel_name"],
- "companyName"=>$comp[$param['companyNo']]??"",
- "companyNo"=>$param['companyNo'],
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $int= Db::name("channel")->where($info)->update($data);
- if($int==false) return error_show(1004,"渠道编辑失败");
- return app_show(0,"渠道编辑成功");
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DbException
- */
- public function status(){
- $param =$this->request->only(["status"=>"","id"=>""],"post","trim");
- $valid =Validate::rule([
- "id|渠道ID"=>"require|number|gt:0",
- "status|状态"=>"require|number|in:0,1"
- ]);
- if($valid->check($param)==false) return error_show(1006,$valid->getError());
- $info=Db::name("channel")->where([["id","=",$param['id']]])->findOrEmpty();
- if(empty($info)) return error_show(1006,"渠道信息未找到");
- $data=[
- "status"=>$param['status'],
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $int= Db::name("channel")->where($info)->update($data);
- if($int==false) return error_show(1004,"渠道编辑失败");
- return app_show(0,"渠道编辑成功");
- }
- public function list(){
- $param =$this->request->only([
- "channel_name"=>"",
- "companyNo"=>"",
- "companyName"=>"",
- "status"=>"",
- "apply_name"=>"",
- "page"=>1,
- "size"=>10
- ],"post","trim");
- $where=[["is_del","=",0]];
- if($param['channel_name']!=='')$where[]=["channel_name","like","%{$param['channel_name']}%"];
- if($param['companyName']!=='')$where[]=["companyName","like","%{$param['companyName']}%"];
- if($param['companyNo']!=='')$where[]=["companyNo","like","%{$param['companyNo']}%"];
- if($param['apply_name']!=='')$where[]=["apply_name","like","%{$param['apply_name']}%"];
- if($param['status']!=='')$where[]=["status","=",$param['status']];
- $count=Db::name("channel")->where($where)->count();
- $total=ceil($count/$param['size']);
- $page= $total>$param['page'] ? $param['page'] : $total;
- $list =Db::name("channel")->where($where)->page(intval($page),intval($param['size']))->order("id desc")->select()->toArray();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- public function query(){
- $param =$this->request->only(["channel_name"=>"","companyNo"=>"","companyName"=>""],"post","trim");
- $where=[["is_del","=",0]];
- if($param['channel_name']!=='')$where[]=["channel_name","like","%{$param['channel_name']}%"];
- if($param['companyName']!=='')$where[]=["companyName","like","%{$param['companyName']}%"];
- if($param['companyNo']!=='')$where[]=["companyNo","=",$param['companyNo']];
- $list =Db::name("channel")->where($where)->field("*,LENGTH(channel_name) as weight")->order("weight desc")
- ->select()->toArray();
- return app_show(0,"获取成功",$list);
- }
- public function delete(){
- $param =$this->request->only(["id"=>""],"post","trim");
- $list =Db::name("channel")->where(["id"=>$param['id']])->findOrEmpty();
- if(empty($list))return error_show(1004,"未找到渠道信息");
- if($list["is_del"]==1)return error_show(1004,"渠道已删除");
- $int=Db::name("channel")->where(["id"=>$param['id']])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
- if($int==false) return error_show(1004,"渠道删除失败");
- Db::name("platform_channel")->where(["channel_id"=>$param['id']])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
- return app_show(0,"渠道删除成功");
- }
- public function channelCatList(){
- $param =$this->request->only(["channel_id"=>"0","cat_id"=>'',"cat_name"=>"","page"=>1,"size"=>15],"post","trim");
- $where=[["is_del","=",0],['pid',"=",0]];
- if($param['cat_id']!=='') $where[]=["cat_id","=",$param['cat_id']];
- if($param['cat_name']!=='') $where[]=["cat_name","=",$param['cat_name']];
- $list =Db::name("cat")
- ->field("id as cat_id,cat_name,{$param['channel_id']} channel_id,0 rate")
- ->where($where)
- ->withAttr("rate",
- function ($value,$data){
- return Db::name("channel_cat")->where(["channel_id"=>$data['channel_id'],"cat_id"=>$data['cat_id'],"is_del"=>0])->value("rate",0);
- })
- ->select();
- return app_show(0,"获取成功",$list);
- }
- public function channleCatSave(){
- $param =$this->request->only(["channel_id"=>"","cat_id"=>'',"rate"=>""],"post","trim");
- $valid =Validate::rule([
- "channel_id|渠道"=>"require|number|gt:0",
- "cat_id|分类"=>"require|number|gt:0",
- "rate|费率"=>"require|float|egt:0",
- ]);
- if($valid->check($param)==false) return error_show(1004,$valid->getError());
- $catifno = Db::name("channel_cat")->where(["channel_id"=>$param['channel_id'],"cat_id"=>$param['cat_id'],"is_del"=>0])->findOrEmpty();
- if(empty($catifno)){
- $catifno=[
- "channel_id"=>$param["channel_id"],
- "cat_id"=>$param["cat_id"],
- "rate"=>$param["rate"],
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- ];
- }else{
- $catifno['rate'] = $param['rate'];
- $catifno['updatetime'] = date("Y-m-d H:i:s");
- }
- $up =Db::name("channel_cat")->save($catifno);
- if($up==false) return error_show(1004,"渠道分类费率编辑失败");
- return app_show(0,"渠道分类费率编辑成功");
- }
- }
|