ActivityManagementService.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace App\Http\Services\Backend\DataManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\Web\Activity;
  5. use App\Models\Web\Receipt;
  6. use App\Models\Web\Syslogact;
  7. class ActivityManagementService
  8. {
  9. // 相關私有 model 調用器宣告
  10. private $activityManagementDb;
  11. private $receiptManagementDb;
  12. private $syslogactManagementDb;
  13. public function __construct()
  14. {
  15. date_default_timezone_set("Asia/Taipei");
  16. // 建構 model 調用器
  17. $this->activityManagementDb = new Activity();
  18. $this->receiptManagementDb = new Receipt();
  19. $this->syslogactManagementDb = new Syslogact();
  20. }
  21. public function getActivitys(
  22. &$cnt = 0,
  23. $orderColumn,
  24. $orderDir,
  25. $start,
  26. $length,
  27. $searchValue
  28. )
  29. {
  30. $activity = $this->activityManagementDb
  31. ->select([
  32. 'id',
  33. \DB::raw("CONCAT('<a href=\"activityManagement/edit/', id, '\">', activityName, '</a>') as activityName"),
  34. \DB::raw("CONCAT(reqTimeBegin, '<br>', reqTimeFinal) as reqTime"),
  35. 'drawTime',
  36. \DB::raw("replace(drawNumbers, ',', '<br>') as drawNumbers"),
  37. \DB::raw("
  38. (case
  39. -- 現在時間小於開獎時間 (NOW是+8時區,UNIX_TIMESTAMP之後就變成+0時區)
  40. when UNIX_TIMESTAMP(NOW()) < UNIX_TIMESTAMP(CONCAT(drawTime, ' 00:00:00'))
  41. then '尚未到開獎時間'
  42. -- 現在時間大於開獎時間
  43. else (case isDraw
  44. -- 決定顯示出來的開獎按鈕可不可以按
  45. when '' then CONCAT('<a href=\"#\" onclick=\"confirmUser(''activityManagement/draw/', id, ''')\">點我開獎</a>')
  46. else '已經開獎'
  47. end)
  48. end) as drawBtn
  49. "),
  50. \DB::raw("CONCAT(redeemTimeBegin, '<br>', redeemTimeFinal) as redeemTime"),
  51. \DB::raw("
  52. (case
  53. -- 現在時間小於驗證時間首日 (NOW是+8時區,UNIX_TIMESTAMP之後就變成+0時區)
  54. when UNIX_TIMESTAMP(NOW()) < UNIX_TIMESTAMP(CONCAT(checkTimeBegin, ' 00:00:00'))
  55. then '尚未到驗證時間'
  56. -- 現在時間大於驗證時間首日
  57. else (case isCheckBegin
  58. -- 決定顯示出來的驗證按鈕可不可以按
  59. when '' then CONCAT('<a href=\"#\" onclick=\"confirmUser(''activityManagement/check/', id, ''')\">點我啟動驗證流程</a>')
  60. else '已啟動驗證流程'
  61. end)
  62. end) as checkBtn
  63. "),
  64. \DB::raw("CONCAT(checkTimeBegin, '<br>', checkTimeFinal) as checkTime"),
  65. \DB::raw("CONCAT(getTimeBegin, '<br>', getTimeFinal) as getTime"),
  66. \DB::raw("
  67. (case
  68. -- 現在時間小於領獎時間迄日 (NOW是+8時區,UNIX_TIMESTAMP之後就變成+0時區)
  69. when UNIX_TIMESTAMP(NOW()) < UNIX_TIMESTAMP(CONCAT(getTimeFinal, ' 00:00:00'))
  70. then '尚未到領獎結束時間'
  71. -- 現在時間大於領獎時間迄日
  72. else (case isGetFinal
  73. -- 決定顯示出來的驗證按鈕可不可以按
  74. when '' then CONCAT('<a href=\"#\" onclick=\"confirmUser(''activityManagement/get/', id, ''')\">點我關閉領獎活動</a>')
  75. else '已關閉領獎活動'
  76. end)
  77. end) as getBtn
  78. "),
  79. 'cdate',
  80. 'mdate',
  81. \DB::raw("(select name from users where id=activity.oid) as oid"),
  82. ]);
  83. // 過濾搜尋條件
  84. // 取總筆數
  85. $cnt = $activity->count();
  86. // 排序
  87. $activity = $activity
  88. ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
  89. // 彙整
  90. // 分頁
  91. $activity = $activity
  92. ->skip($start)->take($length);
  93. // 實際取資料
  94. $activity = $activity
  95. ->get()
  96. ->toArray();
  97. // 整理返回值並返回
  98. return $activity;
  99. }
  100. public function getActivityById($id)
  101. {
  102. // 取得參數
  103. // 調用資料庫(或者其他業務邏輯)
  104. $activity = $this->activityManagementDb->select([
  105. 'id',
  106. 'activityName',
  107. 'reqTimeBegin',
  108. 'reqTimeFinal',
  109. 'drawTime',
  110. 'drawNumbers',
  111. 'redeemTimeBegin',
  112. 'redeemTimeFinal',
  113. 'checkTimeBegin',
  114. 'checkTimeFinal',
  115. 'getTimeBegin',
  116. 'getTimeFinal',
  117. 'cdate',
  118. 'mdate',
  119. \DB::raw("(select name from users where id=activity.oid) as oid"),
  120. ])
  121. ->where('id', $id)
  122. ->first()
  123. ->toArray();
  124. // 整理返回值並返回
  125. return $activity;
  126. }
  127. public function insertActivity($activityName, $reqTimeBegin, $reqTimeFinal, $drawTime, $drawNumbers, $redeemTimeBegin, $redeemTimeFinal, $checkTimeBegin, $checkTimeFinal, $getTimeBegin, $getTimeFinal, $oid)
  128. {
  129. // 取得參數
  130. $data = [
  131. 'activityName' => $activityName,
  132. 'reqTimeBegin' => $reqTimeBegin,
  133. 'reqTimeFinal' => $reqTimeFinal,
  134. 'drawTime' => $drawTime,
  135. 'drawNumbers' => $drawNumbers,
  136. 'redeemTimeBegin' => $redeemTimeBegin,
  137. 'redeemTimeFinal' => $redeemTimeFinal,
  138. 'checkTimeBegin' => $checkTimeBegin,
  139. 'checkTimeFinal' => $checkTimeFinal,
  140. 'getTimeBegin' => $getTimeBegin,
  141. 'getTimeFinal' => $getTimeFinal,
  142. 'cdate' => date('Y-m-d H:i:s'),
  143. 'mdate' => date('Y-m-d H:i:s'),
  144. 'oid' => $oid,
  145. ];
  146. // 調用資料庫(或者其他業務邏輯)
  147. $this->activityManagementDb
  148. ->insert($data);
  149. $id = \DB::getPdo()->lastInsertId();
  150. // syslogact
  151. $this->syslogactManagementDb
  152. ->insert([
  153. 'type' => GeneralConst::LOG_ADMIN,
  154. 'func' => __FUNCTION__,
  155. 'k' => $oid,
  156. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  157. 'memoOut' => json_encode(['id' => $id], JSON_UNESCAPED_UNICODE),
  158. 'cdate' => date("Y-m-d H:i:s"),
  159. ]);
  160. // 整理返回值並返回
  161. return $id;
  162. }
  163. public function modifyActivity($id, $activityName, $reqTimeBegin, $reqTimeFinal, $drawTime, $drawNumbers, $redeemTimeBegin, $redeemTimeFinal, $checkTimeBegin, $checkTimeFinal, $getTimeBegin, $getTimeFinal, $oid)
  164. {
  165. // 取得參數
  166. $data = [
  167. 'activityName' => $activityName,
  168. 'reqTimeBegin' => $reqTimeBegin,
  169. 'reqTimeFinal' => $reqTimeFinal,
  170. 'drawTime' => $drawTime,
  171. 'drawNumbers' => $drawNumbers,
  172. 'redeemTimeBegin' => $redeemTimeBegin,
  173. 'redeemTimeFinal' => $redeemTimeFinal,
  174. 'checkTimeBegin' => $checkTimeBegin,
  175. 'checkTimeFinal' => $checkTimeFinal,
  176. 'getTimeBegin' => $getTimeBegin,
  177. 'getTimeFinal' => $getTimeFinal,
  178. 'mdate' => date('Y-m-d H:i:s'),
  179. 'oid' => $oid,
  180. ];
  181. // 調用資料庫(或者其他業務邏輯)
  182. $res = $this->activityManagementDb
  183. ->where('id', $id)
  184. ->update($data);
  185. $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
  186. $rc = $rc[0]->rc;
  187. // syslogact
  188. $this->syslogactManagementDb
  189. ->insert([
  190. 'type' => GeneralConst::LOG_ADMIN,
  191. 'func' => __FUNCTION__,
  192. 'k' => $oid,
  193. 'memoIn' => json_encode(['id' => $id, 'data' => $data], JSON_UNESCAPED_UNICODE),
  194. 'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
  195. 'cdate' => date("Y-m-d H:i:s"),
  196. ]);
  197. // 整理返回值並返回
  198. return $res;
  199. }
  200. public function draw($id, $oid)
  201. {
  202. // 取得該期獎號
  203. $drawNumbers = $this->activityManagementDb
  204. ->select(['drawNumbers'])
  205. ->where('id', $id)
  206. ->where('isDraw', '') // 尚未開獎
  207. ->where('isCheckBegin', '')
  208. ->where('isGetFinal', '')
  209. ->whereRaw("UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(drawTime, ' 00:00:00'))") // 開獎時間再次確認
  210. ->first()->toArray();
  211. $drawNumbers = explode(',', $drawNumbers['drawNumbers']);
  212. // 掃描[該期][狀態=1=未開獎]的訂單編號,並比對獎號更改狀態,應該是兩句 SQL 就夠了
  213. $receipts = $this->receiptManagementDb
  214. ->select([
  215. 'id',
  216. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranOrderNo), \"" . env('KK') . "\")) as tranOrderNo"),
  217. ])
  218. ->where('aid', $id) // 是指定活動的
  219. ->where('rStatus', GeneralConst::RSTATUS_INIT) // 是初始登錄完畢狀態的
  220. ->where('canGet', '') // 以防萬一連這個欄位也檢查
  221. ->get()->toArray();
  222. $draw_fail = [];
  223. $draw_done = [];
  224. foreach ($receipts as $r) {
  225. if (in_array(substr($r['tranOrderNo'], -3), $drawNumbers)) {
  226. $draw_done[] = $r['id'];
  227. } else {
  228. $draw_fail[] = $r['id'];
  229. }
  230. }
  231. $this->receiptManagementDb
  232. ->whereIn('id', $draw_fail)
  233. ->where('aid', $id) // 是指定活動的
  234. ->where('rStatus', GeneralConst::RSTATUS_INIT) // 是初始登錄完畢狀態的
  235. ->where('canGet', '') // 以防萬一連這個欄位也檢查
  236. ->update([
  237. 'rStatus' => GeneralConst::RSTATUS_DRAW_FAIL,
  238. 'mdate' => date('Y-m-d H:i:s'),
  239. 'oid' => $oid,
  240. ]);
  241. $this->receiptManagementDb
  242. ->whereIn('id', $draw_done)
  243. ->where('aid', $id) // 是指定活動的
  244. ->where('rStatus', GeneralConst::RSTATUS_INIT) // 是初始登錄完畢狀態的
  245. ->where('canGet', '') // 以防萬一連這個欄位也檢查
  246. ->update([
  247. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE,
  248. 'mdate' => date('Y-m-d H:i:s'),
  249. 'oid' => $oid,
  250. ]);
  251. // 更改開獎狀態位
  252. $this->activityManagementDb
  253. ->where('id', $id)
  254. ->where('isDraw', '')
  255. ->where('isCheckBegin', '')
  256. ->where('isGetFinal', '')
  257. ->whereRaw("UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(drawTime, ' 00:00:00'))") // 開獎時間再次確認
  258. ->update([
  259. 'isDraw' => 'Y',
  260. 'mdate' => date('Y-m-d H:i:s'),
  261. 'oid' => $oid,
  262. ]);
  263. // syslogact
  264. $data = [
  265. 'aid' => $id,
  266. 'drawNumbers' => $drawNumbers,
  267. 'draw_fail' => $draw_fail,
  268. 'draw_done' => $draw_done,
  269. ];
  270. $this->syslogactManagementDb
  271. ->insert([
  272. 'type' => GeneralConst::LOG_ADMIN,
  273. 'func' => __FUNCTION__,
  274. 'k' => $oid,
  275. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  276. 'memoOut' => json_encode([], JSON_UNESCAPED_UNICODE),
  277. 'cdate' => date("Y-m-d H:i:s"),
  278. ]);
  279. return true;
  280. }
  281. public function check($id, $oid)
  282. {
  283. // 取得該期ID (有點像脫褲子放屁,但這是為了確認時間是否符合以及各項狀態位的管控)
  284. $chk = $this->activityManagementDb
  285. ->select(['id'])
  286. ->where('id', $id)
  287. ->where('isDraw', 'Y') // 已開獎
  288. ->where('isCheckBegin', '') // 未啟動驗證
  289. ->whereRaw("UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(checkTimeBegin, ' 00:00:00'))") // 驗證流程時間再次確認
  290. ->get();
  291. if (count($chk) == 0) return false; // 碰到這種情況按完鈕後不會有反應,且操作人也不知為何,就需要工程師介入
  292. // 取得該期所有狀態位為已中獎但未送出驗證的紀錄
  293. $receipts = $this->receiptManagementDb
  294. ->select([
  295. 'id',
  296. ])
  297. ->where('aid', $id) // 是指定活動的
  298. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE) // 是已中獎但未送出驗證的紀錄的
  299. ->where('canGet', '') // 以防萬一連這個欄位也檢查
  300. ->get()->toArray();
  301. $draw_done_expired = [];
  302. foreach ($receipts as $r) $draw_done_expired[] = $r['id'];
  303. $this->receiptManagementDb
  304. ->whereIn('id', $draw_done_expired)
  305. ->where('aid', $id) // 是指定活動的
  306. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE) // 是已中獎但未送出驗證的紀錄的
  307. ->where('canGet', '') // 以防萬一連這個欄位也檢查
  308. ->update([
  309. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_EXPIRED,
  310. 'mdate' => date('Y-m-d H:i:s'),
  311. 'oid' => $oid,
  312. ]);
  313. // 更改驗證狀態位
  314. $this->activityManagementDb
  315. ->where('id', $id)
  316. ->where('isDraw', 'Y')
  317. ->where('isCheckBegin', '')
  318. ->whereRaw("UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(checkTimeBegin, ' 00:00:00'))") // 開獎時間再次確認
  319. ->update([
  320. 'isCheckBegin' => 'Y',
  321. 'mdate' => date('Y-m-d H:i:s'),
  322. 'oid' => $oid,
  323. ]);
  324. // syslogact
  325. $data = [
  326. 'aid' => $id,
  327. 'draw_done_expired' => $draw_done_expired,
  328. ];
  329. $this->syslogactManagementDb
  330. ->insert([
  331. 'type' => GeneralConst::LOG_ADMIN,
  332. 'func' => __FUNCTION__,
  333. 'k' => $oid,
  334. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  335. 'memoOut' => json_encode([], JSON_UNESCAPED_UNICODE),
  336. 'cdate' => date("Y-m-d H:i:s"),
  337. ]);
  338. return true;
  339. }
  340. public function get($id, $oid)
  341. {
  342. // 取得該期ID (有點像脫褲子放屁,但這是為了確認時間是否符合以及各項狀態位的管控)
  343. $chk = $this->activityManagementDb
  344. ->select(['id'])
  345. ->where('id', $id)
  346. ->where('isDraw', 'Y') // 已開獎
  347. ->where('isCheckBegin', 'Y') // 已啟動驗證
  348. ->where('isGetFinal', '') // 未關閉活動
  349. ->whereRaw("UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(getTimeFinal, ' 00:00:00'))") // 領獎時間再次確認
  350. ->get();
  351. if (count($chk) == 0) return false; // 碰到這種情況按完鈕後不會有反應,且操作人也不知為何,就需要工程師介入
  352. // 取得該期所有狀態位為已中獎但未送出驗證的紀錄
  353. $receipts = $this->receiptManagementDb
  354. ->select([
  355. 'id',
  356. ])
  357. ->where('aid', $id) // 是指定活動的
  358. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE) // 是已驗證通過但未領取的
  359. ->where('canGet', 'Y') // 以防萬一連這個欄位也檢查
  360. ->get()->toArray();
  361. $get_done_expired = [];
  362. foreach ($receipts as $r) $get_done_expired[] = $r['id'];
  363. $this->receiptManagementDb
  364. ->whereIn('id', $get_done_expired)
  365. ->where('aid', $id) // 是指定活動的
  366. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE) // 是已驗證通過但未領取的
  367. ->where('canGet', 'Y') // 以防萬一連這個欄位也檢查
  368. ->update([
  369. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_EXPIRED,
  370. 'mdate' => date('Y-m-d H:i:s'),
  371. 'oid' => $oid,
  372. ]);
  373. // 更改驗證狀態位
  374. $this->activityManagementDb
  375. ->where('id', $id)
  376. ->where('isDraw', 'Y')
  377. ->where('isCheckBegin', 'Y')
  378. ->where('isGetFinal', '')
  379. ->whereRaw("UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(getTimeFinal, ' 00:00:00'))") // 領獎時間再次確認
  380. ->update([
  381. 'isGetFinal' => 'Y',
  382. 'mdate' => date('Y-m-d H:i:s'),
  383. 'oid' => $oid,
  384. ]);
  385. // syslogact
  386. $data = [
  387. 'aid' => $id,
  388. 'redeem_done_expired' => $get_done_expired,
  389. ];
  390. $this->syslogactManagementDb
  391. ->insert([
  392. 'type' => GeneralConst::LOG_ADMIN,
  393. 'func' => __FUNCTION__,
  394. 'k' => $oid,
  395. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  396. 'memoOut' => json_encode([], JSON_UNESCAPED_UNICODE),
  397. 'cdate' => date("Y-m-d H:i:s"),
  398. ]);
  399. return true;
  400. }
  401. }