123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- <?php
- class DpyhDepartmentAction extends CommonAction {
- const tableName = 'dpyh_department';
- const pkName = 'ID';
-
-
- function sys_base_list( ){
- $list = new \Jms\Gui\ClGrid();
- $list->sql_sort = 'Sort asc';
- $list->right_filter = function($right,$type){
- $page = $_SERVER['PATH_INFO'];
- return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
- };
- $list->sql_filter = function($search,&$cond){
- $id = I('get.id');
- if( $id ){ // 有上一级
- $cond['ParentID'] = $id;
- }else{ // 没有上一级
- $cond['ParentID'] = $this->userinfo['OrganizationId'];
- }
- $cond['Level'] = \Zndp\User\DepartEnum::PLANT_BASE;//种植基地
- //搜索条件
- if($search['DepartName']){ //按部门名称搜索
- $cond['DepartName'] = array('LIKE',"%{$search['DepartName']}%");
- }
- if($search['Charger']){ //按负责人搜索
- $cond['Charger'] = $search['Charger'];
- }
- if($search['Mobile']){ //按负责人电话搜索
- $cond['Mobile'] = $search['Mobile'];
- }
- };
- $list->row_filter = function(&$row)use($list){
- $row['Comment'] = $list->renderXEditableTextarea('编辑备注',$row,'Comment');
- //$row['Sort'] = $list->renderXEditableInput('排序',$row,'Sort');
- };
- $list->toolbar_filter = function(&$button){
- if($button['icon'] == 'add'){
- $button['url'] .= '&parent_id=' .I('get.id');
- }
- };
- $list->display($this);
- }
-
-
- function sys_company_list( ){
- $list = new \Jms\Gui\ClGrid();
- $list->sql_sort = 'Sort asc';
- //权限回调
- $list->right_filter = function($right,$type){
- $page = $_SERVER['PATH_INFO'];
- return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
- };
- $list->sql_filter = function($search,&$cond){
- $cond['Level'] = \Zndp\User\DepartEnum::PARENT_COMPANY;//总公司
- //搜索条件
- if($search['DepartName']){ //按公司名称搜索
- $cond['DepartName'] = array('LIKE',"%{$search['DepartName']}%");
- }
- if($search['Charger']){ //按负责人搜索
- $cond['Charger'] = $search['Charger'];
- }
- if($search['Mobile']){ //按负责人电话搜索
- $cond['Mobile'] = $search['Mobile'];
- }
- };
- $list->row_filter = function(&$row)use($list){
- $row['Comment'] = $list->renderXEditableTextarea('编辑备注',$row,'Comment');
- //$row['Sort'] = $list->renderXEditableInput('排序',$row,'Sort');
- };
- $list->display($this);
- }
-
-
- function sys_service_station_list( ){
- $list = new \Jms\Gui\ClGrid();
- $list->sql_sort = 'Sort asc';
-
- $list->right_filter = function($right,$type){
- $page = $_SERVER['PATH_INFO'];
- return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
- };
- $list->sql_filter = function($search,&$cond){
- $id = I('get.id');
- if( $id ){ // 有上一级
- $cond['ParentID'] = $id;
- }else{ // 没有上一级
- $cond['ParentID'] = $this->userinfo['OrganizationId'];
- }
- $cond['Level'] = \Zndp\User\DepartEnum::SERVICE_STATION; //服务部
- //搜索条件
- if($search['DepartName']){ //按部门名称搜索
- $cond['DepartName'] = array('LIKE',"%{$search['DepartName']}%");
- }
- if($search['Charger']){ //按负责人搜索
- $cond['Charger'] = $search['Charger'];
- }
- if($search['Mobile']){ //按负责人电话搜索
- $cond['Mobile'] = $search['Mobile'];
- }
- };
- $list->row_filter = function(&$row)use($list){
- $row['Comment'] = $list->renderXEditableTextarea('编辑备注',$row,'Comment');
- //$row['Sort'] = $list->renderXEditableInput('排序',$row,'Sort');
- };
- $list->toolbar_filter = function(&$button){
- if($button['icon'] == 'add'){
- $button['url'] .= '&parent_id=' .I('get.id');
- }
- };
- $list->display($this);
- }
-
-
- function sys_workstation_list( ){
- $list = new \Jms\Gui\ClGrid();
- $list->sql_sort = 'Sort asc';
- $list->right_filter = function($right,$type){
- $page = $_SERVER['PATH_INFO'];
- return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
- };
- $list->sql_filter = function($search,&$cond){
- $id = I('get.id');
- if( $id ){ // 有上一级
- $cond['ParentID'] = $id;
- }else{ // 没有上一级
- $cond['ParentID'] = $this->userinfo['OrganizationId'];
- }
- $cond['Level'] = \Zndp\User\DepartEnum::WORK_STATION;//工作站
- //搜索条件
- if($search['DepartName']){ //按部门名称搜索
- $cond['DepartName'] = array('LIKE',"%{$search['DepartName']}%");
- }
- if($search['Charger']){ //按负责人搜索
- $cond['Charger'] = $search['Charger'];
- }
- if($search['Mobile']){ //按负责人电话搜索
- $cond['Mobile'] = $search['Mobile'];
- }
- };
- $list->row_filter = function(&$row)use($list){
- $row['Comment'] = $list->renderXEditableTextarea('编辑备注',$row,'Comment');
- //$row['Sort'] = $list->renderXEditableInput('排序',$row,'Sort');
- };
- $list->toolbar_filter = function(&$button){
- $parent_id = I('get.id');
- if($button['icon'] == 'add'){ // 添加工作站
- $button['url'] .= '&parent_id=' .$parent_id;
- }
- if($button['icon'] == 'import'){ // 导入设备
- $button['url'] .= '&parent_id=' .$parent_id;
- }
- };
- $list->display($this);
- }
-
-
- function sys_list_nav( ){
- //菜单组
- $menus = array(
- //array('id'=>'depart','text'=>'机构管理','expand'=>true),
- array('id'=>'com','text' => '公司列表','url' => 'dpyh_department/sys_company_list','active'=>true),
- //array('pid'=>'depart','text' => '工作站列表','url' => 'dpyh_department/sys_workstation_list'),
- //array('pid'=>'depart','text' => '服务部列表','url' => 'dpyh_department/sys_service_station_list'),
- //array('pid'=>'depart','text' => '基站列表','url' => 'dpyh_department/sys_base_list'),
- //array('id'=>'right','text' => '权限列表','url' => 'uc_right/sys_list'),
- array('id'=>'role','text' => '角色列表','url' => 'dpyh_depart_role/sys_list'),
- //array('pid'=>'depart','text' => '用户列表','url' => 'dpyh_user/sys_list'),
- );
-
- //导航树配置数据
- $data = array(
- 'page' => '机构管理',
- 'title' => '机构管理',
- 'menus' => $menus
- );
- //生成导航树
- $nav = new \Jiaruan\ClNavTree;
- //权限回调
- $nav->right_filter = function($right,$type){
- $page = $_SERVER['PATH_INFO'];
- return \Jms\Ucenter\Right::defaultFilter($page,$right,$type);
- };
- $nav->setData($data);
- $nav->display($this);
- }
-
-
- function sys_addedit_comp( ){
- $form = new \Jms\Gui\ClForm();
- $form->before_add = function(&$data){
- //检查公司是否存在
- $where = array('DepartName'=>$data['DepartName']);
- $count = M('uc_organization')->where($where)->count();
- if($count > 0){
- json_fail('公司已存在');
- }
-
- $data['ParentID'] = 0;
- $data['Level'] = 0;
- };
- $form->after_save = function(&$data){
-
- };
- /*
- $form->after_save = function(&$data){
- //新增负责人时,同时生成部门负责人账号信息(账号,密码默认都是手机号码)
- $where = array('UserName|Mobile' => $data['Mobile']);
- $count = M('uc_user')->where($where)->count();
- if($count > 0){
- json_fail('部门添加成功,因手机号码已注册,部门负责人账号自动添加失败,请稍后手动添加');
- }
- $save_data = array(
- 'ID' => create_guid(),
- 'UserName' => $data['Mobile'],
- 'Password' => \Zndp\User\Util::encPwd($data['Mobile']),
- 'RealName' => $data['Charger'],
- 'Mobile' => $data['Mobile'],
- 'RoleId' => \Zndp\User\RoleEnum::COMPANY_LEADER,
- 'OrganizationId' => $data['ID'],
- 'AddTime' => date('Y-m-d H:i:s'),
- );
- $result = M('uc_user')->createAdd($save_data);
- if(!$result){
- json_fail('部门添加成功,但部门负责人账号自动添加失败,请稍后手动添加');
- }
- };
- */
- $form->display($this);
- }
-
-
- function sys_addedit_depart( ){
- $form = new \Jms\Gui\ClForm();
- $form->before_add = function(&$data){ //添加前回调
- $parent_id = I('get.parent_id');
- if(!$parent_id){
- json_fail('暂不支持添加同级部门,请使用更高权限账号添加');
- }
- $data['ParentID'] = $parent_id;
- $parent_lv = M('uc_organization')->where(array('ID'=>$parent_id))->getField('Level');
- $data['Level'] = $parent_lv + 1;
- };
- $form->after_save = function(&$data){
- /*
- //新增负责人时,同时生成部门负责人账号信息(账号,密码默认手机号码)
- $where = array('UserName|Mobile' => $data['Mobile']);
- $count = M('uc_user')->where($where)->count();
- if($count == 0){
- $leader_list = \Zndp\User\RoleEnum::get_leader_list();
- $save_data = array(
- 'ID' => create_guid(),
- 'UserName' => $data['Mobile'],
- 'Password' => \Zndp\User\Util::encPwd($data['Mobile']),
- 'RealName' => $data['Charger'],
- 'Mobile' => $data['Mobile'],
- 'RoleId' => $leader_list[$data['Level']],
- 'OrganizationId' => $data['ID'],
- 'AddTime' => date('Y-m-d H:i:s'),
- );
- $result1 = M('uc_user')->createAdd($save_data);
- if(!$result1){
- $msg .= '部门负责人账号自动添加失败,';
- }
- }
- */
- // 如果是基地,则默认添加设备组
- $group_name = $data['DepartName'] .'设备组1';
- $where = array('OrgName'=>$group_name);
- $group_count = M('dpsb_group')->where($where)->count();
- if($data['Level'] == \Zndp\User\DepartEnum::PLANT_BASE && $group_count == 0){// 如果是基地,默认添加一个设备组
- $group_data = array(
- 'OrgName' => $data['DepartName'] .'设备组1',
- 'FarmId' => $data['ID'],
- );
- $result2 = M('dpsb_group')->createAdd($group_data);
- if(!$result2){
- $msg .= '设备组自动添加失败,请稍后手动添加';
- json_success($msg);
- }
- }
- };
- $form->display($this);
- }
-
-
- public function sys_del( ){
- $grid = new \Jiaruan\GridData();
- $grid->before_delete = function(&$data){
- $where = array('ParentID'=>$data['ID']);
- $count = M('uc_organization')->where($where)->count();
- if($count > 0){
- json_fail('尚有子部门存在,无法删除!');
- }
- if($data['Level'] == \Zndp\User\DepartEnum::PLANT_BASE){//基地,查看是否有设备组
- $where = array('FarmId'=>$data['ID']);
- $count = M('dpsb_group')->where($where)->count();
- if($count > 0){
- json_fail('基地下有设备组存在,请先删除设备组');
- }
- }
- };
- $grid->deleteByPk($this);
- }
-
-
- function sys_import_excel( ){
- $form = new \Jms\Gui\ClForm();
- $form->import_excel = function($path){
- $im = new \Jms\Excel\Import();
- // 初始化计数
- $im->total = 0;
- $im->success = 0;
- $im->skipped = 0;
- $im->row_filter = function(&$data) use($im){ // 行过滤
- // 计数开始
- $im->total++;
- // 工作站、服务部、基地必须都要有数据
- if($data['WorkStation'] && $data['ServiceStation'] && $data['Base'] && $data['FarmlandNum']){
- $parent_id = I('get.parent_id'); //公司id
- $organization = M('uc_organization');
- // 检查工作站是否存在
- $where = array('DepartName'=>$data['WorkStation'],'ParentID'=>$parent_id);
- $workstation_id = $organization->where($where)->getField('ID');
- if( $workstation_id ){
- // 检查服务部是否存在
- $where = array('DepartName'=>$data['ServiceStation'], 'ParentID'=>$workstation_id );
- $service_id = $organization->where($where)->getField('ID');
- if($service_id){
- // 检查基地是否存在
- $where = array('DepartName'=>$data['Base'],'ParentID'=>$service_id);
- $base_id = $organization->where($where)->getField('ID');
- if($base_id){
- // 检查通道组是否存在,不存在则添加一个
- $where = array('FarmId'=>$base_id);
- $group = M('dpsb_group');
- $group_id = $group->where($where)->getField('ID');
- if(!$group_id){
- $group_name = $data['Base'].'设备组1';
- $group_data = array('OrgName'=>$group_name,'FarmId'=>$base_id);
- $group_id = $group->createAdd($group_data);
- }
- // 检查设备序列号是否已存在
- $where = array('DeviceAddr'=> $data['DeviceAddr']);
- $device = M('dpsb_device');
- $count = $device->where($where)->count();
- if($count > 0){//已存在则跳过
- $im->skipped++;
- }else{
- // 添加设备
- $device_data = array(
- 'DeviceAddr' => $data['DeviceAddr'],
- 'DeviceGroupId' => $group_id,
- 'CellPhone' => $data['CellPhone'],
- 'IccId' => $data['IccId'],
- 'AddTime' => date('Y-m-d H:i:s'),
- );
- $result = $device->createAdd($device_data);
- if($result){
- $im->success++;
- }else{
- $im->failed++;
- }
- }
- }else{ //不存在基地则失败
- $im->failed++;
- }
- }else{ //不存在服务部则失败
- $im->failed++;
- }
- }else{ //不存在工作站则失败
- $im->failed++;
- }
- }else{// 否则失败
- $im->failed++;
- }
- };
- $option = array( // 导入选项
- 'column_fields' => array(
- 'A' => 'Number', //序号
- 'B' => 'WorkStation', //工作站
- 'C' => 'ServiceStation', //服务部
- 'D' => 'Base', //基地
- 'E' => 'FarmlandNum', //大棚编号
- 'F' => 'Manager', //大棚管理员
- 'G' => 'CellPhone', //管理员手机
- 'H' => 'DeviceAddr', //控制器序列号
- 'I' => 'IccId', //SIM卡号
- //'J' => 'TempAddr', //温湿度传感器序列号
- //'K' => 'IlluminationAddr', //光照传感器序列号
- ),
- 'header_row' => 1,
- 'data_start_row' => 2,
- 'max_allow_rows' => 10000, //最大允许行数
- );
- M()->startTrans();
- $result = $im->import( $path ,$option ); // 导入
- M()->commit();
- if(!$result['success']){
- json_fail($result['message']);
- }
- else{
- json_success("共{$im->total},导入成功{$im->success},失败{$im->failed},跳过{$im->skipped}");
- }
- };
- $form->before_add = function(&$data) use($form) {
- if(!$data['FilePath']){
- json_fail('上传文件不能为空');
- }
- $form->import_excel( CACHE_PATH . $data['FilePath'] );
- };
- $form->display($this);
- }
-
-
- function sys_nav_tree( ){
- //获取导航树数据
- $fields = 'ID as id,ParentID as pId,DepartName as name,Level as level';
- $zndp_util = new \Zndp\User\Util();
- $depart_id = $this->userinfo['OrganizationId'];
- $depart_list = $zndp_util->get_depart_option($depart_id,$fields,'name',false);
- if(empty($depart_list)){
- //没有数据,显示该
- $depart_list[0] = array('id'=>'','pid'=>'','name'=>'没有组织','direct_url'=>'');
- }else{
- for($i=0; $i<count($depart_list); $i++){// 放入链接
- /*if($depart_list[$i]['level'] <= \Zndp\User\DepartEnum::WORK_STATION){
- $depart_list[$i]['open']= true;
- }*/
- $depart_list[$i]['open']= true;//默认展开
- if($depart_list[$i]['level'] == 0){ //总公司
- $depart_list[$i]['direct_url'] = '?s=dpyh_department/sys_workstation_list'; //点击显示工作站
- }elseif($depart_list[$i]['level'] == 1){ //工作站
- $depart_list[$i]['direct_url'] = '?s=dpyh_department/sys_service_station_list'; //点击显示服务部
- }elseif($depart_list[$i]['level'] == 2){ //服务部
- $depart_list[$i]['direct_url'] = '?s=dpyh_department/sys_base_list'; //点击显示基地
- }elseif($depart_list[$i]['level'] == 3){ //基地
- $depart_list[$i]['direct_url'] = '?s=dpyh_manager/sys_list'; //点击显示用户
- }else{//没有数据
-
- }
- }
- }
- //平台,超级管理员,插入总公司列表
- $admin_arr = array(\Zndp\User\RoleEnum::GENERAL_ADMINISTRATOR,\Zndp\User\RoleEnum::SUPER_ADMINISTRATOR);
- if( in_array($this->userinfo['RoleId'], $admin_arr) ){
- $company_list = array('id'=>'0','pId'=>'','name'=>'公司列表','direct_url'=>'?s=dpyh_department/sys_company_list','open'=>true);
- array_unshift($depart_list,$company_list);
- }
- $this->assign('title','组织机构');
- $this->assign('tree_data',$depart_list);
- $this->display();
- }
-
-
- function sys_comp_search( ){
-
- }
-
-
- function sys_depart_search( ){
-
- }
-
- }
|