AwardIntegralServices.php 16 KB

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