Policy.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Policy extends Base_Controller {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->load->model('Policy_model');
  7. $this->load->model('Company_model');
  8. $this->load->model('Group_model');
  9. $this->load->model('Lamp_model');
  10. $this->load->model('Project_model');
  11. }
  12. // 获取策略列表
  13. public function getlist(){
  14. $companyid = $this->get_user_info('companyid');
  15. $keyword = $this->input->post('keyword',true);
  16. // $companyid = $this->input->post('companyid',true);
  17. // if (empty($companyid)) {
  18. // exit(json_result('0007',$this->response['0007'],array()));
  19. // }
  20. $page = intval($this->input->post('page',true));
  21. $count = intval($this->input->post('count',true));
  22. $page = empty($page) ? '1' : $page;
  23. $count = empty($count) ? '9' : $count;
  24. $data = $this->Policy_model->get_list_by_company($companyid,$page,$count,$keyword);
  25. $total = $this->Policy_model->get_total_by_company($companyid,$keyword);
  26. exit(json_result('0000',$this->response['0000'],array('list'=>$data,'total'=>ceil($total/$count))));
  27. }
  28. // 检查时间是否有重叠
  29. // private function check_date($datelist){
  30. // for ($i=0; $i < count($datelist); $i++) {
  31. // $date1 = explode('-', $datelist[$i]['date']);
  32. // if (strtotime(date('1970/01/01 H:i:s',strtotime($date1[0]))) >= strtotime(date('1970/01/01 H:i:s',strtotime($date1[1])))) {
  33. // exit(json_result('0904',$this->response['0904'],array()));
  34. // }
  35. // if (strtotime(date('Y/m/d 00:00:00',strtotime($date1[0]))) >= strtotime(date('Y/m/d 00:00:00',strtotime($date1[1])))) {
  36. // exit(json_result('0905',$this->response['0905'],array()));
  37. // }
  38. // for ($j=$i+1; $j < count($datelist); $j++) {
  39. // $date2 = explode('-', $datelist[$j]['date']);
  40. // $date3 = date('Y-m-d',strtotime($date1[0])).'-'.date('Y-m-d',strtotime($date1[1]));
  41. // $date4 = date('Y-m-d',strtotime($date2[0])).'-'.date('Y-m-d',strtotime($date2[1]));
  42. // if ($date3 == $date4) {
  43. // $date1[0] = date('1970/01/01 H:i:s',strtotime($date1[0]));
  44. // $date2[0] = date('1970/01/01 H:i:s',strtotime($date2[0]));
  45. // $date1[1] = date('1970/01/01 H:i:s',strtotime($date1[1]));
  46. // $date2[1] = date('1970/01/01 H:i:s',strtotime($date2[1]));
  47. // $index = '0906';
  48. // }else{
  49. // $index = '0907';
  50. // }
  51. // if (!(strtotime($date1[1]) < strtotime($date2[0]) || strtotime($date2[1]) < strtotime($date1[0]))) {
  52. // exit(json_result($index,$this->response[$index],array()));
  53. // }
  54. // }
  55. // }
  56. // }
  57. // 添加编辑策略
  58. public function save(){
  59. // 权限控制
  60. $role = $this->get_user_info('role');
  61. $userid = $this->get_user_info('id');
  62. $where['id'] = $this->input->post('id',true);
  63. $data['name'] = $this->input->post('name',true);
  64. $datalist = $this->input->post('datalist',true);
  65. // $data['companyid'] = $this->input->post('companyid',true);
  66. $data['companyid'] = $this->get_user_info('companyid');
  67. if(empty($data['name'])) exit(json_result('0908',$this->response['0908'],array()));
  68. if (empty($datalist)) exit(json_result('0909',$this->response['0909'],array()));
  69. // 检查时间
  70. // $this->check_date($datalist);
  71. $data['createtime'] = date('Y-m-d H:i:s');
  72. if (empty($where['id'])) {
  73. // 添加
  74. if ($this->Policy_model->getDataCount(array('name'=>$data['name'],'companyid'=>$data['companyid']))) {
  75. exit(json_result('0901',$this->response['0901'],array()));
  76. }
  77. // $data['owner_id'] = $userid;
  78. $policy_id = $this->Policy_model->insert_policy_info($data);
  79. if (!empty($datalist)) {
  80. foreach ($datalist as $v) {
  81. $policy_cmd = array();
  82. $policy_cmd = ['policyid'=>$policy_id];
  83. $policy_cmd['starttime'] = date('Y-m-d',strtotime($v['date']));
  84. $index = 1;
  85. foreach ($v['timeList'] as $time) {
  86. $policy_cmd['time'.$index] = $time['time'];
  87. $policy_cmd['value'.$index] = $time['value'];
  88. $index += 1;
  89. }
  90. $this->Policy_model->insert($policy_cmd);
  91. }
  92. }
  93. exit(json_result('0000',$this->response['0000'],array('policyid'=>$policy_id)));
  94. }else{
  95. // 编辑
  96. if ($this->Policy_model->getDataCount(array('name'=>$data['name'],'companyid'=>$data['companyid']),$where['id'])) {
  97. exit(json_result('0901',$this->response['0901'],array()));
  98. }
  99. $this->Policy_model->update_policy_info(array('id'=>$where['id']),$data);
  100. // 删除原有策略
  101. $this->Policy_model->del_by_fiter(array('policyid'=>$where['id']));
  102. if (!empty($datalist)) {
  103. foreach ($datalist as $v) {
  104. $policy_cmd = array();
  105. $policy_cmd = ['policyid'=>$where['id']];
  106. $policy_cmd['starttime'] = date('Y-m-d',strtotime($v['date']));
  107. $index = 1;
  108. foreach ($v['timeList'] as $time) {
  109. $policy_cmd['time'.$index] = $time['time'];
  110. $policy_cmd['value'.$index] = $time['value'];
  111. $index += 1;
  112. }
  113. $this->Policy_model->insert($policy_cmd);
  114. }
  115. }
  116. exit(json_result('0000',$this->response['0000'],array('policyid'=>$where['id'])));
  117. }
  118. }
  119. // 策略选择列表
  120. public function policy_nav(){
  121. // $role = $this->get_user_info('role');
  122. // $userid = $this->get_user_info('id');
  123. $companyid = $this->get_user_info('companyid');
  124. $version = $this->session->userdata('version');
  125. $data = $this->Policy_model->get_list_by_fiter(array('companyid'=>$companyid),'id,name');
  126. if (empty($version)) {
  127. $name = '取消策略';
  128. }else{
  129. $name = 'Cancellation strategy';
  130. }
  131. if (empty($data)) {
  132. $data = array();
  133. $data[] = array('name'=>$name,'id' => 0);
  134. }else{
  135. $data[] = array('name'=>$name,'id' => 0);
  136. }
  137. exit(json_result('0000',$this->response['0000'],array('list'=>$data)));
  138. }
  139. // // 地图页分组数
  140. // public function grouplist(){
  141. // $projectid = $this->input->post('projectid');
  142. // if(empty($projectid)){
  143. // exit(json_result('0007',$this->response['0007'],array()));
  144. // }
  145. // $version = $this->session->userdata('version');
  146. // $data = $this->Group_model->get_list(array('projectid'=>$projectid));
  147. // foreach ($data as &$v) {
  148. // if ($v['actiontype'] == 0) { // 亮度开关
  149. // if (empty($version)) {
  150. // $v['content'] = '亮度'.$v['value'].'%';
  151. // }else{
  152. // $v['content'] = 'Brightness:'.$v['value'].'%';
  153. // }
  154. // }elseif ($v['actiontype'] == 1) { // 系统自适应
  155. // if (empty($version)) {
  156. // $v['content'] = '系统自适应';
  157. // }else{
  158. // $v['content'] = 'System adaptation';
  159. // }
  160. // }else{ // 策略
  161. // $v['content'] = $this->Policy_model->get_info_by_fiter(array('id'=>$v['value']));
  162. // }
  163. // }
  164. // exit(json_result('0000',$this->response['0000'],array('list'=>$data,"projectid"=>$projectid)));
  165. // }
  166. // // 分组列表
  167. // public function get_grouplist(){
  168. // $projectid = $this->input->post('projectid');
  169. // if(empty($projectid)){
  170. // exit(json_result('0007',$this->response['0007'],array()));
  171. // }
  172. // $data = $this->Group_model->get_list_by_fiter(array('projectid'=>$projectid));
  173. // exit(json_result('0000',$this->response['0000'],array('list'=>$data,"projectid"=>$projectid)));
  174. // }
  175. // // 编辑/添加分组
  176. // public function groupsave(){
  177. // // 权限控制
  178. // $role = $this->get_user_info('role');
  179. // $where['id'] = $this->input->post('id',true);
  180. // $data['projectid'] = $this->input->post('projectid',true);
  181. // $data['name'] = $this->input->post('name',true);
  182. // $data['lampid'] = $this->input->post('lampid',true);
  183. // $data['updatetime'] = date('Y-m-d H:i:s');
  184. // if (empty($data['name'])) exit(json_result('0910',$this->response['0910'],array()));
  185. // if(empty($data['projectid'])) exit(json_result('0911',$this->response['0911'],array()));
  186. // if (empty($where['id'])) {
  187. // // 添加
  188. // if ($this->Group_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']))) {
  189. // exit(json_result('0902',$this->response['0902'],array()));
  190. // }
  191. // $id = $this->Group_model->insert($data);
  192. // }else{
  193. // // 编辑
  194. // if ($this->Group_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']),$where['id'])) {
  195. // exit(json_result('0902',$this->response['0902'],array()));
  196. // }
  197. // $this->Group_model->update($where,$data);
  198. // $id = $where['id'];
  199. // }
  200. // exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
  201. // }
  202. // // 分组详情
  203. // public function groupinfo(){
  204. // $groupid = $this->input->post('groupid',true);
  205. // if (empty($groupid)) {
  206. // exit(json_result('0903',$this->response['0903'],array()));
  207. // }
  208. // $version = $this->session->userdata('version');
  209. // $data = $this->Group_model->getOne($groupid);
  210. // $userid = $this->get_user_info('id');
  211. // $role = $this->get_user_info('role');
  212. // if ($data['actiontype'] == 0) {
  213. // if (empty($version)) {
  214. // $data['groupwork'] = '亮度'.$data['value'].'%';
  215. // }else{
  216. // $data['groupwork'] = 'Brightness:'.$data['value'].'%';
  217. // }
  218. // $data['policy_list'] = $this->select_list();
  219. // }elseif ($data['actiontype'] == 1) {
  220. // if (empty($version)) {
  221. // $data['groupwork'] = '系统自适应';
  222. // }else{
  223. // $data['groupwork'] = 'System adaptation';
  224. // }
  225. // $data['policy_list'] = $this->select_list();
  226. // }else{
  227. // $res = $this->Policy_model->get_list_by_fiter(array('id'=>$data['value']),'name');
  228. // $data['groupwork'] = empty($res) ? '' : $res[0]['name'];
  229. // $data['policy_list'] = $this->select_list($data['value']);
  230. // }
  231. // $lampIds = explode(',', $data['lampid']);
  232. // $data['count'] = empty($data['lampid']) ? 0 : count($lampIds);
  233. // $field = "L.id,
  234. // L.number,
  235. // L.logtime as updatetime,
  236. // L.address,
  237. // L.lighteness as light,
  238. // L.isfaulted,
  239. // L.projectid";
  240. // $list = $this->Lamp_model->get_list_in('L.id',$lampIds,$field);
  241. // foreach ($list as &$v) {
  242. // if ($v['isfaulted'] == 0) {
  243. // if (empty($version)) {
  244. // $v['isfaulted'] = '无';
  245. // }else{
  246. // $v['isfaulted'] = 'Nothing';
  247. // }
  248. // }else{
  249. // $res = $this->Alarm_model->get_one_by_filter(array('lampid'=>$v['id'],'status'=>0),'stralarmtype');
  250. // if (empty($version)) {
  251. // $v['isfaulted'] = empty($res['stralarmtype']) ? '无' : $res['stralarmtype'];
  252. // }else{
  253. // $v['isfaulted'] = empty($res['stralarmtype']) ? 'Nothing' : alarm_translate($res['stralarmtype']);
  254. // }
  255. // }
  256. // if (!empty($v['updatetime'])) {
  257. // $timezone = $this->Project_model->get_timezone_by_projectid($v['projectid']);
  258. // $v['updatetime'] = date_change($v['updatetime'],8,$timezone['value']);
  259. // }
  260. // $v['light'] = empty($v['light']) ? 0 : $v['light'];
  261. // }
  262. // $data['list'] = $list;
  263. // exit(json_result('0000',$this->response['0000'],$data));
  264. // }
  265. // // 添加灯控页路灯列表
  266. // public function lamp_list(){
  267. // $groupid = $this->input->post('groupid',true);
  268. // $projectid = $this->input->post('projectid',true);
  269. // if (!empty($groupid)) {
  270. // $data = $this->Group_model->getOne($groupid,'lampid');
  271. // $ids = explode(',', $data['lampid']);
  272. // $selectList = $this->Lamp_model->get_list_in('L.id',$ids,"L.id,L.number");
  273. // }else{
  274. // $selectList = array();
  275. // }
  276. // $version = $this->session->userdata('version');
  277. // $field = "L.id,
  278. // L.number,
  279. // L.logtime as updatetime,
  280. // L.address,
  281. // AI.stralarmtype,
  282. // L.lighteness as light,
  283. // L.section,
  284. // N.networkname";
  285. // $list = $this->Lamp_model->get_list_in('L.projectid',array($projectid),$field);
  286. // $timezone = $this->Project_model->get_timezone_by_projectid($projectid);
  287. // // 返回数据处理
  288. // foreach ($list as &$v) {
  289. // if (empty($version)) {
  290. // $v['isfaulted'] = empty($v['stralarmtype']) ? '无' : $v['stralarmtype'];
  291. // }else{
  292. // $v['isfaulted'] = empty($v['stralarmtype']) ? 'Nothing' : alarm_translate($v['stralarmtype']);
  293. // }
  294. // $v['light'] = empty($v['light']) ? 0 : $v['light'];
  295. // $v['section'] = empty($v['section']) ? '' : $v['section'];
  296. // if (!empty($v['updatetime'])) {
  297. // $v['updatetime'] = date_change($v['updatetime'],8,$timezone['value']);
  298. // }
  299. // }
  300. // exit(json_result('0000',$this->response['0000'],array('list'=>$list,'selectList'=>$selectList)));
  301. // }
  302. // // 删除分组中的路灯
  303. // public function lampdel(){
  304. // $role = $this->get_user_info('role');
  305. // $groupid = $this->input->post('groupid',true);
  306. // $lampid = $this->input->post('lampid',true);
  307. // if (empty($groupid) || empty($lampid)) {
  308. // exit(json_result('0007',$this->response['0007'],array()));
  309. // }
  310. // $res = $this->Group_model->getOne($groupid,'lampid');
  311. // $ids = explode(',', $res['lampid']);
  312. // $lampids = explode(',', $lampid);
  313. // $temp = array();
  314. // foreach ($ids as $v) {
  315. // if (in_array($v, $lampids)) {
  316. // continue;
  317. // }
  318. // $temp[] = $v;
  319. // }
  320. // $idStr = implode(',', $temp);
  321. // $this->Group_model->update(array('id'=>$groupid),array('lampid'=>$idStr));
  322. // exit(json_result('0000',$this->response['0000'],array()));
  323. // }
  324. // 策略详情
  325. public function policy_detail(){
  326. $policyid = $this->input->post('policyid',true);
  327. if (empty($policyid)) {
  328. exit(json_result('0007',$this->response['0007'],array()));
  329. }
  330. $data = $this->Policy_model->get_detail(array('id'=>$policyid));
  331. exit(json_result('0000',$this->response['0000'],$data));
  332. }
  333. // 删除策略
  334. public function del_policy(){
  335. $policyid = $this->input->post('policyid',true);
  336. $version = $this->session->userdata('version');
  337. if (empty($policyid)) {
  338. exit(json_result('0007',$this->response['0007'],array()));
  339. }
  340. $groupData = $this->Lamp_model->update(array('policyid'=>$policyid),array('policyid'=>0));
  341. $this->Policy_model->del_by_fiter(array('policyid'=>$policyid)); // 删除策略内容
  342. $this->Policy_model->del_info_fiter(array('id'=>$policyid)); // 删除策略信息
  343. exit(json_result('0000',$this->response['0000'],array()));
  344. }
  345. // 设置策略
  346. public function set_policy(){
  347. $lampid = trim($this->input->post('lampid',true));
  348. $policyid = intval($this->input->post('policyid',true));
  349. if (empty($lampid)) exit(json_result('0007',$this->response['0007']));
  350. $lampid = implode(',', explode(',', $lampid));
  351. $lampData = $this->db->query('select id from lampinfo where id in ('.$lampid.') AND loadtype = 0')->row_array();
  352. if (!empty($lampData) && !empty($lampData['id'])) exit(json_result('0420',$this->response['0420']));
  353. $this->Lamp_model->update(array('id'=>explode(',', $lampid)),array('policyid'=>$policyid));
  354. exit(json_result('0000',$this->response['0000']));
  355. }
  356. // // 分组设置
  357. // public function groupset(){
  358. // $role = $this->get_user_info('role');
  359. // $data['actiontype'] = $this->input->post('type',true);
  360. // $data['name'] = $this->input->post('name',true);
  361. // $where['id'] = $this->input->post('groupid',true);
  362. // if (empty($where['id'])) exit(json_result('0911',$this->response['0911'],array()));
  363. // if(!isset($data['actiontype']) || !is_numeric($data['actiontype'])) exit(json_result('0912',$this->response['0912'],array()));
  364. // if (empty($data['name'])) {
  365. // exit(json_result('0910',$this->response['0910'],array()));
  366. // }
  367. // $res = $this->Group_model->getOne($where['id'],'projectid');
  368. // if ($this->Group_model->getDataCount(array('name'=>$data['name'],'projectid'=>$res['projectid']),$where['id'])) {
  369. // exit(json_result('0902',$this->response['0902'],array()));
  370. // }
  371. // $data['value'] = $this->input->post('value',true);
  372. // if ($data['actiontype'] == 2 && empty($data['value'])) {
  373. // exit(json_result('0913',$this->response['0913'],array()));
  374. // }
  375. // // 修改分组设置
  376. // $this->Group_model->update(array('id'=>$where['id']),$data);
  377. // if (isset($data['actiontype']) && $data['actiontype'] == 0) {
  378. // $cmd = '{"cmd_type":"lamp_group_cmd","cmd_id":'.$where['id'].'}';
  379. // $cmdret = send_cmd($cmd,0);
  380. // }
  381. // if (isset($data['actiontype']) && $data['actiontype'] == 2) {
  382. // $cmd = '{"cmd_type":"policy_cmd","cmd_id":'.$where['id'].'}';
  383. // $cmdret = send_cmd($cmd,0);
  384. // }
  385. // exit(json_result('0000',$this->response['0000'],array()));
  386. // }
  387. // // 删除分组
  388. // public function del_group(){
  389. // $groupid = $this->input->post('groupid',true);
  390. // if (empty($groupid)) {
  391. // exit(json_result('0914',$this->response['0914'],array()));
  392. // }
  393. // $this->Group_model->delOne($groupid);
  394. // exit(json_result('0000',$this->response['0000'],array()));
  395. // }
  396. }
  397. ?>