UserLevelServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. 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\services\FormBuilder as Form;
  18. use think\exception\ValidateException;
  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('修改会员信息失败');
  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('会员等级不存在');
  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('修改会员信息失败');
  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('写入会员信息失败');
  147. }
  148. if (!$user->update(['uid' => $uid], ['level' => $level_id, 'exp' => $vipinfo['exp_num']]))
  149. throw new AdminException('修改用户会员等级失败');
  150. return true;
  151. }
  152. /**
  153. * 会员列表
  154. * @param $where
  155. * @return mixed
  156. */
  157. public function getSytemList($where)
  158. {
  159. /** @var SystemUserLevelServices $systemLevelServices */
  160. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  161. return $systemLevelServices->getLevelList($where);
  162. }
  163. /**
  164. * 获取添加修改需要表单数据
  165. * @param int $id
  166. * @return array
  167. * @throws \FormBuilder\Exception\FormBuilderException
  168. */
  169. public function edit(int $id)
  170. {
  171. if ($id) {
  172. $vipinfo = app()->make(SystemUserLevelServices::class)->getlevel($id);
  173. $vipinfo->image = set_file_url($vipinfo->image);
  174. $vipinfo->icon = set_file_url($vipinfo->icon);
  175. if (!$vipinfo) {
  176. throw new AdminException('数据不存在');
  177. }
  178. $field[] = Form::hidden('id', $id);
  179. $msg = '编辑会员等级';
  180. } else {
  181. $msg = '添加会员等级';
  182. }
  183. $field[] = Form::input('name', '等级名称', isset($vipinfo) ? $vipinfo->name : '')->col(24);
  184. // $field[] = Form::number('valid_date', '有效时间(天)', isset($vipinfo) ? $vipinfo->valid_date : 0)->min(0)->col(12);
  185. $field[] = Form::number('grade', '等级', isset($vipinfo) ? $vipinfo->grade : 0)->min(0)->precision(0)->col(8);
  186. $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 100)->min(0)->col(8)->placeholder('输入折扣数100,代表原价,90代表9折');
  187. $field[] = Form::number('exp_num', '解锁需经验值达到', isset($vipinfo) ? $vipinfo->exp_num : 0)->min(0)->precision(0)->col(8);
  188. $field[] = Form::frameImage('icon', '图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'icon')), isset($vipinfo) ? $vipinfo->icon : '')->icon('ios-add')->width('60%')->height('435px');
  189. $field[] = Form::frameImage('image', '会员背景', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), isset($vipinfo) ? $vipinfo->image : '')->icon('ios-add')->width('60%')->height('435px');
  190. $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  191. $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '');
  192. return create_form($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
  193. }
  194. /*
  195. * 会员等级添加或者修改
  196. * @param $id 修改的等级id
  197. * @return json
  198. * */
  199. public function save(int $id, array $data)
  200. {
  201. /** @var SystemUserLevelServices $systemUserLevel */
  202. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  203. $levelOne = $systemUserLevel->getWhereLevel(['is_del' => 0, 'grade' => $data['grade']]);
  204. $levelTwo = $systemUserLevel->getWhereLevel(['is_del' => 0, 'exp_num' => $data['exp_num']]);
  205. $levelThree = $systemUserLevel->getWhereLevel(['is_del' => 0, 'name' => $data['name']]);
  206. $levelPre = $systemUserLevel->getPreLevel($data['grade']);
  207. $levelNext = $systemUserLevel->getNextLevel($data['grade']);
  208. if ($levelPre && $data['exp_num'] <= $levelPre['exp_num']) {
  209. throw new AdminException('会员经验必须大于上一等级设置的经验');
  210. }
  211. if ($levelNext && $data['exp_num'] >= $levelNext['exp_num']) {
  212. throw new AdminException('会员经验必须小于下一等级设置的经验');
  213. }
  214. //修改
  215. if ($id) {
  216. if (($levelOne && $levelOne['id'] != $id) || ($levelThree && $levelThree['id'] != $id)) {
  217. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  218. }
  219. if ($levelTwo && $levelTwo['id'] != $id) {
  220. throw new AdminException('已检测到您设置过该会员经验值,经验值不可重复');
  221. }
  222. if (!$systemUserLevel->update($id, $data)) {
  223. throw new AdminException('修改失败');
  224. }
  225. return '修改成功';
  226. } else {
  227. if ($levelOne || $levelThree) {
  228. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  229. }
  230. if ($levelTwo) {
  231. throw new AdminException('已检测到您设置过该会员经验值,经验值不可重复');
  232. }
  233. //新增
  234. $data['add_time'] = time();
  235. if (!$systemUserLevel->save($data)) {
  236. throw new AdminException('添加失败');
  237. }
  238. return '添加成功';
  239. }
  240. }
  241. /**
  242. * 假删除
  243. * @param int $id
  244. * @return mixed
  245. */
  246. public function delLevel(int $id)
  247. {
  248. /** @var SystemUserLevelServices $systemUserLevel */
  249. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  250. $level = $systemUserLevel->getWhereLevel(['id' => $id]);
  251. if ($level && $level['is_del'] != 1) {
  252. if (!$systemUserLevel->update($id, ['is_del' => 1]))
  253. throw new AdminException('删除失败');
  254. }
  255. return '删除成功';
  256. }
  257. /**
  258. * 设置是否显示
  259. * @param int $id
  260. * @param $is_show
  261. * @return mixed
  262. */
  263. public function setShow(int $id, int $is_show)
  264. {
  265. /** @var SystemUserLevelServices $systemUserLevel */
  266. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  267. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  268. throw new AdminException('数据不存在');
  269. if ($systemUserLevel->update($id, ['is_show' => $is_show])) {
  270. return $is_show == 1 ? '显示成功' : '隐藏成功';
  271. } else {
  272. throw new AdminException($is_show == 1 ? '显示失败' : '隐藏失败');
  273. }
  274. }
  275. /**
  276. * 快速修改
  277. * @param int $id
  278. * @param $is_show
  279. * @return mixed
  280. */
  281. public function setValue(int $id, array $data)
  282. {
  283. /** @var SystemUserLevelServices $systemUserLevel */
  284. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  285. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  286. throw new AdminException('数据不存在');
  287. if ($systemUserLevel->update($id, [$data['field'] => $data['value']])) {
  288. return true;
  289. } else {
  290. throw new AdminException('保存失败');
  291. }
  292. }
  293. /**
  294. * 检测用户会员升级
  295. * @param $uid
  296. * @return bool
  297. */
  298. public function detection(int $uid)
  299. {
  300. //商城会员是否开启
  301. if (!sys_config('member_func_status')) {
  302. return true;
  303. }
  304. /** @var UserServices $userServices */
  305. $userServices = app()->make(UserServices::class);
  306. $user = $userServices->getUserInfo($uid);
  307. if (!$user) {
  308. throw new ValidateException('没有此用户,无法检测升级会员');
  309. }
  310. /** @var SystemUserLevelServices $systemUserLevel */
  311. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  312. $userAllLevel = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']]]);
  313. if (!$userAllLevel) {
  314. return true;
  315. }
  316. $data = [];
  317. $data['add_time'] = time();
  318. $userLevel = $this->dao->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
  319. foreach ($userAllLevel as $vipinfo) {
  320. if (in_array($vipinfo['id'], $userLevel)) {
  321. continue;
  322. }
  323. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  324. $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  325. if ($uservip) {
  326. //降级在升级情况
  327. $data['status'] = 1;
  328. $data['is_del'] = 0;
  329. if (!$this->dao->update($uservip['id'], $data, 'id')) {
  330. throw new ValidateException('检测升级失败');
  331. }
  332. } else {
  333. $data = array_merge($data, [
  334. 'is_forever' => $vipinfo['is_forever'],
  335. 'status' => 1,
  336. 'is_del' => 0,
  337. 'grade' => $vipinfo['grade'],
  338. 'uid' => $uid,
  339. 'level_id' => $vipinfo['id'],
  340. 'discount' => $vipinfo['discount'],
  341. ]);
  342. if (!$this->dao->save($data)) {
  343. throw new ValidateException('检测升级失败');
  344. }
  345. }
  346. $data['add_time'] += 1;
  347. }
  348. if (!$userServices->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
  349. throw new ValidateException('检测升级失败');
  350. }
  351. return true;
  352. }
  353. /**
  354. * 会员等级列表
  355. * @param int $uid
  356. */
  357. public function grade(int $uid)
  358. {
  359. //商城会员是否开启
  360. if (!sys_config('member_func_status')) {
  361. return [];
  362. }
  363. /** @var UserServices $userServices */
  364. $userServices = app()->make(UserServices::class);
  365. $user = $userServices->getUserInfo($uid);
  366. if (!$user) {
  367. throw new ValidateException('没有此用户,无法检测升级会员');
  368. }
  369. $userLevelInfo = $this->getUerLevelInfoByUid($uid);
  370. if (empty($userLevelInfo)) {
  371. $level_id = 0;
  372. } else {
  373. $level_id = $userLevelInfo['level_id'];
  374. }
  375. /** @var SystemUserLevelServices $systemUserLevel */
  376. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  377. return $systemUserLevel->getLevelListAndGrade($level_id);
  378. }
  379. /**
  380. * 获取会员信息
  381. * @param int $uid
  382. * @return array[]
  383. */
  384. public function getUserLevelInfo(int $uid)
  385. {
  386. $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
  387. //商城会员是否开启
  388. if (!sys_config('member_func_status')) {
  389. return $data;
  390. }
  391. /** @var UserServices $userServices */
  392. $userServices = app()->make(UserServices::class);
  393. $user = $userServices->getUserInfo($uid);
  394. if (!$user) {
  395. throw new ValidateException('没有此会员');
  396. }
  397. $data['user'] = $user;
  398. /** @var SystemUserLevelServices $systemUserLevel */
  399. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  400. $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
  401. $i = 0;
  402. foreach ($levelList as &$level) {
  403. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  404. $level['image'] = set_file_url($level['image']);
  405. $level['icon'] = set_file_url($level['icon']);
  406. $i++;
  407. }
  408. $data['level_list'] = $levelList;
  409. $data['level_info'] = $this->getUerLevelInfoByUid($uid);
  410. $data['level_info']['exp'] = $user['exp'] ?? 0;
  411. /** @var UserBillServices $userBillservices */
  412. $userBillservices = app()->make(UserBillServices::class);
  413. $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
  414. $task = [];
  415. /** @var UserSignServices $userSignServices */
  416. $userSignServices = app()->make(UserSignServices::class);
  417. $task['sign_count'] = $userSignServices->getSignSumDay($uid);
  418. $task['sign'] = sys_config('sign_give_exp', 0);
  419. $task['order'] = sys_config('order_give_exp', 0);
  420. $task['invite'] = sys_config('invite_user_exp', 0);
  421. $data['task'] = $task;
  422. return $data;
  423. }
  424. /**
  425. * 经验列表
  426. * @param int $uid
  427. * @return array
  428. */
  429. public function expList(int $uid)
  430. {
  431. /** @var UserServices $userServices */
  432. $userServices = app()->make(UserServices::class);
  433. $user = $userServices->getUserInfo($uid);
  434. if (!$user) {
  435. throw new ValidateException('没有此用户');
  436. }
  437. /** @var UserBillServices $userBill */
  438. $userBill = app()->make(UserBillServices::class);
  439. $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
  440. $list = $data['list'] ?? [];
  441. return $list;
  442. }
  443. }