AwardIntegralServices.php 18 KB

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