Policy_model.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Policy_model extends Base_model {
  5. protected $table = 'policy_cmd';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. // 添加策略信息
  10. public function insert_policy_info($add){
  11. $this->db->insert('policy_info',$add);
  12. if ($this->db->affected_rows() == 1 ) {
  13. return $this->db->insert_id();
  14. } else {
  15. return false;
  16. }
  17. }
  18. // 通过公司来获取策略列表
  19. public function get_list_by_company($companyid,$page = null,$count = null,$keyword = ''){
  20. if (is_array($companyid)) {
  21. $sql = "select id,name from policy_info where companyid in (".implode(',', $companyid).')';
  22. }else{
  23. $sql = "select id,name from policy_info where companyid={$companyid}";
  24. }
  25. if (!empty($keyword)) {
  26. $sql .= ' AND name like %'.$keyword.'%';
  27. }
  28. $limit = '';
  29. if (!empty($page) && !empty($count)) {
  30. $limit .= " limit ".($page-1)*$count.",{$count}";
  31. }
  32. $sql .= $limit;
  33. $policy_info = $this->db->query($sql)->result_array();
  34. $version = $this->session->userdata('version');
  35. $open = empty($version) ? '开' : 'open';
  36. $close = empty($version) ? '关' : 'close';
  37. $later = empty($version) ? '以后' : 'later';
  38. foreach ($policy_info as &$d) {
  39. $policy_cmd_list = $this->db->select("*")->where('policyid',$d['id'])->order_by('id asc')->get($this->table)->result_array();
  40. $d['list'] = array();
  41. foreach ($policy_cmd_list as $policy_cmd) {
  42. $index = 1;
  43. $list = array();
  44. $list['date'] = $policy_cmd['starttime'];
  45. $list['value'] = array();
  46. while ($index <= 10) {
  47. if ($policy_cmd['value'.$index] == 1) {
  48. $status = $open;
  49. }else{
  50. $status = $close;
  51. }
  52. if ($index == 10) {
  53. $list['value'][] = $policy_cmd['time'.$index].'-'.$later.' '.$status;
  54. break;
  55. }
  56. $n = $index + 1;
  57. if (!empty($policy_cmd['time'.$n])) {
  58. $list['value'][] = $policy_cmd['time'.$index].'-'.$policy_cmd['time'.$n].' '.$status;
  59. }else{
  60. $list['value'][] = $policy_cmd['time'.$index].'-'.$later.' '.$status;
  61. break;
  62. }
  63. $index ++;
  64. }
  65. $d['list'][] = $list;
  66. }
  67. }
  68. return $policy_info;
  69. }
  70. public function get_total_by_company($companyid,$keyword = ''){
  71. if (is_array($companyid)) {
  72. $sql = "select count(*) as total from policy_info where companyid in (".implode(',', $companyid).")";
  73. }else{
  74. $sql = "select count(*) as total from policy_info where companyid={$companyid}";
  75. }
  76. if (!empty($keyword)) {
  77. $sql .= ' AND name like %'.$keyword.'%';
  78. }
  79. $data = $this->db->query($sql)->row_array();
  80. return $data['total'];
  81. }
  82. // 通过条件来获取策略信息
  83. public function get_info_by_fiter($fiter = array()){
  84. if (empty($fiter)) {
  85. return array();
  86. }
  87. foreach ($fiter as $key => $value) {
  88. $this->db->where($key,$value);
  89. }
  90. $policy_info = $this->db->get('policy_info')->row_array();
  91. $policy_cmd_list = $this->db->select("*")->where('policyid',$policy_info['id'])->get($this->table)->result_array();
  92. $res = array();
  93. foreach ($policy_cmd_list as $k=>$v) {
  94. $date = explode('-', $v['date']);
  95. $temp = date('Y/m/d',strtotime($date[0])).'-'.date('Y/m/d',strtotime($date[1]));
  96. $time= date('G点',strtotime($date[0])).'-'.date('G点',strtotime($date[1]));
  97. $res[$temp][$k]['time'] = $time;
  98. $res[$temp][$k]['value'] = $v['light'];
  99. }
  100. if (empty($res)) {
  101. $policy_info['list'] = array();
  102. }else{
  103. foreach ($res as $key => $value) {
  104. $str = array();
  105. foreach ($value as $k => $v) {
  106. $str[] = $v['time']." 亮度".$v['value'].'%';
  107. }
  108. $policy_info['list'][] = array('date'=>$key,'value'=>$str);
  109. }
  110. }
  111. return $policy_info;
  112. }
  113. // 获取策略详情
  114. public function get_detail($filter=array()){
  115. if (empty($filter)) {
  116. return array();
  117. }
  118. foreach ($filter as $key => $value) {
  119. $this->db->where($key,$value);
  120. }
  121. $data = $this->db->get('policy_info')->row_array();
  122. $data['list'] = array();
  123. $list = $this->db->where('policyid',$data['id'])->order_by('id asc')->get($this->table)->result_array();
  124. foreach ($list as $s) {
  125. $temp = array();
  126. $temp['date'] = $s['starttime'];
  127. $temp['timeList'] = array();
  128. $index = 1;
  129. while ($index <= 10) {
  130. if (!empty($s['time'.$index])) {
  131. $temp['timeList'][] = array(
  132. 'time' => date('Y-m-d H:i:s',strtotime($s['starttime'].' '.$s['time'.$index])),
  133. 'value' => $s['value'.$index],
  134. );
  135. }else{
  136. break;
  137. }
  138. $index ++;
  139. }
  140. $data['list'][] = $temp;
  141. }
  142. return $data;
  143. }
  144. // 通过筛选条件来获取策略列表
  145. public function get_list_by_fiter($filter=array(),$field='*'){
  146. $this->db->select($field);
  147. foreach ($filter as $key => $value) {
  148. if (is_array($value)) {
  149. if (!empty($value)) {
  150. $this->db->where_in($key,$value);
  151. }
  152. }else{
  153. $this->db->where($key,$value);
  154. }
  155. }
  156. $data = $this->db->get('policy_info')->result_array();
  157. return $data;
  158. }
  159. // 编辑策略信息
  160. public function update_policy_info($filter,$data){
  161. if (empty($filter)){
  162. return false;
  163. }
  164. foreach ($filter as $k => $v) {
  165. if (is_array($v)) {
  166. $this->db->where_in($k, $v);
  167. } else {
  168. $this->db->where($k,$v);
  169. }
  170. }
  171. return $this->db->update('policy_info',$data);
  172. }
  173. // 删除策略内容
  174. public function del_by_fiter($fiter=array()) {
  175. if (empty($fiter)) {
  176. return;
  177. }
  178. foreach ($fiter as $key => $value) {
  179. if (is_array($value)) {
  180. if (!empty($value)) {
  181. $this->db->where_in($key,$value);
  182. }else{
  183. $this->db->where_in($key,array(0));
  184. }
  185. }else{
  186. $this->db->where($key,$value);
  187. }
  188. }
  189. $this->db->delete($this->table);
  190. if ($this->db->affected_rows() > 0) {
  191. return true;
  192. } else {
  193. return false;
  194. }
  195. }
  196. // 删除策略信息
  197. public function del_info_fiter($fiter=array()) {
  198. if (empty($fiter)) {
  199. return;
  200. }
  201. foreach ($fiter as $key => $value) {
  202. if (is_array($value)) {
  203. if (!empty($value)) {
  204. $this->db->where_in($key,$value);
  205. }else{
  206. $this->db->where_in($key,array(0));
  207. }
  208. }else{
  209. $this->db->where($key,$value);
  210. }
  211. }
  212. $this->db->delete('policy_info');
  213. if ($this->db->affected_rows() > 0) {
  214. return true;
  215. } else {
  216. return false;
  217. }
  218. }
  219. public function getDataCount($condition, $id = 0) {
  220. if (!empty($condition)){
  221. foreach ($condition as $k => $v) {
  222. $this->db->where($k,$v);
  223. }
  224. }
  225. if (!empty($id)) {
  226. $this->db->where('id !=',$id);
  227. }
  228. $query = $this->db->get('policy_info');
  229. $data = $query->row_array();
  230. if (empty($data)) {
  231. return 0;
  232. } else {
  233. return $id == $data['id'] ? 0 : 1;
  234. }
  235. }
  236. }
  237. ?>