VM暫存
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

Parkingquery_model.php 9.7KB

hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
hace 8 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. SUBSTR(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, in_time')
  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']['pic_name'] = $rows['pic_name'];
  73. $data['result']['in_time'] = $rows['in_time'];
  74. $data['result_code'] = 'OK';
  75. }
  76. else // 查無資料, 啟用模糊比對
  77. {
  78. /*
  79. // 讀取最近一筆入場資料
  80. $rows_cario = $this->db
  81. ->select('cario_no, in_time')
  82. ->from('cario')
  83. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'out_time IS NULL' => null))
  84. ->order_by('cario_no', 'desc')
  85. ->limit(1)
  86. ->get()
  87. ->row_array();
  88. // 有入場記錄, 直接猜在頂樓
  89. if (!empty($rows_cario['cario_no']))
  90. {
  91. $data['result']['num'] = $lpr;
  92. $data['result']['location_no'] = "7000";
  93. $data['result']['in_time'] = $rows_cario['in_time'];
  94. $data['result_code'] = 'OK';
  95. }
  96. else
  97. {
  98. $data['result']['num'] = $lpr;
  99. $data['result']['location_no'] = '0';
  100. $data['result_code'] = 'FAIL';
  101. }
  102. */
  103. $data['result']['num'] = $lpr;
  104. $data['result']['location_no'] = '0';
  105. $data['result_code'] = 'FAIL';
  106. }
  107. return $data;
  108. }
  109. /*
  110. // 空車位導引
  111. // http://203.75.167.89/parkingquery.html/get_valid_seat
  112. // 註記現在時間, 並保留10分鐘
  113. public function get_valid_seat($pksno)
  114. {
  115. $data = array();
  116. $this->db->trans_start();
  117. if ($pksno > 0) // 限制從某一個車位開始指派車位
  118. {
  119. $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;";
  120. }
  121. else
  122. {
  123. $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;";
  124. }
  125. $rows = $this->db->query($sql)->row_array();
  126. if (!empty($rows['pksno']))
  127. {
  128. $data['result']['location_no'] = "{$rows['pksno']}";
  129. $data['result_code'] = 'OK';
  130. $sql = "update pks set book_time = addtime(now(), '00:10:00') where pksno = {$rows['pksno']};";
  131. $this->db->query($sql);
  132. }
  133. else
  134. {
  135. $data['result']['location_no'] = '0';
  136. $data['result_code'] = 'FAIL';
  137. }
  138. $this->db->trans_complete();
  139. return $data;
  140. }
  141. */
  142. // 空車位導引
  143. public function get_valid_seat($pksno, $group_type=1)
  144. {
  145. $data = array();
  146. $this->db->trans_start();
  147. $sql = '';
  148. if ($pksno > 0) // 限制從某一個車位開始指派車位
  149. {
  150. // 取得指定車格座標
  151. $sql_xy = " SELECT pks.posx, pks.posy, LEFT(pks.pksno, 2) as pksno_idx
  152. FROM pks
  153. WHERE pks.pksno = {$pksno}
  154. ";
  155. $rows_xy = $this->db->query($sql_xy)->row_array();
  156. if(!empty($rows_xy['posx']) && !empty($rows_xy['posy']))
  157. {
  158. // 找最近
  159. $sql = "
  160. select pks.pksno, pks.posx, pks.posy, pks_group_member.group_id,
  161. (
  162. ABS(cast(pks.pksno as signed) - {$pksno}) +
  163. ABS(cast(pks.posx as signed) - {$rows_xy['posx']}) +
  164. ABS(cast(pks.posy as signed) - {$rows_xy['posy']}) +
  165. ABS(LEFT(pks.pksno, 2) - {$rows_xy['pksno_idx']}) * 1000
  166. ) AS v
  167. from pks
  168. left join pks_group_member on (pks_group_member.pksno = pks.pksno)
  169. left join pks_groups on (pks_groups.group_id = pks_group_member.group_id)
  170. where
  171. pks.status = 'VA' and prioritys != 0 and (pks.book_time is null or pks.book_time <= now())
  172. and pks_groups.group_type = {$group_type}
  173. order by v asc limit 1 for update;
  174. ";
  175. }
  176. }
  177. // 依順序
  178. if(empty($sql))
  179. $sql = "SELECT pks.pksno
  180. FROM pks
  181. LEFT JOIN pks_group_member ON (pks_group_member.pksno = pks.pksno)
  182. LEFT JOIN pks_groups ON (pks_groups.group_id = pks_group_member.group_id)
  183. WHERE pks.status = 'VA'
  184. AND pks.prioritys != 0
  185. AND (pks.book_time IS NULL OR pks.book_time <= now())
  186. AND pks_groups.group_type = {$group_type}
  187. ORDER BY pks.prioritys ASC LIMIT 1 FOR UPDATE;";
  188. trigger_error(__FUNCTION__ . "..sql: {$sql}..");
  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($data, true). print_r($rows, true));
  197. }
  198. else
  199. {
  200. $data['result']['location_no'] = '0';
  201. $data['result_code'] = 'FAIL';
  202. trigger_error(__FUNCTION__ . "[{$pksno}]:" . print_r($data, true));
  203. }
  204. $this->db->trans_complete();
  205. return $data;
  206. }
  207. // 緊急求救
  208. // http://203.75.167.89/parkingquery.html/send_sos/B2/111/123
  209. public function send_sos($floor, $x, $y)
  210. {
  211. $data = array
  212. (
  213. 'result' => array('send_from' => array('floor' => $floor, 'x' => $x, 'y' => $y)),
  214. 'result_code' => 'OK'
  215. );
  216. return $data;
  217. }
  218. // 防盜鎖車
  219. // http://203.75.167.89/parkingquery.html/security_action/ABC1234/pswd/2
  220. public function security_action($lpr, $pswd, $action)
  221. {
  222. $data = array();
  223. /*
  224. $rows = $this->db->select('member_no, passwd, locked')
  225. ->from('members')
  226. ->where(array('lpr' => $lpr, 'passwd' => $pswd))
  227. ->limit(1)
  228. ->get()
  229. ->row_array();
  230. trigger_error('防盜鎖車:'.$this->db->last_query());
  231. // 無資料或密碼錯誤
  232. if (empty($rows['member_no']))
  233. {
  234. $data['result_code'] = 'FAIL';
  235. return($data);
  236. }
  237. */
  238. $rows = $this->db->select('member_no, passwd, locked')
  239. ->from('members')
  240. ->where(array('lpr' => $lpr))
  241. ->limit(1)
  242. ->get()
  243. ->row_array();
  244. trigger_error('防盜鎖車:'.$this->db->last_query());
  245. // 無資料或密碼錯誤
  246. if (empty($rows['member_no']) || md5($rows['passwd']) != $pswd)
  247. {
  248. $data['result_code'] = 'FAIL';
  249. return($data);
  250. }
  251. $data['result_code'] = 'OK';
  252. // 查詢防盜狀態
  253. if ($action == 2)
  254. {
  255. $data['result']['action'] = 'CHECK_SECURITY';
  256. $data['result'][0]['num'] = $lpr;
  257. $data['result'][0]['result'] = $rows['locked'] ? 'ON' : 'OFF';
  258. return $data;
  259. }
  260. $this->db
  261. ->where('member_no', $rows['member_no'])
  262. ->update('members', array('locked' => $action));
  263. $data['result']['action'] = $action == 1 ? 'ON' : 'OFF';
  264. return $data;
  265. }
  266. }