VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Parkingquery_model.php 10KB

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