123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //泰康专属的一个列表
- class Exclusive extends Base
- {
- public function __construct(App $app)
- {
- parent::__construct($app);
- }
- public function list(){
- $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
- $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
- $where =[["e.is_del","=",0]];
- $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
- if($cat_name!==""){
- $where[]=['e.name',"like","%$cat_name%"];
- }
- $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
- if($pid!==""){
- $where[]=['e.pid',"=",$pid];
- }
- $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
- if($status!==""){
- $where[]=['e.status',"=",$status];
- }
- $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
- if ($company_name !== "") $where[] = ["e.createrid", 'in', get_company_item_user_by_name($company_name)];
- $count = Db::name("exclusive")->alias('e')->where($where)->count();
- $total = ceil($count / $size);
- $page = $page >= $total ? $total : $page;
- $list = Db::name('exclusive')
- ->alias('e')
- ->where($where)
- ->page($page, $size)
- ->select()
- ->toArray();
- $all_createrid = array_column($list,'createrid');
- $item = get_company_name_by_uid($all_createrid);
- foreach ($list as &$value){
- $value['company_name']=$item[$value['createrid']]??'';
- }
- return app_show(0, "获取成功", ['list' =>$list, 'count' => $count]);
- }
- public function add(){
- $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
- if($name==""){
- return error_show(1004,"参数name不能为空");
- }
- $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
- if($pid===""){
- return error_show(1002,"父级id不能为空");
- }
- $rename = Db::name('exclusive')->where(["pid"=>$pid,'is_del' => 0, 'name' => $name])->find();
- if (!empty($rename)) {
- return error_show(1002, "专属类型名称已存在");
- }
- $level=1;
- $search = $name;
- if($pid!==0){
- $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
- if($parent==false){
- return error_show(1003,"父级数据未找到");
- }
- $search=$parent['search']."/".$name;
- $level=$parent['level']+1;
- }
- $token = isset($this->post['token'])&&$this->post['token']!="" ? trim($this->post['token']):"";
- $user =GetUserInfo($token);
- if(empty($user)||$user['code']!=0){
- return error_show($user['code'],$user['msg']);
- }
- $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
- $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
- $data=[
- "name"=>$name,
- "pid"=>$pid,
- "level"=>$level,
- "search"=>$search,
- "createrid"=>$createrid,
- "creater"=>$creater,
- "status"=>0,
- "is_del"=>0,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $in =Db::name("exclusive")->insert($data);
- if($in){
- return app_show(0,"新建成功");
- }else{
- return error_show(1003,"新建失败");
- }
- }
- //业务参数-专属类型管理的启用/禁用
- public function status()
- {
- // $id=isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']):"";
- // if($id===""){
- // return error_show(1004,"参数id不能为空");
- // }
- // $exclusive=Db::name("exclusive")->where(["id"=>$id,"is_del"=>0])->find();
- // if($exclusive==false){
- // return error_show(1004,"未找到数据");
- // }
- // $msg = $exclusive['status']==0? "启用":"禁用";
- // $exclusive['status'] = $exclusive['status']==0?1:0;
- // $exclusive['updatetime'] =date("Y-m-d H:i:s");
- // $in =Db::name("exclusive")->save($exclusive);
- // if($in){
- // return app_show(0,"{$msg}成功");
- // }else{
- // return error_show(1004,"{$msg}失败");
- // }
- $param = $this->request->only(['id', 'status']);
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'status' => 'require|number|in:0,1',
- ]);
- if (!$val->check($param)) return error_show(1004, $val->getError());
- Db::startTrans();
- try {
- $res = Db::name('exclusive')
- ->field('id,level,status,pid')
- ->where('id', $param['id'])
- ->find();
- if (empty($res)) throw new Exception('该专属类型不存在');
- //启用
- if ($param['status'] == 1) {
- //一级
- if ($res['level'] == 1) {
- //是否存在子级
- $total = Db::name('exclusive')
- ->where(['pid' => $param['id'], 'is_del' => 0])
- ->count('id');
- if ($total) {
- //有子级的话,子级有一个启用,即可启用
- $child_id = Db::name('exclusive')
- ->field('id')
- ->where(['pid' => $param['id'], 'is_del' => 0, 'status' => 1])
- ->find();
- if (empty($child_id)) throw new Exception('请先启用一个子级');
- }
- } else {
- //二级的话,顺便把它的上级也启用了
- Db::name('exclusive')
- ->where(['id' => $res['pid'], 'is_del' => 0, 'status' => 0])
- ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- }
- } elseif ($param['status'] == 0) {
- //禁用
- if ($res['level'] == 1) {
- //是否存在子级
- $total = Db::name('exclusive')
- ->where(['pid' => $param['id'], 'is_del' => 0])
- ->count('id');
- if ($total) {
- //有子级的话,子级全部禁用,方可禁用
- $child_id = Db::name('exclusive')
- ->field('id')
- ->where(['pid' => $param['id'], 'is_del' => 0, 'status' => 1])
- ->find();
- if (!empty($child_id)) throw new Exception('请先禁用全部子级');
- }
- }else{
- //如果它的兄弟全部禁用的话,连同它的父级也禁用
- $normal_c = Db::name('exclusive')
- ->field('id')
- ->where(['pid' => $res['pid'], 'is_del' => 0, 'status' => 1])
- ->where('id', '<>', $param['id'])
- ->find();
- if (empty($normal_c)) {
- Db::name('exclusive')
- ->where(['id' => $res['pid'], 'status' => 1])
- ->update(['status' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
- }
- }
- }
- //修改自己
- Db::name('exclusive')
- ->where(['id' => $param['id'], 'is_del' => 0])
- ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- Db::commit();
- return error_show(0, "状态更新成功");
- } catch (\Exception $e) {
- Db::rollback();
- return error_show(1005, $e->getMessage());
- }
- // $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
- // if($id===""){
- // return error_show(1002,"参数id不能为空");
- // }
- // $stn = Db::name('exclusive')->where(['id'=>$id])->find();
- // if(empty($stn)){
- // return error_show(1002,"未找到商品数据");
- // }
- // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
- // if($status===""){
- // return error_show(1002,"参数status不能为空");
- // }
- // if ($stn['level'] == 2) {
- // if ($status == 1) {
- // $can = mai($stn['id']);
- // $cat = array_column($can, 'id');//子级如果启用,连同它的父级一起启用
- // } else {
- // $cat = $stn['id'];
- // }
- // // return error_show(1002, "所在级别不能启用");
- // }
- // if ($stn['level'] == 1) {
- // if ($status == 1) {
- // return error_show(1002, "所在等级不能启用");
- // } else {
- // $db = Db::name('exclusive')->where(['pid' => $stn['id'], 'status' => 1, 'is_del' => 0])->count();
- // if ($db == 0) {
- // $cat = $stn['id'];
- // } else {
- // return error_show(1002, "子级分类未禁用");
- // }
- // }
- // }
- // // $pd= $stn['status'];
- // $it = [];
- // $it['status'] = $status;
- // $it['updatetime'] = date("Y-m-d H:i:s");
- // $str = Db::name('exclusive')->where(['id' => $cat, 'is_del' => 0])->save($it);
- // if ($str) {
- //// $order = ["order_code"=>$id,"status"=>$pd,"action_remark"=>'',"action_type"=>"edit"];
- //// ActionLog::logAdd($this->post['token'],$order,"sxd",$stn['status'],$order);
- // return error_show(0, "状态更新成功");
- // } else {
- // return error_show(1002, "状态更新失败");
- // }
- }
- public function query(){
- // $where =[["is_del","=",0]];
- // $cat_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
- // if($cat_name!==""){
- // $where[]=['name',"like","%$cat_name%"];
- // }
- // $pid=isset($this->post['pid']) && $this->post['pid'] !==""? intval($this->post['pid']) :"";
- // if($pid!==""){
- // $where[]=['pid',"=",$pid];
- // }
- // $status=isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']) :"";
- // if($status!==""){
- // $where[]=['status',"=",$status];
- // }
- // $level=isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']) :"";
- // if($level!==""){
- // $where[]=['level',"=",$level];
- // }
- // $list = Db::name("exclusive")->where($where)->select();
- // return app_show(0,"获取成功",$list);
- $where=[["is_del","=",0]];
- $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
- $where[]=["pid","=",$pid];
- $cat_name=isset($this->post['cat_name']) && $this->post['cat_name'] !==""? intval($this->post['cat_name']) :"";
- if($cat_name!==""){
- $where[]=['cat_name',"like","%$cat_name%"];
- }
- $data = Db::name("exclusive")->where($where)->select();
- $vmp = [];
- foreach ($data as $sts){
- $vmp[]=coco($sts);
- }
- return app_show(0,"获取成功",$vmp);
- }
- public function edit(){
- $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
- if($id==""){
- return error_show(1004,"参数id不能为空");
- }
- $info = Db::name('exclusive')->where(['id'=>$id,'is_del'=>0])->find();
- if($info==""){
- return error_show(1002,"未找到数据");
- }
- if($info['status']===1){
- return error_show(1004,"启用状态不可编辑");
- }
- $name = isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
- if($name==""){
- return error_show(1004,"参数name不能为空");
- }
- $pid =isset($this->post['pid'])&&$this->post['pid']!=="" ? intval($this->post['pid']):0;
- if($pid===""){
- return error_show(1004,"参数pid不能为空");
- }
- $rename = Db::name('exclusive')->where(["pid"=>$pid,'is_del' => 0, 'name' => $name])->where("id","<>",$id)
- ->findOrEmpty();
- if (!empty($rename)) {
- return error_show(1002, "专属类型名称已存在");
- }
- $level=1;
- $search = $name;
- if($pid!==0){
- $parent= Db::name("exclusive")->where(["id"=>$pid,"is_del"=>0])->find();
- if($parent==false){
- return error_show(1003,"父级数据未找到");
- }
- $search=$parent['search']."/".$name;
- $level=$parent['level']+1;
- }
- $data=[
- "id"=>$id,
- "name"=>$name,
- "pid"=>$pid,
- "level"=>$level,
- "search"=>$search,
- "status"=>0,
- "is_del"=>0,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $in =Db::name("exclusive")->save($data);
- if($in){
- return app_show(0,"编辑成功");
- }else{
- return error_show(1003,"编辑失败");
- }
- }
- public function delete(){
- $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
- $info = Db::name('exclusive')->where(["is_del"=>0,'id'=>$id])->find();
- if($info==false){
- return error_show(1002,"未找到数据");
- }
- $supp= Db::name('exclusive')->update(['id'=>$id,'is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
- if($supp){
- return error_show(0,"删除成功");
- }else{
- return error_show(1002,"删除失败");
- }
- }
- }
|