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.

598 lines
22KB

  1. <?php
  2. /*
  3. file: carpark_model.php 停車管理系統
  4. */
  5. class Carpark_model extends CI_Model
  6. {
  7. var $vars = array();
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->database();
  12. }
  13. public function init($vars)
  14. {
  15. $this->vars = $vars;
  16. }
  17. // 月租會員加入
  18. public function member_add($data)
  19. {
  20. // 會員車輛基本資料檔
  21. // $data['start_date'] = "{$data['start_date']} 00:00:00";
  22. // $data['end_date'] = "{$data['end_date']} 23:59:59";
  23. $old_lpr = $data['old_lpr'];
  24. unset($data['old_lpr']);
  25. $data_car = array
  26. (
  27. 'lpr' => $data['lpr'],
  28. 'lpr_correct' => $data['lpr'],
  29. 'etag' => $data['etag'],
  30. 'station_no' => $data['station_no'],
  31. 'start_time' => $data['start_date'],
  32. 'end_time' => $data['end_date']
  33. );
  34. $check_member_no = $data['member_no'];
  35. unset($data['member_no']);
  36. trigger_error("members:".print_r($data, true)."car:".print_r($data_car, true));
  37. if ($check_member_no == 0) // 新增一筆會員資料
  38. {
  39. $this->db->insert('members', $data);
  40. $data_car['member_no'] = $this->db->insert_id();
  41. $this->db->insert('member_car', $data_car);
  42. }
  43. else // update會員資料
  44. {
  45. $this->db->update('members', $data, array('member_no' => $check_member_no));
  46. if ($old_lpr == $data['lpr']) // 沒有異動到車牌, 使用update, 否則重建一筆
  47. {
  48. unset($data_car['lpr']);
  49. unset($data_car['lpr_correct']);
  50. $this->db->update('member_car', $data_car, array('member_no' => $check_member_no));
  51. }
  52. else
  53. {
  54. $this->db->delete('member_car', array('member_no' => $check_member_no));
  55. $data_car['member_no'] = $check_member_no;
  56. $this->db->insert('member_car', $data_car);
  57. }
  58. }
  59. return true;
  60. }
  61. // 查詢車牌是否重複
  62. public function check_lpr($lpr)
  63. {
  64. $rows = $this->db->select('count(*) as counts')
  65. ->from('members')
  66. ->where(array('lpr' => $lpr))
  67. ->get()
  68. ->row_array();
  69. return $rows['counts'];
  70. }
  71. // 月租會員查詢
  72. public function member_query()
  73. {
  74. $results = $this->db->select('member_no, lpr, etag, member_name, mobile_no, start_date, end_date, update_time,
  75. contract_no, amt, member_id, tel_h, tel_o, addr, valid_time, station_no')
  76. ->from('members')
  77. ->order_by('station_no, lpr', 'asc')
  78. ->get()
  79. ->result_array();
  80. return $results;
  81. }
  82. // 刪除月租會員
  83. public function member_delete($member_no)
  84. {
  85. $this->db->delete('members', array('member_no' => $member_no));
  86. $this->db->delete('member_car', array('member_no' => $member_no));
  87. return true;
  88. }
  89. // 進出場現況表
  90. public function cario_list()
  91. {
  92. /*
  93. $data_cario = $this->db
  94. ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner')
  95. ->from('cario c')
  96. ->join('members m', 'c.member_no = m.member_no', 'left')
  97. ->where(array('c.err' => 0))
  98. ->order_by('c.update_time', 'desc')
  99. ->limit(10)
  100. ->get()
  101. ->result_array();
  102. */
  103. $sql = '(select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.in_time as time_order
  104. from cario c left join members m on c.obj_id = m.lpr
  105. where c.err = 0 and c.out_time is null)
  106. union
  107. (select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.out_time as time_order
  108. from cario c left join members m on c.obj_id = m.lpr
  109. where c.err = 0 and c.out_time is not null)
  110. order by time_order desc limit 10;';
  111. $data_cario = $this->db->query($sql)->result_array();
  112. // $lane_arr = array(0 => '入1', 1 => '入2', 3 => '出3', 3 => '出4');
  113. $idx = 0;
  114. foreach($data_cario as $rows)
  115. {
  116. ++$rows['in_lane'];
  117. ++$rows['out_lane'];
  118. //$lane_no = $rows['in_out'] == 'CI' ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}";
  119. $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; // 2016/08/22 有離場時間就顯示
  120. $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']);
  121. $arr = explode('-', $pic_name);
  122. $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name;
  123. $data[$idx] = array
  124. (
  125. // 'io_name' => $io_name[$rows['in_out']],
  126. 'io_name' => $lane_no,
  127. 'lpr' => $rows['lpr'],
  128. // 'etag' => $rows['etag'],
  129. 'etag' => $rows['etag'],
  130. 'owner' => $rows['owner'],
  131. 'io_time' => empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)<br>{$rows['out_time']}(出)<br>{$rows['minutes']}分(停留時間)",
  132. 'pic_name' => $pic_path
  133. );
  134. ++$idx;
  135. }
  136. return $data;
  137. }
  138. // 模糊比對
  139. function getLevenshteinSQLStatement($word, $target)
  140. {
  141. $words = array();
  142. if(strlen($word) >= 5)
  143. {
  144. for ($i = 0; $i < strlen($word); $i++) {
  145. // insertions
  146. $words[] = substr($word, 0, $i) . '_' . substr($word, $i);
  147. // deletions
  148. $words[] = substr($word, 0, $i) . substr($word, $i + 1);
  149. // substitutions
  150. //$words[] = substr($word, 0, $i) . '_' . substr($word, $i + 1);
  151. }
  152. }
  153. else
  154. {
  155. for ($i = 0; $i < strlen($word); $i++) {
  156. // insertions
  157. $words[] = substr($word, 0, $i) . '_' . substr($word, $i);
  158. }
  159. }
  160. // last insertion
  161. $words[] = $word . '_';
  162. //return $words;
  163. $fuzzy_statement = ' (';
  164. foreach ($words as $idx => $word)
  165. {
  166. $fuzzy_statement .= " {$target} LIKE '%{$word}%' OR ";
  167. }
  168. $last_or_pos = strrpos($fuzzy_statement, 'OR');
  169. if($last_or_pos !== false)
  170. {
  171. $fuzzy_statement = substr_replace($fuzzy_statement, ')', $last_or_pos, strlen('OR'));
  172. }
  173. return $fuzzy_statement;
  174. }
  175. // 車號入場查詢
  176. public function carin_lpr_query($word)
  177. {
  178. // updated 2016/09/09 fuzzy search
  179. if(empty($word) || strlen($word) < 4 || strlen($word) > 10)
  180. {
  181. return array();
  182. }
  183. $fuzzy_statement = $this->getLevenshteinSQLStatement($word, 'c.obj_id');
  184. trigger_error("模糊比對 {$word} where: {$fuzzy_statement}");
  185. $sql = "SELECT c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner
  186. FROM cario c
  187. LEFT JOIN members m ON c.obj_id = m.lpr
  188. WHERE {$fuzzy_statement} AND c.err = 0 AND c.obj_type = 1
  189. ORDER BY c.update_time DESC
  190. LIMIT 50";
  191. $data_cario = $this->db->query($sql)->result_array();
  192. /*
  193. $data_cario = $this->db
  194. ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner')
  195. ->from('cario c')
  196. ->join('members m', 'c.member_no = m.member_no', 'left')
  197. ->where(array('c.obj_type' => 1, 'obj_id' => $lpr))
  198. ->order_by('c.update_time', 'desc')
  199. ->get()
  200. ->result_array();
  201. */
  202. $data = array();
  203. $idx = 0;
  204. foreach($data_cario as $rows)
  205. {
  206. ++$rows['in_lane'];
  207. ++$rows['out_lane'];
  208. $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}";
  209. $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)<br>{$rows['out_time']}(出)<br>{$rows['minutes']}分(停留時間)";
  210. $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']);
  211. $arr = explode('-', $pic_name);
  212. $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name;
  213. $data[$idx++] = array
  214. (
  215. 'io_name' => $lane_no,
  216. 'lpr' => $rows['lpr'],
  217. 'etag' => $rows['etag'],
  218. 'owner' => empty($rows['owner']) ? '' : $rows['owner'],
  219. 'io_time' => $io_time,
  220. 'pic_name' => $pic_path
  221. );
  222. }
  223. return $data;
  224. }
  225. // 車號入場查詢
  226. public function carin_time_query($time_query, $minutes_range)
  227. {
  228. $curr_time = date('Y-m-d H:i:s');
  229. $start_time = date('Y-m-d H:i:s', strtotime("{$time_query} - {$minutes_range} minutes"));
  230. $end_time = date('Y-m-d H:i:s', strtotime("{$time_query} + {$minutes_range} minutes"));
  231. $data_cario = $this->db
  232. ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner')
  233. ->from('cario c')
  234. ->join('members m', 'c.obj_id = m.lpr', 'left')
  235. ->where(array('c.obj_type' => 1, 'c.in_time >=' => $start_time, 'c.in_time <=' => $end_time))
  236. ->order_by('c.update_time', 'desc')
  237. ->get()
  238. ->result_array();
  239. $data = array();
  240. $idx = 0;
  241. foreach($data_cario as $rows)
  242. {
  243. ++$rows['in_lane'];
  244. ++$rows['out_lane'];
  245. $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}";
  246. $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)<br>{$rows['out_time']}(出)<br>{$rows['minutes']}分(停留時間)";
  247. $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']);
  248. $arr = explode('-', $pic_name);
  249. $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name;
  250. $data[$idx++] = array
  251. (
  252. 'io_name' => $lane_no,
  253. 'lpr' => $rows['lpr'],
  254. 'etag' => $rows['etag'],
  255. 'owner' => empty($rows['owner']) ? '' : $rows['owner'],
  256. 'io_time' => $io_time,
  257. 'pic_name' => $pic_path
  258. );
  259. }
  260. return $data;
  261. }
  262. // 時間長度轉成日時分秒
  263. public function time2str($d1, $d2)
  264. {
  265. $time = strtotime($d2) - strtotime($d1);
  266. $day_str = floor($time/3600/24);
  267. $day_str = $day_str ? $day_str .= '天 ' : '';
  268. $hour_str = floor($time%(24*3600)/3600);
  269. $hour_str = $hour_str ? $hour_str .= '小時 ' : '';
  270. $minute_str = floor($time%3600/60);
  271. $minute_str = $minute_str ? $minute_str .= '分' : '';
  272. /*
  273. $second_str = $time%3600%60;
  274. $second_str = $second_str ? $second_str .= ' seconds ' : '';
  275. $n_time = floor($time/3600/24)."days".floor($time%(24*3600)/3600)."Hour".floor($time%3600/60)."Minute".($time%3600%60)."Second";
  276. */
  277. $n_time = $day_str . $hour_str . $minute_str;
  278. return $n_time;
  279. }
  280. // 在席車位檢查未有入場資料清單
  281. public function pks_check_list($max_rows)
  282. {
  283. $data = array();
  284. $data = $this->db
  285. ->select('pksno, lpr, in_time, pic_name')
  286. ->from('pks')
  287. ->where(array('status' => 'LR', 'cario_no' => 0, 'confirms' => 0, 'station_no' => STATION_NO))
  288. ->order_by('in_time', 'desc')
  289. ->limit($max_rows)
  290. ->get()
  291. ->result_array();
  292. return $data;
  293. }
  294. // 重設在席查核
  295. public function reset_pks_check()
  296. {
  297. // 讀出未查核過的資料
  298. $data_pks = $this->db
  299. ->select('pksno, lpr, in_time')
  300. ->from('pks')
  301. ->where(array('status' => 'LR', 'cario_no' => 0, 'station_no' => STATION_NO))
  302. ->get()
  303. ->result_array();
  304. // $tot = $this->db->num_rows(); // 總筆數
  305. $tot = count($data_pks); // 總筆數
  306. $num_cario = 0; // 入場資料筆數
  307. foreach($data_pks as $rows)
  308. {
  309. $lpr = $rows['lpr'];
  310. if ($lpr == 'NONE') continue; // 車辨失敗者不處理
  311. $pksno = $rows['pksno'];
  312. $pks_in_time = $rows['in_time'];
  313. // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23)
  314. $rows_cario = $this->db
  315. ->select('cario_no, in_time')
  316. ->from('cario')
  317. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO))
  318. ->order_by('cario_no', 'desc')
  319. ->limit(1)
  320. ->get()
  321. ->row_array();
  322. // if ($this->db->num_rows() == 1) // 有入場資料
  323. if (!empty($rows_cario['cario_no'])) // 有入cario_no場資料
  324. {
  325. $cario_no = $rows_cario['cario_no']; // 入場序號
  326. $in_time = $rows_cario['in_time'];
  327. // 在席與入場資料相符, 分別在cario與pks記錄之
  328. $data = array
  329. (
  330. 'pksno' => $pksno,
  331. 'pks_time' => $pks_in_time
  332. );
  333. $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO));
  334. $data = array
  335. (
  336. 'cario_no' => $cario_no,
  337. 'in_time' => $in_time
  338. );
  339. // 車號及照片檔名填入資料庫內
  340. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  341. ++$num_cario;
  342. }
  343. }
  344. return array('tot' => $tot, 'tot_correct' => $num_cario);
  345. }
  346. // 更正在席車號
  347. public function correct_pks_lpr($pksno, $lpr)
  348. {
  349. // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23)
  350. $rows_cario = $this->db
  351. ->select('cario_no, in_time')
  352. ->from('cario')
  353. ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO))
  354. ->order_by('cario_no', 'desc')
  355. ->limit(1)
  356. ->get()
  357. ->row_array();
  358. if (!empty($rows_cario['cario_no'])) // 有cario_no入場資料
  359. {
  360. $cario_no = $rows_cario['cario_no']; // 入場序號
  361. $in_time = $rows_cario['in_time'];
  362. // 在席與入場資料相符, 分別在cario與pks記錄之
  363. $data = array
  364. (
  365. 'pksno' => $pksno,
  366. 'pks_time' => $in_time
  367. );
  368. $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO));
  369. $data = array
  370. (
  371. 'confirms' => 1,
  372. 'cario_no' => $cario_no,
  373. 'lpr' => $lpr,
  374. 'in_time' => $in_time
  375. );
  376. // 車號及照片檔名填入資料庫內
  377. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  378. $results = array
  379. (
  380. 'err' => 0,
  381. 'cario_no' => $cario_no
  382. );
  383. }
  384. else // 無入場資料
  385. {
  386. $data = array
  387. (
  388. 'confirms' => 1,
  389. 'lpr' => $lpr
  390. );
  391. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  392. $results = array
  393. (
  394. 'err' => 0,
  395. 'cario_no' => 0 // 車號查無入場資料
  396. );
  397. }
  398. return $results;
  399. }
  400. // 入場車號查核在席無資料清單
  401. public function carin_check_list($max_rows)
  402. {
  403. $data = array();
  404. $rows_cario = $this->db
  405. ->select('cario_no, obj_id as lpr, in_time, member_no, in_pic_name')
  406. ->from('cario')
  407. ->where('in_out', 'CI')
  408. ->where(array('pksno' => 0, 'finished' => 0, 'err' => 0, 'confirms' => 0, 'station_no' => STATION_NO, 'in_time <=' => 'date_sub(now(), interval 20 minute)'), null, false)
  409. ->order_by('cario_no', 'desc')
  410. ->limit($max_rows)
  411. ->get()
  412. ->result_array();
  413. $idx = 0;
  414. foreach($rows_cario as $rows)
  415. {
  416. $data[$idx++] = array
  417. (
  418. 'cario_no' => $rows['cario_no'],
  419. 'lpr' => $rows['lpr'],
  420. 'in_time' => $rows['in_time'],
  421. 'type' => $rows['member_no'] == 0 ? '' : '月租',
  422. 'pic_name' => str_replace('-', '', substr($rows['in_time'], 0, 10)) . '/' . $rows['in_pic_name']
  423. );
  424. }
  425. return $data;
  426. }
  427. // 更正入場車號
  428. public function correct_carin_lpr($cario_no, $lpr, $in_time)
  429. {
  430. $rows = $this->db
  431. ->select('pksno, cario_no, in_time')
  432. ->from('pks')
  433. ->where(array('status' => 'LR', 'lpr' => $lpr, 'confirms' => 0, 'station_no' => STATION_NO))
  434. ->limit(1)
  435. ->get()
  436. ->row_array();
  437. // 如果在席資料相符
  438. if (!empty($rows['pksno']))
  439. {
  440. $pksno = $rows['pksno'];
  441. $data = array
  442. (
  443. 'cario_no' => $cario_no,
  444. 'in_time' => $in_time
  445. );
  446. $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO));
  447. $data_cario = array
  448. (
  449. 'obj_id' => $lpr,
  450. 'pksno' => $pksno,
  451. 'pks_time' => $in_time,
  452. 'confirms' => 1
  453. );
  454. }
  455. else // 無在席資料
  456. {
  457. $data_cario = array
  458. (
  459. 'obj_id' => $lpr,
  460. 'pksno' => 0,
  461. 'confirms' => 1
  462. );
  463. }
  464. $this->db->update('cario', $data_cario, array('cario_no' => $cario_no, 'station_no' => STATION_NO));
  465. return(array('pksno' => $data_cario['pksno']));
  466. }
  467. // 查詢行動支付記錄
  468. public function tx_bill_query()
  469. {
  470. $sql = "SELECT
  471. tx_bill.order_no as order_no, tx_bill.lpr as lpr, tx_bill.invoice_no as invoice_no, tx_bill.in_time as in_time, tx_bill.balance_time as balance_time, tx_bill.company_no as company_no, tx_bill.email as email, tx_bill.mobile as mobile, tx_bill.amt as amt, tx_bill.tx_time as tx_time,
  472. allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type,
  473. cario.out_before_time as out_before_time
  474. FROM tx_bill
  475. LEFT JOIN cario ON tx_bill.cario_no = cario.cario_no
  476. LEFT JOIN allpay_feedback_log ON tx_bill.order_no = allpay_feedback_log.merchant_trade_no
  477. WHERE tx_bill.status = 111
  478. ORDER BY tx_bill.tx_time DESC";
  479. return $this->db->query($sql)->result_array();
  480. }
  481. // 查詢月租繳款機記錄
  482. public function tx_bill_ats_query()
  483. {
  484. $sql = "SELECT
  485. tx_bill_ats.order_no as order_no, tx_bill_ats.lpr as lpr, tx_bill_ats.invoice_no as invoice_no,
  486. tx_bill_ats.end_time as end_time, tx_bill_ats.next_start_time as next_start_time, tx_bill_ats.next_end_time as next_end_time,
  487. tx_bill_ats.company_no as company_no, tx_bill_ats.email as email, tx_bill_ats.mobile as mobile, tx_bill_ats.amt as amt,
  488. tx_bill_ats.remarks as remarks, tx_bill_ats.member_name as member_name, tx_bill_ats.tx_time as tx_time,
  489. allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type
  490. FROM tx_bill_ats
  491. LEFT JOIN allpay_feedback_log ON tx_bill_ats.order_no = allpay_feedback_log.merchant_trade_no
  492. WHERE tx_bill_ats.status = 111
  493. ORDER BY tx_bill_ats.tx_time DESC";
  494. return $this->db->query($sql)->result_array();
  495. }
  496. // 查詢樓層在席群組
  497. public function pks_group_query()
  498. {
  499. $sql = "SELECT
  500. pks_groups.station_no, pks_groups.group_name as group_name, pks_groups.tot as tot, pks_groups.parked as parked, pks_groups.availables as availables, pks_groups.group_id as group_id, pks_groups.renum as renum
  501. FROM pks_groups
  502. ORDER BY pks_groups.group_name * 1 DESC";
  503. return $this->db->query($sql)->result_array();
  504. }
  505. // 送出至message queue(目前用mqtt)
  506. public function mq_send($topic, $msg)
  507. {
  508. $this->vars['mqtt']->publish($topic, $msg, 0);
  509. trigger_error("mqtt:{$topic}|{$msg}");
  510. }
  511. }