Orl 短網址,供三星、福斯使用

ProductManagementService.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace App\Http\Services\Backend\EcManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\EcManagement\ProductManagement;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use Redis;
  8. class ProductManagementService
  9. {
  10. // 相關私有 model 調用器宣告
  11. private $productManagementDb;
  12. public function __construct()
  13. {
  14. // 建構 model 調用器
  15. $this->productManagementDb = new ProductManagement();
  16. }
  17. public function insertProduct($data)
  18. {
  19. // 取得參數
  20. // 調用資料庫(或者其他業務邏輯)
  21. $res = $this->productManagementDb->insert($data);
  22. // 整理返回值並返回
  23. return $res;
  24. }
  25. public function deleteProductByBatchNo($del_batch_no)
  26. {
  27. // 取得參數
  28. // 調用資料庫(或者其他業務邏輯)
  29. $this->productManagementDb
  30. ->where('batch_no', '<=', $del_batch_no . ' 23:59:59')
  31. ->delete();
  32. // 整理返回值並返回
  33. return true;
  34. }
  35. public function getProductDetail($brand_no, $batch_no, $g_id)
  36. {
  37. // 取得參數
  38. // 調用資料庫(或者其他業務邏輯)
  39. $res = $this->productManagementDb
  40. ->select([
  41. 'm_status',
  42. 'm_comment',
  43. ])
  44. ->where('brand_no', $brand_no)
  45. ->where('batch_no', $batch_no)
  46. ->where('g_id', (int)$g_id)
  47. ->get()
  48. ->toArray();
  49. // 整理返回值並返回
  50. return $res[0] ?? [
  51. 'm_status' => '',
  52. 'm_comment' => '',
  53. ];
  54. }
  55. public function updateProduct($brand_no, $batch_no, $g_id, $m_status, $m_comment)
  56. {
  57. // 取得參數
  58. // 調用資料庫(或者其他業務邏輯)
  59. $this->productManagementDb
  60. ->where('brand_no', $brand_no)
  61. ->where('batch_no', $batch_no)
  62. ->where('g_id', (int)$g_id)
  63. ->update([
  64. 'm_status' => $m_status,
  65. 'm_comment' => $m_comment,
  66. ]);
  67. // 整理返回值並返回
  68. return true;
  69. }
  70. public function getProdocts(
  71. &$cnt = 0,
  72. $orderColumn,
  73. $orderDir,
  74. $start,
  75. $length,
  76. $searchValue,
  77. $brand_no,
  78. $batch_no,
  79. $g_title,
  80. $g_description,
  81. $m_comment,
  82. $m_status
  83. )
  84. {
  85. // 調用資料庫(或者其他業務邏輯)
  86. // 選欄位
  87. // 選欄位
  88. $mStatusStr = '';
  89. foreach (GeneralConst::$mStatusMap as $k => $v) {
  90. $mStatusStr .= ' when \'' . $k . '\' then \'' . $v['ANAME'] . '\'';
  91. }
  92. $products = $this->productManagementDb
  93. ->select([
  94. 'serno',
  95. 'g_id',
  96. \DB::raw('concat(\'<a href="\', g_link, \'" target="_blank">\', g_title, \'</a>\') as g_title'),
  97. \DB::raw('concat(\'<button type="button" class="btn btn-outline-success btn-sm" data-toggle="popover" title="\', g_title, \'" data-content="\', g_description, \'">\', left(g_description, 10), \' ... </button>\') as g_description'),
  98. \DB::raw('concat(\'<img src="\', g_image_link, \'" width="100" height="100">\') as g_image_link'),
  99. 'g_price',
  100. 'g_sale_price',
  101. \DB::raw("(case m_status $mStatusStr end) as m_status"),
  102. 'm_comment',
  103. ]);
  104. // 過濾搜尋條件
  105. $products = $products
  106. ->where('brand_no', '=', $brand_no)
  107. ->where('batch_no', '=', $batch_no)
  108. ->where('g_title', 'LIKE', '%' . $g_title . '%')
  109. ->where('g_description', 'LIKE', '%' . $g_description . '%')
  110. ->where('m_comment', 'LIKE', '%' . $m_comment . '%')
  111. ->where('m_status', '=', $m_status);
  112. // 取總筆數
  113. $cnt = $products->count();
  114. // 排序
  115. $products = $products
  116. ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
  117. // 分頁
  118. $products = $products
  119. ->skip($start)->take($length);
  120. // 實際取資料
  121. $result = $products
  122. ->get()
  123. ->toArray();
  124. // 整理返回值並返回
  125. return $result;
  126. }
  127. public function getEndpointProdocts($brand_no, $batch_no)
  128. {
  129. $products = $this->productManagementDb
  130. ->select([
  131. 'g_id',
  132. 'g_title',
  133. 'g_description',
  134. 'g_link',
  135. 'g_image_link',
  136. 'g_condition',
  137. 'g_availability',
  138. 'g_price',
  139. 'g_sale_price',
  140. 'g_brand',
  141. 'g_google_product_category',
  142. 'g_product_type',
  143. 'g_custom_label_0',
  144. ]);
  145. // 過濾搜尋條件
  146. $products = $products
  147. ->where('brand_no', '=', $brand_no)
  148. ->where('batch_no', '=', $batch_no)
  149. ->whereIn('m_status', [GeneralConst::MSTATUS_PASS, GeneralConst::MSTATUS_WARNING]);
  150. // 實際取資料
  151. $result = $products
  152. ->get()
  153. ->toArray();
  154. // 整理返回值並返回
  155. return $result;
  156. }
  157. public function getExportProdocts($param)
  158. {
  159. // 調用資料庫(或者其他業務邏輯)
  160. // 選欄位
  161. $mStatusStr = '';
  162. foreach (GeneralConst::$mStatusMap as $k => $v) {
  163. $mStatusStr .= ' when \'' . $k . '\' then \'' . $v['ANAME'] . '\'';
  164. }
  165. $brand_no = $param['brand_no'] ?? '';
  166. $batch_no = $param['batch_no'] ?? '';
  167. $products = $this->productManagementDb
  168. ->select([
  169. 'serno',
  170. 'g_id',
  171. 'g_title',
  172. 'g_description',
  173. 'g_link',
  174. 'g_image_link',
  175. 'g_price',
  176. 'g_sale_price',
  177. \DB::raw("(case m_status $mStatusStr end) as m_status"),
  178. 'm_comment',
  179. ]);
  180. // 過濾搜尋條件
  181. $products = $products
  182. ->where('brand_no', '=', $brand_no)
  183. ->where('batch_no', '=', $batch_no)
  184. ->whereIn('m_status', [GeneralConst::MSTATUS_WARNING, GeneralConst::MSTATUS_DISAPPROVED]);
  185. $products = $products
  186. ->get()
  187. ->toArray();
  188. // 整理返回值並返回
  189. return $products;
  190. }
  191. public function downloadExcel($titles = [], $datas = [], $fileName = 'simple')
  192. {
  193. $spreadsheet = new Spreadsheet();
  194. ini_set('memory_limit', '1024M');
  195. ini_set("max_execution_time", "600");
  196. $spreadsheet->getActiveSheet()
  197. ->fromArray(
  198. $titles, // The data to set
  199. null, // Array values with this value will not be set
  200. 'A1' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
  201. );
  202. $spreadsheet->getActiveSheet()
  203. ->fromArray(
  204. $datas, // The data to set
  205. null, // Array values with this value will not be set
  206. 'A2' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
  207. );
  208. // Redirect output to a client’s web browser (Xlsx)
  209. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  210. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  211. header('Cache-Control: max-age=0');
  212. // If you're serving to IE 9, then the following may be needed
  213. header('Cache-Control: max-age=1');
  214. // If you're serving to IE over SSL, then the following may be needed
  215. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  216. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  217. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  218. header('Pragma: public'); // HTTP/1.0
  219. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  220. $writer->save('php://output');
  221. exit;
  222. }
  223. public function getBrands()
  224. {
  225. $brands = [];
  226. foreach (GeneralConst::$brandMap as $k => $v) {
  227. $brands[] = [
  228. 'id' => $k,
  229. 'brandName' => $v['NS'],
  230. 'brandLabel' => $v['LB'],
  231. ];
  232. }
  233. return $brands;
  234. }
  235. public function getBatchs()
  236. {
  237. $ret = $this->productManagementDb
  238. ->select([
  239. 'batch_no',
  240. \DB::raw("max(m_status) as m_status"),
  241. ])
  242. ->groupBy('batch_no')
  243. ->orderBy('batch_no', 'desc')
  244. ->get()
  245. ->toArray();
  246. // 正在抓的批次號需要去除
  247. if (Redis::get('TLW_0_XML')) {
  248. array_shift($ret);
  249. }
  250. return $ret;
  251. }
  252. public function getMStatuss()
  253. {
  254. $mstatuss = [];
  255. foreach (GeneralConst::$mStatusMap as $k => $v) {
  256. $mstatuss[] = [
  257. 'id' => $k,
  258. 'mstatusName' => $v['ANAME'],
  259. ];
  260. }
  261. return $mstatuss;
  262. }
  263. }