ReceiptManagementService.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Http\Services\Backend\DataManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\Web\Receipt;
  5. use App\Models\Web\Syslogact;
  6. use PhpOffice\PhpSpreadsheet\IOFactory;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use Illuminate\Support\Facades\DB;
  9. use App\Models\Web\Activity;
  10. class ReceiptManagementService
  11. {
  12. // 相關私有 model 調用器宣告
  13. private $receiptManagementDb;
  14. private $activityManagementDb;
  15. private $syslogactManagementDb;
  16. public function __construct()
  17. {
  18. // 建構 model 調用器
  19. $this->receiptManagementDb = new Receipt();
  20. $this->activityManagementDb = new Activity();
  21. $this->syslogactManagementDb = new Syslogact();
  22. }
  23. public function getActivitys()
  24. {
  25. $activity = $this->activityManagementDb->select([
  26. 'id',
  27. 'activityName',
  28. ])
  29. ->get()
  30. ->toArray();
  31. return $activity;
  32. }
  33. public function getReceipts(
  34. &$cnt = 0,
  35. $orderColumn,
  36. $orderDir,
  37. $start,
  38. $length,
  39. $searchValue,
  40. $tranDateStart,
  41. $tranDateFinal,
  42. $tranBank,
  43. $tranAccount,
  44. $tranOrderNo,
  45. $lineId,
  46. $lineName,
  47. $activity,
  48. $rStatus,
  49. $canGet
  50. )
  51. {
  52. // 選欄位
  53. $rStatusStr = '';
  54. foreach (GeneralConst::$rStatusMap as $k => $v) {
  55. $rStatusStr .= ' when \'' . $k . '\' then \'' . $v['back'] . '\'';
  56. }
  57. $receipt = $this->receiptManagementDb
  58. ->leftJoin('activity', 'receipt.aid', '=', 'activity.id')
  59. ->select([
  60. 'receipt.id',
  61. 'receipt.tranDate',
  62. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranBank), \"" . env('KK') . "\")) as tranBank"),
  63. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranAccount), \"" . env('KK') . "\")) as tranAccount"),
  64. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranOrderNo), \"" . env('KK') . "\")) as tranOrderNo"),
  65. 'receipt.lineId',
  66. 'receipt.lineName',
  67. 'activity.activityName',
  68. \DB::raw("(case receipt.rStatus $rStatusStr end) as rStatus"),
  69. \DB::raw("
  70. (CASE
  71. WHEN (
  72. receipt.canGet = '' -- 檢查專用的狀態位為空
  73. AND receipt.rStatus = " . GeneralConst::RSTATUS_DRAW_DONE_REDEEM . " -- 紀錄的狀態位為檢查中
  74. AND UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(activity.checkTimeBegin, ' 00:00:00')) -- 活動已達驗證時間
  75. AND activity.isCheckBegin = '" . GeneralConst::ACTIVE_YES . "' -- 活動已開放驗證
  76. ) THEN
  77. CONCAT(
  78. '<input class=\"form-check-input\" type=\"checkbox\" name=\"chk[',
  79. receipt.id,
  80. ']\" value=\"',
  81. receipt.id,
  82. '\"',
  83. '>'
  84. )
  85. WHEN receipt.canGet = '" . GeneralConst::CANGET_YES . "' THEN '是'
  86. WHEN receipt.canGet = '" . GeneralConst::CANGET_NO . "' THEN '否'
  87. ELSE ''
  88. END) as canGet
  89. "),
  90. 'receipt.cdate',
  91. 'receipt.mdate',
  92. \DB::raw("(select name from users where id=receipt.oid) as oid"),
  93. ]);
  94. // 過濾搜尋條件
  95. $receipt = $receipt->where('receipt.tranDate', '>=', $tranDateStart . ' 00:00:00');
  96. $receipt = $receipt->where('receipt.tranDate', '<=', $tranDateFinal . ' 23:59:59');
  97. $receipt = $receipt->whereRaw("(
  98. FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranBank), \"" . env('KK') . "\")) like '%" . $tranBank . "%'
  99. AND FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranAccount), \"" . env('KK') . "\")) like '%" . $tranAccount . "%'
  100. AND FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranOrderNo), \"" . env('KK') . "\")) like '%" . $tranOrderNo . "'
  101. )");
  102. if ($lineId != '') $receipt = $receipt->where('receipt.lineId', 'like', '%' . $lineId . '%');
  103. if ($lineName != '') $receipt = $receipt->where('receipt.lineName', 'like', '%' . $lineName . '%');
  104. if ($activity != '') $receipt = $receipt->where('activity.id', '=', $activity);
  105. if ($rStatus != '') $receipt = $receipt->where('receipt.rStatus', '=', $rStatus);
  106. if ($canGet != '') $receipt = $receipt->where('receipt.canGet', '=', $canGet);
  107. // 取總筆數
  108. $cnt = $receipt->count();
  109. // 排序
  110. $receipt = $receipt
  111. ->orderByRaw((int)$orderColumn . ' ' . 'ASC');
  112. // 彙整
  113. // 分頁
  114. $receipt = $receipt
  115. ->skip($start)->take($length);
  116. // 實際取資料
  117. $receipt = $receipt
  118. ->get()
  119. ->toArray();
  120. // 整理返回值並返回
  121. return $receipt;
  122. }
  123. public function getExports($param)
  124. {
  125. // 選欄位
  126. $rStatusStr = '';
  127. foreach (GeneralConst::$rStatusMap as $k => $v) {
  128. $rStatusStr .= ' when \'' . $k . '\' then \'' . $v['back'] . '\'';
  129. }
  130. $receipt = $this->receiptManagementDb
  131. ->leftJoin('activity', 'receipt.aid', '=', 'activity.id')
  132. ->select([
  133. 'receipt.id',
  134. 'receipt.tranDate',
  135. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranBank), \"" . env('KK') . "\")) as tranBank"),
  136. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranAccount), \"" . env('KK') . "\")) as tranAccount"),
  137. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranOrderNo), \"" . env('KK') . "\")) as tranOrderNo"),
  138. 'receipt.lineId',
  139. 'receipt.lineName',
  140. 'activity.activityName',
  141. \DB::raw("(case rStatus $rStatusStr end) as rStatus"),
  142. 'receipt.canGet',
  143. 'receipt.cdate',
  144. 'receipt.mdate',
  145. \DB::raw("(select name from users where id=receipt.oid) as oid"),
  146. ]);
  147. // 過濾搜尋條件
  148. $receipt = $receipt->where('receipt.tranDate', '>=', $param["tranDateStart"] . ' 00:00:00');
  149. $receipt = $receipt->where('receipt.tranDate', '<=', $param["tranDateFinal"] . ' 23:59:59');
  150. $receipt = $receipt->whereRaw("(
  151. FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranBank), \"" . env('KK') . "\")) like '%" . $param["tranBank"] . "%'
  152. AND FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranAccount), \"" . env('KK') . "\")) like '%" . $param["tranAccount"] . "%'
  153. AND FROM_BASE64(AES_DECRYPT(UNHEX(receipt.tranOrderNo), \"" . env('KK') . "\")) like '" . $param["tranOrderNo"] . "%'
  154. )");
  155. if ($param["lineId"] != '') $receipt = $receipt->where('receipt.lineId', 'like', '%' . $param["lineId"] . '%');
  156. if ($param["lineName"] != '') $receipt = $receipt->where('receipt.lineName', 'like', '%' . $param["lineName"] . '%');
  157. if ($param["activity"] != '') $receipt = $receipt->where('activity.id', '=', $param["activity"]);
  158. if ($param["rStatus"] != '') $receipt = $receipt->where('receipt.rStatus', '=', $param["rStatus"]);
  159. if ($param["canGet"] != '') $receipt = $receipt->where('receipt.canGet', '=', $param["canGet"]);
  160. // 實際取資料
  161. $receipt = $receipt
  162. ->get()
  163. ->toArray();
  164. // 整理返回值並返回
  165. return $receipt;
  166. }
  167. public function downloadExcel($titles = [], $datas = [], $fileName = 'simple')
  168. {
  169. $spreadsheet = new Spreadsheet();
  170. ini_set('memory_limit', '1024M');
  171. ini_set("max_execution_time", "600");
  172. $spreadsheet->getActiveSheet()
  173. ->fromArray(
  174. $titles, // The data to set
  175. null, // Array values with this value will not be set
  176. 'A1' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
  177. );
  178. $spreadsheet->getActiveSheet()
  179. ->fromArray(
  180. $datas, // The data to set
  181. null, // Array values with this value will not be set
  182. 'A2' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
  183. );
  184. // Redirect output to a client’s web browser (Xlsx)
  185. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  186. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  187. header('Cache-Control: max-age=0');
  188. // If you're serving to IE 9, then the following may be needed
  189. header('Cache-Control: max-age=1');
  190. // If you're serving to IE over SSL, then the following may be needed
  191. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  192. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  193. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  194. header('Pragma: public'); // HTTP/1.0
  195. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  196. $writer->save('php://output');
  197. exit;
  198. }
  199. public function redeemDone($chk, $oid)
  200. {
  201. $this->receiptManagementDb
  202. ->whereIn('id', $chk)
  203. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM)
  204. ->where('canGet', '')
  205. ->update([
  206. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE,
  207. 'canGet' => GeneralConst::CANGET_YES,
  208. 'mdate' => date('Y-m-d H:i:s'),
  209. 'oid' => $oid,
  210. ]);
  211. // syslogact
  212. $data = [
  213. 'redeem_done' => $chk,
  214. ];
  215. $this->syslogactManagementDb
  216. ->insert([
  217. 'type' => GeneralConst::LOG_ADMIN,
  218. 'func' => __FUNCTION__,
  219. 'k' => $oid,
  220. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  221. 'memoOut' => json_encode([], JSON_UNESCAPED_UNICODE),
  222. 'cdate' => date("Y-m-d H:i:s"),
  223. ]);
  224. // 整理返回值並返回
  225. return true;
  226. }
  227. public function redeemFail($chk, $oid)
  228. {
  229. $this->receiptManagementDb
  230. ->whereIn('id', $chk)
  231. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM)
  232. ->where('canGet', '')
  233. ->update([
  234. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_FAIL,
  235. 'canGet' => GeneralConst::CANGET_NO,
  236. 'mdate' => date('Y-m-d H:i:s'),
  237. 'oid' => $oid,
  238. ]);
  239. // syslogact
  240. $data = [
  241. 'redeem_fail' => $chk,
  242. ];
  243. $this->syslogactManagementDb
  244. ->insert([
  245. 'type' => GeneralConst::LOG_ADMIN,
  246. 'func' => __FUNCTION__,
  247. 'k' => $oid,
  248. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  249. 'memoOut' => json_encode([], JSON_UNESCAPED_UNICODE),
  250. 'cdate' => date("Y-m-d H:i:s"),
  251. ]);
  252. // 整理返回值並返回
  253. return true;
  254. }
  255. }