AwardIntegralServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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\dao\user\AwardIntegralDao;
  14. use app\model\system\AwardLake;
  15. use app\model\user\UserIntegral;
  16. use app\services\BaseServices;
  17. use crmeb\services\FormBuilder;
  18. use crmeb\traits\ServicesTrait;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\Exception;
  23. use think\facade\Route;
  24. /**
  25. *
  26. * Class UserRechargeServices
  27. * @package app\services\user
  28. * @mixin AwardIntegralDao
  29. */
  30. class AwardIntegralServices extends BaseServices
  31. {
  32. use ServicesTrait;
  33. /**
  34. * UserRechargeServices constructor.
  35. * @param AwardIntegralDao $dao
  36. */
  37. public function __construct(AwardIntegralDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 获取充值列表
  43. * @param array $where
  44. * @param string $field
  45. * @param int $limit
  46. * @return array
  47. */
  48. public function getIntegralList(array $where, string $field = '*', int $limit = 0, array $with = ['user_order'])
  49. {
  50. $whereData = $where;
  51. if ($limit) {
  52. [$page] = $this->getPageValue();
  53. } else {
  54. [$page, $limit] = $this->getPageValue();
  55. }
  56. $list = $this->dao->getList($whereData, $field, $page, $limit, $with);
  57. $count = $this->dao->count($whereData);
  58. foreach ($list as &$item) {
  59. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  60. }
  61. return compact('list', 'count');
  62. }
  63. /**
  64. * 获取单条数据
  65. * @param int $id
  66. * @param array $field
  67. */
  68. public function getIntegral(int $id, array $field = [])
  69. {
  70. return $this->dao->get($id, $field);
  71. }
  72. /**
  73. * 获取单条数据
  74. * @return float
  75. */
  76. public function getIntegralSum($where, $field = 'num')
  77. {
  78. return $this->dao->sum($where, $field, true);
  79. }
  80. /**
  81. * 获取单条数据
  82. * @return float
  83. */
  84. public function getPaySum($uid)
  85. {
  86. $where = ['uid' => $uid, 'valid' => 1];
  87. return $this->dao->sum($where, 'order_price', true);
  88. }
  89. /**
  90. * 获取单条数据
  91. * @return array|\think\Model|null
  92. */
  93. public function getFirstStaticIntegral($uid)
  94. {
  95. $where = ['uid' => $uid, 'valid' => 1];
  96. return $this->dao->search($where)->order('id', 'asc')->find();
  97. }
  98. /**
  99. * 获取用户业绩
  100. * @param $uid
  101. * @return float
  102. */
  103. public function getAchievement($uid)
  104. {
  105. $where = ['uid' => array_merge([$uid], get_group_user($uid))];
  106. $service = app()->make(UserAwardIntegralServices::class);
  107. return $this->dao->sum($where, 'order_price', true) + $service->getAchievement($uid, 1);
  108. }
  109. /**
  110. * 加积分
  111. * @param int $uid 用户
  112. * @param string $price 价格
  113. * @param string $total 总额
  114. * @param float $order_price
  115. * @param int $type 类型
  116. * @param string $extract_sum
  117. * @param int $link_id 关联ID
  118. * @param string $mark
  119. * @return \crmeb\basic\BaseModel|\think\Model
  120. */
  121. public function incIntegral(int $uid, string $total, float $order_price, int $order_id = 0, string $mark = '', $add_time = 0)
  122. {
  123. // $inc_integral = bcdiv($total, $price, 5);
  124. //计算释放天数
  125. $default_send_day = sys_config('default_send_day', 90);
  126. $reorder_inc = sys_config('reorder_int_send_day', 10);
  127. $order_inc = sys_config('int_send_day', 10);
  128. //寻找复投机会
  129. $main_orders = $this->dao->search(['uid' => $uid, 'link_id' => 0, 'order_price_elt' => $order_price, 'valid' => 2])->order('id asc')->column('id');//满足复投条件的主单
  130. $link_id = 0;
  131. $reorder_count = 0;
  132. foreach ($main_orders as $v) {
  133. $can_reorder = $this->dao->search(['uid' => $uid, 'valid' => 1, 'link_id' => $v])->find();
  134. if (!$can_reorder) {
  135. $link_id = $v;
  136. $reorder_count = ($this->dao->search(['uid' => $uid, 'link_id' => $v])->max('reorder_count')) + 1;
  137. break;
  138. }
  139. }
  140. $last_order_send_day = $this->dao->search(['uid' => $uid])->max('send_day');
  141. if (!$last_order_send_day) $send_day = $default_send_day;
  142. else {
  143. if ($link_id > 0) $send_day = $last_order_send_day + $reorder_inc;
  144. else $send_day = $last_order_send_day + $order_inc;
  145. }
  146. return $this->dao->save([
  147. 'uid' => $uid,
  148. 'num' => $total,
  149. 'send_day' => $send_day,
  150. 'day_send' => bcdiv((string)$total, (string)$send_day, 2),
  151. 'sum_price' => $total,
  152. 'order_id' => $order_id,
  153. 'link_id' => $link_id,
  154. 'reorder_count' => $reorder_count,
  155. 'mark' => $mark,
  156. 'order_price' => $order_price,
  157. 'add_time' => $add_time ?: time(),
  158. 'status' => $total > 0 ? 0 : 1,
  159. 'send_time' => time()
  160. ]);
  161. }
  162. // public function incIntegralAdmin(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '', $add_time = 0)
  163. // {
  164. // $inc_integral = bcdiv($total, $price, 5);
  165. // $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->whereLike('mark', '%' . $mark . '%')->find();
  166. // if ($old) {
  167. // $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5);
  168. // } else {
  169. // $dump = '用户' . $uid . $mark . '补' . $inc_integral;
  170. // }
  171. // @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND);
  172. // if ($old) {
  173. // UserIntegral::where('id', $old['id'])->delete();
  174. // }
  175. // return UserIntegral::create([
  176. // 'uid' => $uid,
  177. // 'type' => $type == 1 ? 1 : 0,
  178. // 'num' => $inc_integral,
  179. // 'price' => $price,
  180. // 'sum_price' => $total,
  181. // 'extract_sum' => $extract_sum,
  182. // 'link_id' => $link_id,
  183. // 'mark' => $mark,
  184. // 'order_price' => $order_price,
  185. // 'add_time' => $add_time,
  186. // 'status' => $inc_integral > 0 ? 0 : 1,
  187. // ]);
  188. //// return true;
  189. // }
  190. // public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  191. // {
  192. // if ($price > 0) {
  193. // $inc_integral = bcdiv($total, $price, 5);
  194. // } else {
  195. // $inc_integral = $total;
  196. // }
  197. // $origin = $this->get($id);
  198. // return $this->dao->update($id, [
  199. // 'num' => bcadd($origin['num'], $inc_integral, 5),
  200. // 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  201. // ]);
  202. // }
  203. public function upSpeedIntegral(int $id, string $total)
  204. {
  205. $origin = $this->get($id);
  206. $real_send = $total;
  207. $left = $origin['num'] - $origin['sent_num'];
  208. $update = [];
  209. if ($real_send >= $left) {
  210. $real_send = $left;
  211. $update['up_speed_used'] = $origin['up_speed'];
  212. $update['sent_num'] = $origin['num'];
  213. $update['status'] = 1;
  214. } else {
  215. $update['sent_num'] = bcadd((string)$origin['sent_num'], (string)$real_send, 2);
  216. }
  217. $update['extract_num'] = bcadd((string)$real_send, (string)$origin['extract_num'], 2);
  218. $res = $this->dao->update($id, $update);
  219. /** @var UserBillServices $billService */
  220. $billService = app()->make(UserBillServices::class);
  221. $billService->income('integral_speed_up', $origin['uid'], $real_send, $this->getIntegralSum(['uid' => $origin['uid']], 'extract_num') + $real_send, $origin['id']);
  222. if (($update['status'] ?? 0) == 1) {
  223. $res = $res && $this->extract($origin['id'], $update['extract_num'], true);
  224. }
  225. return $res;
  226. // $inc_integral = $total;
  227. // return $this->dao->update($id, [
  228. // 'up_speed' => bcadd((string)$origin['up_speed'], (string)$inc_integral, 2),
  229. // ]);
  230. }
  231. //每日释放
  232. public function daySend($test = false)
  233. {
  234. $sum = $this->dao->search()->whereTime('add_time', 'yesterday')->sum('order_price');
  235. $system_check = sys_config('system_achievement_check', 10000);
  236. $list = $this->dao->search()->where('status', 0)->where('sent_num<num')->where('send_time', '<', $test ? (time() + 1000) : strtotime('today'))->limit($test ? 10000 : 50)->select();
  237. foreach ($list as $v) {
  238. $day_send = $v['day_send'];
  239. $send_day = $v['send_day'];
  240. $up_speed = $v['up_speed'] - $v['up_speed_used'];
  241. if ($sum < $system_check) {
  242. if (!$test) $send_day = $v['send_day'] + sys_config('fail_inc_send_day', 1);
  243. $day_send = bcdiv((string)(($v['num'] - $v['sent_num'] + $v['up_speed_used'])), (string)($send_day - $v['sent_day']), 2);
  244. }
  245. $up_speed = bcdiv((string)$up_speed, (string)($send_day - $v['sent_day']), 2);
  246. $real_send = $day_send + $up_speed;
  247. $left = $v['num'] - $v['sent_num'];
  248. $update = [];
  249. $update['day_send'] = $day_send;
  250. $update['up_speed_used'] = $v['up_speed_used'] + $up_speed;
  251. $update['sent_day'] = $v['sent_day'] + 1;
  252. $update['send_day'] = $send_day;
  253. if ($update['sent_day'] == $update['send_day']) {
  254. $real_send = $left;
  255. }
  256. if ($real_send >= $left) {
  257. $real_send = $left;
  258. $update['up_speed_used'] = $v['up_speed'];
  259. $update['sent_num'] = $v['num'];
  260. $update['status'] = 1;
  261. } else {
  262. $update['sent_num'] = bcadd((string)$v['sent_num'], (string)$real_send, 2);
  263. }
  264. $update['extract_num'] = bcadd((string)$real_send, (string)$v['extract_num'], 2);
  265. $update['send_time'] = time();
  266. $this->dao->update($v['id'], $update);
  267. /** @var UserBillServices $billService */
  268. $billService = app()->make(UserBillServices::class);
  269. $billService->income('integral_day_send', $v['uid'], $real_send, $this->getIntegralSum(['uid' => $v['uid']], 'extract_num') + $real_send, $v['id']);
  270. if (($update['status'] ?? 0) == 1) {
  271. $this->extract($v['id'], $update['extract_num'], true);
  272. }
  273. }
  274. }
  275. //提取
  276. public function extract($id, $num, $system = false)
  277. {
  278. $origin = $this->get($id);
  279. if ($origin['extract_num'] < $num) {
  280. if (!$system) {
  281. throw new Exception('可提取参考分不足');
  282. }
  283. $num = $origin['extract_num'];
  284. }
  285. if ($num <= 0) return true;
  286. $update['extract_num'] = bcsub((string)$origin['extract_num'], (string)$num, 2);
  287. $update['extracted_num'] = bcadd((string)$origin['extracted_num'], (string)$num, 2);
  288. $this->update($id, $update);
  289. $res = true;
  290. /** @var UserBrokerageServices $brokerageService */
  291. $brokerageService = app()->make(UserBrokerageServices::class);
  292. /** @var UserBillServices $billService */
  293. $billService = app()->make(UserBillServices::class);
  294. /** @var UserMoneyServices $moneyService */
  295. $moneyService = app()->make(UserMoneyServices::class);
  296. /** @var UserServices $userService */
  297. $userService = app()->make(UserServices::class);
  298. $billService->income('extract_award_integral', $origin['uid'], $num, $this->getIntegralSum(['uid' => $origin['uid']], 'extract_num') - $num, $id);
  299. $uid = $origin['uid'];
  300. $user = $userService->getUserInfo($uid);
  301. $reorder_set = sys_config('reorder_times', 2);
  302. $to_integral = sys_config('shop_integral_ratio');
  303. $mark = '用户参考分' . ($system ? '出局' : '提取') . $num;
  304. $to_integral = bcdiv(bcmul((string)$num, (string)$to_integral), '100', 2);
  305. if ($to_integral > 0) {
  306. $balance = bcadd((string)$user['integral'], (string)$to_integral, 2);
  307. $res = $res && $billService->income('extract_integral_shop', $uid, [
  308. 'mark' => $mark,
  309. 'number' => floatval($to_integral),
  310. ], $balance, $id);
  311. // 添加用户佣金
  312. $res = $res && $userService->bcInc($uid, 'integral', (string)$to_integral, 'uid');
  313. }
  314. if ($to_integral < $num) {
  315. $left = bcsub((string)$num, (string)$to_integral, 2);
  316. if ($reorder_set >= $origin['reorder_count']) {
  317. $to_now_money = sys_config('re_now_money');
  318. $to_now_money = bcdiv(bcmul((string)$left, (string)$to_now_money), '100', 2);
  319. if ($to_now_money > 0) {
  320. $balance = bcadd((string)$user['now_money'], (string)$to_now_money, 2);
  321. $res = $res && $moneyService->income('extract_integral_static', $uid, [
  322. 'mark' => $mark,
  323. 'number' => floatval($to_now_money),
  324. ], $balance, $id);
  325. // 添加用户佣金
  326. $res = $res && $userService->bcInc($uid, 'now_money', $to_now_money, 'uid');
  327. }
  328. $to_energy = sys_config('re_energy');
  329. $to_energy = bcdiv(bcmul((string)$left, (string)$to_energy), '100', 2);
  330. if ($to_energy > 0) {
  331. $balance = bcadd((string)$user['energy'], (string)$to_energy, 2);
  332. $res = $res && $billService->income('extract_integral_static', $uid, [
  333. 'mark' => $mark,
  334. 'number' => floatval($to_energy),
  335. ], $balance, $id);
  336. // 添加用户佣金
  337. $res = $res && $userService->bcInc($uid, 'energy', $to_energy, 'uid');
  338. }
  339. $to_brokerage = 100 - $to_now_money - $to_energy;
  340. if ($to_brokerage < 0) $to_brokerage = 0;
  341. $to_brokerage = bcdiv(bcmul((string)$left, (string)$to_brokerage), '100', 2);
  342. if ($to_brokerage > 0) {
  343. $balance = bcadd((string)$user['brokerage_price'], (string)$to_brokerage, 2);
  344. $res = $res && $brokerageService->income('extract_integral_static', $uid, [
  345. 'mark' => $mark,
  346. 'number' => floatval($to_energy),
  347. ], $balance, $id);
  348. // 添加用户佣金
  349. $res = $res && $userService->bcInc($uid, 'brokerage_price', $to_brokerage, 'uid');
  350. }
  351. } else {
  352. //消费分,佣金
  353. $balance = bcadd((string)$user['brokerage_price'], (string)$left, 2);
  354. $res = $res && $brokerageService->income('extract_integral_static', $uid, [
  355. 'mark' => $mark,
  356. 'number' => floatval($left),
  357. ], $balance, $id);
  358. // 添加用户佣金
  359. $res = $res && $userService->bcInc($uid, 'brokerage_price', $left, 'uid');
  360. }
  361. }
  362. return $res;
  363. }
  364. }