AwardIntegralServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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' => 3];
  87. return $this->dao->sum($where, 'order_price', true);
  88. }
  89. /**
  90. * 获取单条数据
  91. * @return float
  92. */
  93. public function getPaySum2($uid)
  94. {
  95. $where = ['uid' => $uid, 'valids' => 3];
  96. return $this->dao->sum($where, 'order_price', true);
  97. }
  98. /**
  99. * 获取单条数据
  100. * @return array|\think\Model|null
  101. */
  102. public function getFirstStaticIntegral($uid)
  103. {
  104. $where = ['uid' => $uid, 'valid' => 1];
  105. return $this->dao->search($where)->order('id', 'asc')->find();
  106. }
  107. /**
  108. * 获取用户业绩
  109. * @param $uid
  110. * @return float
  111. */
  112. public function getAchievement($uid)
  113. {
  114. $where = ['uid' => array_merge([$uid], get_group_user($uid))];
  115. $service = app()->make(UserAwardIntegralServices::class);
  116. return $this->dao->sum($where, 'order_price', true) + $service->getAchievement($uid, 1);
  117. }
  118. /**
  119. * 加积分
  120. * @param int $uid 用户
  121. * @param string $price 价格
  122. * @param string $total 总额
  123. * @param float $order_price
  124. * @param int $type 类型
  125. * @param string $extract_sum
  126. * @param int $link_id 关联ID
  127. * @param string $mark
  128. * @return \crmeb\basic\BaseModel|\think\Model
  129. */
  130. public function incIntegral(int $uid, string $total, float $order_price, int $order_id = 0, string $mark = '', $add_time = 0, $pay_type = 'yue', $yue_pay_price = 0)
  131. {
  132. if ($pay_type == 'yue') {
  133. $yue_pay_price = $order_price;
  134. }
  135. // $inc_integral = bcdiv($total, $price, 5);
  136. //计算释放天数
  137. $default_send_day = sys_config('default_send_day', 90);
  138. $rest_time = sys_config('default_send_day_start_time', 0);
  139. $reorder_inc = sys_config('reorder_int_send_day', 10);
  140. $order_inc = sys_config('int_send_day', 10);
  141. //寻找复投机会
  142. $main_orders = $this->dao->search(['uid' => $uid, 'link_id' => 0, 'order_price_elt' => $order_price, 'valid' => 2])->order('id asc')->column('id');//满足复投条件的主单
  143. $link_id = 0;
  144. $reorder_count = 0;
  145. foreach ($main_orders as $v) {
  146. $can_reorder = $this->dao->search(['uid' => $uid, 'valid' => 1, 'link_id' => $v])->find();
  147. if (!$can_reorder) {
  148. $link_id = $v;
  149. $reorder_count = ($this->dao->search(['uid' => $uid, 'link_id' => $v])->max('reorder_count')) + 1;
  150. break;
  151. }
  152. }
  153. $last_order_send_day = $this->dao->search(['uid' => $uid, 'add_time_gt' => $rest_time])->order('id desc')->value('send_day');
  154. if (!$last_order_send_day) $send_day = $default_send_day;
  155. else {
  156. if ($link_id > 0) $send_day = $last_order_send_day + $reorder_inc;
  157. else $send_day = $last_order_send_day + $order_inc;
  158. }
  159. $ratio = 10 * ($reorder_count - 1);
  160. if ($ratio > 0) {
  161. if ($ratio > 100) $ratio = 100;
  162. $yue_limit = bcdiv(bcmul(bcsub('100', (string)$ratio), (string)$order_price), '100', 2);
  163. if ($yue_pay_price > $yue_limit) {
  164. $add_integral = $ratio;
  165. }
  166. }
  167. return $this->dao->save([
  168. 'uid' => $uid,
  169. 'num' => $total,
  170. 'send_day' => $send_day,
  171. 'day_send' => bcdiv((string)$total, (string)$send_day, 2),
  172. 'sum_price' => $total,
  173. 'order_id' => $order_id,
  174. 'link_id' => $link_id,
  175. 'pay_type' => $pay_type,
  176. 'reorder_count' => $reorder_count,
  177. 'mark' => $mark,
  178. 'order_price' => $order_price,
  179. 'add_time' => $add_time ?: time(),
  180. 'status' => $total > 0 ? 0 : 1,
  181. 'yue_pay' => $yue_pay_price,
  182. 'send_time' => time(),
  183. 'add_integral' => $add_integral ?? 0
  184. ]);
  185. }
  186. public function checkReorder($uid, $order_price)
  187. {
  188. //寻找复投机会
  189. $main_orders = $this->dao->search(['uid' => $uid, 'link_id' => 0, 'order_price_elt' => $order_price, 'valid' => 2])->order('id asc')->column('id');//满足复投条件的主单
  190. $link_id = 0;
  191. $reorder_count = 0;
  192. foreach ($main_orders as $v) {
  193. $can_reorder = $this->dao->search(['uid' => $uid, 'valid' => 1, 'link_id' => $v])->find();
  194. if (!$can_reorder) {
  195. $link_id = $v;
  196. $reorder_count = ($this->dao->search(['uid' => $uid, 'link_id' => $v])->max('reorder_count')) + 1;
  197. break;
  198. }
  199. }
  200. return [$link_id, $reorder_count];
  201. }
  202. // 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)
  203. // {
  204. // $inc_integral = bcdiv($total, $price, 5);
  205. // $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->whereLike('mark', '%' . $mark . '%')->find();
  206. // if ($old) {
  207. // $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5);
  208. // } else {
  209. // $dump = '用户' . $uid . $mark . '补' . $inc_integral;
  210. // }
  211. // @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND);
  212. // if ($old) {
  213. // UserIntegral::where('id', $old['id'])->delete();
  214. // }
  215. // return UserIntegral::create([
  216. // 'uid' => $uid,
  217. // 'type' => $type == 1 ? 1 : 0,
  218. // 'num' => $inc_integral,
  219. // 'price' => $price,
  220. // 'sum_price' => $total,
  221. // 'extract_sum' => $extract_sum,
  222. // 'link_id' => $link_id,
  223. // 'mark' => $mark,
  224. // 'order_price' => $order_price,
  225. // 'add_time' => $add_time,
  226. // 'status' => $inc_integral > 0 ? 0 : 1,
  227. // ]);
  228. //// return true;
  229. // }
  230. // public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  231. // {
  232. // if ($price > 0) {
  233. // $inc_integral = bcdiv($total, $price, 5);
  234. // } else {
  235. // $inc_integral = $total;
  236. // }
  237. // $origin = $this->get($id);
  238. // return $this->dao->update($id, [
  239. // 'num' => bcadd($origin['num'], $inc_integral, 5),
  240. // 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  241. // ]);
  242. // }
  243. public function upSpeedIntegral(int $id, string $total)
  244. {
  245. $origin = $this->get($id);
  246. $real_send = $total;
  247. $left = $origin['num'] - $origin['sent_num'];
  248. $update = [];
  249. if ($real_send >= $left) {
  250. $real_send = $left;
  251. $update['up_speed_used'] = $origin['up_speed'];
  252. $update['sent_num'] = $origin['num'];
  253. $update['status'] = 1;
  254. } else {
  255. $update['sent_num'] = bcadd((string)$origin['sent_num'], (string)$real_send, 2);
  256. }
  257. $update['extract_num'] = bcadd((string)$real_send, (string)$origin['extract_num'], 2);
  258. $res = $this->dao->update($id, $update);
  259. /** @var UserBillServices $billService */
  260. $billService = app()->make(UserBillServices::class);
  261. $billService->income('integral_speed_up', $origin['uid'], $real_send, $this->getIntegralSum(['uid' => $origin['uid']], 'extract_num') + $real_send, $origin['id']);
  262. if (($update['status'] ?? 0) == 1) {
  263. $res = $res && $this->extract($origin['id'], $update['extract_num'], true);
  264. }
  265. return $res;
  266. // $inc_integral = $total;
  267. // return $this->dao->update($id, [
  268. // 'up_speed' => bcadd((string)$origin['up_speed'], (string)$inc_integral, 2),
  269. // ]);
  270. }
  271. //每日释放
  272. public function daySend($test = false)
  273. {
  274. $sum = $this->dao->search()->whereTime('add_time', 'yesterday')->sum('order_price');
  275. $system_check = sys_config('system_achievement_check', 10000);
  276. $list = $this->dao->search()->where('status', 0)->where('sent_num<num')->where('send_time', '<', $test ? (time() + 1000) : strtotime('today'))->limit($test ? 10000 : 50)->select();
  277. foreach ($list as $v) {
  278. $update = [];
  279. if ($sum < $system_check) {
  280. $v['stop_day'] = $v['stop_day'] + sys_config('fail_inc_send_day', 1);
  281. }
  282. if ($v['stop_day'] > 0) {
  283. // $this->dao->search()->where('id', $v['id'])->dec('stop_day', 1)->update();
  284. $update['send_time'] = time();
  285. $update['stop_day'] = $v['stop_day'] - 1;
  286. $this->dao->search()->where('id', $v['id'])->update($update);
  287. continue;
  288. }
  289. $day_send = $v['day_send'];
  290. $send_day = $v['send_day'];
  291. $up_speed = $v['up_speed'] - $v['up_speed_used'];
  292. $up_speed = bcdiv((string)$up_speed, (string)($send_day - $v['sent_day']), 2);
  293. $real_send = $day_send + $up_speed;
  294. $left = $v['num'] - $v['sent_num'];
  295. $update['day_send'] = $day_send;
  296. $update['up_speed_used'] = $v['up_speed_used'] + $up_speed;
  297. $update['sent_day'] = $v['sent_day'] + 1;
  298. $update['send_day'] = $send_day;
  299. if ($update['sent_day'] == $update['send_day']) {
  300. $real_send = $left;
  301. }
  302. if ($real_send >= $left) {
  303. $real_send = $left;
  304. $update['up_speed_used'] = $v['up_speed'];
  305. $update['sent_num'] = $v['num'];
  306. $update['status'] = 1;
  307. } else {
  308. $update['sent_num'] = bcadd((string)$v['sent_num'], (string)$real_send, 2);
  309. }
  310. $update['extract_num'] = bcadd((string)$real_send, (string)$v['extract_num'], 2);
  311. $update['send_time'] = time();
  312. $this->dao->update($v['id'], $update);
  313. /** @var UserBillServices $billService */
  314. $billService = app()->make(UserBillServices::class);
  315. $billService->income('integral_day_send', $v['uid'], $real_send, $this->getIntegralSum(['uid' => $v['uid']], 'extract_num') + $real_send, $v['id']);
  316. if (($update['status'] ?? 0) == 1) {
  317. $this->extract($v['id'], $update['extract_num'], true);
  318. }
  319. }
  320. }
  321. //提取
  322. public function extract($id, $num, $system = false)
  323. {
  324. $origin = $this->get($id);
  325. if ($origin['extract_num'] < $num) {
  326. if (!$system) {
  327. throw new Exception('可提取参考分不足');
  328. }
  329. $num = $origin['extract_num'];
  330. }
  331. if ($num <= 0) return true;
  332. $update['extract_num'] = bcsub((string)$origin['extract_num'], (string)$num, 2);
  333. $update['extracted_num'] = bcadd((string)$origin['extracted_num'], (string)$num, 2);
  334. $this->update($id, $update);
  335. $res = true;
  336. /** @var UserBrokerageServices $brokerageService */
  337. $brokerageService = app()->make(UserBrokerageServices::class);
  338. /** @var UserBillServices $billService */
  339. $billService = app()->make(UserBillServices::class);
  340. /** @var UserMoneyServices $moneyService */
  341. $moneyService = app()->make(UserMoneyServices::class);
  342. /** @var UserServices $userService */
  343. $userService = app()->make(UserServices::class);
  344. $billService->income('extract_award_integral', $origin['uid'], $num, $this->getIntegralSum(['uid' => $origin['uid']], 'extract_num') - $num, $id);
  345. $uid = $origin['uid'];
  346. $user = $userService->getUserInfo($uid);
  347. $reorder_set = sys_config('reorder_times', 2);
  348. $to_integral = sys_config('shop_integral_ratio');
  349. $to_integral = bcadd((string)$to_integral, (string)$origin['add_integral'], 2);
  350. if ($to_integral > 100) {
  351. $to_integral = 100;
  352. }
  353. $mark = '用户参考分' . ($system ? '出局' : '提取') . $num;
  354. $to_integral = bcdiv(bcmul((string)$num, (string)$to_integral), '100', 2);
  355. if ($to_integral > 0) {
  356. $balance = bcadd((string)$user['integral'], (string)$to_integral, 2);
  357. $res = $res && $billService->income('extract_integral_shop', $uid, [
  358. 'mark' => $mark,
  359. 'number' => floatval($to_integral),
  360. ], $balance, $id);
  361. // 添加用户佣金
  362. $res = $res && $userService->bcInc($uid, 'integral', (string)$to_integral, 'uid');
  363. }
  364. if ($to_integral < $num) {
  365. $left = bcsub((string)$num, (string)$to_integral, 2);
  366. if ($reorder_set >= $origin['reorder_count']) {
  367. $to_now_money = sys_config('re_now_money');
  368. $to_now_money = bcdiv(bcmul((string)$left, (string)$to_now_money), '100', 2);
  369. if ($to_now_money > 0) {
  370. $balance = bcadd((string)$user['now_money'], (string)$to_now_money, 2);
  371. $res = $res && $moneyService->income('extract_integral_static', $uid, [
  372. 'mark' => $mark,
  373. 'number' => floatval($to_now_money),
  374. ], $balance, $id);
  375. // 添加用户佣金
  376. $res = $res && $userService->bcInc($uid, 'now_money', $to_now_money, 'uid');
  377. }
  378. $to_energy = sys_config('re_energy');
  379. $to_energy = bcdiv(bcmul((string)$left, (string)$to_energy), '100', 2);
  380. if ($to_energy > 0) {
  381. $balance = bcadd((string)$user['energy'], (string)$to_energy, 2);
  382. $res = $res && $billService->income('extract_integral_static', $uid, [
  383. 'mark' => $mark,
  384. 'number' => floatval($to_energy),
  385. ], $balance, $id);
  386. // 添加用户佣金
  387. $res = $res && $userService->bcInc($uid, 'energy', $to_energy, 'uid');
  388. }
  389. $to_brokerage = 100 - $to_now_money - $to_energy;
  390. if ($to_brokerage < 0) $to_brokerage = 0;
  391. $to_brokerage = bcdiv(bcmul((string)$left, (string)$to_brokerage), '100', 2);
  392. if ($to_brokerage > 0) {
  393. $balance = bcadd((string)$user['brokerage_price'], (string)$to_brokerage, 2);
  394. $res = $res && $brokerageService->income('extract_integral_static', $uid, [
  395. 'mark' => $mark,
  396. 'number' => floatval($to_energy),
  397. ], $balance, $id);
  398. // 添加用户佣金
  399. $res = $res && $userService->bcInc($uid, 'brokerage_price', $to_brokerage, 'uid');
  400. }
  401. } else {
  402. //消费分,佣金
  403. $balance = bcadd((string)$user['brokerage_price'], (string)$left, 2);
  404. $res = $res && $brokerageService->income('extract_integral_static', $uid, [
  405. 'mark' => $mark,
  406. 'number' => floatval($left),
  407. ], $balance, $id);
  408. // 添加用户佣金
  409. $res = $res && $userService->bcInc($uid, 'brokerage_price', $left, 'uid');
  410. }
  411. }
  412. return $res;
  413. }
  414. }