CardService.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\service\coupon;
  3. use app\model\Card;
  4. use app\model\MemberAccount;
  5. use support\Log;
  6. use support\exception\BusinessException;
  7. class CardService
  8. {
  9. /**
  10. * @Desc 生成卡ID
  11. * @Author Gorden
  12. * @Date 2024/11/25 13:46
  13. *
  14. * @param $params
  15. * @param $i
  16. * @return false|string
  17. */
  18. public static function generateCardId($params, $i)
  19. {
  20. $prefixLength = strlen($params['card_issue_serial_prefix']);
  21. $suffixLength = strlen($params['card_issue_serial_suffix']);
  22. $cardId = $params['card_issue_serial_prefix'] . str_pad($params['card_issue_serial_begin'] + $i, $params['card_issue_serial_length'] - $prefixLength - $suffixLength, $params['card_issue_serial_fill'], STR_PAD_LEFT) . $params['card_issue_serial_suffix'];
  23. if (!empty($params['card_issue_not_contain'])) {
  24. $notContains = explode(',', $params['card_issue_not_contain']);
  25. foreach ($notContains as $notContain) {
  26. if (strpos($cardId, $notContain) !== false) {
  27. return false;
  28. }
  29. }
  30. }
  31. return $cardId;
  32. }
  33. /**
  34. * @Desc 储值卡过期
  35. * @Author Gorden
  36. * @Date 2024/12/6 14:38
  37. *
  38. * @return void
  39. */
  40. public static function checkExpired()
  41. {
  42. $unixTime = time();
  43. $cards = Card::whereRaw("CAST(UNIX_TIMESTAMP(card_deadline_datetime) as SIGNED) <= {$unixTime}")
  44. ->whereRaw("CAST(UNIX_TIMESTAMP(card_deadline_datetime) as SIGNED) > 0")
  45. ->whereIn('card_status', ['INIT', 'WAITING', 'PENDING', 'USED'])
  46. ->select('card_id')
  47. ->get()
  48. ->toArray();
  49. foreach ($cards as $card) {
  50. // 检查是否关联会员账户
  51. $account = MemberAccount::where('member_account_nbr', $card['card_id'])->first();
  52. if (!empty($account) && $account->member_account_status == 'ACTIVED') {
  53. $account->member_account_status = 'EXPIRED';
  54. $account->save();
  55. }
  56. $cardNow = Card::where('card_id', $card['card_id'])->first();
  57. $cardNow->card_status = 'EXPIRED';
  58. $cardExtendJson = [];
  59. if (!empty($cardNow->card_extend_json)) {
  60. $cardExtendJson = json_decode($cardNow->card_extend_json, true);
  61. }
  62. $cardExtendJson['expired_datetime'] = date('Y-m-d H:i:s');
  63. $cardNow->card_extend_json = json_encode($cardExtendJson);
  64. $cardNow->save();
  65. _syslog('储值卡',"【" . $card['card_id'] . '】已过期',false,['card_id'=>$card['card_id']],1001);
  66. Log::info("储值卡:【" . $card['card_id'] . '】已过期');
  67. }
  68. }
  69. public static $status = [
  70. 'INIT' => '待分配',
  71. 'WAITING' => '已分配,待售',
  72. 'PENDING' => '已售,待激活',
  73. 'USED' => '已激活',
  74. 'DONE' => '使用完成',
  75. 'EXPIRED' => '已过期',
  76. 'PAUSED' => '冻结'
  77. ];
  78. }