CardService.php 9.7 KB

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