AdminCompanyController.php 11.3 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\portal\controller;

use app\portal\model\UserModel;
use cmf\controller\AdminBaseController;
use app\portal\model\CompanyModel;
use think\Db;
use think\db\Query;

class AdminCompanyController extends AdminBaseController
{

    //列表页
    public function index(){
        $list = Db::name('company')
            ->where('pid',0)
            ->where(function (Query $query) {
                $data = $this->request->param();
                if (!empty($data['company_name'])) {
                    $company_name = $data['company_name'];
                    $query->where('company_name', 'like', "%$company_name%");
                }
            })
            ->field('id,company_name,u_l_id')
            ->order('id desc')
            ->paginate(10,false,['query'=>request()->param()]);
        $res = $list->toArray();
        // 获取分页显示
        $page = $list->render();
        foreach($res['data'] as &$value){
            $ids = explode(',',trim($value['u_l_id'],','));
            $user = $this->getLeader($ids);
            $value['user_login'] = $user['user_login'];
//            $value['mobile'] = $user['mobile'];
        }
        $this->assign('list', $res['data']);
        $this->assign('page', $page);

        //获取身份信息
        $user = new \app\user\controller\AdminIndexController();
        $identity = $user->identity();
        $this->assign('identity',$identity);
        // 渲染模板输出
        return $this->fetch();
    }

    //获取领导用户名
    public function getLeader($ids){
        $res = Db::name('user')
            ->whereIn('id',$ids)
            ->where('user_status',1)
            ->field('id,user_login,mobile')
            ->select()
            ->toArray();
        $arr['user_login'] = implode(',',array_values(array_column($res,'user_login')));
//        $arr['mobile'] = implode(',',array_values(array_column($res,'mobile')));
        return $arr;
    }

    //新增页面
    public function add(){
        //查询领导
        $userLeader = $this->getStaff(1);
        $this->assign('user_leader',$userLeader);

        //查询总领导
        $userLeaders = $this->getStaff(2);
        $this->assign('user_leaders',$userLeaders);

        //查询员工
        $userStaff = $this->getStaff(0);
        $this->assign('user_staff',$userStaff);
        return $this->fetch();
    }

    //获取用户信息
    public function getStaff($identity){
        $user = Db::name('user')
            ->where(['user_type'=>2,'user_status'=>1,'identity'=>$identity])
            ->field('id,user_login,mobile')
            ->select()
            ->toArray();
        return $user;
    }

    //新增提交
    public function addPost(){
        $data = $this->request->param();
        $result    = $this->validate($data, 'Company');
        if ($result !== true) {
            $this->error($result);
        }
        //甲方数据
        $companyModel = new CompanyModel();
//        if(strpos($data['logo'],$companyModel::HOST) === false){
//            $data['logo'] = $companyModel::HOST.$data['logo'];
//        }
//        if(strpos($data['logo1'],$companyModel::HOST) === false){
//            $data['logo1'] = $companyModel::HOST.$data['logo1'];
//        }
//        if(strpos($data['license'],$companyModel::HOST) === false){
//            $data['license'] = $companyModel::HOST.$data['license'];
//        }
//        if(strpos($data['license1'],$companyModel::HOST) === false){
//            $data['license1'] = $companyModel::HOST.$data['license1'];
//        }

        //乙方数据
        $part_B['company_name'] = $data['company_name'];
        //负责人uid
        $part_B['u_l_id'] = ','.implode(',',$data['u_l_id']).',';
        //员工uid,以逗号隔开
        $part_B['u_s_id'] = ','.implode(',',$data['u_s_id']).',';
        //营业执照
        $part_B['license'] = $data['license'];
        $part_B['create_time'] = time();
        //公司logo
        $part_B['logo'] = $data['logo'];
        $res = $companyModel->create($part_B);

        //甲方数据
        if(isset($data['flag'])){
            foreach($data['flag']  as $value){
                $part_A = [];
                $part_A['pid'] = $res->id;
                $part_A['company_name'] = $data['company_name'.$value];
                $is_children = $data['is_children'.$value];
                if($is_children == 1){
                    //总领导uid
                    $part_A['u_ls_id'] = $data['u_ls_id'.$value];
                    $part_A['address'] = $data['address'.$value];
                    $part_A['company_name_head'] = $data['company_name_head'.$value];
                }
                $part_A['is_children'] = $data['is_children'.$value];
                //负责人uid
                $part_A['u_l_id'] = ','.implode(',',$data['u_l_id'.$value]).',';
                //员工uid,以逗号隔开
                $part_A['u_s_id'] = ','.implode(',',$data['u_s_id'.$value]).',';
                //营业执照
                $part_A['license'] = $data['license'.$value];
                $part_A['create_time'] = time();
                //公司logo
                $part_A['logo'] = $data['logo'.$value];
                $companyModel->create($part_A);
            }
        }

//        //创建乙方登录普通管理员账号
//        $userModel = new UserModel();
//        $arr['user_login'] = $res->company_name;
//        $arr['user_pass'] = cmf_password('123456');
//        $arr['company_id'] = $res->id;
//        $res1 = $userModel->create($arr);
//        //role_user表
//        $role['role_id'] = 2;
//        $role['user_id'] = $res1->id;
//        Db::name('RoleUser')->insert($role);
        $this->success("添加成功!", url("AdminCompany/index"));
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id', 0, 'intval');
        //获取乙方数据
        $res = Db::name('company')
            ->where('id',$id)
            ->find();
        $res['u_l_id'] = explode(',',trim($res['u_l_id'],','));
        $res['u_s_id'] = explode(',',trim($res['u_s_id'],','));
        //获取甲方数据
        $res1 = Db::name('company')
            ->where('pid',$id)
            ->select()
            ->toArray();
        foreach($res1 as &$value){
            $value['u_l_id'] = explode(',',trim($value['u_l_id'],','));
            $value['u_s_id'] = explode(',',trim($value['u_s_id'],','));
        }
        //数组数量
        $index = count($res1);
        $this->assign('index',$index);
        $this->assign('post', $res);
        $this->assign('post1', $res1);

        //查询领导
        $userLeader = $this->getStaff(1);
        $this->assign('user_leader',$userLeader);

        //查询总领导
        $userLeaders = $this->getStaff(2);
        $this->assign('user_leaders',$userLeaders);

        //查询员工
        $userStaff = $this->getStaff(0);
        $this->assign('user_staff',$userStaff);
        return $this->fetch();
    }

