UserIntegralServices.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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;
  12. use app\dao\user\UserBillDao;
  13. use app\jobs\user\UserIntegralJob;
  14. use app\services\BaseServices;
  15. /**
  16. * 用户积分
  17. * Class UserIntegralServices
  18. * @package app\services\user
  19. * @mixin UserBillDao
  20. */
  21. class UserIntegralServices extends BaseServices
  22. {
  23. /**
  24. * UserIntegralServices constructor.
  25. * @param UserBillDao $dao
  26. */
  27. public function __construct(UserBillDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 清空到期积分(分批加入队列)
  33. * @return bool
  34. */
  35. public function clearExpireIntegral()
  36. {
  37. //是否开启积分有效期
  38. if (!sys_config('integral_effective_status', 0)) {
  39. return true;
  40. }
  41. /** @var UserServices $userServices */
  42. $userServices = app()->make(UserServices::class);
  43. $users = $userServices->getColumn([['integral', '>', 0]], 'uid');
  44. if ($users) {
  45. //拆分大数组
  46. $uidsArr = array_chunk($users, 100);
  47. foreach ($uidsArr as $uids) {
  48. //加入同步|更新用户队列
  49. UserIntegralJob::dispatch([$uids]);
  50. }
  51. }
  52. return true;
  53. }
  54. /**
  55. * 执行清空到期积分
  56. * @param array $uids
  57. * @return bool
  58. */
  59. public function doClearExpireIntegral(array $uids)
  60. {
  61. if (!$uids) return true;
  62. //是否开启积分有效期
  63. if (!sys_config('integral_effective_status', 0)) {
  64. return true;
  65. }
  66. [$clear_time, $start_time, $end_time] = $this->getTime();
  67. $start = date('Y年m月d日', $start_time);
  68. $end = date('Y年m月d日', $end_time);
  69. $where = ['category' => 'integral', 'pm' => 1, 'status' => 1];
  70. $where['add_time'] = [$start_time, $end_time];
  71. /** @var UserServices $userServices */
  72. $userServices = app()->make(UserServices::class);
  73. $users = $userServices->getColumn([['uid', 'in', $uids]], 'uid,integral', 'uid');
  74. /** @var UserBillServices $userBillServices */
  75. $userBillServices = app()->make(UserBillServices::class);
  76. //查询是否清除过
  77. $clear_uids = $userBillServices->getColumn([['category', '=', 'integral'], ['type', '=', 'system_clear'], ['add_time', '>=', $clear_time], ['add_time', '<=', $clear_time + 86400]], 'uid');
  78. foreach ($uids as $uid) {
  79. $number = 0;
  80. if (!isset($users[$uid]) || in_array($uid, $clear_uids)) continue;
  81. $user = $users[$uid];
  82. $where['uid'] = $uid;
  83. $where['pm'] = 1;
  84. $userSumIntegralInc = $userBillServices->getBillSum($where);
  85. $where['pm'] = 0;
  86. $userSumIntegralDec = $userBillServices->getBillSum($where);
  87. $userSumIntegral = $userSumIntegralInc > $userSumIntegralDec ? (int)bcsub((string)$userSumIntegralInc, (string)$userSumIntegralDec, 0) : 0;
  88. if ($userSumIntegral) {
  89. $user_data = [];
  90. $number = $userSumIntegral;
  91. if ($userSumIntegral >= $user['integral']) {
  92. $number = $user['integral'];
  93. $user_data['integral'] = 0;
  94. } else {
  95. $user_data['integral'] = (int)bcsub((string)$user['integral'], (string)$userSumIntegral, 0);
  96. }
  97. //记录清除积分
  98. $userBillServices->income('system_clear_integral', $uid, (int)$number, (int)$user_data['integral'], $uid);
  99. $userServices->update($uid, $user_data);
  100. }
  101. }
  102. return true;
  103. }
  104. /**
  105. * 获取用户清空到期积分
  106. * @param int $uid
  107. * @param array $user
  108. * @return array|int[]
  109. */
  110. public function getUserClearIntegral(int $uid, $user = [])
  111. {
  112. if (!$uid) return [0, 0];
  113. //是否开启积分有效期
  114. if (!sys_config('integral_effective_status', 0)) {
  115. return [0, 0];
  116. }
  117. [$clear_time, $start_time, $end_time] = $this->getTime();
  118. $where = ['category' => 'integral', 'pm' => 1, 'status' => 1];
  119. $where['add_time'] = [$start_time, $end_time];
  120. if (!$user) {
  121. /** @var UserServices $userServices */
  122. $userServices = app()->make(UserServices::class);
  123. $user = $userServices->get($uid, 'uid,integral');
  124. }
  125. /** @var UserBillServices $userBillServices */
  126. $userBillServices = app()->make(UserBillServices::class);
  127. $where['uid'] = $uid;
  128. $userSumIntegralInc = $userBillServices->getBillSum($where);
  129. $where['pm'] = 0;
  130. $userSumIntegralDec = $userBillServices->getBillSum($where);
  131. $userSumIntegral = $userSumIntegralInc > $userSumIntegralDec ? (int)bcsub((string)$userSumIntegralInc, (string)$userSumIntegralDec, 0) : 0;
  132. if ($userSumIntegral) {
  133. if ($userSumIntegral >= $user['integral']) {
  134. $userSumIntegral = $user['integral'];
  135. }
  136. }
  137. return [$userSumIntegral, $clear_time];
  138. }
  139. /**
  140. * 获取清空积分时间段
  141. * @param int $type
  142. * @return int[]
  143. */
  144. public function getTime(int $type = 0)
  145. {
  146. if (!$type) $type = (int)sys_config('integral_effective_time', 3);
  147. switch ($type) {
  148. case 1://月
  149. $start = date('Y-m-01 00:00:00', strtotime('-1 month'));
  150. $end = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
  151. $clear_end = date('Y-m-t', strtotime(date('Y-m-d')));
  152. break;
  153. case 2://季度
  154. $season = ceil((date('n')) / 3) - 1;//上季度是第几季度
  155. $start = date('Y-m-d 00:00:00', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
  156. $end = date('Y-m-d 23:59:59', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
  157. $clear_end = date('Y-m-t', mktime(0, 0, 0, ($season + 1) * 3, 1, date('Y')));
  158. break;
  159. case 3://年
  160. default://默认年
  161. $start = date('Y-01-01 00:00:00', strtotime('-1 year'));
  162. $end = date('Y-m-d 23:59:59', strtotime($start . "+12 month -1 day"));
  163. $clear_end = date('Y-12-31');
  164. break;
  165. }
  166. return [strtotime($clear_end), strtotime($start), strtotime($end)];
  167. }
  168. /**
  169. * 新人礼赠送积分
  170. * @param int $uid
  171. * @return bool
  172. */
  173. public function newcomerGiveIntegral(int $uid)
  174. {
  175. if (!sys_config('newcomer_status')) {
  176. return false;
  177. }
  178. $status = sys_config('register_integral_status');
  179. if (!$status) {//未开启
  180. return true;
  181. }
  182. $integral = (int)sys_config('register_give_integral', []);
  183. if (!$integral) {
  184. return true;
  185. }
  186. /** @var UserServices $userServices */
  187. $userServices = app()->make(UserServices::class);
  188. $userInfo = $userServices->getUserInfo($uid);
  189. if (!$userInfo) {
  190. return true;
  191. }
  192. $balance = bcadd((string)$userInfo['integral'], (string)$integral, 2);
  193. /** @var UserBillServices $userBillServices */
  194. $userBillServices = app()->make(UserBillServices::class);
  195. $userBillServices->income('newcomer_give_integral', $uid, (int)$integral, (int)$balance);
  196. $userServices->update($uid, ['integral' => $balance]);
  197. return true;
  198. }
  199. /**
  200. * 会员卡激活赠送积分
  201. * @param int $uid
  202. * @return bool
  203. */
  204. public function levelGiveIntegral(int $uid)
  205. {
  206. /** @var UserServices $userServices */
  207. $userServices = app()->make(UserServices::class);
  208. $userInfo = $userServices->getUserInfo($uid);
  209. if (!$userInfo) {
  210. return true;
  211. }
  212. $status = sys_config('level_activate_status');
  213. if (!$status) {//是否需要激活
  214. return true;
  215. }
  216. $status = sys_config('level_integral_status');
  217. if (!$status) {//未开启
  218. return true;
  219. }
  220. $integral = (int)sys_config('level_give_integral', []);
  221. if (!$integral) {
  222. return true;
  223. }
  224. $balance = bcadd((string)$userInfo['integral'], (string)$integral, 2);
  225. /** @var UserBillServices $userBillServices */
  226. $userBillServices = app()->make(UserBillServices::class);
  227. $userBillServices->income('level_give_integral', $uid, (int)$integral, (int)$balance);
  228. $userServices->update($uid, ['integral' => $balance]);
  229. return true;
  230. }
  231. }