SettleService.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\CouponModel;
  14. use App\Models\MemberCouponModel;
  15. use App\Models\MemberModel;
  16. use App\Models\OrderCommissionModel;
  17. use App\Models\OrderModel;
  18. use App\Models\PtAccountModel;
  19. use App\Models\StoreModel;
  20. use App\Services\BaseService;
  21. use App\Services\Common\MemberLevelService;
  22. use App\Services\ConfigService;
  23. use App\Services\RedisService;
  24. use Illuminate\Support\Facades\DB;
  25. /**
  26. * 结算管理-服务类
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * @package App\Services\Api
  30. */
  31. class SettleService extends BaseService
  32. {
  33. /**
  34. * 构造函数
  35. * @author laravel开发员
  36. * @since 2020/11/11
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new AccountLogModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 商家收益结算
  55. * @param $storeId
  56. * @param $money 收益
  57. * @param $order 订单数据
  58. * @return array|false|int
  59. */
  60. public function storeBonus($storeId, $money, $order)
  61. {
  62. $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
  63. if ($money <= 0 && $storeId <= 0) {
  64. $this->error = '无企业佣金可结算';
  65. return false;
  66. }
  67. $storeInfo = StoreModel::where(['id' => $storeId, 'mark' => 1])->first();
  68. $balance = isset($storeInfo['bonus_total']) ? $storeInfo['bonus_total'] : 0;
  69. $storeUserId = isset($storeInfo['user_id']) ? $storeInfo['user_id'] : 0;
  70. if ($storeUserId <= 0) {
  71. $this->error = '企业账号错误';
  72. return false;
  73. }
  74. if (!StoreModel::where(['id' => $storeId])->update(['bonus_total' => DB::raw("bonus_total + {$money}"), 'update_time' => time()])) {
  75. $this->error = '收货错误,企业结算错误,请联系客服处理';
  76. return -1;
  77. }
  78. $log = [
  79. 'user_id' => $storeUserId,
  80. 'store_id' => $storeId,
  81. 'source_order_no' => isset($order['order_no']) ? $order['order_no'] : '',
  82. 'type' => 7,
  83. 'money' => $money,
  84. 'after_money' => moneyFormat($balance + $money, 2),
  85. 'date' => date('Y-m-d'),
  86. 'create_time' => time(),
  87. 'remark' => '收益',
  88. 'status' => 1
  89. ];
  90. if (!$id = $this->model->insertGetId($log)) {
  91. $this->error = '企业收益结算失败,请联系客服处理';
  92. return -1;
  93. }
  94. $result = ['id' => $id, 'store_id' => $storeId, 'bonus' => $money];
  95. if (env('APP_DEBUG')) {
  96. RedisService::set("caches:settle:{$orderNo}:store_{$storeId}", $result, 7200);
  97. }
  98. return $result;
  99. }
  100. /**
  101. * @param $order
  102. * @param int $orderType
  103. */
  104. public function commissionCount($order, $orderGoods, $userInfo, $orderType = 1)
  105. {
  106. $userId = isset($order['user_id']) ? $order['user_id'] : 0;
  107. $storeId = isset($order['store_id']) ? $order['store_id'] : 0;
  108. $mealId = isset($order['meal_id']) ? $order['meal_id'] : 0;
  109. $orderId = isset($order['id']) ? $order['id'] : 0;
  110. $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
  111. $orderTotal = isset($order['pay_total']) ? $order['pay_total'] : 0;
  112. $profitTotal = isset($order['profit_total']) ? $order['profit_total'] : 0;
  113. $data = [
  114. 'order_id' => $orderId,
  115. 'user_id' => $userId,
  116. 'order_no' => $orderNo,
  117. 'store_id' => $storeId,
  118. 'total' => $orderTotal,
  119. 'create_time' => time(),
  120. 'status' => 2,
  121. 'mark' => 1,
  122. ];
  123. $errors = [];
  124. if ($orderType == 2) {
  125. /* TODO 计算直推奖 */
  126. $pointId = isset($userInfo['point_id']) ? $userInfo['point_id'] : 0;
  127. $directBonus = ConfigService::make()->getConfigByCode('business_direct_bonus', 0);
  128. $directBonus = $directBonus > 0 && $directBonus < $orderTotal ? $directBonus : 0;
  129. $pointInfo = MemberModel::where(['id' => $pointId, 'mark' => 1])
  130. ->select(['id', 'nickname', 'buy_type', 'bonus_status', 'status', 'mark'])
  131. ->first();
  132. $bonusStatus = isset($pointInfo['bonus_status']) ? $pointInfo['bonus_status'] : 0;
  133. $data['direct_uid'] = $pointId;
  134. if ($pointInfo && $bonusStatus == 1) {
  135. $data['direct_bonus'] = json_encode(['uid' => $pointId, 'bonus' => $directBonus], 256);
  136. } else {
  137. $data['direct_bonus'] = json_encode(['uid' => $pointId, 'direct_bonus' => $directBonus, 'meal_id' => $mealId, 'error' => '节点推荐人不存在或收益已领完', 'point' => $pointInfo], 256);
  138. }
  139. /* TODO 计算见单奖和分红奖励 */
  140. $points = isset($userInfo['points']) ? $userInfo['points'] : '';
  141. $points = $points ? explode(',', $points) : [];
  142. $points = array_filter($points);
  143. $points = array_reverse($points); // 由下到上1-6层
  144. $rewardLayer = ConfigService::make()->getConfigByCode('business_reward_layer', 6);
  145. $points = array_splice($points, 0, $rewardLayer);
  146. $jdBonus = ConfigService::make()->getConfigByCode("business_jd_bonus_{$mealId}", 0);
  147. $jdBonus = $jdBonus > 0 && $jdBonus < $orderTotal ? $jdBonus : 0;
  148. // 计算当前平台下各个等级的会员数量
  149. $levelCounts = MemberService::make()->getCountsByLevel();
  150. $levelList = MemberLevelService::make()->getListByLevel();
  151. if ($points) {
  152. $pointList = MemberModel::with(['levelData'])->whereIn('id', $points)
  153. ->select(['id', 'nickname', 'mobile', 'buy_type', 'member_level', 'bonus_status', 'status', 'mark'])
  154. ->orderBy(DB::raw("FIELD(id, " . implode(',', $points) . ")"))
  155. ->get();
  156. foreach ($pointList as $k => $point) {
  157. // 默认只分6层
  158. $layer = $k + 1;
  159. if ($layer > $rewardLayer) {
  160. continue;
  161. }
  162. // 处理见单奖
  163. $pid = isset($point['id']) ? $point['id'] : 0;
  164. $pMark = isset($point['mark']) ? $point['mark'] : 0;
  165. $buyType = isset($point['buy_type']) ? $point['buy_type'] : 0;
  166. $bonusStatus = isset($point['bonus_status']) ? $point['bonus_status'] : 0;
  167. $pLevel = isset($point['member_level']) ? $point['member_level'] : 0;
  168. $levelWeightCountData = isset($levelCounts[$pLevel]) ? $levelCounts[$pLevel] : 0;
  169. $levelData = isset($levelList[$pLevel]) ? $levelList[$pLevel] : 0;
  170. $levelWeightCount = isset($levelWeightCountData['count']) ? $levelWeightCountData['count'] : 0;
  171. $levelBonus = isset($levelData['bonus']) ? $levelData['bonus'] : 0;
  172. $levelName = isset($levelData['name']) ? $levelData['name'] : '';
  173. $data["bonus_{$layer}_uid"] = $pid;
  174. $bonusData = [
  175. 'id' => $pid,
  176. 'nickname' => isset($point['nickname']) ? $point['nickname'] : '',
  177. 'mobile' => isset($point['mobile']) ? $point['mobile'] : '',
  178. 'jd_bonus' => 0,
  179. 'level' => $pLevel,
  180. 'level_name' => $levelName,
  181. 'weight' => $levelWeightCount,
  182. 'weight_bonus' => $levelBonus,
  183. 'level_bonus' => 0,
  184. ];
  185. if ($pMark == 1 && $bonusStatus == 1) {
  186. $bonusData['jd_bonus'] = $jdBonus;
  187. } else {
  188. $bonusData["error"] = "上{$layer}级节点推荐人不存在或收益已领完";
  189. }
  190. // 处理加权分红
  191. if ($pLevel > 0 && $levelWeightCount > 0 && $levelBonus > 0 && $bonusStatus == 1) {
  192. $bonusData['level_bonus'] = moneyFormat($levelBonus * 6 / $levelWeightCount, 2);
  193. }
  194. // 数据
  195. $data["bonus_{$layer}_data"] = json_encode($bonusData, 256);
  196. }
  197. }
  198. } else {
  199. /* TODO 计算商家佣金 */
  200. $storeInfo = StoreModel::where(['id' => $storeId])->first();
  201. $bonusRate = isset($storeInfo['bonus_rate']) ? floatval($storeInfo['bonus_rate']) : 0;
  202. $storeBonusRate = ConfigService::make()->getConfigByCode('store_bonus_rate', 0);
  203. $storeBonusRate = $storeBonusRate > 0 && $storeBonusRate <= 100 ? $storeBonusRate : 0;
  204. $bonusRate = $bonusRate > 0 && $bonusRate <= 100 ? $bonusRate : $storeBonusRate;
  205. $bonus = moneyFormat($orderTotal * $bonusRate / 100, 2);
  206. $data['bonus'] = $bonus;
  207. $data['bonus_rate'] = $bonusRate;
  208. /* TODO 计算绿色积分奖励 */
  209. $lsScoreTotal = 0;
  210. $data['ls_score_rates'] = [];
  211. $ptLsScoreRate = ConfigService::make()->getConfigByCode('ls_score_rate', 0);
  212. $ptLsScoreRate = $ptLsScoreRate > 0 && $ptLsScoreRate <= 50 ? $ptLsScoreRate : 0;
  213. if ($orderGoods) {
  214. foreach ($orderGoods as $goods) {
  215. $lsScoreRate = isset($goods['ls_score_rate']) ? $goods['ls_score_rate'] : 0;
  216. $lsScoreRate = $lsScoreRate > 0 && $lsScoreRate <= 50 ? $lsScoreRate : $ptLsScoreRate;
  217. $profitTotal = isset($goods['profit_total']) ? $goods['profit_total'] : 0;
  218. if ($profitTotal > 0 && $lsScoreRate > 0) {
  219. $lsScoreTotal += moneyFormat($profitTotal * $lsScoreRate / 100, 2);
  220. }
  221. $data['ls_score_rates'][$goods['goods_id']] = [
  222. 'goods_id' => $goods['goods_id'],
  223. 'score' => $lsScoreRate
  224. ];
  225. }
  226. }
  227. $data['ls_score'] = $lsScoreTotal;
  228. $data['ls_score_rates'] = json_encode($data['ls_score_rates'], 256);
  229. /* TODO 计算数字资产+底池金额 */
  230. $ptPoolRate = ConfigService::make()->getConfigByCode('pt_pool_rate', 0);
  231. $ptPoolRate = $ptPoolRate > 0 && $ptPoolRate < 50 ? $ptPoolRate : 0;
  232. $rewardPropertyRate = ConfigService::make()->getConfigByCode('reward_property_rate', 0);
  233. $rewardPropertyRate = $rewardPropertyRate > 0 && $rewardPropertyRate <= 50 ? $rewardPropertyRate : 0;
  234. $propertyTotal = moneyFormat($rewardPropertyRate * $profitTotal / 100, 2);
  235. $poolTotal = moneyFormat($ptPoolRate * $profitTotal / 100, 2);
  236. $data['pt_pool_rate'] = $ptPoolRate;
  237. $data['pt_pool'] = $poolTotal;
  238. $data['property'] = $propertyTotal;
  239. $data['property_rate'] = $rewardPropertyRate;
  240. }
  241. // 佣金数据入库
  242. if (!$cid = OrderCommissionModel::where(['order_no' => $orderNo])->value('id')) {
  243. $cid = OrderCommissionModel::insertGetId($data);
  244. } else {
  245. OrderCommissionModel::where(['id' => $cid])->update($data);
  246. }
  247. $this->error = '佣金计算成功';
  248. $data['id'] = $cid;
  249. return $data;
  250. }
  251. /**
  252. * 订单结算
  253. * @param $orderId
  254. * @return array|false
  255. */
  256. public function commissionSettle($orderId)
  257. {
  258. try {
  259. $info = OrderModel::with(['commission'])
  260. ->where(['id' => $orderId, 'mark' => 1])
  261. ->select(['id', 'order_no', 'type', 'user_id', 'total', 'pay_total', 'bd_score', 'pay_status', 'status', 'refund_status'])
  262. ->first();
  263. $info = $info ? $info->toArray() : [];
  264. if (empty($info)) {
  265. $this->error = '结算订单不存在';
  266. return false;
  267. }
  268. if ($info['status'] <= 1 || $info['pay_status'] == 10) {
  269. $this->error = '订单未支付';
  270. return false;
  271. }
  272. if (in_array($info['refund_status'], [1, 2, 3])) {
  273. $this->error = '订单已退款';
  274. return false;
  275. }
  276. $orderType = isset($info['type']) ? $info['type'] : 1;
  277. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  278. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  279. $commission = isset($info['commission']) ? $info['commission'] : [];
  280. $commissionId = isset($commission['id']) ? $commission['id'] : 0;
  281. if (empty($commission) || $commissionId <= 0) {
  282. $this->error = '结算订单收益参数错误';
  283. return false;
  284. }
  285. if ($commission['status'] == 1) {
  286. $this->error = '订单收益已结算';
  287. return false;
  288. }
  289. $cacheKey = "caches:ordersCommission:{$orderNo}_{$orderUserId}:";
  290. if (RedisService::get("{$cacheKey}lock")) {
  291. $this->error = '订单结算中';
  292. return false;
  293. }
  294. // 创业订单
  295. $logs = [];
  296. RedisService::set("{$cacheKey}lock", $info, 300);
  297. if ($orderType == 2) {
  298. // TODO 结算直推奖
  299. $directData = isset($commission['direct_bonus']) && $commission['direct_bonus'] ? json_decode($commission['direct_bonus'], true) : [];
  300. $directUserId = isset($commission['direct_uid']) ? $commission['direct_uid'] : 0;
  301. $directBonus = isset($directData['bonus']) ? $directData['bonus'] : 0;
  302. if ($directUserId > 0 && $directBonus > 0) {
  303. $directUser = isset($commission['direct_user']) ? $commission['direct_user'] : [];
  304. $directUserBalance = isset($directUser['balance']) ? $directUser['balance'] : 0;
  305. if ($directUser) {
  306. $updateData = [
  307. 'balance' => DB::raw("balance + {$directBonus}"),
  308. 'bonus_total' => DB::raw("bonus_total + {$directBonus}"),
  309. 'update_time' => time()
  310. ];
  311. if (!MemberModel::where(['id' => $directUserId])->update($updateData)) {
  312. DB::rollBack();
  313. RedisService::clear("{$cacheKey}lock");
  314. $this->error = '直推奖励结算错误';
  315. return false;
  316. }
  317. $data = [
  318. 'user_id' => $directUserId,
  319. 'source_order_no' => $orderNo,
  320. 'user_type' => 1,
  321. 'account_type' => 1, // 余额
  322. 'type' => 8,
  323. 'bonus_type' => 1,
  324. 'money' => $directBonus,
  325. 'after_money' => moneyFormat($directUserBalance + $directBonus, 2),
  326. 'date' => date('Y-m-d'),
  327. 'create_time' => time(),
  328. 'remark' => '直推奖',
  329. 'remark1' => "用户ID:{$orderUserId}购买礼包直推奖励",
  330. 'status' => 1,
  331. 'mark' => 1
  332. ];
  333. $logs[] = $data;
  334. }
  335. }
  336. // TODO 结算见单奖+分红
  337. $bonusUids = [];
  338. for ($i = 1; $i <= 6; $i++) {
  339. $uid = isset($commission["bonus_{$i}_uid"]) ? $commission["bonus_{$i}_uid"] : 0;
  340. if ($uid) {
  341. $bonusUids[] = $uid;
  342. }
  343. }
  344. $bonusUsers = MemberModel::whereIn('id', $bonusUids)
  345. ->select(['id', 'openid', 'balance', 'mobile', 'nickname', 'property', 'ls_score', 'status'])
  346. ->get()
  347. ->keyBy('id');
  348. $bonusUsers = $bonusUsers ? $bonusUsers->toArray() : [];
  349. for ($i = 1; $i <= 6; $i++) {
  350. $uid = isset($commission["bonus_{$i}_uid"]) ? $commission["bonus_{$i}_uid"] : 0;
  351. $bonusData = isset($commission["bonus_{$i}_data"]) ? $commission["bonus_{$i}_data"] : '';
  352. $bonusData = $bonusData ? json_decode($bonusData, true) : [];
  353. $jdBonus = isset($bonusData['jd_bonus']) ? $bonusData['jd_bonus'] : 0;
  354. $levelName = isset($bonusData['level_name']) ? $bonusData['level_name'] : '';
  355. $levelBonus = isset($bonusData['level_bonus']) ? $bonusData['level_bonus'] : 0;
  356. $bonusUser = isset($bonusUsers[$uid]) ? $bonusUsers[$uid] : [];
  357. $bonusUserBalance = isset($bonusUser['balance']) ? $bonusUser['balance'] : 0;
  358. $bonusTotal = moneyFormat($jdBonus + $levelBonus, 2);
  359. if ($uid && $bonusUser && $bonusTotal > 0) {
  360. $updateData = [
  361. 'balance' => DB::raw("balance + {$bonusTotal}"),
  362. 'bonus_total' => DB::raw("bonus_total + {$bonusTotal}"),
  363. 'update_time' => time()
  364. ];
  365. if (!MemberModel::where(['id' => $uid])->update($updateData)) {
  366. $this->error = '见单和分红奖励结算错误';
  367. RedisService::clear("{$cacheKey}lock");
  368. return false;
  369. }
  370. if ($jdBonus > 0) {
  371. $bonusUserBalance = moneyFormat($bonusUserBalance + $jdBonus, 2);
  372. $data = [
  373. 'user_id' => $uid,
  374. 'source_order_no' => $orderNo,
  375. 'user_type' => 1,
  376. 'account_type' => 1, // 余额
  377. 'type' => 8,
  378. 'bonus_type' => 2,
  379. 'money' => $jdBonus,
  380. 'after_money' => $bonusUserBalance,
  381. 'date' => date('Y-m-d'),
  382. 'create_time' => time() + $i,
  383. 'remark' => '见单奖',
  384. 'remark1' => "下{$i}级用户ID:{$orderUserId}购买礼包见单奖",
  385. 'status' => 1,
  386. 'mark' => 1
  387. ];
  388. $logs[] = $data;
  389. }
  390. if ($levelBonus > 0) {
  391. $bonusUserBalance = moneyFormat($bonusUserBalance + $levelBonus, 2);
  392. $data = [
  393. 'user_id' => $uid,
  394. 'source_order_no' => $orderNo,
  395. 'user_type' => 1,
  396. 'account_type' => 1, // 余额
  397. 'type' => 8,
  398. 'bonus_type' => 3,
  399. 'money' => $levelBonus,
  400. 'after_money' => $bonusUserBalance,
  401. 'date' => date('Y-m-d'),
  402. 'create_time' => time() + $i,
  403. 'remark' => '分红收益',
  404. 'remark1' => "下{$i}级用户ID:{$orderUserId}购买礼包等级[{$levelName}]分红收益",
  405. 'status' => 1,
  406. 'mark' => 1
  407. ];
  408. $logs[] = $data;
  409. }
  410. }
  411. }
  412. } // 商城订单
  413. else {
  414. // TODO 商家结算
  415. $storeInfo = isset($commission['store']) ? $commission['store'] : [];
  416. $storeId = isset($commission['store_id']) ? $commission['store_id'] : 0;
  417. $storeBonus = isset($commission['bonus']) ? $commission['bonus'] : 0;
  418. $storeUserId = isset($storeInfo['user_id']) ? $storeInfo['user_id'] : 0;
  419. $storeBalance = isset($storeInfo['balance']) ? $storeInfo['balance'] : 0;
  420. if ($storeId > 0 && $storeInfo && $storeBonus > 0) {
  421. $updateData = [
  422. 'balance' => DB::raw("balance + {$storeBonus}"),
  423. 'income' => DB::raw("income + {$storeBonus}"),
  424. 'update_time' => time()
  425. ];
  426. if (!StoreModel::where(['id' => $storeId])->update($updateData)) {
  427. $this->error = '商家提成结算失败';
  428. RedisService::clear("{$cacheKey}lock");
  429. return false;
  430. }
  431. $storeBalance = moneyFormat($storeBalance + $storeBonus, 2);
  432. $data = [
  433. 'user_id' => $storeUserId,
  434. 'store_id' => $storeId,
  435. 'source_order_no' => $orderNo,
  436. 'user_type' => 2,
  437. 'account_type' => 1, // 余额
  438. 'type' => 7,
  439. 'bonus_type' => 0,
  440. 'money' => $storeBonus,
  441. 'after_money' => $storeBalance,
  442. 'date' => date('Y-m-d'),
  443. 'create_time' => time(),
  444. 'remark' => '商家提成结算',
  445. 'remark1' => "用户ID:{$orderUserId}购买商品提成结算",
  446. 'status' => 1,
  447. 'mark' => 1
  448. ];
  449. $logs[] = $data;
  450. }
  451. // TODO 绿色积分结算
  452. $userInfo = isset($commission['user']) ? $commission['user'] : [];
  453. $userId = isset($commission['user_id']) ? $commission['user_id'] : 0;
  454. $userLsScore = isset($userInfo['ls_score']) ? $userInfo['ls_score'] : 0;
  455. $lsScore = isset($commission['ls_score']) ? $commission['ls_score'] : 0;
  456. if ($userId > 0 && $userInfo && $lsScore > 0) {
  457. $updateData = [
  458. 'ls_score' => DB::raw("ls_score + {$lsScore}"),
  459. 'ls_score_total' => DB::raw("ls_score_total + {$lsScore}"),
  460. 'update_time' => time()
  461. ];
  462. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  463. $this->error = '绿色积分奖励结算错误';
  464. RedisService::clear("{$cacheKey}lock");
  465. return false;
  466. }
  467. $data = [
  468. 'user_id' => $userId,
  469. 'store_id' => 0,
  470. 'source_order_no' => $orderNo,
  471. 'user_type' => 1,
  472. 'account_type' => 4, // 绿色积分
  473. 'type' => 8,
  474. 'bonus_type' => 4,
  475. 'money' => $lsScore,
  476. 'after_money' => moneyFormat($userLsScore + $lsScore, 2),
  477. 'date' => date('Y-m-d'),
  478. 'create_time' => time(),
  479. 'remark' => '绿色积分奖励',
  480. 'remark1' => "用户ID:{$orderUserId}购买商品绿色积分奖励",
  481. 'status' => 1,
  482. 'mark' => 1
  483. ];
  484. $logs[] = $data;
  485. }
  486. // TODO 数字资产和底池
  487. $property = isset($commission['property']) ? $commission['property'] : 0;
  488. $userProperty = isset($userInfo['property']) ? $userInfo['property'] : 0;
  489. if ($userId > 0 && $userInfo && $property > 0) {
  490. $updateData = [
  491. 'property' => DB::raw("property + {$property}"),
  492. 'property_total' => DB::raw("property_total + {$property}"),
  493. 'update_time' => time()
  494. ];
  495. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  496. $this->error = '数字资产奖励结算错误';
  497. RedisService::clear("{$cacheKey}lock");
  498. return false;
  499. }
  500. $data = [
  501. 'user_id' => $userId,
  502. 'store_id' => 0,
  503. 'source_order_no' => $orderNo,
  504. 'user_type' => 1,
  505. 'account_type' => 2, // 数字资产
  506. 'type' => 8,
  507. 'bonus_type' => 5,
  508. 'money' => $property,
  509. 'after_money' => moneyFormat($userProperty + $property, 2),
  510. 'date' => date('Y-m-d'),
  511. 'create_time' => time(),
  512. 'remark' => '数字资产奖励',
  513. 'remark1' => "用户ID:{$orderUserId}购买商品数字资产奖励",
  514. 'status' => 1,
  515. 'mark' => 1
  516. ];
  517. $logs[] = $data;
  518. }
  519. // 平台底池结算
  520. $ptAccount = PtAccountModel::where(['mark' => 1])->first();
  521. $accountId = isset($ptAccount['id']) ? $ptAccount['id'] : 0;
  522. $poolTotal = isset($ptAccount['pool_total']) ? $ptAccount['pool_total'] : 0;
  523. $ptPool = isset($commission['pt_pool']) ? $commission['pt_pool'] : 0;
  524. if ($ptAccount && ($ptPool > 0 || $property > 0)) {
  525. $updateData = [
  526. 'pool_total' => DB::raw("pool_total + {$ptPool}"),
  527. 'today_pool' => DB::raw("today_pool + {$ptPool}"),
  528. 'today_property' => DB::raw("today_property + {$property}"),
  529. 'update_time' => time()
  530. ];
  531. if (!PtAccountModel::where(['id' => $accountId])->update($updateData)) {
  532. $this->error = '平台底池和资产结算错误';
  533. RedisService::clear("{$cacheKey}lock");
  534. return false;
  535. }
  536. // 底池明细
  537. if ($ptPool > 0) {
  538. $data = [
  539. 'user_id' => 0,
  540. 'store_id' => 0,
  541. 'source_order_no' => $orderNo,
  542. 'user_type' => 0,
  543. 'account_type' => 5, // 平台账户
  544. 'type' => 10,
  545. 'bonus_type' => 6,
  546. 'money' => $ptPool,
  547. 'after_money' => moneyFormat($poolTotal + $ptPool, 2),
  548. 'date' => date('Y-m-d'),
  549. 'create_time' => time(),
  550. 'remark' => '返回底池',
  551. 'remark1' => "用户ID:{$orderUserId}购买商品返回底池",
  552. 'status' => 1,
  553. 'mark' => 1
  554. ];
  555. $logs[] = $data;
  556. }
  557. }
  558. }
  559. // TODO 明细入表
  560. RedisService::set("{$cacheKey}logs", $logs, 3600);
  561. if ($logs && !AccountLogModel::insert($logs)) {
  562. $this->error = '奖励明细结算处理错误';
  563. RedisService::clear("{$cacheKey}lock");
  564. return false;
  565. }
  566. // 订单结算状态更新
  567. if(!OrderCommissionModel::where(['id'=>$commissionId])->update(['status'=>1,'arrival_at'=>date('Y-m-d H:i:s'),'update_time'=>time()])){
  568. $this->error = '订单结算状态更新失败';
  569. return false;
  570. }
  571. $this->error = '订单结算完成';
  572. RedisService::clear("{$cacheKey}lock");
  573. RedisService::clear("caches:orders:settleList");
  574. RedisService::set("{$cacheKey}result", $info, 3600);
  575. $result = ['id' => $orderId, 'user_id' => $orderUserId, 'logs' => $logs, 'commission' => $commission];
  576. RedisService::set("caches:settles:{$orderId}_{$orderNo}_success", $result, 7200);
  577. return $result;
  578. } catch (\Exception $exception){
  579. RedisService::set("caches:settles:{$orderId}_error", ['error'=>$exception->getMessage(),'trace'=>$exception->getTrace()], 7200);
  580. return false;
  581. }
  582. }
  583. /**
  584. * 更新用户等级和下级团队购买礼包人数
  585. * @param $userId
  586. * @param $points
  587. * @return bool
  588. */
  589. public function updateUserData($userId, $points)
  590. {
  591. // 上级用户复购参数更新
  592. try {
  593. $points = $points ? explode(',', $points) : [];
  594. $points = array_filter($points);
  595. $points = array_reverse($points);
  596. $rewardLayer = ConfigService::make()->getConfigByCode('business_reward_layer', 6);
  597. $points = array_splice($points, 0, $rewardLayer);
  598. if ($points) {
  599. // 累计购买人数
  600. MemberModel::where(['bonus_status'=>1,'mark' => 1])->whereIn('id', $points)
  601. ->update(['buy_count' => DB::raw("buy_count + 1"), 'update_time' => time()]);
  602. // 购买人数满N层暂停收益
  603. $buyCount = pow(2, $rewardLayer + 1) - 2;
  604. MemberModel::where('buy_count', '>=', $buyCount)
  605. ->where(['bonus_status'=>1])
  606. ->whereIn('id', $points)
  607. ->update(['buy_count' => 0, 'buy_type' => DB::raw("buy_type+1"), 'bonus_status' => 2, 'update_time' => time()]);
  608. }
  609. // 用户升级
  610. $userInfo = MemberModel::with(['parent'])->where(['id' => $userId, 'mark' => 1])
  611. ->select(['id', 'member_level', 'parent_id', 'status'])
  612. ->first();
  613. $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : [];
  614. $parent = isset($userInfo['parent']) ? $userInfo['parent'] : [];
  615. $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
  616. $parentLevel = isset($parent['member_level']) ? $parent['member_level'] : 0;
  617. $upperLevel = 0;
  618. $parentUpperLevel = 0;
  619. $levelList = MemberLevelService::make()->getListByLevel();
  620. $orderCount = OrderModel::from('orders as a')
  621. ->distinct('user_id')
  622. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  623. ->where('a.status', '>', 1)
  624. ->where(['a.main_no' => '', 'a.type' => 2, 'a.user_id' => $userId, 'a.pay_status' => 20])
  625. ->count();
  626. $parentOrderCount = 0;
  627. if ($parentId > 0) {
  628. $parentOrderCount = OrderModel::from('orders as a')
  629. ->distinct('user_id')
  630. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  631. ->where('a.status', '>', 1)
  632. ->where(['a.main_no' => '', 'a.type' => 2, 'b.parent_id' => $parentId, 'a.pay_status' => 20])
  633. ->count();
  634. }
  635. // 升级检查
  636. if($orderCount||$parentOrderCount){
  637. foreach ($levelList as $item) {
  638. $level = isset($item['id']) ? $item['id'] : 0;
  639. $upperCount = isset($item['upper_count']) ? $item['upper_count'] : 0;
  640. if ($orderCount >= $upperCount && $level > $memberLevel) {
  641. $upperLevel = $level;
  642. }
  643. if ($parentOrderCount >= $upperCount && $level > $parentLevel) {
  644. $parentUpperLevel = $level;
  645. }
  646. }
  647. }
  648. if ($upperLevel > 0) {
  649. MemberModel::where(['id' => $userId])->update(['member_level' => $upperLevel, 'update_time' => time()]);
  650. }
  651. if ($parentUpperLevel > 0) {
  652. MemberModel::where(['id' => $parentId])->update(['member_level' => $parentUpperLevel, 'update_time' => time()]);
  653. }
  654. $this->error = $upperLevel || $parentUpperLevel? '等级更新成功':'没有等级可更新';
  655. $result = ['user_id' => $userId, 'member_level' => $memberLevel, 'upper_level' => $upperLevel,'order_count'=>$orderCount, 'parent_level' => $parentLevel, 'parent_upper_level' => $parentUpperLevel,'parent_order_count'=>$parentOrderCount, 'parent_id' => $parentId];
  656. RedisService::set("caches:upperLevel:{$userId}_success", $result, 7200);
  657. return $result;
  658. } catch (\Exception $exception){
  659. RedisService::set("caches:upperLevel:{$userId}_error", ['error'=>$exception->getMessage(),'trace'=>$exception->getTrace()], 7200);
  660. return false;
  661. }
  662. }
  663. /**
  664. * 新人注册奖励
  665. * @param $userId
  666. * @return array|bool
  667. */
  668. public function registerReward($userId)
  669. {
  670. $rewardOpen = ConfigService::make()->getConfigByCode('register_award_coupon', 0);
  671. $rewardCouponId = ConfigService::make()->getConfigByCode('register_reward_coupon_id', 0);
  672. if ($rewardCouponId <= 0 || $rewardOpen != 1) {
  673. $this->error = '未开启或配置注册优惠券奖励';
  674. return true;
  675. }
  676. $couponInfo = CouponModel::where(['id' => $rewardCouponId, 'mark' => 1])
  677. ->first();
  678. $status = isset($couponInfo['status']) ? $couponInfo['status'] : 0;
  679. if (empty($couponInfo) || $status != 1) {
  680. $this->error = '配置的注册奖励优惠券不存在或无效';
  681. return true;
  682. }
  683. $data = [
  684. 'coupon_id' => $rewardCouponId,
  685. 'user_id' => $userId,
  686. 'store_id' => isset($couponInfo['store_id']) ? $couponInfo['store_id'] : 0,
  687. 'name' => isset($couponInfo['name']) ? $couponInfo['name'] : '',
  688. 'coupon_type' => isset($couponInfo['coupon_type']) ? $couponInfo['coupon_type'] : 20,
  689. 'reduce_price' => isset($couponInfo['reduce_price']) ? $couponInfo['reduce_price'] : 0,
  690. 'discount' => isset($couponInfo['discount']) ? $couponInfo['discount'] : 0,
  691. 'min_price' => isset($couponInfo['min_price']) ? $couponInfo['min_price'] : 0,
  692. 'expire_day' => isset($couponInfo['expire_day']) ? $couponInfo['expire_day'] : 0,
  693. 'start_time' => isset($couponInfo['start_time']) ? $couponInfo['start_time'] : 0,
  694. 'end_time' => isset($couponInfo['end_time']) ? $couponInfo['end_time'] : 0,
  695. 'goods_ids' => isset($couponInfo['goods_ids']) && $couponInfo['goods_ids'] ? $couponInfo['goods_ids'] : '',
  696. 'create_time' => time(),
  697. 'status' => 1,
  698. ];
  699. if (!$id = MemberCouponModel::insertGetId($data)) {
  700. $this->error = '奖励注册优惠券失败';
  701. return true;
  702. }
  703. $data['id'] = $id;
  704. $data['discount'] = floatval($data['discount']);
  705. $data['reduce_price'] = floatval($data['reduce_price']);
  706. unset($data['create_time']);
  707. unset($data['status']);
  708. unset($data['goods_ids']);
  709. $this->error = '奖励注册优惠券成功';
  710. return $data;
  711. }
  712. }