AwardIntegralServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 UserIntegralDao
  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 = [])
  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)
  77. {
  78. return $this->dao->sum($where, 'num', 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. return $this->dao->sum($where, 'order_price', true);
  107. }
  108. /**
  109. * 加积分
  110. * @param int $uid 用户
  111. * @param string $price 价格
  112. * @param string $total 总额
  113. * @param float $order_price
  114. * @param int $type 类型
  115. * @param string $extract_sum
  116. * @param int $link_id 关联ID
  117. * @param string $mark
  118. * @return \crmeb\basic\BaseModel|\think\Model
  119. */
  120. public function incIntegral(int $uid, string $total, float $order_price, int $order_id = 0, string $mark = '', $add_time = 0)
  121. {
  122. // $inc_integral = bcdiv($total, $price, 5);
  123. //计算释放天数
  124. $default_send_day = sys_config('default_send_day', 90);
  125. $reorder_inc = sys_config('reorder_int_send_day', 10);
  126. $order_inc = sys_config('int_send_day', 10);
  127. //寻找复投机会
  128. $main_orders = $this->dao->search(['uid' => $uid, 'link_id' => 0, 'order_price_elt' => $order_price, 'valid' => 2])->order('id asc')->column('id');//满足复投条件的主单
  129. $link_id = 0;
  130. $reorder_count = 0;
  131. foreach ($main_orders as $v) {
  132. $can_reorder = $this->dao->search(['uid' => $uid, 'valid' => 1, 'link_id' => $v])->find();
  133. if (!$can_reorder) {
  134. $link_id = $v;
  135. $reorder_count = ($this->dao->search(['uid' => $uid, 'link_id' => $v])->max('reorder_count')) + 1;
  136. break;
  137. }
  138. }
  139. $last_order_send_day = $this->dao->search(['uid' => $uid])->max('send_day');
  140. if (!$last_order_send_day) $send_day = $default_send_day;
  141. else {
  142. if ($link_id > 0) $send_day = $last_order_send_day + $reorder_inc;
  143. else $send_day = $last_order_send_day + $order_inc;
  144. }
  145. return $this->dao->save([
  146. 'uid' => $uid,
  147. 'num' => $total,
  148. 'send_day' => $send_day,
  149. 'day_send' => bcdiv($total, $send_day, 2),
  150. 'sum_price' => $total,
  151. 'order_id' => $order_id,
  152. 'link_id' => $link_id,
  153. 'reorder_count' => $reorder_count,
  154. 'mark' => $mark,
  155. 'order_price' => $order_price,
  156. 'add_time' => $add_time ?: time(),
  157. 'status' => $total > 0 ? 0 : 1,
  158. 'send_time' => time()
  159. ]);
  160. }
  161. // 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)
  162. // {
  163. // $inc_integral = bcdiv($total, $price, 5);
  164. // $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->whereLike('mark', '%' . $mark . '%')->find();
  165. // if ($old) {
  166. // $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5);
  167. // } else {
  168. // $dump = '用户' . $uid . $mark . '补' . $inc_integral;
  169. // }
  170. // @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND);
  171. // if ($old) {
  172. // UserIntegral::where('id', $old['id'])->delete();
  173. // }
  174. // return UserIntegral::create([
  175. // 'uid' => $uid,
  176. // 'type' => $type == 1 ? 1 : 0,
  177. // 'num' => $inc_integral,
  178. // 'price' => $price,
  179. // 'sum_price' => $total,
  180. // 'extract_sum' => $extract_sum,
  181. // 'link_id' => $link_id,
  182. // 'mark' => $mark,
  183. // 'order_price' => $order_price,
  184. // 'add_time' => $add_time,
  185. // 'status' => $inc_integral > 0 ? 0 : 1,
  186. // ]);
  187. //// return true;
  188. // }
  189. // public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  190. // {
  191. // if ($price > 0) {
  192. // $inc_integral = bcdiv($total, $price, 5);
  193. // } else {
  194. // $inc_integral = $total;
  195. // }
  196. // $origin = $this->get($id);
  197. // return $this->dao->update($id, [
  198. // 'num' => bcadd($origin['num'], $inc_integral, 5),
  199. // 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  200. // ]);
  201. // }
  202. public function upSpeedIntegral(int $id, string $total)
  203. {
  204. $inc_integral = $total;
  205. $origin = $this->get($id);
  206. return $this->dao->update($id, [
  207. 'up_speed' => bcadd($origin['up_speed'], $inc_integral, 2),
  208. ]);
  209. }
  210. //每日释放
  211. public function daySend()
  212. {
  213. $sum = $this->dao->search()->whereTime('add_time', 'yesterday')->sum('order_price');
  214. $system_check = sys_config('system_achievement_check', 10000);
  215. $list = $this->dao->search()->where('status', 0)->where('sent_num<num')->where('send_time', '<', strtotime('today'))->limit(50)->select();
  216. foreach ($list as $v) {
  217. $day_send = $v['day_send'];
  218. $send_day = $v['send_day'];
  219. $up_speed = $v['up_speed'] - $v['up_speed_used'];
  220. if ($sum < $system_check) {
  221. $send_day = $v['send_day'] + sys_config('fail_inc_send_day', 1);
  222. $day_send = bcdiv((string)(($v['num'] - $v['sent_num'])), (string)$send_day, 2);
  223. }
  224. $up_speed = bcdiv((string)$up_speed, (string)($send_day - $v['sent_day']), 2);
  225. $real_send = $day_send + $up_speed;
  226. $left = $v['num'] - $v['sent_num'];
  227. $update['day_send'] = $day_send;
  228. $update['up_speed_used'] = $v['up_speed_used'] + $up_speed;
  229. $update['sent_day'] = $v['sent_day'] + 1;
  230. $update['send_day'] = $send_day;
  231. if ($real_send >= $left) {
  232. $real_send = $left;
  233. $update['up_speed_used'] = $v['up_speed'];
  234. $update['sent_num'] = $v['num'];
  235. $update['status'] = 1;
  236. }
  237. $update['extract_num'] = bcadd($real_send, $v['extract_num'], 2);
  238. $update['send_time'] = time();
  239. $this->dao->update($v['id'], $update);
  240. if (($update['status'] ?? 0) == 1) {
  241. $this->extract($id, $update['extract_num'], true);
  242. }
  243. }
  244. }
  245. //提取
  246. public function extract($id, $num, $system = false)
  247. {
  248. $origin = $this->get($id);
  249. if ($origin['extract_num'] < $num) {
  250. if (!$system) {
  251. throw new Exception('可提取参考分不足');
  252. }
  253. $num = $origin['extract_num'];
  254. }
  255. if ($num <= 0) return true;
  256. $update['extract_num'] = bcsub($origin['extract_num'], $num, 2);
  257. $update['extracted_num'] = bcadd($origin['extracted_num'], $num, 2);
  258. $this->update($id, $update);
  259. $res = true;
  260. /** @var UserBrokerageServices $brokerageService */
  261. $brokerageService = app()->make(UserBrokerageServices::class);
  262. /** @var UserBillServices $billService */
  263. $billService = app()->make(UserBillServices::class);
  264. /** @var UserMoneyServices $moneyService */
  265. $moneyService = app()->make(UserMoneyServices::class);
  266. /** @var UserServices $userService */
  267. $userService = app()->make(UserServices::class);
  268. $uid = $origin['uid'];
  269. $user = $userService->getUserInfo($uid);
  270. $reorder_set = sys_config('reorder_times', 2);
  271. $to_integral = sys_config('shop_integral_ratio');
  272. $mark = '用户参考分' . $system ? '出局' : '提取' . $num;
  273. $to_integral = bcdiv(bcmul((string)$num, (string)$to_integral), '100', 2);
  274. if ($to_integral > 0) {
  275. $balance = bcadd($user['integral'], $to_integral, 2);
  276. $res = $res && $billService->income('extract_integral_shop', $uid, [
  277. 'mark' => $mark,
  278. 'number' => floatval($to_integral),
  279. ], $balance, $id);
  280. // 添加用户佣金
  281. $res = $res && $userService->bcInc($uid, 'integral', $to_integral, 'uid');
  282. }
  283. if ($to_integral < $num) {
  284. $left = bcsub((string)$num, $to_integral, 2);
  285. if ($reorder_set > $origin['reorder_count']) {
  286. $to_now_money = sys_config('re_now_money');
  287. $to_now_money = bcdiv(bcmul($left, (string)$to_now_money), '100', 2);
  288. if ($to_now_money > 0) {
  289. $balance = bcadd($user['now_money'], $to_now_money, 2);
  290. $res = $res && $moneyService->income('extract_integral_static', $uid, [
  291. 'mark' => $mark,
  292. 'number' => floatval($to_now_money),
  293. ], $balance, $id);
  294. // 添加用户佣金
  295. $res = $res && $userService->bcInc($uid, 'now_money', $to_now_money, 'uid');
  296. }
  297. $to_energy = sys_config('re_energy');
  298. $to_energy = bcdiv(bcmul($left, (string)$to_energy), '100', 2);
  299. if ($to_energy > 0) {
  300. $balance = bcadd($user['energy'], $to_energy, 2);
  301. $res = $res && $billService->income('extract_integral_static', $uid, [
  302. 'mark' => $mark,
  303. 'number' => floatval($to_energy),
  304. ], $balance, $id);
  305. // 添加用户佣金
  306. $res = $res && $userService->bcInc($uid, 'energy', $to_energy, 'uid');
  307. }
  308. $to_brokerage = 100 - $to_now_money - $to_energy;
  309. if ($to_brokerage < 0) $to_brokerage = 0;
  310. $to_brokerage = bcdiv(bcmul($left, (string)$to_brokerage), '100', 2);
  311. if ($to_brokerage > 0) {
  312. $balance = bcadd($user['brokerage_price'], $to_brokerage, 2);
  313. $res = $res && $brokerageService->income('extract_integral_static', $uid, [
  314. 'mark' => $mark,
  315. 'number' => floatval($to_energy),
  316. ], $balance, $id);
  317. // 添加用户佣金
  318. $res = $res && $userService->bcInc($uid, 'brokerage_price', $to_brokerage, 'uid');
  319. }
  320. } else {
  321. //消费分,佣金
  322. $balance = bcadd($user['brokerage_price'], $left, 2);
  323. $res = $res && $brokerageService->income('extract_integral_static', $uid, [
  324. 'mark' => $mark,
  325. 'number' => floatval($left),
  326. ], $balance, $id);
  327. // 添加用户佣金
  328. $res = $res && $userService->bcInc($uid, 'brokerage_price', $left, 'uid');
  329. }
  330. }
  331. return $res;
  332. }
  333. }