    //获取甲方数据时候有子类
    public function is_children(){
        $id = $this->request->param('id', 0, 'intval');
        $res = Db::name('company')
            ->where('pid',$id)
            ->field('id,pid,is_children')
            ->select()
            ->toArray();
        return json_encode($res);
    }

    //编辑保存页面
    public function editPost(){
        $data = $this->request->param();
        $result    = $this->validate($data, 'Company');

        if ($result !== true) {
            $this->error($result);
        }
        //甲方数据
        $companyModel = new CompanyModel();
//        if(strpos($data['logo'],$companyModel::HOST) === false){
//            $data['logo'] = $companyModel::HOST.$data['logo'];
//        }
//        if(strpos($data['logo1'],$companyModel::HOST) === false){
//            $data['logo1'] = $companyModel::HOST.$data['logo1'];
//        }
//        if(strpos($data['license'],$companyModel::HOST) === false){
//            $data['license'] = $companyModel::HOST.$data['license'];
//        }
//        if(strpos($data['license1'],$companyModel::HOST) === false){
//            $data['license1'] = $companyModel::HOST.$data['license1'];
//        }

        //乙方数据
        $part_B['company_name'] = $data['company_name'];
        //负责人uid
        $part_B['u_l_id'] = ','.implode(',',$data['u_l_id']).',';
        //员工uid,以逗号隔开
        $part_B['u_s_id'] = ','.implode(',',$data['u_s_id']).',';
        //营业执照
        $part_B['license'] = $data['license'];
        $part_B['update_time'] = time();
        //公司logo
        $part_B['logo'] = $data['logo'];
        $companyModel->where('id',$data['id'])->update($part_B);
        //甲方数据
        if(isset($data['flag'])){
            foreach($data['flag']  as $value){
                $part_A = [];
                $part_A['pid'] = $data['id'];
                $part_A['company_name'] = $data['company_name'.$value];
                $is_children = $data['is_children'.$value];
                if($is_children == 1){
                    //总领导uid
                    $part_A['u_ls_id'] = $data['u_ls_id'.$value];
                    $part_A['address'] = $data['address'.$value];
                    $part_A['company_name_head'] = $data['company_name_head'.$value];
                }
                $part_A['is_children'] = $data['is_children'.$value];
                //负责人uid
                $part_A['u_l_id'] = ','.implode(',',$data['u_l_id'.$value]).',';
                //员工uid,以逗号隔开
                $part_A['u_s_id'] = ','.implode(',',$data['u_s_id'.$value]).',';
                //营业执照
                $part_A['license'] = $data['license'.$value];

                //公司logo
                $part_A['logo'] = $data['logo'.$value];
                if(isset($data['id'.$value])){
                    $id = $data['id'.$value];
                    $part_A['update_time'] = time();
                    $companyModel->where('id',$id)->update($part_A);
                }else{
                    $part_A['create_time'] = time();
                    $companyModel->create($part_A);
                }
            }
        }

        //移除数据
        $del_ids = $data['del'];
        if($del_ids != 0){
            $del_ids = explode(',',$del_ids);
            $res = $this->is_project($del_ids);
            if($res){
                $this->error("甲方公司存在项目,不能删除!");
            }
            $companyModel->whereIn('id',$del_ids)->delete();
        }

        $this->success("编辑成功!", url("AdminCompany/index"));
    }

    //查询公司下是否有甲方项目
    public function is_project($del_ids){
        $res = Db::name('project')
            ->whereIn('a_cid',$del_ids)
            ->select()
            ->toArray();
        return $res;
    }
}