MemberCardServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\MemberCardDao;
  13. use app\services\BaseServices;
  14. use app\services\order\OtherOrderServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use app\services\user\UserServices;
  17. use crmeb\exceptions\AdminException;
  18. use crmeb\exceptions\ApiException;
  19. use crmeb\services\SystemConfigService;
  20. class MemberCardServices extends BaseServices
  21. {
  22. /** 初始化,获得dao层句柄
  23. * MemberCardServices constructor.
  24. * @param MemberCardDao $memberCardDao
  25. */
  26. public static $_memberTypePrefix = ['month', 'quarter', 'year', 'ever', 'free', 'owner'];
  27. public function __construct(MemberCardDao $memberCardDao)
  28. {
  29. $this->dao = $memberCardDao;
  30. }
  31. public function getSearchList(array $where = [])
  32. {
  33. /** @var UserServices $userService */
  34. $userService = app()->make(UserServices::class);
  35. [$page, $limit] = $this->getPageValue();
  36. $where['batch_card_id'] = $where['card_batch_id'];
  37. if ($where['is_use'] != "") {
  38. if ($where['is_use'] == 0) {
  39. $where['use_time'] = 0;
  40. } else {
  41. $where['use_time'] = 1;
  42. }
  43. }
  44. unset($where['is_use']);
  45. $list = $this->dao->getSearchList($where, $page, $limit);
  46. $userIds = array_column($list->toArray(), 'use_uid');
  47. $userList = $userService->getColumn([['uid', 'in', $userIds]], 'nickname,phone,real_name', 'uid');
  48. foreach ($list as $k => $v) {
  49. if ($v['use_uid']) {
  50. $list[$k]['username'] = $userList[$v['use_uid']]['real_name'] ?: $userList[$v['use_uid']]['nickname'];
  51. $list[$k]['phone'] = $userList[$v['use_uid']] ? $userList[$v['use_uid']]['phone'] : "";
  52. }
  53. $list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  54. $list[$k]['use_time'] = $v['use_time'] != 0 ? date('Y-m-d H:i:s', $v['use_time']) : "未使用";
  55. }
  56. $count = $this->dao->count($where);
  57. return compact('list', 'count');
  58. }
  59. /** 生成免费会员卡
  60. * @param array $data
  61. */
  62. public function addCard(array $data)
  63. {
  64. 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) {
  65. throw new AdminException(100100);
  66. }
  67. try {
  68. if (!isset($data['total_num'])) throw new AdminException(100100);
  69. $num = $data['total_num'];
  70. unset($data['total_num']);
  71. $res = [];
  72. for ($i = 0; $i < $num; $i++) {
  73. $data['card_number'] = $this->makeRandomNumber("CR", $data['card_batch_id']);
  74. $data['card_password'] = $this->makeRandomNumber();
  75. $data['status'] = 1;
  76. $data['add_time'] = time();
  77. $res[] = $data;
  78. }
  79. //数据切片批量插入,提高性能。
  80. $chunk_inster_card = array_chunk($res, 100, true);
  81. foreach ($chunk_inster_card as $v) {
  82. $this->dao->saveAll($v);
  83. }
  84. return true;
  85. } catch (\Exception $exception) {
  86. throw new AdminException(400620);
  87. }
  88. }
  89. /**获取制卡卡号随机数
  90. * @param bool $prefix
  91. * @param bool $random
  92. * @return string
  93. */
  94. public function makeRandomNumber($prefix = false, $random = false)
  95. {
  96. if (!$prefix) {
  97. $prefix = "";
  98. }
  99. if (!$random || !is_numeric($random)) {
  100. $one_random = mt_rand(11111, 99999);
  101. } else {
  102. $one_random = sprintf("%05d", $random);
  103. }
  104. $date_random = date('ymd', time());
  105. $random_tmp = strlen($one_random);
  106. $two_randow = str_pad(mt_rand(1, 99999), $random_tmp, '0', STR_PAD_LEFT);
  107. if (!$random) {
  108. return $two_randow;
  109. } else {
  110. return $prefix . $one_random . $date_random . $two_randow;
  111. }
  112. }
  113. /** 领取会员卡
  114. * @param array $data
  115. * @param int $uid
  116. */
  117. public function drawMemberCard(array $data, int $uid)
  118. {
  119. if (!$uid || !$data) throw new ApiException(100100);
  120. $isOpenMember = $this->isOpenMemberCard();
  121. if (!$isOpenMember) throw new ApiException(400621);
  122. if (!isset($data['member_card_code']) || !$data['member_card_code']) throw new ApiException(400622);
  123. if (!isset($data['member_card_code']) || !$data['member_card_pwd']) throw new ApiException(400623);
  124. $card_info = $this->dao->getOneByWhere(['card_number' => trim($data['member_card_code'])]);
  125. if (!$card_info) throw new ApiException(400624);
  126. /** @var MemberCardBatchServices $memberBatchServices */
  127. $memberBatchServices = app()->make(MemberCardBatchServices::class);
  128. $batch_info = $memberBatchServices->getOne($card_info['card_batch_id']);
  129. if (!$batch_info) throw new ApiException(400625);
  130. if ($batch_info->status != 1) throw new ApiException(400625);
  131. if ($card_info['status'] == 0) throw new ApiException(400625);
  132. if ($card_info['card_password'] != trim($data['member_card_pwd'])) throw new ApiException(400626);
  133. if ($card_info['use_uid'] && $card_info['use_time']) throw new ApiException(400627);
  134. /** @var UserServices $userServices */
  135. $userServices = app()->make(UserServices::class);
  136. $user_info = $userServices->getUserInfo($uid);
  137. if (!$user_info) throw new ApiException(400214);
  138. if ($user_info->is_money_level > 0 && $user_info->is_ever_level == 1) throw new ApiException(400628);
  139. /**
  140. * 批次卡具体使用期限,业务需要打开即可,勿删。
  141. */
  142. if ($card_info->status != 1) throw new ApiException(400625);
  143. $this->transaction(function () use ($card_info, $user_info, $batch_info, $memberBatchServices, $userServices, $data) {
  144. $res1 = $this->dao->update($card_info->id, ['use_uid' => $user_info->uid, 'use_time' => time(), 'update_time' => time()], 'id');
  145. if ($res1) {
  146. $res2 = $memberBatchServices->useCardSetInc($batch_info->id, 'use_num', 1);
  147. $overdue_time = 0;
  148. switch ($user_info->is_money_level) {
  149. case 1:
  150. case 2:
  151. case 3:
  152. $overdue_time = bcadd(bcmul($batch_info->use_day, 86400, 0), $user_info->overdue_time, 0);
  153. $overdue_time = mktime(date('H'), date('i'), date('s'), date('m', $overdue_time), date('d', $overdue_time), date('Y', $overdue_time));
  154. break;
  155. case 0:
  156. $overdue_time = bcadd(bcmul($batch_info->use_day, 86400, 0), time(), 0);
  157. break;
  158. }
  159. $channel_type = $data['from'];
  160. /** @var OtherOrderServices $OtherOrderServices */
  161. $OtherOrderServices = app()->make(OtherOrderServices::class);
  162. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  163. $record_data['uid'] = $user_info->uid;
  164. $record_data['member_code'] = $card_info->card_number;
  165. $record_data['use_day'] = $batch_info->use_day;
  166. $record_data['overdue_time'] = $overdue_time;
  167. $record_data['order_id'] = $storeOrderCreateService->getNewOrderId();
  168. $record_data['channel_type'] = $channel_type;
  169. $record_data['member_type'] = "free";
  170. $record_data['vip_day'] = $batch_info->use_day;
  171. $record_data['type'] = 2;
  172. $record_data['paid'] = 1;
  173. $record_data['pay_time'] = time();
  174. $res3 = $OtherOrderServices->addOtherOrderData($record_data);
  175. //if ($res3) $res4 = $userServices->update($user_info->uid, ['level' => 1, 'overdue_time' => $overdue_time, 'is_permanent' => 0], 'uid');
  176. /** @var UserServices $userServices */
  177. $userServices = app()->make(UserServices::class);
  178. $res4 = $userServices->setMemberOverdueTime($batch_info->use_day, $user_info->uid, 2, $record_data['member_type']);
  179. $res5 = $res1 && $res2 && $res3 && $res4;
  180. return $res5;
  181. }
  182. });
  183. }
  184. /** 验证是否存在此类型会员卡
  185. * @param string $member_type
  186. * @return bool
  187. */
  188. public function checkmemberType(string $member_type)
  189. {
  190. $member_type_arr = $this->getMemberTypeInfo();
  191. if (!array_key_exists($member_type, $member_type_arr)) throw new ApiException(400629);
  192. return true;
  193. }
  194. /** 获取会员权益和说明配置
  195. * @return array
  196. */
  197. public function getMemberRightsInfo()
  198. {
  199. /** @var MemberRightServices $memberRightService */
  200. $memberRightService = app()->make(MemberRightServices::class);
  201. $memberRight = $memberRightService->getSearchList(['status' => 1]);
  202. if ($memberRight['list']) {
  203. foreach ($memberRight['list'] as $k => &$v) {
  204. $v['title'] = $v['show_title'];
  205. $v['pic'] = $v['image'];
  206. $v['right'] = $v['explain'];
  207. // $v['number'] = $v['number'];
  208. }
  209. }
  210. return ['member_right' => $memberRight['list']];
  211. }
  212. /**获取会员卡配置
  213. * @return array
  214. */
  215. public function getMemberTypeInfo()
  216. {
  217. /** @var SystemConfigService $systemConfigService */
  218. $systemConfigService = app()->make(SystemConfigService::class);
  219. $data = [];
  220. foreach (self::$_memberTypePrefix as $v) {
  221. $data[$v] = $systemConfigService::more([$v . '_title', $v . '_vip_day', $v . '_pre_price', $v . '_price']);
  222. }
  223. return $data;
  224. }
  225. /**会员卡数据处理
  226. * @return array
  227. */
  228. public function DoMemberType()
  229. {
  230. $data = array();
  231. /** @var MemberShipServices $memberShipService */
  232. $memberShipService = app()->make(MemberShipServices::class);
  233. $list = $memberShipService->getApiList(['is_del' => 0]);
  234. foreach ($list as $v) {
  235. $data[] = [
  236. 'mc_id' => $v['id'],
  237. 'title' => $v['title'],
  238. 'type' => $v['type'],
  239. 'vip_day' => $v['vip_day'],
  240. 'pre_price' => $v['pre_price'],
  241. 'price' => $v['price'],
  242. ];
  243. }
  244. return $data;
  245. }
  246. /**会员类型数据
  247. * @return bool
  248. */
  249. public function getMemberTypeValue()
  250. {
  251. $member_type = $this->DoMemberType();
  252. if (!$member_type) return false;
  253. $new_member_data = [];
  254. foreach ($member_type as $k => $v) {
  255. $new_member_data[$v['mc_id']] = $v;
  256. }
  257. return $new_member_data;
  258. }
  259. /**导出会员卡
  260. * @param $where
  261. * @return \think\Collection
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. public function getExportData($where)
  267. {
  268. $data = $this->dao->getSearchList($where);
  269. /** @var UserServices $userService */
  270. $userService = app()->make(UserServices::class);
  271. /** @var MemberCardBatchServices $batchService */
  272. $batchService = app()->make(MemberCardBatchServices::class);
  273. foreach ($data as $k => $v) {
  274. $data[$k]['use_time'] = $v['use_time'] != 0 ? date('Y-m-d H:i:s', $v['use_time']) : "";
  275. $data[$k]['user_name'] = '';
  276. $data[$k]['user_phone'] = '';
  277. if ($v['use_uid'] != 0) {
  278. $userInfo = $userService->get($v['use_uid']);
  279. $data[$k]['user_name'] = $userInfo['nickname'] ?: $userInfo['account'];
  280. $data[$k]['user_phone'] = $userInfo['phone'];
  281. }
  282. }
  283. $batchInfo = $batchService->getOne($where['batch_card_id']);
  284. $dataArray['title'] = $batchInfo ? $batchInfo['title'] : "";
  285. $dataArray['data'] = $data;
  286. return $dataArray;
  287. }
  288. /**获取会员记录
  289. * @param array $where
  290. * @return array
  291. */
  292. public function getSearchRecordList(array $where)
  293. {
  294. /** @var OtherOrderServices $otherOrderSevice */
  295. $otherOrderSevice = app()->make(OtherOrderServices::class);
  296. return $otherOrderSevice->getMemberRecord($where);
  297. }
  298. /**
  299. * 看是否开启会员功能
  300. * @param string $rightType
  301. * @param bool $get_number
  302. * @return bool|mixed
  303. * @throws \think\db\exception\DataNotFoundException
  304. * @throws \think\db\exception\DbException
  305. * @throws \think\db\exception\ModelNotFoundException
  306. */
  307. public function isOpenMemberCard(string $rightType = '', bool $get_number = true)
  308. {
  309. $isOpen = sys_config('member_card_status', 1);
  310. //如果传入权益类别,查看是否具有某权益
  311. if (!$rightType) {
  312. if ($isOpen) return true;
  313. return false;
  314. } else {
  315. /** @var MemberRightServices $memberRightService */
  316. $memberRightService = app()->make(MemberRightServices::class);
  317. $memberRight = $memberRightService->getOne(['right_type' => $rightType], 'status,number');
  318. if ($isOpen && $memberRight && $memberRight['status']) {
  319. if ($get_number) {
  320. $number = $memberRight['number'];
  321. if (!$number) return false;
  322. return $number;
  323. }
  324. return true;
  325. }
  326. return false;
  327. }
  328. }
  329. /**
  330. * 修改会员卡状态
  331. * @param $id
  332. * @param $status
  333. * @return bool
  334. */
  335. public function setStatus($id, $status)
  336. {
  337. $res = $this->dao->update($id, ['status' => $status]);
  338. if ($res) return true;
  339. return false;
  340. }
  341. }