PriceController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers\Service;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Http;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Traits\PagingTrait;
  7. /*
  8. * 한국농수산식품유통공사_화훼 경매 시세
  9. */
  10. class PriceController extends Controller
  11. {
  12. use PagingTrait;
  13. const API_CLOUD_KEY = 'vQz8tIxrdhjerG6DE1w1hcVEli5S27LtIsCvx0axiieZmRgOpB4vToQ77VmvknAkIC9YjxlPx2gDZcl06S88Xw==';
  14. const API_CLOUD_HOST = 'https://api.odcloud.kr/api/15052544/v1/uddi:9e23a38b-3d9f-4e5c-aa4f-ecfa1b8a640a';
  15. const API_FLOWER_KEY = '5F70416B443241BE846D0807744FC489';
  16. const API_FLOWER_HOST = 'https://flower.at.or.kr/api/returnData.api';
  17. public function __construct()
  18. {
  19. $this->middleware('front');
  20. }
  21. /**
  22. * 환율 정보
  23. * @method GET
  24. * @see /service/price/flower
  25. */
  26. public function flower(Request $request)
  27. {
  28. // 공통 변수
  29. $page = $request->get('page', 1);
  30. $perPage = $request->get('per_page', 15);
  31. // 검색 시 필요
  32. $gubun = $request->get('gubun'); // 부류
  33. $date = $request->get("date"); // 기준일
  34. $pName = $request->get('p_name'); // 품목명
  35. $gName = $request->get('g_name'); // 품종명
  36. $pid = $request->get('pid');
  37. $total = 0;
  38. $maxAmt = 0;
  39. $minAmt = 0;
  40. $avgAmt = 0;
  41. $totQty = 0;
  42. $totAmt = 0;
  43. $list = [];
  44. if(!$gubun && !$date) {
  45. $params = [
  46. 'serviceKey' => self::API_CLOUD_KEY,
  47. 'page' => $page,
  48. 'perPage' => $perPage,
  49. 'returnType' => 'json'
  50. ];
  51. $response = Http::acceptJson()->withHeaders([
  52. 'Authorization' => self::API_CLOUD_KEY
  53. ])->get(self::API_CLOUD_HOST, $params);
  54. if($response->ok()) {
  55. $result = $response->object();
  56. $total = $result->totalCount;
  57. $num = listNum($total, $page, $perPage);
  58. if($result && $total) {
  59. foreach($result->data as $row) {
  60. $row = (array)($row);
  61. $maxAmt += $row['최고단가(원)'];
  62. $minAmt += $row['최저단가(원)'];
  63. $avgAmt += $row['평균단가(원)'];
  64. $totQty += $row['총수량'];
  65. $totAmt += $row['총금액(원)'];
  66. $list[] = [
  67. 'num' => $num--,
  68. 'saleDate' => $row['정산일자'],
  69. 'flowerGubn' => $row['화훼구분명'],
  70. 'pumName' => $row['품목명'],
  71. 'goodName' => $row['품종명'],
  72. 'lvNm' => $row['등급명'],
  73. 'maxAmt' => number_format($row['최고단가(원)']),
  74. 'minAmt' => number_format($row['최저단가(원)']),
  75. 'avgAmt' => number_format($row['평균단가(원)']),
  76. 'totQty' => number_format($row['총수량']),
  77. 'totAmt' => number_format($row['총금액(원)'])
  78. ];
  79. }
  80. }
  81. }
  82. }else{
  83. $params = [
  84. 'kind' => 'f001',
  85. 'serviceKey' => self::API_FLOWER_KEY,
  86. 'baseDate' => ($date ?? now()->format('Y-m-d')),
  87. 'flowerGubn' => ($gubun ?? 1),
  88. 'dataType' => 'json',
  89. ];
  90. if($pName || $gName) {
  91. if($pName) {
  92. $params['pumName'] = $pName;
  93. }
  94. if($gName) {
  95. $params['goodName'] = $gName;
  96. }
  97. }
  98. // total
  99. $params['countPerPage'] = 10000;
  100. $response = Http::get(self::API_FLOWER_HOST, $params);
  101. if($response->ok()) {
  102. $total = intval($response->json('response')['numOfRows']);
  103. }
  104. // list
  105. if(!$gName) {
  106. $params['currentPage'] = $page;
  107. $params['countPerPage'] = $perPage;
  108. }
  109. $response = Http::get(self::API_FLOWER_HOST, $params);
  110. if($response->ok()) {
  111. $result = $response->json('response');
  112. if($result && $result['numOfRows'] > 0) {
  113. $num = listNum($total, $page, $perPage);
  114. foreach($result['items'] as $i => &$row) {
  115. $row['num'] = $num--;
  116. $maxAmt += $row['maxAmt'];
  117. $minAmt += $row['minAmt'];
  118. $avgAmt += $row['avgAmt'];
  119. $totQty += $row['totQty'];
  120. $totAmt += $row['totAmt'];
  121. $list[] = array_merge($row, [
  122. 'maxAmt' => number_format($row['maxAmt']),
  123. 'minAmt' => number_format($row['minAmt']),
  124. 'avgAmt' => number_format($row['avgAmt']),
  125. 'totQty' => number_format($row['totQty']),
  126. 'totAmt' => number_format($row['totAmt']),
  127. ]);
  128. }
  129. }
  130. }
  131. }
  132. $list = $this->getPaginator(
  133. $list, $total, $perPage, $page
  134. );
  135. return view(layout('service.price.flower'), [
  136. 'menuID' => 'flower',
  137. 'gubun' => $gubun,
  138. 'date' => $date,
  139. 'pName' => $pName,
  140. 'gName' => $gName,
  141. 'pid' => $pid,
  142. 'total' => $total,
  143. 'list' => $list,
  144. 'maxAmt' => number_format($maxAmt),
  145. 'minAmt' => number_format($minAmt),
  146. 'avgAmt' => number_format($avgAmt),
  147. 'totQty' => number_format($totQty),
  148. 'totAmt' => number_format($totAmt),
  149. 'page' => $page,
  150. 'perPage' => $perPage
  151. ]);
  152. }
  153. }