Group.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Map extends Base_Controller{
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. $this->load->model('Group_model');
  8. }
  9. // 分组列表
  10. public function get_list(){
  11. $userId = $this->get_user_info('id');
  12. $role = $this->get_user_info('role');
  13. $where = array();
  14. $keywords = $this->input->post('keywords',true);
  15. if ($keywords !== '' && $keywords !== null) $where['name|'] = $keywords;
  16. if ($role != SYSTEM_ADMIN) $where['userId'] = $userId;
  17. $list = $this->Group_model->get_list($where,'id,name,number,lampid,value as light,remarks');
  18. foreach ($list as $key => $value) {
  19. $list[$key]['lampCount'] = count(array_unique(explode(',', $value['lampid'])));
  20. unset($value['lampid']);
  21. $list[$key]['status'] = $value['light'] > 0 ? 1 : 0;
  22. }
  23. exit(json_result('0000',$this->response['0000'],array('list'=>$list)));
  24. }
  25. // 添加编辑分组
  26. public function save(){
  27. $userId = $this->get_user_info('id');
  28. $id = intval($this->input->post('id',true));
  29. $name = trim($this->input->post('name',true));
  30. if($name === '' || $name === null) exit(json_result('',$this->response['']));
  31. $number = $this->input->post('number',true);
  32. if($name === '' || $name === null) exit(json_result('',$this->response['']));
  33. $lampid = $this->input->post('lampId',true);
  34. $lampid = $lampid === null ? '' : $lampid;
  35. $remarks = $this->input->post('remarks',true);
  36. $remarks = $remarks === null ? '' : $remarks;
  37. if (empty($id)) { // 添加
  38. if ($this->Group_model->getDataCount(array('name'=>$name,'userId'=>$id))) {
  39. exit(json_result('',$this->response['']));
  40. }
  41. if ($this->Group_model->getDataCount(array('name'=>$name,'userId'=>$id))) {
  42. exit(json_result('',$this->response['']));
  43. }
  44. $data = array('name'=>$name,'number'=>$number,'lampid'=>$lampid,'remarks'=>$remarks);
  45. $id = $this->Group_model->add($data);
  46. }else { // 编辑
  47. if ($this->Group_model->getDataCount(array('name'=>$name,'userId'=>$id),$id)) {
  48. exit(json_result('',$this->response['']));
  49. }
  50. if ($this->Group_model->getDataCount(array('name'=>$name,'userId'=>$id),$id)) {
  51. exit(json_result('',$this->response['']));
  52. }
  53. $data = array('name'=>$name,'number'=>$number,'lampid'=>$lampid,'remarks'=>$remarks);
  54. $this->Group_model->update($data,['id'=>$id]);
  55. }
  56. exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
  57. }
  58. // 删除分组
  59. public function del(){
  60. $id = intval($this->input->post('id',true));
  61. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  62. $this->Group_model->delete(['id'=>$id]);
  63. exit(json_result('0000',$this->response['0000']));
  64. }
  65. // 分组调光
  66. public function turn_light(){
  67. $light = intval($this->input->post('light',true));
  68. $id = intval($this->input->post('id',true));
  69. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  70. $this->Group_model->update(['value'=>$light],['id'=>$id]);
  71. exit(json_result('0000',$this->response['0000']));
  72. }
  73. // 分组开关灯
  74. public function turn_on_off(){
  75. $status = intval($this->input->post('status',true));
  76. $id = intval($this->input->post('id',true));
  77. $light = $status == 0 ? 0 : 100;
  78. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  79. $this->Group_model->update(['value'=>$light],['id'=>$id]);
  80. exit(json_result('0000',$this->response['0000']));
  81. }
  82. }