UserLevelServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 : '')->col(24)->required();
  209. // $field[] = Form::number('valid_date', '有效时间(天)', isset($vipinfo) ? $vipinfo->valid_date : 0)->min(0)->col(12);
  210. $field[] = Form::number('grade', '等级', isset($vipinfo) ? $vipinfo->grade : 0)->min(0)->precision(0)->col(8)->required();
  211. $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 100)->min(0)->max(100)->col(8)->placeholder('输入折扣数100,代表原价,90代表9折')->required();
  212. $field[] = Form::number('exp_num', '解锁需经验值达到', isset($vipinfo) ? $vipinfo->exp_num : 0)->min(0)->precision(0)->col(8)->required();
  213. $field[] = Form::frameImage('icon', '图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'icon')), isset($vipinfo) ? $vipinfo->icon : '')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
  214. $field[] = Form::frameImage('image', '用户等级背景', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), isset($vipinfo) ? $vipinfo->image : '')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
  215. $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  216. $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '')->required();
  217. return create_form($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
  218. }
  219. /*
  220. * 会员等级添加或者修改
  221. * @param $id 修改的等级id
  222. * @return json
  223. * */
  224. public function save(int $id, array $data)
  225. {
  226. /** @var SystemUserLevelServices $systemUserLevel */
  227. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  228. $levelOne = $systemUserLevel->getWhereLevel(['is_del' => 0, 'grade' => $data['grade']]);
  229. $levelTwo = $systemUserLevel->getWhereLevel(['is_del' => 0, 'exp_num' => $data['exp_num']]);
  230. $levelThree = $systemUserLevel->getWhereLevel(['is_del' => 0, 'name' => $data['name']]);
  231. $levelPre = $systemUserLevel->getPreLevel($data['grade']);
  232. $levelNext = $systemUserLevel->getNextLevel($data['grade']);
  233. if ($levelPre && $data['exp_num'] <= $levelPre['exp_num']) {
  234. throw new AdminException(400673);
  235. }
  236. if ($levelNext && $data['exp_num'] >= $levelNext['exp_num']) {
  237. throw new AdminException(400674);
  238. }
  239. //修改
  240. if ($id) {
  241. if (($levelOne && $levelOne['id'] != $id) || ($levelThree && $levelThree['id'] != $id)) {
  242. throw new AdminException(400675);
  243. }
  244. if ($levelTwo && $levelTwo['id'] != $id) {
  245. throw new AdminException(400676);
  246. }
  247. if (!$systemUserLevel->update($id, $data)) {
  248. throw new AdminException(100007);
  249. }
  250. return '修改成功';
  251. } else {
  252. if ($levelOne || $levelThree) {
  253. throw new AdminException(400675);
  254. }
  255. if ($levelTwo) {
  256. throw new AdminException(400676);
  257. }
  258. //新增
  259. $data['add_time'] = time();
  260. if (!$systemUserLevel->save($data)) {
  261. throw new AdminException(100022);
  262. }
  263. return '添加成功';
  264. }
  265. }
  266. /**
  267. * 假删除
  268. * @param int $id
  269. * @return mixed
  270. */
  271. public function delLevel(int $id)
  272. {
  273. /** @var SystemUserLevelServices $systemUserLevel */
  274. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  275. $level = $systemUserLevel->getWhereLevel(['id' => $id]);
  276. if ($level && $level['is_del'] != 1) {
  277. if (!$systemUserLevel->update($id, ['is_del' => 1]))
  278. throw new AdminException(100008);
  279. }
  280. return '删除成功';
  281. }
  282. /**
  283. * 设置是否显示
  284. * @param int $id
  285. * @param $is_show
  286. * @return mixed
  287. */
  288. public function setShow(int $id, int $is_show)
  289. {
  290. /** @var SystemUserLevelServices $systemUserLevel */
  291. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  292. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  293. throw new AdminException(100026);
  294. if ($systemUserLevel->update($id, ['is_show' => $is_show])) {
  295. return 100014;
  296. } else {
  297. throw new AdminException(100015);
  298. }
  299. }
  300. /**
  301. * 快速修改
  302. * @param int $id
  303. * @param $is_show
  304. * @return mixed
  305. */
  306. public function setValue(int $id, array $data)
  307. {
  308. /** @var SystemUserLevelServices $systemUserLevel */
  309. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  310. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  311. throw new AdminException(100026);
  312. if ($systemUserLevel->update($id, [$data['field'] => $data['value']])) {
  313. return true;
  314. } else {
  315. throw new AdminException(100006);
  316. }
  317. }
  318. /**
  319. * 检测用户会员升级
  320. * @param $uid
  321. * @return bool
  322. */
  323. public function detection(int $uid)
  324. {
  325. //商城会员是否开启
  326. if (!sys_config('member_func_status')) {
  327. return true;
  328. }
  329. /** @var UserServices $userServices */
  330. $userServices = app()->make(UserServices::class);
  331. $user = $userServices->getUserInfo($uid);
  332. if (!$user) {
  333. throw new ApiException(410284);
  334. }
  335. /** @var SystemUserLevelServices $systemUserLevel */
  336. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  337. $userAllLevel = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']]]);
  338. if (!$userAllLevel) {
  339. return true;
  340. }
  341. $data = [];
  342. $data['add_time'] = time();
  343. $userLevel = $this->dao->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
  344. foreach ($userAllLevel as $vipinfo) {
  345. if (in_array($vipinfo['id'], $userLevel)) {
  346. continue;
  347. }
  348. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  349. $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  350. if ($uservip) {
  351. //降级在升级情况
  352. $data['status'] = 1;
  353. $data['is_del'] = 0;
  354. if (!$this->dao->update($uservip['id'], $data, 'id')) {
  355. throw new ApiException(410285);
  356. }
  357. } else {
  358. $data = array_merge($data, [
  359. 'is_forever' => $vipinfo['is_forever'],
  360. 'status' => 1,
  361. 'is_del' => 0,
  362. 'grade' => $vipinfo['grade'],
  363. 'uid' => $uid,
  364. 'level_id' => $vipinfo['id'],
  365. 'discount' => $vipinfo['discount'],
  366. ]);
  367. if (!$this->dao->save($data)) {
  368. throw new ApiException(410285);
  369. }
  370. }
  371. $data['add_time'] += 1;
  372. }
  373. if (!$userServices->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
  374. throw new ApiException(410285);
  375. }
  376. return true;
  377. }
  378. /**
  379. * 会员等级列表
  380. * @param int $uid
  381. */
  382. public function grade(int $uid)
  383. {
  384. //商城会员是否开启
  385. if (!sys_config('member_func_status')) {
  386. return [];
  387. }
  388. /** @var UserServices $userServices */
  389. $userServices = app()->make(UserServices::class);
  390. $user = $userServices->getUserInfo($uid);
  391. if (!$user) {
  392. throw new ApiException(410284);
  393. }
  394. $userLevelInfo = $this->getUerLevelInfoByUid($uid);
  395. if (empty($userLevelInfo)) {
  396. $level_id = 0;
  397. } else {
  398. $level_id = $userLevelInfo['level_id'];
  399. }
  400. /** @var SystemUserLevelServices $systemUserLevel */
  401. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  402. return $systemUserLevel->getLevelListAndGrade($level_id);
  403. }
  404. /**
  405. * 获取会员信息
  406. * @param int $uid
  407. * @return array[]
  408. */
  409. public function getUserLevelInfo(int $uid)
  410. {
  411. $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
  412. //商城会员是否开启
  413. if (!sys_config('member_func_status')) {
  414. return $data;
  415. }
  416. /** @var UserServices $userServices */
  417. $userServices = app()->make(UserServices::class);
  418. $user = $userServices->getUserInfo($uid);
  419. if (!$user) {
  420. throw new ApiException(410032);
  421. }
  422. $data['user'] = $user;
  423. /** @var SystemUserLevelServices $systemUserLevel */
  424. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  425. $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
  426. $i = 0;
  427. foreach ($levelList as &$level) {
  428. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  429. $level['image'] = set_file_url($level['image']);
  430. $level['icon'] = set_file_url($level['icon']);
  431. $i++;
  432. }
  433. $data['level_list'] = $levelList;
  434. $data['level_info'] = $this->getUerLevelInfoByUid($uid);
  435. $data['level_info']['exp'] = $user['exp'] ?? 0;
  436. /** @var UserBillServices $userBillservices */
  437. $userBillservices = app()->make(UserBillServices::class);
  438. $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
  439. $task = [];
  440. /** @var UserSignServices $userSignServices */
  441. $userSignServices = app()->make(UserSignServices::class);
  442. $task['sign_count'] = $userSignServices->getSignSumDay($uid);
  443. $task['sign'] = sys_config('sign_give_exp', 0);
  444. $task['order'] = sys_config('order_give_exp', 0);
  445. $task['invite'] = sys_config('invite_user_exp', 0);
  446. $data['task'] = $task;
  447. return $data;
  448. }
  449. /**
  450. * 经验列表
  451. * @param int $uid
  452. * @return array
  453. */
  454. public function expList(int $uid)
  455. {
  456. /** @var UserServices $userServices */
  457. $userServices = app()->make(UserServices::class);
  458. $user = $userServices->getUserInfo($uid);
  459. if (!$user) {
  460. throw new ApiException(410032);
  461. }
  462. /** @var UserBillServices $userBill */
  463. $userBill = app()->make(UserBillServices::class);
  464. $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
  465. $list = $data['list'] ?? [];
  466. return $list;
  467. }
  468. }