| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');include_once(FCPATH . 'application/controllers/Base_Controller.php');class Policy extends Base_Controller {    public function __construct() {        parent::__construct();        $this->load->model('Policy_model');        $this->load->model('Company_model');        $this->load->model('Group_model');        $this->load->model('Lamp_model');        $this->load->model('Project_model');    }    // 获取策略列表    public function getlist(){        $companyid = $this->get_user_info('companyid');        $keyword = $this->input->post('keyword',true);        // $companyid = $this->input->post('companyid',true);        // if (empty($companyid)) {        //     exit(json_result('0007',$this->response['0007'],array()));        // }        $page = intval($this->input->post('page',true));        $count = intval($this->input->post('count',true));        $page = empty($page) ? '1' : $page;        $count = empty($count) ? '9' : $count;        $data = $this->Policy_model->get_list_by_company($companyid,$page,$count,$keyword);    	$total = $this->Policy_model->get_total_by_company($companyid,$keyword);    	exit(json_result('0000',$this->response['0000'],array('list'=>$data,'total'=>ceil($total/$count))));    }    // 检查时间是否有重叠    // private function check_date($datelist){    //     for ($i=0; $i < count($datelist); $i++) {     //         $date1 = explode('-', $datelist[$i]['date']);    //         if (strtotime(date('1970/01/01 H:i:s',strtotime($date1[0]))) >= strtotime(date('1970/01/01 H:i:s',strtotime($date1[1])))) {    //             exit(json_result('0904',$this->response['0904'],array()));    //         }    //         if (strtotime(date('Y/m/d 00:00:00',strtotime($date1[0]))) >= strtotime(date('Y/m/d 00:00:00',strtotime($date1[1])))) {    //             exit(json_result('0905',$this->response['0905'],array()));    //         }    //         for ($j=$i+1; $j < count($datelist); $j++) {     //             $date2 = explode('-', $datelist[$j]['date']);    //             $date3 = date('Y-m-d',strtotime($date1[0])).'-'.date('Y-m-d',strtotime($date1[1]));    //             $date4 = date('Y-m-d',strtotime($date2[0])).'-'.date('Y-m-d',strtotime($date2[1]));    //             if ($date3 == $date4) {    //                 $date1[0] = date('1970/01/01 H:i:s',strtotime($date1[0]));    //                 $date2[0] = date('1970/01/01 H:i:s',strtotime($date2[0]));    //                 $date1[1] = date('1970/01/01 H:i:s',strtotime($date1[1]));    //                 $date2[1] = date('1970/01/01 H:i:s',strtotime($date2[1]));    //                 $index = '0906';    //             }else{    //                 $index = '0907';    //             }    //             if (!(strtotime($date1[1]) < strtotime($date2[0]) || strtotime($date2[1]) < strtotime($date1[0]))) {    //                 exit(json_result($index,$this->response[$index],array()));    //             }    //         }    //     }    // }    // 添加编辑策略    public function save(){    	// 权限控制    	$role = $this->get_user_info('role');        $userid = $this->get_user_info('id');        $where['id'] = $this->input->post('id',true);    	    	$data['name'] = $this->input->post('name',true);    	$datalist = $this->input->post('datalist',true);    	// $data['companyid'] = $this->input->post('companyid',true);        $data['companyid'] = $this->get_user_info('companyid');        if(empty($data['name'])) exit(json_result('0908',$this->response['0908'],array()));        if (empty($datalist)) exit(json_result('0909',$this->response['0909'],array()));        // 检查时间        // $this->check_date($datalist);        $data['createtime'] = date('Y-m-d H:i:s');    	if (empty($where['id'])) {                		// 添加    		if ($this->Policy_model->getDataCount(array('name'=>$data['name'],'companyid'=>$data['companyid']))) {    			exit(json_result('0901',$this->response['0901'],array()));    		}                        // $data['owner_id'] = $userid;    		$policy_id = $this->Policy_model->insert_policy_info($data);            if (!empty($datalist)) {                foreach ($datalist as $v) {                    $policy_cmd = array();                    $policy_cmd = ['policyid'=>$policy_id];                    $policy_cmd['starttime'] = date('Y-m-d',strtotime($v['date']));                    $index = 1;                    foreach ($v['timeList'] as $time) {                        $policy_cmd['time'.$index] = $time['time'];                        $policy_cmd['value'.$index] = $time['value'];                        $index += 1;                    }                    $this->Policy_model->insert($policy_cmd);                }            }    		exit(json_result('0000',$this->response['0000'],array('policyid'=>$policy_id)));    	}else{    		// 编辑    		if ($this->Policy_model->getDataCount(array('name'=>$data['name'],'companyid'=>$data['companyid']),$where['id'])) {    			exit(json_result('0901',$this->response['0901'],array()));    		}    		$this->Policy_model->update_policy_info(array('id'=>$where['id']),$data);            // 删除原有策略    		$this->Policy_model->del_by_fiter(array('policyid'=>$where['id']));    		            if (!empty($datalist)) {                foreach ($datalist as $v) {                    $policy_cmd = array();                    $policy_cmd = ['policyid'=>$where['id']];                    $policy_cmd['starttime'] = date('Y-m-d',strtotime($v['date']));                    $index = 1;                    foreach ($v['timeList'] as $time) {                        $policy_cmd['time'.$index] = $time['time'];                        $policy_cmd['value'.$index] = $time['value'];                        $index += 1;                    }                    $this->Policy_model->insert($policy_cmd);                }            }    		exit(json_result('0000',$this->response['0000'],array('policyid'=>$where['id'])));    	}    }    // 策略选择列表    public function policy_nav(){        // $role = $this->get_user_info('role');        // $userid = $this->get_user_info('id');    	$companyid = $this->get_user_info('companyid');        $version = $this->session->userdata('version');    	$data = $this->Policy_model->get_list_by_fiter(array('companyid'=>$companyid),'id,name');        if (empty($version)) {            $name = '取消策略';        }else{            $name = 'Cancellation strategy';        }        if (empty($data)) {            $data = array();            $data[] = array('name'=>$name,'id' => 0);        }else{            $data[] = array('name'=>$name,'id' => 0);        }        exit(json_result('0000',$this->response['0000'],array('list'=>$data)));    }    // // 地图页分组数    // public function grouplist(){    // 	$projectid = $this->input->post('projectid');    // 	if(empty($projectid)){    // 		exit(json_result('0007',$this->response['0007'],array()));    // 	}    //     $version = $this->session->userdata('version');    //     $data = $this->Group_model->get_list(array('projectid'=>$projectid));    //     foreach ($data as &$v) {    //         if ($v['actiontype'] == 0) {  // 亮度开关    //             if (empty($version)) {    //                 $v['content'] = '亮度'.$v['value'].'%';    //             }else{    //                 $v['content'] = 'Brightness:'.$v['value'].'%';    //             }                    //         }elseif ($v['actiontype'] == 1) {  // 系统自适应    //             if (empty($version)) {    //                 $v['content'] = '系统自适应';    //             }else{    //                 $v['content'] = 'System adaptation';    //             }                    //         }else{  // 策略    //             $v['content'] = $this->Policy_model->get_info_by_fiter(array('id'=>$v['value']));    //         }    //     }    // 	exit(json_result('0000',$this->response['0000'],array('list'=>$data,"projectid"=>$projectid)));    // }    // // 分组列表    // public function get_grouplist(){    //     $projectid = $this->input->post('projectid');    //     if(empty($projectid)){    //         exit(json_result('0007',$this->response['0007'],array()));    //     }    //     $data = $this->Group_model->get_list_by_fiter(array('projectid'=>$projectid));    //     exit(json_result('0000',$this->response['0000'],array('list'=>$data,"projectid"=>$projectid)));    // }    // // 编辑/添加分组    // public function groupsave(){    // 	// 权限控制    // 	$role = $this->get_user_info('role');    // 	$where['id'] = $this->input->post('id',true);    // 	$data['projectid'] = $this->input->post('projectid',true);    // 	$data['name'] = $this->input->post('name',true);    // 	$data['lampid'] = $this->input->post('lampid',true);    //     $data['updatetime'] = date('Y-m-d H:i:s');    // 	if (empty($data['name'])) exit(json_result('0910',$this->response['0910'],array()));    //     if(empty($data['projectid'])) exit(json_result('0911',$this->response['0911'],array()));    // 	if (empty($where['id'])) {    // 		// 添加    // 		if ($this->Group_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']))) {    // 			exit(json_result('0902',$this->response['0902'],array()));    // 		}    // 		$id = $this->Group_model->insert($data);    // 	}else{    // 		// 编辑    // 		if ($this->Group_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']),$where['id'])) {    // 			exit(json_result('0902',$this->response['0902'],array()));    // 		}    // 		$this->Group_model->update($where,$data);    // 		$id = $where['id'];    // 	}    // 	exit(json_result('0000',$this->response['0000'],array('id'=>$id)));    // }    // // 分组详情    // public function groupinfo(){    // 	$groupid = $this->input->post('groupid',true);    // 	if (empty($groupid)) {    // 		exit(json_result('0903',$this->response['0903'],array()));    // 	}    //     $version = $this->session->userdata('version');    // 	$data = $this->Group_model->getOne($groupid);    //     $userid = $this->get_user_info('id');    //     $role = $this->get_user_info('role');    // 	if ($data['actiontype'] == 0) {    //         if (empty($version)) {    //             $data['groupwork'] = '亮度'.$data['value'].'%';    //         }else{    //             $data['groupwork'] = 'Brightness:'.$data['value'].'%';    //         }    		    //         $data['policy_list'] = $this->select_list();    // 	}elseif ($data['actiontype'] == 1) {    //         if (empty($version)) {    //             $data['groupwork'] = '系统自适应';    //         }else{    //             $data['groupwork'] = 'System adaptation';    //         }    //         $data['policy_list'] = $this->select_list();    // 	}else{    // 		$res = $this->Policy_model->get_list_by_fiter(array('id'=>$data['value']),'name');    // 		$data['groupwork'] = empty($res) ? '' : $res[0]['name'];    //         $data['policy_list'] = $this->select_list($data['value']);    // 	}    // 	$lampIds = explode(',', $data['lampid']);    //     $data['count'] = empty($data['lampid']) ? 0 : count($lampIds);    // 	$field = "L.id,    // 			  L.number,    // 			  L.logtime as updatetime,    // 			  L.address,    // 			  L.lighteness as light,    //               L.isfaulted,    //               L.projectid";    // 	$list = $this->Lamp_model->get_list_in('L.id',$lampIds,$field);    // 	foreach ($list as &$v) {    //         if ($v['isfaulted'] == 0) {    //             if (empty($version)) {    //                 $v['isfaulted'] = '无';    //             }else{    //                 $v['isfaulted'] = 'Nothing';    //             }                    //         }else{    //             $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$v['id'],'status'=>0),'stralarmtype');    //             if (empty($version)) {    //                 $v['isfaulted'] = empty($res['stralarmtype']) ? '无' : $res['stralarmtype'];    //             }else{    //                 $v['isfaulted'] = empty($res['stralarmtype']) ? 'Nothing' : alarm_translate($res['stralarmtype']);    //             }    //         }    //         if (!empty($v['updatetime'])) {    //             $timezone = $this->Project_model->get_timezone_by_projectid($v['projectid']);    //             $v['updatetime'] = date_change($v['updatetime'],8,$timezone['value']);    //         }    //         $v['light'] = empty($v['light']) ? 0 : $v['light'];    // 	}    // 	$data['list'] = $list;    // 	exit(json_result('0000',$this->response['0000'],$data));    // }    // // 添加灯控页路灯列表   	// public function lamp_list(){   	// 	$groupid = $this->input->post('groupid',true);   	// 	$projectid = $this->input->post('projectid',true);   	// 	if (!empty($groupid)) {   	// 		$data = $this->Group_model->getOne($groupid,'lampid');   	// 		$ids = explode(',', $data['lampid']);    //         $selectList = $this->Lamp_model->get_list_in('L.id',$ids,"L.id,L.number");   	// 	}else{    //         $selectList = array();   	// 	}    //     $version = $this->session->userdata('version');   	// 	$field = "L.id,    // 			  L.number,    // 			  L.logtime as updatetime,    // 			  L.address,    // 			  AI.stralarmtype,    // 			  L.lighteness as light,    //               L.section,    //               N.networkname";    //     $list = $this->Lamp_model->get_list_in('L.projectid',array($projectid),$field);    //     $timezone = $this->Project_model->get_timezone_by_projectid($projectid);    	    //     // 返回数据处理    // 	foreach ($list as &$v) {    //         if (empty($version)) {    //             $v['isfaulted'] = empty($v['stralarmtype']) ? '无' : $v['stralarmtype'];    //         }else{    //             $v['isfaulted'] = empty($v['stralarmtype']) ? 'Nothing' : alarm_translate($v['stralarmtype']);    //         }    //         $v['light'] = empty($v['light']) ? 0 : $v['light'];    //         $v['section'] = empty($v['section']) ? '' : $v['section'];    //         if (!empty($v['updatetime'])) {    //             $v['updatetime'] = date_change($v['updatetime'],8,$timezone['value']);    //         }    // 	}    // 	exit(json_result('0000',$this->response['0000'],array('list'=>$list,'selectList'=>$selectList)));   	// }    // // 删除分组中的路灯    // public function lampdel(){    // 	$role = $this->get_user_info('role');            // 	$groupid = $this->input->post('groupid',true);    // 	$lampid = $this->input->post('lampid',true);    // 	if (empty($groupid) || empty($lampid)) {    // 		exit(json_result('0007',$this->response['0007'],array()));    // 	}    // 	$res = $this->Group_model->getOne($groupid,'lampid');    // 	$ids = explode(',', $res['lampid']);    // 	$lampids = explode(',', $lampid);    // 	$temp = array();    // 	foreach ($ids as $v) {    // 		if (in_array($v, $lampids)) {    // 			continue;    // 		}    // 		$temp[] = $v;    // 	}    // 	$idStr = implode(',', $temp);    // 	$this->Group_model->update(array('id'=>$groupid),array('lampid'=>$idStr));    // 	exit(json_result('0000',$this->response['0000'],array()));    // }    // 策略详情    public function policy_detail(){        $policyid = $this->input->post('policyid',true);        if (empty($policyid)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $data = $this->Policy_model->get_detail(array('id'=>$policyid));        exit(json_result('0000',$this->response['0000'],$data));    }    // 删除策略    public function del_policy(){        $policyid = $this->input->post('policyid',true);        $version = $this->session->userdata('version');        if (empty($policyid)) {            exit(json_result('0007',$this->response['0007'],array()));        }        $groupData = $this->Lamp_model->update(array('policyid'=>$policyid),array('policyid'=>0));        $this->Policy_model->del_by_fiter(array('policyid'=>$policyid));   // 删除策略内容        $this->Policy_model->del_info_fiter(array('id'=>$policyid));    // 删除策略信息        exit(json_result('0000',$this->response['0000'],array()));    }    // 设置策略    public function set_policy(){        $lampid = trim($this->input->post('lampid',true));        $policyid = intval($this->input->post('policyid',true));        if (empty($lampid)) exit(json_result('0007',$this->response['0007']));        $lampid = implode(',', explode(',', $lampid));        $lampData = $this->db->query('select id from lampinfo where id in ('.$lampid.') AND loadtype = 0')->row_array();        if (!empty($lampData) && !empty($lampData['id'])) exit(json_result('0420',$this->response['0420']));        $this->Lamp_model->update(array('id'=>explode(',', $lampid)),array('policyid'=>$policyid));        exit(json_result('0000',$this->response['0000']));    }    // // 分组设置    // public function groupset(){    //     $role = $this->get_user_info('role');    //     $data['actiontype'] = $this->input->post('type',true);    //     $data['name'] = $this->input->post('name',true);    //     $where['id'] = $this->input->post('groupid',true);    //     if (empty($where['id'])) exit(json_result('0911',$this->response['0911'],array()));    //     if(!isset($data['actiontype']) || !is_numeric($data['actiontype'])) exit(json_result('0912',$this->response['0912'],array()));    //     if (empty($data['name'])) {    //         exit(json_result('0910',$this->response['0910'],array()));    //     }    //     $res = $this->Group_model->getOne($where['id'],'projectid');    //     if ($this->Group_model->getDataCount(array('name'=>$data['name'],'projectid'=>$res['projectid']),$where['id'])) {    //         exit(json_result('0902',$this->response['0902'],array()));    //     }    //     $data['value'] = $this->input->post('value',true);    //     if ($data['actiontype'] == 2 && empty($data['value'])) {    //         exit(json_result('0913',$this->response['0913'],array()));    //     }    //     // 修改分组设置    //     $this->Group_model->update(array('id'=>$where['id']),$data);    //     if (isset($data['actiontype']) && $data['actiontype'] == 0) {    //         $cmd = '{"cmd_type":"lamp_group_cmd","cmd_id":'.$where['id'].'}';    //         $cmdret = send_cmd($cmd,0);    //     }    //     if (isset($data['actiontype']) && $data['actiontype'] == 2) {    //         $cmd = '{"cmd_type":"policy_cmd","cmd_id":'.$where['id'].'}';    //         $cmdret = send_cmd($cmd,0);    //     }    //     exit(json_result('0000',$this->response['0000'],array()));    // }    // // 删除分组    // public function del_group(){    //     $groupid = $this->input->post('groupid',true);    //     if (empty($groupid)) {    //         exit(json_result('0914',$this->response['0914'],array()));    //     }    //     $this->Group_model->delOne($groupid);    //     exit(json_result('0000',$this->response['0000'],array()));    // }}?>
 |