RealCard.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. namespace addon\giftcard\model\card;
  3. use addon\giftcard\model\giftcard\CardStat;
  4. use addon\giftcard\model\giftcard\GiftCard;
  5. use addon\giftcard\model\membercard\MemberCard;
  6. use think\facade\Cache;
  7. /**
  8. * 实体卡(线下)
  9. * Class GiftCardRecords
  10. * @package addon\giftcard\model\records
  11. */
  12. class RealCard extends Card
  13. {
  14. public function addCard($params)
  15. {
  16. $source = $params[ 'source' ] ?? '';
  17. $card_right_goods_type = $params[ 'card_right_goods_type' ];
  18. $card_right_goods_count = $params[ 'card_right_goods_count' ];
  19. $insert_data = array (
  20. 'card_name' => $params[ 'card_name' ] ?? '',
  21. 'card_cover' => $params[ 'card_cover' ] ?? '',
  22. 'card_right_goods_type' => $card_right_goods_type,
  23. 'card_right_goods_count' => $card_right_goods_count,
  24. 'card_cdk' => $params[ 'card_cdk' ],
  25. 'status' => 'to_activate',
  26. 'card_import_id' => $params[ 'import_id' ]
  27. );
  28. $params[ 'card_type' ] = 'real';
  29. $params[ 'insert_data' ] = $insert_data;
  30. $card_prefix = $params[ 'card_prefix' ];
  31. $card_suffix = $params[ 'card_suffix' ];
  32. $card_no = $params[ 'card_no' ] ?? $this->createNo($params[ 'site_id' ], $member_id ?? 0, $card_prefix, $card_suffix);
  33. $params[ 'card_no' ] = $card_no;
  34. $result = $this->addCardItem($params);
  35. if ($result[ 'code' ] >= 0) {
  36. $card_id = $result[ 'data' ];
  37. ( new CardLog() )->add([
  38. 'card_id' => $card_id,
  39. 'type' => 'create',
  40. 'operator_type' => 'shop',//todo 暂时是确定的
  41. 'operator_data' => $params[ 'operator_data' ],
  42. 'type_id' => $params[ 'import_id' ]
  43. ]);
  44. }
  45. return $result;
  46. }
  47. /**
  48. * 制卡
  49. * @param $params
  50. * @return array
  51. */
  52. public function cdkLog($params)
  53. {
  54. set_time_limit(0);
  55. ini_set('memory_limit', '512M');
  56. $import_id = $params[ 'import_id' ];
  57. $site_id = $params[ 'site_id' ] ?? 0;
  58. $card_import_model = new CardImport();
  59. $import_condition = array (
  60. [ 'import_id', '=', $import_id ],
  61. );
  62. if ($site_id > 0) {
  63. $import_condition[] = [ 'site_id', '=', $site_id ];
  64. }
  65. $import_info = $card_import_model->getCardImportInfo($import_condition)[ 'data' ] ?? [];
  66. if (empty($import_info))
  67. return $this->error();
  68. $type = $import_info[ 'type' ];
  69. $giftcard_id = $import_info[ 'giftcard_id' ];
  70. $card_type = $import_info[ 'card_type' ];
  71. $giftcard_model = new GiftCard();
  72. $condition = [ [ 'giftcard_id', '=', $giftcard_id ] ];
  73. $info = $giftcard_model->getGiftcardInfo($condition)[ 'data' ] ?? [];
  74. if (empty($info))
  75. return $this->error();
  76. $info[ 'operator_data' ] = $params[ 'operator_data' ];
  77. if ($card_type != 'real') {
  78. return $this->error('', '该礼品不支持制卡');
  79. }
  80. if (empty($type) && !in_array($type, [ 'auto', 'manual', 'import' ]))
  81. return $this->error();
  82. $data = array (
  83. 'site_id' => $info[ 'site_id' ],
  84. 'card_type' => $info[ 'card_type' ],
  85. 'giftcard_id' => $info[ 'giftcard_id' ],
  86. 'create_time' => time(),
  87. 'card_right_type' => $info[ 'card_right_type' ],
  88. 'valid_time' => $this->getValidityTime($info),
  89. 'balance' => $info[ 'balance' ] ?? 0,
  90. 'card_right_goods_type' => $info[ 'card_right_goods_type' ] ?? '',
  91. 'card_right_goods_count' => $info[ 'card_right_goods_count' ] ?? '',
  92. 'card_name' => $info[ 'card_name' ] ?? '',
  93. 'card_cover' => $info[ 'card_cover' ] ?? '',
  94. 'status' => 'to_activate',
  95. 'card_import_id' => $import_id
  96. );
  97. $info[ 'card_data' ] = $data;
  98. $info[ 'import_id' ] = $import_id;
  99. switch ( $type ) {
  100. case 'auto':
  101. $info[ 'num' ] = $import_info[ 'total_count' ] ?? 0;
  102. $result = $this->createCdk($info);
  103. break;
  104. case 'manual':
  105. $card_cdk = $params[ 'card_cdk' ];
  106. $info[ 'card_cdk' ] = $card_cdk;
  107. $result = $this->addCdk($info);
  108. break;
  109. case 'import':
  110. $info[ 'num' ] = $import_info[ 'total_count' ];
  111. $result = $this->importCdk($info);
  112. break;
  113. }
  114. //制卡统计
  115. ( new CardStat() )->stat([ 'stat_type' => 'create', 'giftcard_id' => $giftcard_id, 'num' => $result[ 'data' ][ 'success_count' ] ?? 0 ]);
  116. return $result;
  117. }
  118. public function addCdk($params)
  119. {
  120. $result = $this->addCard($params);
  121. $total_count = 1;
  122. if ($result[ 'code' ] < 0) {
  123. //生成卡密失败后,继续还是退出
  124. $fail_count = 1;
  125. $success_count = 0;
  126. $error = $result[ 'message' ];
  127. } else {
  128. $fail_count = 0;
  129. $success_count = 1;
  130. }
  131. $import_id = $params[ 'import_id' ] ?? 0;
  132. if ($import_id > 0) {
  133. model('giftcard_card_import')->setInc([ [ 'import_id', '=', $import_id ] ], 'imported_count', 1);
  134. $card_import_model = new CardImport();
  135. $card_import_model->update([
  136. 'import_time' => time(),
  137. // 'total_count' => $total_count,
  138. 'fail_count' => $fail_count,
  139. 'success_count' => $success_count,
  140. 'card_cdk' => $params[ 'card_cdk' ],
  141. 'error' => $error ?? ''
  142. ], [ [ 'import_id', '=', $import_id ] ]);
  143. }
  144. return $this->success([ 'total_count' => $total_count, 'fail_count' => $fail_count, 'success_count' => $success_count, 'error' => $error ?? '' ]);
  145. }
  146. public function createCdk($params)
  147. {
  148. $num = $params[ 'num' ];//生成卡密数量(一般上限一次1000个)
  149. $num_dict = '0123456789';
  150. $latter_dict = 'abcdefghijklmnopqrstuvwxyz';
  151. $big_latter_dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  152. $cdk_length = $params[ 'cdk_length' ];
  153. $card_prefix = $params[ 'card_prefix' ];
  154. $card_suffix = $params[ 'card_suffix' ];
  155. // $cdk_prefix_length = strlen($cdk_prefix);
  156. // $cdk_suffix_length = strlen($cdk_suffix);
  157. // $length = $cdk_length - $cdk_prefix_length - $cdk_suffix_length;
  158. $length = $cdk_length;
  159. $cdk_type = $params[ 'cdk_type' ];
  160. $dict = '';
  161. if (strstr($cdk_type, '0-9')) {
  162. $dict .= $num_dict;
  163. }
  164. if (strstr($cdk_type, 'a-z')) {
  165. $dict .= $latter_dict;
  166. }
  167. if (strstr($cdk_type, 'A-Z')) {
  168. $dict .= $big_latter_dict;
  169. }
  170. $dict_len = strlen($dict) - 1;
  171. $start_num = 1;
  172. $total_count = $num;
  173. $fail_count = 0;
  174. $success_count = 0;
  175. $import_id = $params[ 'import_id' ] ?? 0;
  176. $import_info = model('giftcard_card_import')->getInfo([ [ 'import_id', '=', $import_id ] ], "*");
  177. Cache::set("card_import_log_" . $import_id, $import_info);
  178. $common_data = $params[ 'card_data' ];
  179. $insert_data = array ();
  180. while ($start_num <= $num) {
  181. $randstr = '';
  182. for ($i = 0; $i < $length; $i++) {
  183. $temp_num = mt_rand(0, $dict_len);
  184. $randstr .= $dict[ $temp_num ];
  185. }
  186. $card_cdk = $randstr;
  187. $success_count++;
  188. $item_data = $common_data;
  189. $item_data[ 'card_no' ] = $this->createNo($params[ 'site_id' ], $import_id ?? 0, $card_prefix, $card_suffix);
  190. $item_data[ 'card_cdk' ] = $card_cdk;
  191. $insert_data[] = $item_data;
  192. if (( $start_num % 100 ) == 0) {
  193. model('giftcard_card')->addList($insert_data);
  194. model('giftcard_card_import')->update([ 'imported_count' => $start_num ], [ [ 'import_id', '=', $import_id ] ]);
  195. $insert_data = [];
  196. } else if ($start_num >= $num) {
  197. model('giftcard_card')->addList($insert_data);
  198. model('giftcard_card_import')->update([ 'imported_count' => $start_num ], [ [ 'import_id', '=', $import_id ] ]);
  199. $insert_data = [];
  200. }
  201. $start_num++;
  202. }
  203. model('giftcard_card_import')->update([ 'imported_count' => $import_info[ 'total_count' ] ], [ [ 'import_id', '=', $import_id ] ]);
  204. if ($import_id > 0) {
  205. $card_import_model = new CardImport();
  206. $card_import_model->update([
  207. 'cdk_length' => $cdk_length,
  208. 'card_prefix' => $card_prefix,
  209. 'card_suffix' => $card_suffix,
  210. 'cdk_type' => $cdk_type,
  211. 'import_time' => time(),
  212. // 'total_count' => $total_count,
  213. 'fail_count' => $fail_count,
  214. 'success_count' => $success_count,
  215. ], [ [ 'import_id', '=', $import_id ] ]);
  216. }
  217. return $this->success([ 'total_count' => $total_count, 'fail_count' => $fail_count, 'success_count' => $success_count ]);
  218. }
  219. public function importCdk($params)
  220. {
  221. //之后可以配合匿名函数封装公共函数
  222. $total_count = 0;
  223. $fail_count = 0;
  224. $success_count = 0;
  225. $import_id = $params[ 'import_id' ] ?? 0;
  226. $file_path = 'upload/giftcard/giftcard_card_import' . $import_id . '.csv';
  227. $import_info = model('giftcard_card_import')->getInfo([ [ 'import_id', '=', $import_id ] ], "*");
  228. Cache::set("card_import_log_" . $import_id, $import_info);
  229. $common_data = $params[ 'card_data' ];
  230. $card_no_array = [];
  231. foreach (getCsvRow($file_path) as $row) {
  232. if (!empty($row)) {
  233. $total_count++;
  234. if ($total_count > 1) {
  235. $card_cdk = $row[ 1 ] ?? '';
  236. $card_no = $row[ 0 ] ?? '';
  237. $card_cdk = trim($card_cdk);
  238. $card_no = trim($card_no);
  239. if (!empty($card_no) && !empty($card_cdk)) {
  240. $params[ 'card_cdk' ] = $card_cdk;
  241. $params[ 'card_no' ] = $card_no;
  242. // $result = $this->addCard($params);
  243. // if ($result['code'] < 0) {
  244. // //生成卡密失败后,继续还是退出
  245. // $fail_count++;
  246. // } else {
  247. // $success_count++;
  248. // }
  249. $item_data = $common_data;
  250. $item_data[ 'card_no' ] = $card_no;
  251. $card_no_array[] = $card_no;
  252. $item_data[ 'card_cdk' ] = $card_cdk;
  253. $insert_data[] = $item_data;
  254. if (( $total_count % 100 ) == 0) {
  255. $column = model('giftcard_card')->getColumn([ [ 'card_no', 'in', $card_no_array ] ], 'card_no');
  256. $column = array_unique($column);
  257. foreach ($insert_data as $k => $v) {
  258. if (in_array($v[ 'card_no' ], $column)) {
  259. unset($insert_data[ $k ]);
  260. $fail_count++;
  261. }
  262. }
  263. model('giftcard_card')->addList($insert_data);
  264. model('giftcard_card_import')->update([ 'imported_count' => $total_count ], [ [ 'import_id', '=', $import_id ] ]);
  265. $success_count += count($insert_data);
  266. $insert_data = [];
  267. $card_no_array = [];
  268. }
  269. } else {
  270. $fail_count++;
  271. }
  272. }
  273. }
  274. }
  275. $column = model('giftcard_card')->getColumn([ [ 'card_no', 'in', $card_no_array ] ], 'card_no');
  276. $column = array_unique($column);
  277. foreach ($insert_data as $k => $v) {
  278. if (in_array($v[ 'card_no' ], $column)) {
  279. unset($insert_data[ $k ]);
  280. $fail_count++;
  281. }
  282. }
  283. $success_count += count($insert_data);
  284. //最后一次补充提交
  285. model('giftcard_card')->addList($insert_data);
  286. model('giftcard_card_import')->update([ 'imported_count' => $import_info[ 'total_count' ] ], [ [ 'import_id', '=', $import_id ] ]);
  287. if ($import_id > 0) {
  288. $original_name = Cache::get('giftcard/giftcard_card_import_name' . $import_id);//文件原名
  289. $card_import_model = new CardImport();
  290. $card_import_model->update([
  291. 'import_time' => time(),
  292. // 'total_count' => $total_count,
  293. 'fail_count' => $fail_count,
  294. 'success_count' => $success_count,
  295. 'file_name' => $original_name
  296. ], [ [ 'import_id', '=', $import_id ] ]);
  297. }
  298. return $this->success([ 'total_count' => $total_count, 'fail_count' => $fail_count, 'success_count' => $success_count ]);
  299. }
  300. /**
  301. * 会员激活卡密
  302. * @param $params
  303. * @return array
  304. */
  305. public function memberCardActivate($params)
  306. {
  307. $card_no = $params[ 'card_no' ];
  308. $card_cdk = $params[ 'card_cdk' ];
  309. $site_id = $params[ 'site_id' ] ?? 0;
  310. $member_id = $params[ 'member_id' ];
  311. $card_condition = array (
  312. [ 'gc.card_no', '=', $card_no ],
  313. [ 'gc.card_cdk', '=', $card_cdk ],
  314. [ 'gc.member_id', '=', 0 ],
  315. [ 'g.is_delete', '=', 0 ],
  316. );
  317. if ($site_id > 0) {
  318. $card_condition[] = [ 'gc.site_id', '=', $site_id ];
  319. }
  320. $card_info = $this->getCardInfo($card_condition, 'gc.*,g.is_delete,g.status as giftcard_status', 'gc', [
  321. [ 'giftcard g', 'gc.giftcard_id = g.giftcard_id', 'inner' ]
  322. ])[ 'data' ] ?? [];
  323. if (empty($card_info))
  324. return $this->error([], '当前卡密无效或已被激活');
  325. if (empty($card_info[ 'giftcard_status' ]))
  326. return $this->error([], '当前礼品卡已下架');
  327. $card_id = $card_info[ 'card_id' ];
  328. $result = $this->activate([ 'card_id' => $card_id, 'member_id' => $member_id, 'site_id' => $site_id ]);
  329. return $result;
  330. }
  331. /**
  332. * 激活卡密
  333. * @param $params
  334. */
  335. public function activate($params)
  336. {
  337. $site_id = $params[ 'site_id' ] ?? 0;
  338. $member_id = $params[ 'member_id' ];
  339. $card_id = $params[ 'card_id' ];
  340. $card_condition = array (
  341. [ 'card_id', '=', $card_id ],
  342. [ 'member_id', '=', 0 ]
  343. );
  344. if ($site_id > 0) {
  345. $card_condition[] = [ 'site_id', '=', $site_id ];
  346. }
  347. $card_model = new Card();
  348. $card_info = $card_model->getCardInfo($card_condition)[ 'data' ] ?? [];
  349. if (empty($card_info))
  350. return $this->error();
  351. $giftcard_model = new GiftCard();
  352. $giftcard_id = $card_info[ 'giftcard_id' ];
  353. $condition = [
  354. [ 'giftcard_id', '=', $giftcard_id ]
  355. ];
  356. $info = $giftcard_model->getGiftcardInfo($condition)[ 'data' ] ?? [];
  357. if (empty($info))
  358. return $this->error();
  359. $data = array (
  360. 'card_type' => $info[ 'card_type' ],
  361. 'card_right_type' => $info[ 'card_right_type' ],
  362. 'valid_time' => $this->getValidityTime($info),
  363. 'balance' => $info[ 'balance' ] ?? 0,
  364. 'card_right_goods_type' => $info[ 'card_right_goods_type' ] ?? '',
  365. 'card_right_goods_count' => $info[ 'card_right_goods_count' ] ?? '',
  366. 'card_name' => $info[ 'card_name' ] ?? '',
  367. 'card_cover' => $info[ 'card_cover' ] ?? ''
  368. );
  369. model('giftcard_card')->update($data, $card_condition);
  370. //需要更新卡项数据的
  371. if ($info[ 'card_right_type' ] == 'goods') {
  372. $goods_list = $giftcard_model->getGiftcardGoodsList([ [ 'giftcard_id', '=', $giftcard_id ] ], 'cg.*,gs.sku_name,gs.sku_image,gs.goods_name,gs.sku_no,gs.price', '', 'cg', [
  373. [ 'goods_sku gs', 'cg.sku_id=gs.sku_id', 'inner' ]
  374. ])[ 'data' ] ?? [];
  375. foreach ($goods_list as $k => $v) {
  376. if ($info[ 'card_right_goods_type' ] == 'all') {
  377. $goods_list[ $k ][ 'num' ] = 0;
  378. } else {
  379. $goods_list[ $k ][ 'num' ] = $v[ 'goods_num' ];
  380. }
  381. }
  382. } else {
  383. $goods_list = array (
  384. [
  385. 'site_id' => $info[ 'site_id' ],
  386. 'giftcard_id' => $giftcard_id,
  387. 'sku_name' => $info[ 'card_name' ],
  388. 'sku_image' => $info[ 'card_cover' ],
  389. 'goods_name' => $info[ 'card_name' ],
  390. 'balance' => $info[ 'balance' ],//储值余额
  391. 'total_balance' => $info[ 'balance' ],
  392. 'total_num' => 1,//购买数量
  393. ]
  394. );
  395. }
  396. foreach ($goods_list as $k => $v) {
  397. $v[ 'card_id' ] = $card_id;
  398. $v[ 'giftcard_id' ] = $giftcard_id;
  399. $v[ 'card_right_type' ] = $info[ 'card_right_type' ];
  400. $this->addCardItemGoods($v);
  401. }
  402. $data = array (
  403. 'status' => 'to_use',
  404. 'init_member_id' => $member_id,
  405. 'member_id' => $member_id,
  406. 'activate_time' => time()
  407. );
  408. model('giftcard_card')->update($data, $card_condition);
  409. //生成会员所属记录
  410. $member_card_model = new MemberCard();
  411. $card_params = array (
  412. 'site_id' => $site_id,
  413. 'form_member_id' => 0,
  414. 'member_id' => $member_id,
  415. 'card_id' => $card_id,
  416. 'source' => '',
  417. );
  418. $result = $member_card_model->addMemberCard($card_params);
  419. //数据统计
  420. ( new CardStat() )->stat(array_merge($params, [ 'stat_type' => 'activate' ]));
  421. return $result;
  422. }
  423. }