TGoodManagementService.php 4.8KB

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