|
@@ -4,6 +4,7 @@ namespace app\admin\controller;
|
|
|
|
|
|
use app\BaseController;
|
|
|
use think\App;
|
|
|
+use think\facade\Db;
|
|
|
|
|
|
class Customer extends BaseController
|
|
|
{
|
|
@@ -15,6 +16,139 @@ class Customer extends BaseController
|
|
|
}
|
|
|
/*列表*/
|
|
|
public function list(){
|
|
|
+ $condition[] = ['is_del',"=",0];
|
|
|
+ $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "";
|
|
|
+ if($pid!==""){
|
|
|
+ $condition[]=["pid","=",$pid];
|
|
|
+ }
|
|
|
+ $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
|
|
|
+ if($name!=""){
|
|
|
+ $condition[]=["name","like","%{$this->post['name']}%"];
|
|
|
+ }
|
|
|
+ $list = Db::name('customer_org1')->where($condition)->column("pid,name,level,depart_link");
|
|
|
+ return app_show(0,"获取成功",$list);
|
|
|
+ }
|
|
|
+ /*新建*/
|
|
|
+ public function create()
|
|
|
+ {
|
|
|
+ $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
|
|
|
+ if ($name == "") {
|
|
|
+ return error_show(1003, "公司名称不能为空");
|
|
|
+ }
|
|
|
+ $rename = Db::name('customer_org1')->where(['is_del' => 0, 'name' => $name])->find();
|
|
|
+ if (!empty($rename)) {
|
|
|
+ return error_show(1002, "公司名称已存在");
|
|
|
+ }
|
|
|
+ $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "0";
|
|
|
+ $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "";
|
|
|
+ if ($pid === "") {
|
|
|
+ return error_show(1002, "父级id不能为空");
|
|
|
+ }
|
|
|
+ if($pid!=0){
|
|
|
+ $spid = Db::name('company_item')->where(['id' => $pid, 'is_del' => 0])->find();
|
|
|
+ if(empty($spid)){
|
|
|
+ return error_show(1004,"父级不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "1";
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $tada = [
|
|
|
+ "name" => $name,
|
|
|
+ "weight" => $weight, "pid" => $pid,
|
|
|
+ "level" => $level,
|
|
|
+ "updatetime" => date("Y-m-d H:i:s"),
|
|
|
+ "addtime" => date("Y-m-d H:i:s"),
|
|
|
+ "depart_link" => "", "is_del" => 0
|
|
|
+ ];
|
|
|
+ $t = Db::name('customer_org1')->insert($tada, true);
|
|
|
+ if ($t > 0) {
|
|
|
+ if(isset($spid)){
|
|
|
+ $depart_link = $spid['depart_link']."{$t}-";
|
|
|
+ }else{
|
|
|
+ $depart_link = "{$t}-";
|
|
|
+ }
|
|
|
+ $level = explode('-', $depart_link);
|
|
|
+ $level = array_filter($level);
|
|
|
+ $level = count($level);
|
|
|
+ $k = ['depart_link' => $depart_link, 'level' => $level];
|
|
|
+ $u = Db::name('customer_org1')->where(['id' => $t])->save($k);
|
|
|
+ //var_dump( Db::name('customer_org1')->getLastSql());
|
|
|
+ if ($u) {
|
|
|
+ Db::commit();
|
|
|
+ return error_show(0, "新建成功");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Db::rollback();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ return error_show(1003, $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*更新*/
|
|
|
+ public function updat(){
|
|
|
+ $id = isset($this->post['id'])?intval($this->post['id']):"";
|
|
|
+ $sid = Db::name('customer_org1')->where("id","=","$id")->find();
|
|
|
+ // var_dump(Db::name('customer_org1')->getLastSql());
|
|
|
+ if($sid==false){
|
|
|
+ return error_show(1004,"公司不存在");
|
|
|
+ }
|
|
|
+ $pid = isset($this->post['pid']) && $this->post['pid'] !=="" ? intval($this->post['pid']):"";
|
|
|
+ if($pid===""){
|
|
|
+ return error_show(1004,"父级id不能为空");
|
|
|
+ }
|
|
|
|
|
|
+ if($pid!=0){
|
|
|
+ $fpid = Db::name('customer_org1')->where(['id'=>"$pid",'is_del'=>0])->find();
|
|
|
+ if(empty($fpid)){
|
|
|
+ return error_show(1004,"父级不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $weight = isset($this->post['weight']) && $this->post['weight'] !==""? intval($this->post['weight']):"0";
|
|
|
+
|
|
|
+// $level = isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']):"";
|
|
|
+ $name = isset($this->post['name'])? trim($this->post['name']):"";
|
|
|
+ if($name==""){
|
|
|
+ return error_show(1004,"公司名称不能为空");
|
|
|
+ }
|
|
|
+ $repeat_name = Db::name('customer_org1')->where(["is_del"=>0,"name"=>$name])->where("id","<>","$id")->find();
|
|
|
+ // echo Db::name('customer_org1')->getLastSql();
|
|
|
+ if(!empty($repeat_name)){
|
|
|
+ return error_show(1004,"部门名称已存在");
|
|
|
+ }
|
|
|
+ if(isset($fpid)){
|
|
|
+ $depart_link=$fpid['depart_link']."{$id}-";
|
|
|
+ }else{
|
|
|
+ $depart_link= "{$id}-";
|
|
|
+ }
|
|
|
+ $level =explode('-',$depart_link);
|
|
|
+ $level = array_filter($level);
|
|
|
+ $level= count($level);
|
|
|
+ $vir=[
|
|
|
+ "id"=>$id,
|
|
|
+ "name"=>$name,"pid"=>$pid,
|
|
|
+ "weight"=>$weight,"depart_link"=>$depart_link,
|
|
|
+ "level"=>$level,"is_del"=>0,"addtime"=>date("Y-m-d H:i:s"),
|
|
|
+ "updatetime"=>date("Y-m-d H:i:s"),
|
|
|
+ ];
|
|
|
+ $org = Db::name('customer_org1')->save($vir);
|
|
|
+ return $org ? error_show(0,"更新成功") : error_show(1004,"更新失败");
|
|
|
+ }
|
|
|
+ /*查询*/
|
|
|
+ public function selec(){
|
|
|
+ $tod=Db::name('customer_org1')->where(['depart_link','is_del'=>0])->select();
|
|
|
+ return app_show(0,"获取成功",$tod);
|
|
|
+ }
|
|
|
+ /*删除*/
|
|
|
+ public function dell(){
|
|
|
+ $id=isset($this->post['id']) ?intval($this->post['id']):"";
|
|
|
+ $custy = Db::name('customer_org1')->where(["is_del"=>0,'id'=>$id])->find();
|
|
|
+ if($custy==false){
|
|
|
+ return error_show(1004,"公司不存在");
|
|
|
+ }
|
|
|
+ $custy['is_del']=1;
|
|
|
+ $custy['updatetime']=date("Y-m-d H:i:s");
|
|
|
+ $compy = Db::name('customer_org1')->save($custy);
|
|
|
+ return $compy ? error_show(0,"删除成功"):error_show(1004,"删除失败");
|
|
|
}
|
|
|
}
|