VM暫存
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Parkingquery_model.php 9.5KB

8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /*
  3. file: Parkingquery_model.php 停車管理系統(提供資策會使用)
  4. */
  5. class Parkingquery_model extends CI_Model
  6. {
  7. function __construct()
  8. {
  9. parent::__construct();
  10. $this->load->database();
  11. }
  12. public function init($vars)
  13. {
  14. // do nothing
  15. }
  16. // 查詢各樓層剩餘車位
  17. // http://203.75.167.89/parkingquery.html/check_space/12345
  18. public function check_space($seqno, $group_type=1)
  19. {
  20. $data = array();
  21. $results = $this->db->select('group_id, availables, tot')
  22. ->from('pks_groups')
  23. ->where('group_type', $group_type)
  24. ->get()
  25. ->result_array();
  26. foreach($results as $idx => $rows)
  27. {
  28. $data['result']['floor'][$idx] = array
  29. (
  30. 'floor_name' => $rows['group_id'],
  31. 'valid_count' => $rows['availables'],
  32. 'total_count' => $rows['tot']
  33. );
  34. }
  35. return $data;
  36. }
  37. // 停車位置查詢(板橋好停車)
  38. // http://203.75.167.89/parkingquery.html/check_location/ABC1234
  39. public function check_location($lpr)
  40. {
  41. $lpr = strtoupper($lpr); // 一律轉大寫
  42. $data = array();
  43. $rows = $this->db->select('pksno, pic_name')
  44. ->from('pks')
  45. ->where('lpr', $lpr)
  46. ->limit(1)
  47. ->get()
  48. ->row_array();
  49. if (!empty($rows['pksno']))
  50. {
  51. $data['result']['num'] = $lpr;
  52. $data['result']['location_no'] = "{$rows['pksno']}";
  53. $data['result_code'] = 'OK';
  54. $data['result']['pic_name'] = $rows['pic_name'];
  55. }
  56. else // 查無資料, 啟用模糊比對
  57. {
  58. // 讀取最近一筆入場資料
  59. $rows_cario = $this->db
  60. ->select('cario_no')
  61. ->from('cario')
  62. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'out_time IS NULL' => null))
  63. ->order_by('cario_no', 'desc')
  64. ->limit(1)
  65. ->get()
  66. ->row_array();
  67. // 有入場記錄, 直接猜在頂樓
  68. if (!empty($rows_cario['cario_no']))
  69. {
  70. $data['result']['num'] = $lpr;
  71. $data['result']['location_no'] = "7000";
  72. $data['result_code'] = 'OK';
  73. }
  74. else
  75. {
  76. $data['result']['num'] = $lpr;
  77. $data['result']['location_no'] = '0';
  78. $data['result_code'] = 'FAIL';
  79. }
  80. }
  81. return $data;
  82. }
  83. /*
  84. // 空車位導引
  85. // http://203.75.167.89/parkingquery.html/get_valid_seat
  86. // 註記現在時間, 並保留10分鐘
  87. public function get_valid_seat($pksno)
  88. {
  89. $data = array();
  90. $this->db->trans_start();
  91. if ($pksno > 0) // 限制從某一個車位開始指派車位
  92. {
  93. $sql = "select pksno from pks where status = 'VA' and pksno >= {$pksno} and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  94. }
  95. else
  96. {
  97. $sql = "select pksno from pks where status = 'VA' and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  98. }
  99. $rows = $this->db->query($sql)->row_array();
  100. if (!empty($rows['pksno']))
  101. {
  102. $data['result']['location_no'] = "{$rows['pksno']}";
  103. $data['result_code'] = 'OK';
  104. $sql = "update pks set book_time = addtime(now(), '00:10:00') where pksno = {$rows['pksno']};";
  105. $this->db->query($sql);
  106. }
  107. else
  108. {
  109. $data['result']['location_no'] = '0';
  110. $data['result_code'] = 'FAIL';
  111. }
  112. $this->db->trans_complete();
  113. return $data;
  114. }
  115. */
  116. // 空車位導引
  117. public function get_valid_seat($pksno, $group_type=1)
  118. {
  119. $data = array();
  120. $this->db->trans_start();
  121. if ($pksno > 0) // 限制從某一個車位開始指派車位
  122. {
  123. //$sql = "select pksno from pks where status = 'VA' and pksno >= {$pksno} and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  124. // 2017/04/12 調整為支援找最近
  125. //$sql_xy = "select posx, posy from pks where pksno = {$pksno}";
  126. $sql_xy = "
  127. select pks.posx, pks.posy, RIGHT(pks_group_member.group_id, 1 ) as group_idx
  128. from pks
  129. left join pks_group_member on (pks_group_member.pksno = pks.pksno)
  130. left join pks_groups on (pks_groups.group_id = pks_group_member.group_id)
  131. where pks.pksno = {$pksno} AND pks_groups.group_type = {$group_type}
  132. ";
  133. $rows_xy = $this->db->query($sql_xy)->row_array();
  134. if(!empty($rows_xy['posx']) && !empty($rows_xy['posy']))
  135. {
  136. // 找最近
  137. $sql = "
  138. select pks.pksno, pks.posx, pks.posy, pks_group_member.group_id,
  139. (
  140. ABS(cast(pks.pksno as signed) - {$pksno}) +
  141. ABS(cast(pks.posx as signed) - {$rows_xy['posx']}) +
  142. ABS(cast(pks.posy as signed) - {$rows_xy['posy']}) +
  143. ABS(RIGHT(pks_group_member.group_id, 1 ) - {$rows_xy['group_idx']}) * 1000
  144. ) AS v
  145. from pks
  146. left join pks_group_member on (pks_group_member.pksno = pks.pksno)
  147. left join pks_groups on (pks_groups.group_id = pks_group_member.group_id)
  148. where
  149. pks.status = 'VA' and prioritys != 0 and (pks.book_time is null or pks.book_time <= now())
  150. and pks_groups.group_type = 1
  151. order by v asc limit 10 for update;
  152. ";
  153. /*
  154. $sql = "select pksno,
  155. ( ABS(cast(pksno as signed) - {$pksno}) + ABS(cast(posx as signed) - {$rows_xy['posx']}) + ABS(cast(posy as signed) - {$rows_xy['posy']}) ) AS v
  156. from pks where status = 'VA' and prioritys != 0 and (book_time is null or book_time <= now())
  157. order by v asc limit 1 for update;";
  158. */
  159. }
  160. else
  161. {
  162. // 依順序
  163. $sql = "select pksno from pks where status = 'VA' and pksno >= {$pksno} and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  164. }
  165. }
  166. else
  167. {
  168. $sql = "select pksno from pks where status = 'VA' and prioritys != 0 and (book_time is null or book_time <= now()) order by prioritys asc limit 1 for update;";
  169. }
  170. $rows = $this->db->query($sql)->row_array();
  171. if (!empty($rows['pksno']))
  172. {
  173. $data['result']['location_no'] = "{$rows['pksno']}";
  174. $data['result_code'] = 'OK';
  175. $sql = "update pks set book_time = addtime(now(), '00:10:00') where pksno = {$rows['pksno']};";
  176. $this->db->query($sql);
  177. trigger_error(__FUNCTION__ . "[{$pksno}]:" . print_r($rows, true));
  178. }
  179. else
  180. {
  181. $data['result']['location_no'] = '0';
  182. $data['result_code'] = 'FAIL';
  183. }
  184. $this->db->trans_complete();
  185. return $data;
  186. }
  187. // 緊急求救
  188. // http://203.75.167.89/parkingquery.html/send_sos/B2/111/123
  189. public function send_sos($floor, $x, $y)
  190. {
  191. $data = array
  192. (
  193. 'result' => array('send_from' => array('floor' => $floor, 'x' => $x, 'y' => $y)),
  194. 'result_code' => 'OK'
  195. );
  196. return $data;
  197. }
  198. // 防盜鎖車
  199. // http://203.75.167.89/parkingquery.html/security_action/ABC1234/pswd/2
  200. public function security_action($lpr, $pswd, $action)
  201. {
  202. $data = array();
  203. /*
  204. $rows = $this->db->select('member_no, passwd, locked')
  205. ->from('members')
  206. ->where(array('lpr' => $lpr, 'passwd' => $pswd))
  207. ->limit(1)
  208. ->get()
  209. ->row_array();
  210. trigger_error('防盜鎖車:'.$this->db->last_query());
  211. // 無資料或密碼錯誤
  212. if (empty($rows['member_no']))
  213. {
  214. $data['result_code'] = 'FAIL';
  215. return($data);
  216. }
  217. */
  218. $rows = $this->db->select('member_no, passwd, locked')
  219. ->from('members')
  220. ->where(array('lpr' => $lpr))
  221. ->limit(1)
  222. ->get()
  223. ->row_array();
  224. trigger_error('防盜鎖車:'.$this->db->last_query());
  225. // 無資料或密碼錯誤
  226. if (empty($rows['member_no']) || md5($rows['passwd']) != $pswd)
  227. {
  228. $data['result_code'] = 'FAIL';
  229. return($data);
  230. }
  231. $data['result_code'] = 'OK';
  232. // 查詢防盜狀態
  233. if ($action == 2)
  234. {
  235. $data['result']['action'] = 'CHECK_SECURITY';
  236. $data['result'][0]['num'] = $lpr;
  237. $data['result'][0]['result'] = $rows['locked'] ? 'ON' : 'OFF';
  238. return $data;
  239. }
  240. $this->db
  241. ->where('member_no', $rows['member_no'])
  242. ->update('members', array('locked' => $action));
  243. $data['result']['action'] = $action == 1 ? 'ON' : 'OFF';
  244. return $data;
  245. }
  246. }