MemberCardServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user\member;
  12. use app\dao\user\member\MemberCardDao;
  13. use app\services\BaseServices;
  14. use app\services\order\OtherOrderServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\SystemConfigService;
  18. use think\exception\ValidateException;
  19. use app\services\user\UserServices;
  20. use think\Route;
  21. /**
  22. * Class MemberCardServices
  23. * @package app\services\user\member
  24. * @mixin MemberCardDao
  25. */
  26. class MemberCardServices extends BaseServices
  27. {
  28. /**
  29. * 初始化,获得dao层句柄
  30. * MemberCardServices constructor.
  31. * @param MemberCardDao $memberCardDao
  32. */
  33. public static $_memberTypePrefix = ['month', 'quarter', 'year', 'ever', 'free'];
  34. public function __construct(MemberCardDao $memberCardDao)
  35. {
  36. $this->dao = $memberCardDao;
  37. }
  38. public function getSearchList(array $where = [])
  39. {
  40. [$page, $limit] = $this->getPageValue();
  41. $where['batch_card_id'] = $where['card_batch_id'];
  42. if ($where['is_use'] != "") {
  43. if ($where['is_use'] == 0) {
  44. $where['use_time'] = 0;
  45. } else {
  46. $where['use_time'] = 1;
  47. }
  48. }
  49. unset($where['is_use']);
  50. $list = $this->dao->getSearchList($where, $page, $limit);
  51. if ($list) {
  52. /** @var UserServices $userService */
  53. $userService = app()->make(UserServices::class);
  54. $userInfos = $userService->getColumn([['uid', 'in', array_unique(array_column($list, 'use_uid'))]], 'uid,real_name,nickname,phone', 'uid');
  55. foreach ($list as $k => $v) {
  56. $list[$k]['username'] = $list[$k]['phone'] = '';
  57. $user_info = $userInfos[$v['use_uid']] ?? [];
  58. if ($v['use_uid'] && $user_info) {
  59. $list[$k]['username'] = $user_info['real_name'] ? $user_info['real_name'] : $user_info['nickname'];
  60. $list[$k]['phone'] = $user_info ? $user_info['phone'] : "";
  61. }
  62. $list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  63. $list[$k]['use_time'] = $v['use_time'] != 0 ? date('Y-m-d H:i:s', $v['use_time']) : "未使用";
  64. }
  65. }
  66. $count = $this->dao->count($where);
  67. return compact('list', 'count');
  68. }
  69. /**
  70. * 生成免费会员卡
  71. * @param array $data
  72. * @return bool
  73. */
  74. public function addCard(array $data)
  75. {
  76. if (!isset($data['card_batch_id']) || !$data['card_batch_id'] || $data['card_batch_id'] == 0 || !isset($data['total_num']) || !$data['total_num'] || $data['total_num'] == 0) {
  77. throw new AdminException("非法参数");
  78. }
  79. try {
  80. if (!isset($data['total_num'])) throw new AdminException("数据缺失");
  81. $num = $data['total_num'];
  82. unset($data['total_num']);
  83. for ($i = 0; $i < $num; $i++) {
  84. $data['card_number'] = $this->makeRandomNumber("CR", $data['card_batch_id']);
  85. $data['card_password'] = $this->makeRandomNumber();
  86. $data['status'] = 1;
  87. $data['add_time'] = time();
  88. $res[] = $data;
  89. }
  90. //数据切片批量插入,提高性能。
  91. $chunk_inster_card = array_chunk($res, 100, true);
  92. foreach ($chunk_inster_card as $v) {
  93. $this->dao->saveAll($v);
  94. }
  95. return true;
  96. } catch (\Exception $exception) {
  97. throw new AdminException("生成卡失败");
  98. }
  99. }
  100. /**
  101. * 获取制卡卡号随机数
  102. * @param false $prefix
  103. * @param false $random
  104. * @return string
  105. */
  106. public function makeRandomNumber($prefix = false, $random = false)
  107. {
  108. if (!$prefix) {
  109. $prefix = "";
  110. }
  111. if (!$random || !is_numeric($random)) {
  112. mt_srand();
  113. $one_random = mt_rand(11111, 99999);
  114. } else {
  115. $one_random = sprintf("%05d", $random);
  116. }
  117. $date_random = date('ymd', time());
  118. $random_tmp = strlen($one_random);
  119. mt_srand();
  120. $two_randow = str_pad(mt_rand(1, 99999), $random_tmp, '0', STR_PAD_LEFT);
  121. if (!$random) {
  122. return $two_randow;
  123. } else {
  124. return $prefix . $one_random . $date_random . $two_randow;
  125. }
  126. }
  127. /**
  128. * 领取会员卡
  129. * @param array $data
  130. * @param int $uid
  131. */
  132. public function drawMemberCard(array $data, int $uid)
  133. {
  134. if (!$uid || !$data) throw new ValidateException('参数缺失!');
  135. $isOpenMember = $this->isOpenMemberCardCache();
  136. if (!$isOpenMember) throw new ValidateException('会员功能暂未开启!');
  137. if (!isset($data['member_card_code']) || !$data['member_card_code']) throw new ValidateException('请输入会员卡号!');
  138. if (!isset($data['member_card_code']) || !$data['member_card_pwd']) throw new ValidateException('请输入领取卡密!');
  139. $card_info = $this->dao->getOneByWhere(['card_number' => trim($data['member_card_code'])]);
  140. if (!$card_info) throw new ValidateException('会员卡不存在!');
  141. /** @var MemberCardBatchServices $memberBatchServices */
  142. $memberBatchServices = app()->make(MemberCardBatchServices::class);
  143. $batch_info = $memberBatchServices->getOne($card_info['card_batch_id']);
  144. if (!$batch_info) throw new ValidateException('会员卡未激活,暂无法使用.');
  145. if ($batch_info->status != 1) throw new ValidateException('会员卡未激活,暂无法使用..');
  146. if ($card_info['status'] == 0) throw new ValidateException('会员卡暂未激活');
  147. if ($card_info['card_password'] != trim($data['member_card_pwd'])) throw new ValidateException('会员卡密码有误!');
  148. if ($card_info['use_uid'] && $card_info['use_time']) throw new ValidateException('会员卡已使用!');
  149. /** @var UserServices $userServices */
  150. $userServices = app()->make(UserServices::class);
  151. $user_info = $userServices->getUserInfo($uid);
  152. if (!$user_info) throw new ValidateException('用户不存在,请重新登录');
  153. if ($user_info->is_money_level > 0 && $user_info->is_ever_level == 1) throw new ValidateException('您已是永久会员,无需再领取,可以将此卡转送亲朋好友,一起享受优惠');
  154. /**
  155. * 批次卡具体使用期限,业务需要打开即可,勿删。
  156. */
  157. if ($card_info->status != 1) throw new ValidateException('会员卡未激活,暂无法使用...');
  158. $this->transaction(function () use ($card_info, $user_info, $batch_info, $memberBatchServices, $userServices, $data) {
  159. $res1 = $this->dao->update($card_info->id, ['use_uid' => $user_info->uid, 'use_time' => time(), 'update_time' => time()], 'id');
  160. if ($res1) {
  161. $res2 = $memberBatchServices->useCardSetInc($batch_info->id, 'use_num', 1);
  162. if ($user_info->overdue_time > time()) {
  163. $overdue_time = bcadd(bcmul($batch_info->use_day, 86400, 0), $user_info->overdue_time, 0);
  164. } else {
  165. $overdue_time = bcadd(bcmul($batch_info->use_day, 86400, 0), time(), 0);
  166. }
  167. $channel_type = $data['from'];
  168. /** @var OtherOrderServices $OtherOrderServices */
  169. $OtherOrderServices = app()->make(OtherOrderServices::class);
  170. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  171. $record_data['uid'] = $user_info->uid;
  172. $record_data['member_code'] = $card_info->card_number;
  173. $record_data['use_day'] = $batch_info->use_day;
  174. $record_data['overdue_time'] = $overdue_time;
  175. $record_data['order_id'] = $storeOrderCreateService->getNewOrderId();
  176. $record_data['channel_type'] = $channel_type;
  177. $record_data['member_type'] = "free";
  178. $record_data['vip_day'] = $batch_info->use_day;
  179. $record_data['type'] = 2;
  180. $record_data['paid'] = 1;
  181. $record_data['pay_time'] = time();
  182. $res3 = $OtherOrderServices->addOtherOrderData($record_data, 2);
  183. /** @var UserServices $userServices */
  184. $userServices = app()->make(UserServices::class);
  185. $res4 = $userServices->setMemberOverdueTime($batch_info->use_day, $user_info->uid, 2, $record_data['member_type']);
  186. $res5 = $res1 && $res2 && $res3 && $res4;
  187. return $res5;
  188. }
  189. });
  190. }
  191. /** 验证是否存在此类型会员卡
  192. * @param string $member_type
  193. * @return bool
  194. */
  195. public function checkmemberType(string $member_type)
  196. {
  197. $member_type_arr = $this->getMemberTypeInfo();
  198. if (!array_key_exists($member_type, $member_type_arr)) throw new ValidateException('暂无此类型会员卡');
  199. return true;
  200. }
  201. /** 获取会员权益和说明配置
  202. * @return array
  203. */
  204. public function getMemberRightsInfo()
  205. {
  206. /** @var MemberRightServices $memberRightService */
  207. $memberRightService = app()->make(MemberRightServices::class);
  208. $memberRight = $memberRightService->getSearchList(['status' => 1]);
  209. if ($memberRight['list']) {
  210. foreach ($memberRight['list'] as $k => &$v) {
  211. $v['title'] = $v['show_title'];
  212. $v['pic'] = $v['image'];
  213. $v['right'] = $v['explain'];
  214. $v['number'] = $v['number'];
  215. }
  216. }
  217. return ['member_right' => $memberRight['list']];
  218. }
  219. /**
  220. * 获取会员卡配置
  221. * @return array
  222. */
  223. public function getMemberTypeInfo()
  224. {
  225. /** @var SystemConfigService $systemConfigService */
  226. $systemConfigService = app()->make(SystemConfigService::class);
  227. $data = [];
  228. foreach (self::$_memberTypePrefix as $v) {
  229. $data[$v] = $systemConfigService::more([$v . '_title', $v . '_vip_day', $v . '_pre_price', $v . '_price']);
  230. }
  231. return $data;
  232. }
  233. /**
  234. * 会员卡数据处理
  235. * @param int $uid
  236. * @param bool $is_free
  237. * @return array
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. */
  242. public function DoMemberType(int $uid = 0, bool $is_free = true)
  243. {
  244. $data = array();
  245. /** @var MemberShipServices $memberShipService */
  246. $memberShipService = app()->make(MemberShipServices::class);
  247. if ($is_free) {
  248. $where = [['is_del', '=', 0]];
  249. } else {
  250. $where = [['is_del', '=', 0], ['type', '<>', 'free']];
  251. }
  252. $userInfo = [];
  253. if ($uid) {
  254. /** @var UserServices $userServices */
  255. $userServices = app()->make(UserServices::class);
  256. $userInfo = $userServices->getUserInfo($uid);
  257. }
  258. $list = $memberShipService->getApiList($where);
  259. foreach ($list as $v) {
  260. $data[] = [
  261. 'id' => $v['id'],
  262. 'title' => $v['title'],
  263. 'type' => $v['type'],
  264. 'vip_day' => $v['vip_day'],
  265. 'pre_price' => $v['pre_price'],
  266. 'price' => $v['price'],
  267. 'overdue_time' => $memberShipService->getOverdueTime($uid, (int)$v['id'], $userInfo, $v)
  268. ];
  269. }
  270. return $data;
  271. }
  272. /**
  273. * 会员类型数据
  274. * @param int $uid
  275. * @return array
  276. * @throws \think\db\exception\DataNotFoundException
  277. * @throws \think\db\exception\DbException
  278. * @throws \think\db\exception\ModelNotFoundException
  279. */
  280. public function getMemberTypeValue(int $uid = 0)
  281. {
  282. $member_type = $this->DoMemberType($uid);
  283. $new_member_data = [];
  284. if ($member_type) {
  285. foreach ($member_type as $k => $v) {
  286. $new_member_data[$v['id']] = $v;
  287. }
  288. }
  289. return $new_member_data;
  290. }
  291. /**
  292. * 导出会员卡
  293. * @param $where
  294. * @return \think\Collection
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\DbException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. */
  299. public function getExportData($where, int $limit = 0)
  300. {
  301. if ($limit) {
  302. [$page] = $this->getPageValue();
  303. } else {
  304. [$page, $limit] = $this->getPageValue();
  305. }
  306. $data = $this->dao->getSearchList($where, $page, $limit);
  307. if ($data) {
  308. /** @var UserServices $userService */
  309. $userService = app()->make(UserServices::class);
  310. $userInfos = $userService->getColumn([['uid', 'in', array_unique(array_column($data, 'use_uid'))]], 'uid,real_name,nickname,phone', 'uid');
  311. foreach ($data as $k => $v) {
  312. $data[$k]['use_time'] = $v['use_time'] != 0 ? date('Y-m-d H:i:s', $v['use_time']) : "";
  313. $data[$k]['user_name'] = '';
  314. $data[$k]['user_phone'] = '';
  315. $userInfo = $userInfos[$v['use_uid']] ?? [];
  316. if ($v['use_uid'] && $userInfo) {
  317. $data[$k]['user_name'] = $userInfo['real_name'] ? $userInfo['real_name'] : $userInfo['nickname'];
  318. $data[$k]['user_phone'] = $userInfo['phone'];
  319. }
  320. }
  321. }
  322. /** @var MemberCardBatchServices $batchService */
  323. $batchService = app()->make(MemberCardBatchServices::class);
  324. $batchInfo = $batchService->getOne($where['batch_card_id']);
  325. $dataArray['title'] = $batchInfo ? $batchInfo['title'] : "";
  326. $dataArray['data'] = $data;
  327. return $dataArray;
  328. }
  329. /**
  330. * 获取会员记录
  331. * @param array $where
  332. * @return array
  333. */
  334. public function getSearchRecordList(array $where)
  335. {
  336. /** @var OtherOrderServices $otherOrderSevice */
  337. $otherOrderSevice = app()->make(OtherOrderServices::class);
  338. return $otherOrderSevice->getMemberRecord($where);
  339. }
  340. /**
  341. * 看是会员权益时候开启|并返回数据
  342. * @param string $rightType
  343. * @param bool $get_number
  344. * @param int $member_status
  345. * @return bool|mixed
  346. * @throws \think\db\exception\DataNotFoundException
  347. * @throws \think\db\exception\DbException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. */
  350. public function isOpenMemberCard(string $rightType = '', bool $get_number = true, $member_status = -1)
  351. {
  352. if ($member_status == -1) {
  353. $isOpen = sys_config('member_card_status', 1);
  354. } else {
  355. $isOpen = $member_status;
  356. }
  357. //如果传入权益类别,查看是否具有某权益
  358. if (!$rightType) {
  359. if ($isOpen) return true;
  360. return false;
  361. } else {
  362. if ($isOpen) {
  363. /** @var MemberRightServices $memberRightService */
  364. $memberRightService = app()->make(MemberRightServices::class);
  365. $memberRight = $memberRightService->getOne(['right_type' => $rightType], 'status,number');
  366. if ($memberRight && $memberRight['status']) {
  367. if ($get_number) {
  368. $number = $memberRight['number'];
  369. if (!$number) return false;
  370. return $number;
  371. }
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. }
  378. /**
  379. * 看是会员权益时候开启|并返回数据
  380. * @param string $rightType
  381. * @param bool $get_number
  382. * @param int $member_status
  383. * @param int $expire
  384. * @return bool|mixed|null
  385. * @throws \throwable
  386. */
  387. public function isOpenMemberCardCache(string $rightType = '', bool $get_number = true, $member_status = -1, int $expire = 30)
  388. {
  389. if ($member_status == -1) {
  390. $isOpen = sys_config('member_card_status', 1);
  391. } else {
  392. $isOpen = $member_status;
  393. }
  394. //如果传入权益类别,查看是否具有某权益
  395. if (!$rightType) {
  396. if ($isOpen) return true;
  397. return false;
  398. } else {
  399. if ($isOpen) {
  400. /** @var MemberRightServices $memberRightService */
  401. $memberRightService = app()->make(MemberRightServices::class);
  402. $memberRight = $memberRightService->getMemberRightCache($rightType);
  403. if ($memberRight && $memberRight['status']) {
  404. if ($get_number) {
  405. $number = $memberRight['number'];
  406. if (!$number) return false;
  407. return $number;
  408. }
  409. return true;
  410. }
  411. return false;
  412. }
  413. return false;
  414. }
  415. }
  416. /**
  417. * 修改会员卡状态
  418. * @param $id
  419. * @param $status
  420. * @return bool
  421. */
  422. public function setStatus($id, $status)
  423. {
  424. $res = $this->dao->update($id, ['status' => $status]);
  425. if ($res) return true;
  426. return false;
  427. }
  428. }