GoodManagementService.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Services\Backend\DataManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\Web\Good;
  5. use App\Models\Web\Syslog;
  6. class GoodManagementService
  7. {
  8. // 相關私有 model 調用器宣告
  9. private $goodManagementDb;
  10. private $syslogManagementDb;
  11. public function __construct()
  12. {
  13. date_default_timezone_set("Asia/Taipei");
  14. // 建構 model 調用器
  15. $this->goodManagementDb = new Good();
  16. $this->syslogManagementDb = new Syslog();
  17. }
  18. public function getGoods(
  19. &$cnt = 0,
  20. $orderColumn,
  21. $orderDir,
  22. $start,
  23. $length,
  24. $searchValue
  25. )
  26. {
  27. $goods = $this->goodManagementDb
  28. ->select([
  29. 'id',
  30. \DB::raw("CONCAT('<a href=\"goodManagement/edit/', id, '\">', lp, '</a>') as lp"),
  31. 'gp',
  32. 'totalQty',
  33. 'issuedQty',
  34. 'cdate',
  35. 'mdate',
  36. \DB::raw("(select name from users where id=goods.oid) as oid"),
  37. ]);
  38. // 過濾搜尋條件
  39. // 取總筆數
  40. $cnt = $goods->count();
  41. // 排序
  42. $goods = $goods
  43. ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
  44. // 彙整
  45. // 分頁
  46. $goods = $goods
  47. ->skip($start)->take($length);
  48. // 實際取資料
  49. $goods = $goods
  50. ->get()
  51. ->toArray();
  52. // 整理返回值並返回
  53. return $goods;
  54. }
  55. public function getGoodById($id)
  56. {
  57. // 取得參數
  58. // 調用資料庫(或者其他業務邏輯)
  59. $goods = $this->goodManagementDb->select([
  60. 'id',
  61. 'lp',
  62. 'gp',
  63. 'totalQty',
  64. 'issuedQty',
  65. 'cdate',
  66. 'mdate',
  67. \DB::raw("(select name from users where id=goods.oid) as oid"),
  68. ])
  69. ->where('id', $id)
  70. ->first()
  71. ->toArray();
  72. // 整理返回值並返回
  73. return $goods;
  74. }
  75. public function insertGood($lp, $gp, $totalQty, $oid)
  76. {
  77. // 取得參數
  78. $data = [
  79. 'lp' => $lp,
  80. 'gp' => $gp,
  81. 'totalQty' => $totalQty,
  82. 'issuedQty' => 0,
  83. 'cdate' => date('Y-m-d H:i:s'),
  84. 'mdate' => date('Y-m-d H:i:s'),
  85. 'oid' => $oid,
  86. ];
  87. // 調用資料庫(或者其他業務邏輯)
  88. $this->goodManagementDb
  89. ->insert($data);
  90. $id = \DB::getPdo()->lastInsertId();
  91. // syslog
  92. $this->syslogManagementDb
  93. ->insert([
  94. 'type' => GeneralConst::LOG_ADMIN,
  95. 'func' => __FUNCTION__,
  96. 'k' => $oid,
  97. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  98. 'memoOut' => json_encode(['id' => $id], JSON_UNESCAPED_UNICODE),
  99. 'cdate' => date("Y-m-d H:i:s"),
  100. ]);
  101. // 整理返回值並返回
  102. return $id;
  103. }
  104. public function modifyGood($id, $lp, $gp, $totalQty, $oid)
  105. {
  106. // 取得參數
  107. $data = [
  108. 'lp' => $lp,
  109. 'gp' => $gp,
  110. 'totalQty' => $totalQty,
  111. 'mdate' => date('Y-m-d H:i:s'),
  112. 'oid' => $oid,
  113. ];
  114. // 調用資料庫(或者其他業務邏輯)
  115. $res = $this->goodManagementDb
  116. ->where('id', $id)
  117. ->update($data);
  118. $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
  119. $rc = $rc[0]->rc;
  120. // syslog
  121. $this->syslogManagementDb
  122. ->insert([
  123. 'type' => GeneralConst::LOG_ADMIN,
  124. 'func' => __FUNCTION__,
  125. 'k' => $oid,
  126. 'memoIn' => json_encode(['id' => $id, 'data' => $data], JSON_UNESCAPED_UNICODE),
  127. 'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
  128. 'cdate' => date("Y-m-d H:i:s"),
  129. ]);
  130. // 整理返回值並返回
  131. return $res;
  132. }
  133. }