CardService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public static function checkExpired()
  34. {
  35. $unixTime = time();
  36. $cards = Card::whereRaw("CAST(UNIX_TIMESTAMP(card_deadline_datetime) as SIGNED) <= {$unixTime}")
  37. ->whereRaw("CAST(UNIX_TIMESTAMP(card_deadline_datetime) as SIGNED) > 0")
  38. ->whereIn('card_status', ['INIT', 'WAITING', 'PENDING', 'USED'])
  39. ->select('card_id')
  40. ->get()
  41. ->toArray();
  42. foreach ($cards as $card) {
  43. // 检查是否关联会员账户
  44. $account = MemberAccount::where('member_account_nbr', $card['card_id'])->first();
  45. if (!empty($account) && $account->member_account_status == 'ACTIVED') {
  46. $account->member_account_status = 'EXPIRED';
  47. $account->save();
  48. }
  49. $cardNow = Card::where('card_id', $card['card_id'])->first();
  50. $cardNow->card_status = 'EXPIRED';
  51. $cardExtendJson = [];
  52. if (!empty($cardNow->card_extend_json)) {
  53. $cardExtendJson = json_decode($cardNow->card_extend_json, true);
  54. }
  55. $cardExtendJson['expired_datetime'] = date('Y-m-d H:i:s');
  56. $cardNow->card_extend_json = json_encode($cardExtendJson);
  57. $cardNow->save();
  58. Log::info("储值卡:【" . $card['card_id'] . '】已过期');
  59. }
  60. }
  61. public static $status = [
  62. 'INIT' => '待分配',
  63. 'WAITING' => '已分配,待售',
  64. 'PENDING' => '已售,待激活',
  65. 'USED' => '已激活',
  66. 'DONE' => '使用完成',
  67. 'EXPIRED' => '已过期',
  68. 'PAUSED' => '冻结'
  69. ];
  70. }