AwardIntegralServices.php 16 KB

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