UserLevelServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserLevelDao;
  15. use app\services\system\SystemUserLevelServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. use crmeb\services\FormBuilder as Form;
  19. use think\facade\Route as Url;
  20. /**
  21. *
  22. * Class UserLevelServices
  23. * @package app\services\user
  24. * @method getDiscount(int $uid, string $field)
  25. */
  26. class UserLevelServices extends BaseServices
  27. {
  28. /**
  29. * UserLevelServices constructor.
  30. * @param UserLevelDao $dao
  31. */
  32. public function __construct(UserLevelDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 某些条件获取单个
  38. * @param array $where
  39. * @param string $field
  40. * @return mixed
  41. */
  42. public function getWhereLevel(array $where, string $field = '*')
  43. {
  44. return $this->getOne($where, $field);
  45. }
  46. /**
  47. * 获取一些用户等级信息
  48. * @param array $uids
  49. * @param string $field
  50. * @param string $key
  51. * @return array
  52. */
  53. public function getUsersLevelInfo(array $uids)
  54. {
  55. return $this->dao->getColumn([['uid', 'in', $uids]], 'level_id,is_forever,valid_time', 'uid');
  56. }
  57. /**
  58. * 清除用户等级
  59. * @param $uids
  60. * @return \crmeb\basic\BaseModel|mixed
  61. */
  62. public function delUserLevel($uids)
  63. {
  64. $where = [];
  65. if (is_array($uids)) {
  66. $where[] = ['uid', 'IN', $uids];
  67. $re = $this->dao->batchUpdate($uids, ['is_del' => 1, 'status' => 0], 'uid');
  68. } else {
  69. $where[] = ['uid', '=', $uids];
  70. $re = $this->dao->update($uids, ['is_del' => 1, 'status' => 0], 'uid');
  71. }
  72. if (!$re)
  73. throw new AdminException(400671);
  74. $where[] = ['category', 'IN', ['exp']];
  75. /** @var UserBillServices $userbillServices */
  76. $userbillServices = app()->make(UserBillServices::class);
  77. $userbillServices->update($where, ['status' => -1]);
  78. return true;
  79. }
  80. /**
  81. * 根据用户uid 获取用户等级详细信息
  82. * @param int $uid
  83. * @param string $field
  84. */
  85. public function getUerLevelInfoByUid(int $uid, string $field = '')
  86. {
  87. $userLevelInfo = $this->dao->getUserLevel($uid);
  88. $data = [];
  89. if ($userLevelInfo) {
  90. $data = ['id' => $userLevelInfo['id'], 'level_id' => $userLevelInfo['level_id'], 'add_time' => $userLevelInfo['add_time']];
  91. $data['discount'] = $userLevelInfo['levelInfo']['discount'] ?? 0;
  92. $data['name'] = $userLevelInfo['levelInfo']['name'] ?? '';
  93. $data['money'] = $userLevelInfo['levelInfo']['money'] ?? 0;
  94. $data['icon'] = $userLevelInfo['levelInfo']['icon'] ?? '';
  95. $data['is_pay'] = $userLevelInfo['levelInfo']['is_pay'] ?? 0;
  96. $data['grade'] = $userLevelInfo['levelInfo']['grade'] ?? 0;
  97. $data['exp_num'] = $userLevelInfo['levelInfo']['exp_num'] ?? 0;
  98. }
  99. if ($field) return $data[$field] ?? '';
  100. return $data;
  101. }
  102. /**
  103. * 设置用户等级
  104. * @param $uid 用户uid
  105. * @param $level_id 等级id
  106. * @return UserLevel|bool|\think\Model
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @throws \think\exception\DbException
  110. */
  111. public function setUserLevel(int $uid, int $level_id, $vipinfo = [])
  112. {
  113. /** @var SystemUserLevelServices $systemLevelServices */
  114. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  115. if (!$vipinfo) {
  116. $vipinfo = $systemLevelServices->getLevel($level_id);
  117. if (!$vipinfo) {
  118. throw new AdminException(400672);
  119. }
  120. }
  121. /** @var $user */
  122. $user = app()->make(UserServices::class);
  123. $userinfo = $user->getUserInfo($uid);
  124. //把之前等级作废
  125. $this->dao->update(['uid' => $uid], ['status' => 0, 'is_del' => 1]);
  126. //检查是否购买过
  127. $uservipinfo = $this->getWhereLevel(['uid' => $uid, 'level_id' => $level_id]);
  128. $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  129. $data['add_time'] = time();
  130. if ($uservipinfo) {
  131. $data['status'] = 1;
  132. $data['is_del'] = 0;
  133. if (!$this->dao->update(['id' => $uservipinfo['id']], $data))
  134. throw new AdminException(400671);
  135. } else {
  136. $data = array_merge($data, [
  137. 'is_forever' => $vipinfo->is_forever,
  138. 'status' => 1,
  139. 'is_del' => 0,
  140. 'grade' => $vipinfo->grade,
  141. 'uid' => $uid,
  142. 'level_id' => $level_id,
  143. 'discount' => $vipinfo->discount,
  144. ]);
  145. $data['valid_time'] = 0;
  146. if (!$this->dao->save($data)) throw new AdminException(100006);
  147. }
  148. if ($level_id > $userinfo['level']) {
  149. $change_exp = $vipinfo['exp_num'] - $userinfo['exp'];
  150. $pm = 1;
  151. $type = 'system_exp_add';
  152. $title = '系统增加经验';
  153. $mark = '系统增加' . $change_exp . '经验';
  154. } else {
  155. $change_exp = $userinfo['exp'] - $vipinfo['exp_num'];
  156. $pm = 0;
  157. $type = 'system_exp_sub';
  158. $title = '系统减少经验';
  159. $mark = '系统减少' . $change_exp . '经验';
  160. }
  161. $bill_data['uid'] = $uid;
  162. $bill_data['pm'] = $pm;
  163. $bill_data['title'] = $title;
  164. $bill_data['category'] = 'exp';
  165. $bill_data['type'] = $type;
  166. $bill_data['number'] = $change_exp;
  167. $bill_data['balance'] = $userinfo['exp'];
  168. $bill_data['mark'] = $mark;
  169. $bill_data['status'] = 1;
  170. $bill_data['add_time'] = time();
  171. /** @var UserBillServices $userBillService */
  172. $userBillService = app()->make(UserBillServices::class);
  173. if (!$userBillService->save($bill_data)) throw new AdminException(100006);
  174. if (!$user->update(['uid' => $uid], ['level' => $level_id, 'exp' => $vipinfo['exp_num']])) throw new AdminException(100007);
  175. return true;
  176. }
  177. /**
  178. * 会员列表
  179. * @param $where
  180. * @return mixed
  181. */
  182. public function getSytemList($where)
  183. {
  184. /** @var SystemUserLevelServices $systemLevelServices */
  185. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  186. return $systemLevelServices->getLevelList($where);
  187. }
  188. /**
  189. * 获取添加修改需要表单数据
  190. * @param int $id
  191. * @return array
  192. * @throws \FormBuilder\Exception\FormBuilderException
  193. */
  194. public function edit(int $id)
  195. {
  196. if ($id) {
  197. $vipInfo = app()->make(SystemUserLevelServices::class)->getlevel($id);
  198. $vipInfo->image = set_file_url($vipInfo->image);
  199. $vipInfo->icon = set_file_url($vipInfo->icon);
  200. if (!$vipInfo) {
  201. throw new AdminException(100026);
  202. }
  203. $field[] = Form::hidden('id', $id);
  204. $msg = '编辑用户等级';
  205. } else {
  206. $msg = '添加用户等级';
  207. }
  208. $field[] = Form::input('name', '等级名称', isset($vipInfo) ? $vipInfo->name : '')->maxlength(10)->col(24)->required();
  209. $field[] = Form::number('grade', '等级', isset($vipInfo) ? $vipInfo->grade : 0)->min(0)->precision(0)->required();
  210. $field[] = Form::number('discount', '享受折扣', isset($vipInfo) ? $vipInfo->discount : 100)->min(0)->max(100)->placeholder('输入折扣数100,代表原价,90代表9折')->required();
  211. $field[] = Form::number('exp_num', '解锁经验值', isset($vipInfo) ? $vipInfo->exp_num : 0)->min(0)->precision(0)->required();
  212. $field[] = Form::frameImage('icon', '图标', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'icon')), isset($vipInfo) ? $vipInfo->icon : '')->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  213. $field[] = Form::frameImage('image', '用户等级背景', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'image')), isset($vipInfo) ? $vipInfo->image : '')->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  214. $field[] = Form::radio('is_show', '是否显示', isset($vipInfo) ? $vipInfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  215. return create_form($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
  216. }
  217. /*
  218. * 会员等级添加或者修改
  219. * @param $id 修改的等级id
  220. * @return json
  221. * */
  222. public function save(int $id, array $data)
  223. {
  224. /** @var SystemUserLevelServices $systemUserLevel */
  225. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  226. $levelOne = $systemUserLevel->getWhereLevel(['is_del' => 0, 'grade' => $data['grade']]);
  227. $levelTwo = $systemUserLevel->getWhereLevel(['is_del' => 0, 'exp_num' => $data['exp_num']]);
  228. $levelThree = $systemUserLevel->getWhereLevel(['is_del' => 0, 'name' => $data['name']]);
  229. $levelPre = $systemUserLevel->getPreLevel($data['grade']);
  230. $levelNext = $systemUserLevel->getNextLevel($data['grade']);
  231. if ($levelPre && $data['exp_num'] <= $levelPre['exp_num']) {
  232. throw new AdminException(400673);
  233. }
  234. if ($levelNext && $data['exp_num'] >= $levelNext['exp_num']) {
  235. throw new AdminException(400674);
  236. }
  237. //修改
  238. if ($id) {
  239. if (($levelOne && $levelOne['id'] != $id) || ($levelThree && $levelThree['id'] != $id)) {
  240. throw new AdminException(400675);
  241. }
  242. if ($levelTwo && $levelTwo['id'] != $id) {
  243. throw new AdminException(400676);
  244. }
  245. if (!$systemUserLevel->update($id, $data)) {
  246. throw new AdminException(100007);
  247. }
  248. return true;
  249. } else {
  250. if ($levelOne || $levelThree) {
  251. throw new AdminException(400675);
  252. }
  253. if ($levelTwo) {
  254. throw new AdminException(400676);
  255. }
  256. //新增
  257. $data['add_time'] = time();
  258. if (!$systemUserLevel->save($data)) {
  259. throw new AdminException(100022);
  260. }
  261. return true;
  262. }
  263. }
  264. /**
  265. * 假删除
  266. * @param int $id
  267. * @return mixed
  268. */
  269. public function delLevel(int $id)
  270. {
  271. /** @var SystemUserLevelServices $systemUserLevel */
  272. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  273. $level = $systemUserLevel->getWhereLevel(['id' => $id]);
  274. if ($level && $level['is_del'] != 1) {
  275. if (!$systemUserLevel->update($id, ['is_del' => 1]))
  276. throw new AdminException(100008);
  277. }
  278. return 100002;
  279. }
  280. /**
  281. * 设置是否显示
  282. * @param int $id
  283. * @param $is_show
  284. * @return mixed
  285. */
  286. public function setShow(int $id, int $is_show)
  287. {
  288. /** @var SystemUserLevelServices $systemUserLevel */
  289. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  290. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  291. throw new AdminException(100026);
  292. if ($systemUserLevel->update($id, ['is_show' => $is_show])) {
  293. return 100014;
  294. } else {
  295. throw new AdminException(100015);
  296. }
  297. }
  298. /**
  299. * 快速修改
  300. * @param int $id
  301. * @param $is_show
  302. * @return mixed
  303. */
  304. public function setValue(int $id, array $data)
  305. {
  306. /** @var SystemUserLevelServices $systemUserLevel */
  307. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  308. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  309. throw new AdminException(100026);
  310. if ($systemUserLevel->update($id, [$data['field'] => $data['value']])) {
  311. return true;
  312. } else {
  313. throw new AdminException(100006);
  314. }
  315. }
  316. /**
  317. * 检测用户会员升级
  318. * @param $uid
  319. * @return bool
  320. */
  321. public function detection(int $uid)
  322. {
  323. //商城会员是否开启
  324. if (!sys_config('member_func_status')) {
  325. return true;
  326. }
  327. /** @var UserServices $userServices */
  328. $userServices = app()->make(UserServices::class);
  329. $user = $userServices->getUserInfo($uid);
  330. if (!$user) {
  331. throw new ApiException(410284);
  332. }
  333. /** @var SystemUserLevelServices $systemUserLevel */
  334. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  335. $userAllLevel = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']]]);
  336. if (!$userAllLevel) {
  337. return true;
  338. }
  339. $data = [];
  340. $data['add_time'] = time();
  341. $userLevel = $this->dao->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
  342. foreach ($userAllLevel as $vipinfo) {
  343. if (in_array($vipinfo['id'], $userLevel)) {
  344. continue;
  345. }
  346. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  347. $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  348. if ($uservip) {
  349. //降级在升级情况
  350. $data['status'] = 1;
  351. $data['is_del'] = 0;
  352. if (!$this->dao->update($uservip['id'], $data, 'id')) {
  353. throw new ApiException(410285);
  354. }
  355. } else {
  356. $data = array_merge($data, [
  357. 'is_forever' => $vipinfo['is_forever'],
  358. 'status' => 1,
  359. 'is_del' => 0,
  360. 'grade' => $vipinfo['grade'],
  361. 'uid' => $uid,
  362. 'level_id' => $vipinfo['id'],
  363. 'discount' => $vipinfo['discount'],
  364. ]);
  365. if (!$this->dao->save($data)) {
  366. throw new ApiException(410285);
  367. }
  368. }
  369. $data['add_time'] += 1;
  370. }
  371. if (!$userServices->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
  372. throw new ApiException(410285);
  373. }
  374. return true;
  375. }
  376. /**
  377. * 会员等级列表
  378. * @param int $uid
  379. */
  380. public function grade(int $uid)
  381. {
  382. //商城会员是否开启
  383. if (!sys_config('member_func_status')) {
  384. return [];
  385. }
  386. /** @var UserServices $userServices */
  387. $userServices = app()->make(UserServices::class);
  388. $user = $userServices->getUserInfo($uid);
  389. if (!$user) {
  390. throw new ApiException(410284);
  391. }
  392. $userLevelInfo = $this->getUerLevelInfoByUid($uid);
  393. if (empty($userLevelInfo)) {
  394. $level_id = 0;
  395. } else {
  396. $level_id = $userLevelInfo['level_id'];
  397. }
  398. /** @var SystemUserLevelServices $systemUserLevel */
  399. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  400. return $systemUserLevel->getLevelListAndGrade($level_id);
  401. }
  402. /**
  403. * 获取会员信息
  404. * @param int $uid
  405. * @return array[]
  406. */
  407. public function getUserLevelInfo(int $uid)
  408. {
  409. $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
  410. //商城会员是否开启
  411. if (!sys_config('member_func_status')) {
  412. return $data;
  413. }
  414. /** @var UserServices $userServices */
  415. $userServices = app()->make(UserServices::class);
  416. $user = $userServices->getUserInfo($uid);
  417. if (!$user) {
  418. throw new ApiException(410032);
  419. }
  420. $data['user'] = $user;
  421. /** @var SystemUserLevelServices $systemUserLevel */
  422. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  423. $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
  424. $i = 0;
  425. foreach ($levelList as &$level) {
  426. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  427. $level['image'] = set_file_url($level['image']);
  428. $level['icon'] = set_file_url($level['icon']);
  429. $i++;
  430. }
  431. $data['level_list'] = $levelList;
  432. $data['level_info'] = $this->getUerLevelInfoByUid($uid);
  433. $data['level_info']['exp'] = $user['exp'] ?? 0;
  434. /** @var UserBillServices $userBillservices */
  435. $userBillservices = app()->make(UserBillServices::class);
  436. $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
  437. $task = [];
  438. /** @var UserSignServices $userSignServices */
  439. $userSignServices = app()->make(UserSignServices::class);
  440. $task['sign_count'] = $userSignServices->getSignSumDay($uid);
  441. $task['sign'] = sys_config('sign_give_exp', 0);
  442. $task['order'] = sys_config('order_give_exp', 0);
  443. $task['invite'] = sys_config('invite_user_exp', 0);
  444. $data['task'] = $task;
  445. return $data;
  446. }
  447. /**
  448. * 经验列表
  449. * @param int $uid
  450. * @return array
  451. */
  452. public function expList(int $uid)
  453. {
  454. /** @var UserServices $userServices */
  455. $userServices = app()->make(UserServices::class);
  456. $user = $userServices->getUserInfo($uid);
  457. if (!$user) {
  458. throw new ApiException(410032);
  459. }
  460. /** @var UserBillServices $userBill */
  461. $userBill = app()->make(UserBillServices::class);
  462. $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
  463. $list = $data['list'] ?? [];
  464. return $list;
  465. }
  466. }