CardService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace app\admin\service\coupon;
  3. use app\model\Card;
  4. use app\model\CardMain;
  5. use app\model\Goods;
  6. use app\model\MemberAccount;
  7. use app\model\MemberAccountList;
  8. use app\model\SysCategory;
  9. use support\Db;
  10. use support\Log;
  11. use support\exception\BusinessException;
  12. class CardService
  13. {
  14. /**
  15. * @Desc 生成卡ID
  16. * @Author Gorden
  17. * @Date 2024/11/25 13:46
  18. *
  19. * @param $params
  20. * @param $i
  21. * @return false|string
  22. */
  23. public static function generateCardId($params, $i)
  24. {
  25. $prefixLength = strlen($params['card_issue_serial_prefix']);
  26. $suffixLength = strlen($params['card_issue_serial_suffix']);
  27. $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'];
  28. if (!empty($params['card_issue_not_contain'])) {
  29. $notContains = explode(',', $params['card_issue_not_contain']);
  30. foreach ($notContains as $notContain) {
  31. if (strpos($cardId, $notContain) !== false) {
  32. return false;
  33. }
  34. }
  35. }
  36. return $cardId;
  37. }
  38. /**
  39. * @Desc 生成按需卡号
  40. * @Author Gorden
  41. * @Date 2024/12/9 15:02
  42. *
  43. * @param $demandConfig
  44. * @param $lastCard
  45. * @return string
  46. */
  47. public static function generateDemandCardId($demandConfig, $lastCard)
  48. {
  49. $prefixLength = strlen($demandConfig['prefix']);
  50. $suffixLength = strlen($demandConfig['suffix']);
  51. $rand = $demandConfig['rand'] ?? 0;
  52. $begin = $demandConfig['begin'] ?? 1;
  53. if (!empty($lastCard)) {
  54. $oldCardId = $lastCard->card_id;
  55. $oldCardId = ltrim($oldCardId, $demandConfig['prefix']);
  56. $oldCardId = rtrim($oldCardId, $demandConfig['suffix']);
  57. $oldCardId = ltrim($oldCardId, $demandConfig['rand']);
  58. $number = intval($oldCardId) + 1;
  59. } else {
  60. $number = $begin;
  61. }
  62. return $demandConfig['prefix'] . str_pad($number, $demandConfig['length'] - $prefixLength - $suffixLength, $rand, STR_PAD_LEFT) . $demandConfig['suffix'];
  63. }
  64. /**
  65. * @Desc 储值卡过期
  66. * @Author Gorden
  67. * @Date 2024/12/6 14:38
  68. *
  69. * @return void
  70. */
  71. public static function checkExpired()
  72. {
  73. $unixTime = time();
  74. $cards = Card::whereRaw("CAST(UNIX_TIMESTAMP(card_deadline_datetime) as SIGNED) <= {$unixTime}")
  75. ->whereRaw("CAST(UNIX_TIMESTAMP(card_deadline_datetime) as SIGNED) > 0")
  76. ->whereIn('card_status', ['INIT', 'WAITING', 'PENDING', 'USED'])
  77. ->select('card_id')
  78. ->get()
  79. ->toArray();
  80. foreach ($cards as $card) {
  81. // 检查是否关联会员账户
  82. $account = MemberAccount::where('member_account_nbr', $card['card_id'])->first();
  83. if (!empty($account) && $account->member_account_status == 'ACTIVED') {
  84. $account->member_account_status = 'EXPIRED';
  85. $account->save();
  86. }
  87. $cardNow = Card::where('card_id', $card['card_id'])->first();
  88. $cardNow->card_status = 'EXPIRED';
  89. $cardExtendJson = [];
  90. if (!empty($cardNow->card_extend_json)) {
  91. $cardExtendJson = json_decode($cardNow->card_extend_json, true);
  92. }
  93. $cardExtendJson['expired_datetime'] = date('Y-m-d H:i:s');
  94. $cardNow->card_extend_json = json_encode($cardExtendJson);
  95. $cardNow->save();
  96. _syslog('储值卡', "【" . $card['card_id'] . '】已过期', false, ['card_id' => $card['card_id']], 1001);
  97. Log::info("储值卡:【" . $card['card_id'] . '】已过期');
  98. }
  99. }
  100. /**
  101. * @Desc 按需发卡
  102. * @Author Gorden
  103. * @Date 2024/12/9 16:02
  104. *
  105. * @param $memberId
  106. * @param $good
  107. * @param $params
  108. * @return string
  109. * @throws BusinessException
  110. */
  111. public static function sendDemandCard($memberId, $good, $params)
  112. {
  113. try {
  114. $goodsModel = Goods::where('goods_id', $good['goods_id'])->select('goods_attribute_json')->first();
  115. $goodsAttributeJson = json_decode($goodsModel->goods_attribute_json, true);
  116. if (!empty($goodsAttributeJson['card_main_id'])) {
  117. $cardMain = CardMain::where('card_main_id', $goodsAttributeJson['card_main_id'])->first();
  118. $cardMainJson = json_decode($cardMain->card_main_json, true);
  119. if ($cardMainJson['issue'] == 'DEMAND') {
  120. return self::issueDemandCard($memberId, $goodsAttributeJson['card_main_id'], $params, $good);;
  121. }
  122. }
  123. return false;
  124. } catch (\Exception $e) {
  125. throw new BusinessException("储值卡发行失败");
  126. }
  127. }
  128. /**
  129. * @Desc 发行按需卡
  130. * @Author Gorden
  131. * @Date 2024/12/9 15:27
  132. *
  133. * @param $memberId
  134. * @param $cardMainId
  135. * @param $params
  136. * @param $good
  137. */
  138. public static function issueDemandCard($memberId, $cardMainId, $params, $good)
  139. {
  140. try {
  141. $lastCard = Card::where('join_card_main_id', $cardMainId)->orderByDesc('card_addtimes')->first();
  142. $cardMain = CardMain::where('card_main_id', $cardMainId)->first();
  143. $cardMainJson = json_decode($cardMain->card_main_json, true);
  144. $demandConfig = $cardMainJson['demand_config'];
  145. $cardId = self::generateDemandCardId($demandConfig, $lastCard);
  146. // 开卡账户
  147. $accountDuedate = '';
  148. if ($cardMain->card_main_valid_mode == 'LONG') {
  149. $accountDuedate = 0;
  150. } elseif ($cardMain->card_main_valid_mode == 'DAYS') {
  151. $accountDuedate = date('Y-m-d 23:59:59', strtotime("+ " . ($cardMain->card_main_valid_days - 1) . 'day'));
  152. } elseif ($cardMain->card_main_valid_mode == 'DATE') {
  153. $accountDuedate = $cardMain->card_main_valid_end;
  154. }
  155. $accountId = MemberAccount::insertGetId([
  156. 'join_account_member_id' => $memberId,
  157. 'member_account_classify' => 'CARD',
  158. 'member_account_status' => 'ACTIVED',
  159. 'member_account_category' => 'NORMAL',
  160. 'member_account_nbr' => $cardId,
  161. 'member_account_name' => $cardMain->card_main_name,
  162. 'member_account_income' => $cardMain->card_main_amount,
  163. 'member_account_surplus' => $cardMain->card_main_amount,
  164. 'member_account_duedate' => $accountDuedate,
  165. 'member_account_addtimes' => time()
  166. ]);
  167. // member_account_list 加卡充值记录
  168. MemberAccountList::insert([
  169. 'join_list_member_account_nbr' => $cardId,
  170. 'join_member_account_list_member_id' => $memberId,
  171. 'member_account_list_status' => 'ACTIVED',
  172. 'member_account_list_attr' => 'IN',
  173. 'member_account_list_classify' => 'CARD',
  174. 'member_account_list_category' => '绑卡',
  175. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  176. 'member_account_list_amount' => $cardMain->card_main_amount,
  177. 'member_account_list_addtimes' => time()
  178. ]);
  179. // 存卡数据
  180. $cardModel = new Card();
  181. $cardModel->card_id = $cardId;
  182. $cardModel->join_card_member_id = $memberId;
  183. $cardModel->join_card_member_account_id = $accountId;
  184. $cardModel->join_card_main_id = $cardMainId;
  185. $cardModel->card_assign_datetime = date('Y-m-d H:i:s');
  186. $cardModel->card_status = 'USED';
  187. $cardModel->card_json = json_encode([
  188. 'order_id' => $params['orderId'],
  189. 'goods_id' => $good['goods_id'],
  190. 'order_amount_pay' => $params['order_amount_pay']
  191. ]);
  192. $cardModel->is_issue = 'Y';
  193. $cardModel->card_extend_json = json_encode([
  194. 'buy_member' => $memberId,
  195. 'buy_datetime' => date('Y-m-d H:i:s'),
  196. 'buy_order_id' => $params['orderId'],
  197. 'used_datetime' => date('Y-m-d H:i:s')
  198. ]);
  199. $cardModel->card_addtimes = time();
  200. $cardModel->save();
  201. return $cardId;
  202. } catch (\Exception $e) {
  203. Log::error("生成卡号失败", ['msg' => $e->getMessage()]);
  204. throw new BusinessException("生成卡号失败");
  205. }
  206. }
  207. /**
  208. * @Desc 卡包含的所哟商品
  209. * @Author Gorden
  210. * @Date 2024/12/9 17:03
  211. *
  212. * @param $goodsId
  213. * @param $goodsCategory
  214. * @return array
  215. */
  216. public static function getCardGoods($goodsId, $goodsCategory)
  217. {
  218. $categoryId = [];
  219. foreach ($goodsCategory as $item) {
  220. array_push($categoryId, $item);
  221. $categoryIds = SysCategory::where('category_super_path', 'like', '#' . $item . '#')->pluck('category_id')->toArray();
  222. $categoryId = array_merge($categoryId, $categoryIds);
  223. }
  224. $goodsIds = Goods::whereIn('join_goods_category_id', $categoryId)->pluck('goods_id')->toArray();
  225. return array_unique(array_merge($goodsId,$goodsIds));
  226. }
  227. public static $status = [
  228. 'INIT' => '待分配',
  229. 'WAITING' => '已分配,待售',
  230. 'PENDING' => '已售,待激活',
  231. 'USED' => '已激活',
  232. 'DONE' => '使用完成',
  233. 'EXPIRED' => '已过期',
  234. 'PAUSED' => '冻结'
  235. ];
  236. }