statistics.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. const emptyStr = `<tr>
  2. <td><span>조회 된 항목이 없습니다.</span></td>
  3. </tr>`;
  4. $(()=>{
  5. init();
  6. })
  7. function init () {
  8. const $year = $('.year');
  9. const $month = $('.month');
  10. const $date = $('.date');
  11. const $type = $('.type');
  12. const $amBtn = $('.button.amount');
  13. const $comBtn = $('.button.comm');
  14. const $conBtn = $('.button.congest');
  15. const $toggle = $('.table-toggle');
  16. const now = new Date();
  17. const nowYear = now.getFullYear();
  18. const nowMonth = now.getMonth() + 1;
  19. const nowDate = now.getDate();
  20. $amBtn.on('click', () => searchStatisticsAmount());
  21. $comBtn.on('click', ()=> searchStatisticsComm());
  22. $conBtn.on('click', ()=> searchStatisticsCongest());
  23. $type.on('change', function(){
  24. const isDisabled = !($(this).val() === 'dd');
  25. $date.attr('disabled', isDisabled);
  26. const opacity = isDisabled ? 0 : 1;
  27. $date.css('opacity', opacity);
  28. })
  29. if ($year[0]) {
  30. let options = getDateOptions(2022, nowYear, '년');
  31. $year.html(options);
  32. $year.val(nowYear);
  33. $year.on('change', function() {
  34. const value = $(this).val();
  35. let lastMonth = 12;
  36. if (Number(value) === nowYear) {
  37. lastMonth = nowMonth;
  38. }
  39. if ($month[0]) {
  40. const monthOptions = getDateOptions(1, lastMonth, '월');
  41. const monthVal = $month.val();
  42. $month.html(monthOptions);
  43. if (Number(monthVal) > lastMonth) {
  44. $month.val(1);
  45. if ($date[0]) {
  46. $date.val(1);
  47. }
  48. }
  49. else {
  50. $month.val(monthVal);
  51. }
  52. if (Number(monthVal) === nowMonth) {
  53. const dateOptions = getDateOptions(1, nowDate, '일');
  54. $date.html(dateOptions);
  55. const dateVal = $date.val();
  56. if (Number(dateVal) > nowDate) {
  57. $date.val(1);
  58. }
  59. }
  60. }
  61. })
  62. }
  63. if ($month[0]) {
  64. let options = getDateOptions(1, nowMonth, '월');
  65. $month.html(options);
  66. $month.val(nowMonth);
  67. $month.on('change', function() {
  68. const yearVal = $year.val();
  69. const monthVal = $(this).val();
  70. if ($date[0]) {
  71. const dateVal = $date.val();
  72. let lastDate = new Date(yearVal, monthVal, 0).getDate();
  73. if (Number(yearVal) === nowYear && Number(monthVal) === nowMonth) {
  74. lastDate = nowDate;
  75. }
  76. const dateOptions = getDateOptions(1, lastDate, '일');
  77. $date.html(dateOptions);
  78. if (Number(dateVal) > nowDate) {
  79. $date.val(1);
  80. }
  81. else {
  82. $date.val(dateVal);
  83. }
  84. }
  85. })
  86. }
  87. if ($date[0]) {
  88. let options = getDateOptions(1, nowDate, '일');
  89. $date.html(options);
  90. $date.val(nowDate);
  91. }
  92. if ($toggle[0]) {
  93. $toggle.children().on('click', function() {
  94. const isActive = $(this).hasClass("active");
  95. if (isActive) return;
  96. $('.table-toggle .active').removeClass("active");
  97. $(this).addClass("active");
  98. })
  99. }
  100. }
  101. /**
  102. * Select 날짜 옵션값 생성
  103. * @param start 시작값
  104. * @param limit 종료값
  105. * @param word 구분 글자
  106. * @returns {string} option 문자
  107. */
  108. function getDateOptions(start, limit, word) {
  109. if (isNaN(start) || isNaN(limit) || !word) {
  110. return;
  111. }
  112. else if (start > limit) {
  113. return;
  114. }
  115. let options = '';
  116. for (let ii = start; ii <= limit; ii++) {
  117. options += `<option value="${ii}">${ii} ${word}</option>`
  118. }
  119. return options;
  120. }
  121. /**
  122. * 교통량 통계 조회
  123. */
  124. function searchStatisticsAmount() {
  125. const $roadNmbr = $('.road_nmbr');
  126. const $type = $('.type');
  127. const $year = $('.year');
  128. const $month = $('.month');
  129. const $date = $('.date');
  130. const $thead = $('.thead');
  131. const $tbody = $('.table-content');
  132. const $table = $('.table-box table')
  133. const type = $type.val();
  134. const roadNmbr = $roadNmbr.val();
  135. const year = Number($year.val());
  136. const month = Number($month.val());
  137. const date = Number($date.val());
  138. let fromDt = "";
  139. let toDt = "";
  140. let limit = 23;
  141. let start = 0;
  142. const dateValue = getSearchDateValue(year, month, date);
  143. fromDt = dateValue + '000000';
  144. toDt = dateValue + '235959';
  145. if (type === 'mn') {
  146. fromDt = getSearchDateValue(year, month, 1) + '000000';
  147. toDt = getSearchDateValue(year, month, new Date(year, month, 0).getDate()) + '235959';
  148. limit = new Date(year, month, 0).getDate();
  149. start = 1;
  150. }
  151. const param = {
  152. roadNmbr : roadNmbr,
  153. fromDt : fromDt,
  154. toDt : toDt,
  155. }
  156. // let height = 'calc(-400px + 100vh)';
  157. // $table.css('height', height);
  158. $tbody.html('<tr><td><span><img src="/images/icon/loading.gif"></span></td></tr>');
  159. getDataAsync('/api/statistics/amount/' + type, "POST", param, null, (jsonData)=>{
  160. let str = emptyStr;
  161. let thead = "";
  162. if (jsonData && jsonData.length > 0) {
  163. str = "";
  164. jsonData.forEach((obj)=>{
  165. const { strt_name, end_name } = obj;
  166. str +=`<tr>
  167. <td>${strt_name} -> ${end_name}</td>`
  168. for (let ii = start; ii <= limit; ii++) {
  169. let value = ii;
  170. if (value < 10) value = "0" + ii;
  171. value = obj['t' + value];
  172. if (value === '0' || !value) value = '-';
  173. str += `<td>${value}</td>`
  174. }
  175. str += `</tr>`
  176. });
  177. // height = 'auto';
  178. // $table.css('height', height);
  179. }
  180. if (limit) {
  181. thead = '<th>구간</th>';
  182. for (let ii = start; ii <= limit; ii++) {
  183. thead += '<th>'+ii+'</th>';
  184. }
  185. }
  186. $thead.html(thead);
  187. $tbody.html(str);
  188. }, null);
  189. }
  190. /**
  191. * 소통 통계 조회
  192. */
  193. function searchStatisticsComm() {
  194. const $atrdNm = $('.atrd_nm');
  195. const $type = $('.type');
  196. const $year = $('.year');
  197. const $month = $('.month');
  198. const $date = $('.date');
  199. const $thead = $('.thead');
  200. const $tbody = $('.table-content');
  201. const $table = $('.table-box table');
  202. const $active = $('.table-toggle .active');
  203. const active = $active.text();
  204. const type = $type.val();
  205. const atrdNm = $atrdNm.val();
  206. const year = Number($year.val());
  207. const month = Number($month.val());
  208. const date = Number($date.val());
  209. let fromDt = "";
  210. let toDt = "";
  211. let limit = 23;
  212. let start = 0;
  213. const dateValue = getSearchDateValue(year, month, date);
  214. fromDt = dateValue + '000000';
  215. toDt = dateValue + '235959';
  216. if (type === 'mn') {
  217. fromDt = getSearchDateValue(year, month, 1) + '000000';
  218. toDt = getSearchDateValue(year, month, new Date(year, month, 0).getDate()) + '235959';
  219. limit = new Date(year, month, 0).getDate();
  220. start = 1;
  221. }
  222. const param = {
  223. atrdNm : atrdNm,
  224. fromDt : fromDt,
  225. toDt : toDt,
  226. }
  227. // let height = 'calc(-443px + 100vh)';
  228. let url = "/speed";
  229. // $table.css('height', height);
  230. $tbody.html('<tr><td><span><img src="/images/icon/loading.gif"></span></td></tr>');
  231. if (active === '지/정체통계') url = '/grade';
  232. let thead = "";
  233. if (limit) {
  234. thead = '<th>구간</th>';
  235. for (let ii = start; ii <= limit; ii++) {
  236. thead += '<th>'+ii+'</th>';
  237. }
  238. }
  239. getDataAsync('/api/statistics' + url + '/' + type, "POST", param, null, (jsonData)=>{
  240. let str = emptyStr;
  241. if (jsonData && jsonData.length > 0) {
  242. str = "";
  243. jsonData.forEach((obj)=>{
  244. const { strt_name, end_name } = obj;
  245. str +=`<tr>
  246. <td>${strt_name} -> ${end_name}</td>`
  247. for (let ii = start; ii <= limit; ii++) {
  248. let value = ii;
  249. if (value < 10) value = "0" + ii;
  250. value = obj['t' + value];
  251. if (url === '/grade') {
  252. let text = "●";
  253. if (!value) {
  254. text = "○";
  255. }
  256. str += '<td class="LTC'+ value+'">' + text + '</td>'
  257. }
  258. else {
  259. if (value === '0' || !value) value = '-';
  260. str += `<td>${value}</td>`
  261. }
  262. }
  263. str += `</tr>`
  264. });
  265. // height = 'auto';
  266. // $table.css('height', height);
  267. }
  268. $thead.html(thead);
  269. $tbody.html(str);
  270. }, ()=>{
  271. $thead.html(thead);
  272. $tbody.html(emptyStr);
  273. });
  274. }
  275. /**
  276. * 정체 통계 조회
  277. */
  278. function searchStatisticsCongest() {
  279. const $type = $('.type');
  280. const $day = $('.day');
  281. const $thead = $('.thead');
  282. const $tbody = $('.table-content');
  283. const $table = $('.table-box table');
  284. const type = $type.val();
  285. const day = $day.val();
  286. const now = new Date();
  287. const year = now.getFullYear();
  288. let month = (Number(now.getMonth()) - 1) - Number(type);
  289. if (month < 10) {
  290. month = "0" + month;
  291. }
  292. const statYm = year + month;
  293. const param = {
  294. statYm : statYm,
  295. day : day,
  296. }
  297. // let height = 'calc(-443px + 100vh)';
  298. // $table.css('height', height);
  299. $tbody.html('<tr><td><span><img src="/images/icon/loading.gif"></span></td></tr>');
  300. getDataAsync('/api/statistics/congest' , "POST", param, null, (jsonData)=>{
  301. let str = emptyStr;
  302. if (jsonData && jsonData.length > 0) {
  303. str = "";
  304. jsonData.forEach((obj)=>{
  305. const { road_nm, strt_nm, end_nm, strt_tm, end_tm, sped} = obj;
  306. str += `<tr>
  307. <td>${road_nm}</td>
  308. <td>${strt_nm}</td>
  309. <td>${end_nm}</td>
  310. <td>${strt_tm}</td>
  311. <td>${end_tm}</td>
  312. <td>${sped} km</td>
  313. </tr>`
  314. });
  315. // height = 'auto';
  316. // $table.css('height', height);
  317. }
  318. $tbody.html(str);
  319. }, ()=>{
  320. $tbody.html(emptyStr);
  321. });
  322. }
  323. /**
  324. * 조회 날짜 값 포맷
  325. * @param year 년도
  326. * @param month 월
  327. * @param date 일
  328. * @returns {*} 포맷 문자
  329. */
  330. function getSearchDateValue(year, month, date) {
  331. if (!year || !month || !date) {
  332. return;
  333. }
  334. if (isNaN(year) || isNaN(month) || isNaN(date)) {
  335. return;
  336. }
  337. month = (month < 10 ? ('0'+ month) : month);
  338. date = (date < 10 ? ('0'+ date) : date);
  339. return year + month + date;
  340. }