SettleService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. DB::beginTransaction();
  298. if ($orderType == 2) {
  299. // TODO 结算直推奖
  300. $directData = isset($commission['direct_bonus']) && $commission['direct_bonus'] ? json_decode($commission['direct_bonus'], true) : [];
  301. $directUserId = isset($commission['direct_uid']) ? $commission['direct_uid'] : 0;
  302. $directBonus = isset($directData['bonus']) ? $directData['bonus'] : 0;
  303. if ($directUserId > 0 && $directBonus > 0) {
  304. $directUser = isset($commission['direct_user']) ? $commission['direct_user'] : [];
  305. $directUserBalance = isset($directUser['balance']) ? $directUser['balance'] : 0;
  306. if ($directUser) {
  307. $updateData = [
  308. 'balance' => DB::raw("balance + {$directBonus}"),
  309. 'bonus_total' => DB::raw("bonus_total + {$directBonus}"),
  310. 'update_time' => time()
  311. ];
  312. if (!MemberModel::where(['id' => $directUserId])->update($updateData)) {
  313. DB::rollBack();
  314. RedisService::clear("{$cacheKey}lock");
  315. $this->error = '直推奖励结算错误';
  316. return false;
  317. }
  318. $data = [
  319. 'user_id' => $directUserId,
  320. 'source_order_no' => $orderNo,
  321. 'user_type' => 1,
  322. 'account_type' => 1, // 余额
  323. 'type' => 8,
  324. 'bonus_type' => 1,
  325. 'money' => $directBonus,
  326. 'after_money' => moneyFormat($directUserBalance + $directBonus, 2),
  327. 'date' => date('Y-m-d'),
  328. 'create_time' => time(),
  329. 'remark' => '直推奖',
  330. 'remark1' => "用户ID:{$orderUserId}购买礼包直推奖励",
  331. 'status' => 1,
  332. 'mark' => 1
  333. ];
  334. $logs[] = $data;
  335. }
  336. }
  337. // TODO 结算见单奖+分红
  338. $bonusUids = [];
  339. for ($i = 1; $i <= 6; $i++) {
  340. $uid = isset($commission["bonus_{$i}_uid"]) ? $commission["bonus_{$i}_uid"] : 0;
  341. if ($uid) {
  342. $bonusUids[] = $uid;
  343. }
  344. }
  345. $bonusUsers = MemberModel::whereIn('id', $bonusUids)
  346. ->select(['id', 'openid', 'balance', 'mobile', 'nickname', 'property', 'ls_score', 'status'])
  347. ->get()
  348. ->keyBy('id');
  349. $bonusUsers = $bonusUsers ? $bonusUsers->toArray() : [];
  350. for ($i = 1; $i <= 6; $i++) {
  351. $uid = isset($commission["bonus_{$i}_uid"]) ? $commission["bonus_{$i}_uid"] : 0;
  352. $bonusData = isset($commission["bonus_{$i}_data"]) ? $commission["bonus_{$i}_data"] : '';
  353. $bonusData = $bonusData ? json_decode($bonusData, true) : [];
  354. $jdBonus = isset($bonusData['jd_bonus']) ? $bonusData['jd_bonus'] : 0;
  355. $levelName = isset($bonusData['level_name']) ? $bonusData['level_name'] : '';
  356. $levelBonus = isset($bonusData['level_bonus']) ? $bonusData['level_bonus'] : 0;
  357. $bonusUser = isset($bonusUsers[$uid]) ? $bonusUsers[$uid] : [];
  358. $bonusUserBalance = isset($bonusUser['balance']) ? $bonusUser['balance'] : 0;
  359. $bonusTotal = moneyFormat($jdBonus + $levelBonus, 2);
  360. if ($uid && $bonusUser && $bonusTotal > 0) {
  361. $updateData = [
  362. 'balance' => DB::raw("balance + {$bonusTotal}"),
  363. 'bonus_total' => DB::raw("bonus_total + {$bonusTotal}"),
  364. 'update_time' => time()
  365. ];
  366. if (!MemberModel::where(['id' => $uid])->update($updateData)) {
  367. DB::rollBack();
  368. $this->error = '见单和分红奖励结算错误';
  369. RedisService::clear("{$cacheKey}lock");
  370. return false;
  371. }
  372. if ($jdBonus > 0) {
  373. $bonusUserBalance = moneyFormat($bonusUserBalance + $jdBonus, 2);
  374. $data = [
  375. 'user_id' => $uid,
  376. 'source_order_no' => $orderNo,
  377. 'user_type' => 1,
  378. 'account_type' => 1, // 余额
  379. 'type' => 8,
  380. 'bonus_type' => 2,
  381. 'money' => $jdBonus,
  382. 'after_money' => $bonusUserBalance,
  383. 'date' => date('Y-m-d'),
  384. 'create_time' => time() + $i,
  385. 'remark' => '见单奖',
  386. 'remark1' => "下{$i}级用户ID:{$orderUserId}购买礼包见单奖",
  387. 'status' => 1,
  388. 'mark' => 1
  389. ];
  390. $logs[] = $data;
  391. }
  392. if ($levelBonus > 0) {
  393. $bonusUserBalance = moneyFormat($bonusUserBalance + $levelBonus, 2);
  394. $data = [
  395. 'user_id' => $uid,
  396. 'source_order_no' => $orderNo,
  397. 'user_type' => 1,
  398. 'account_type' => 1, // 余额
  399. 'type' => 8,
  400. 'bonus_type' => 3,
  401. 'money' => $levelBonus,
  402. 'after_money' => $bonusUserBalance,
  403. 'date' => date('Y-m-d'),
  404. 'create_time' => time() + $i,
  405. 'remark' => '分红收益',
  406. 'remark1' => "下{$i}级用户ID:{$orderUserId}购买礼包等级[{$levelName}]分红收益",
  407. 'status' => 1,
  408. 'mark' => 1
  409. ];
  410. $logs[] = $data;
  411. }
  412. }
  413. }
  414. } // 商城订单
  415. else {
  416. // TODO 商家结算
  417. $storeInfo = isset($commission['store']) ? $commission['store'] : [];
  418. $storeId = isset($commission['store_id']) ? $commission['store_id'] : 0;
  419. $storeBonus = isset($commission['bonus']) ? $commission['bonus'] : 0;
  420. $storeUserId = isset($storeInfo['user_id']) ? $storeInfo['user_id'] : 0;
  421. $storeBalance = isset($storeInfo['balance']) ? $storeInfo['balance'] : 0;
  422. if ($storeId > 0 && $storeInfo && $storeBonus > 0) {
  423. $updateData = [
  424. 'balance' => DB::raw("balance + {$storeBonus}"),
  425. 'income' => DB::raw("income + {$storeBonus}"),
  426. 'update_time' => time()
  427. ];
  428. if (!StoreModel::where(['id' => $storeId])->update($updateData)) {
  429. DB::rollBack();
  430. $this->error = '商家提成结算失败';
  431. RedisService::clear("{$cacheKey}lock");
  432. return false;
  433. }
  434. $storeBalance = moneyFormat($storeBalance + $storeBonus, 2);
  435. $data = [
  436. 'user_id' => $storeUserId,
  437. 'store_id' => $storeId,
  438. 'source_order_no' => $orderNo,
  439. 'user_type' => 2,
  440. 'account_type' => 1, // 余额
  441. 'type' => 7,
  442. 'bonus_type' => 0,
  443. 'money' => $storeBonus,
  444. 'after_money' => $storeBalance,
  445. 'date' => date('Y-m-d'),
  446. 'create_time' => time(),
  447. 'remark' => '商家提成结算',
  448. 'remark1' => "用户ID:{$orderUserId}购买商品提成结算",
  449. 'status' => 1,
  450. 'mark' => 1
  451. ];
  452. $logs[] = $data;
  453. }
  454. // TODO 绿色积分结算
  455. $userInfo = isset($commission['user']) ? $commission['user'] : [];
  456. $userId = isset($commission['user_id']) ? $commission['user_id'] : 0;
  457. $userLsScore = isset($userInfo['ls_score']) ? $userInfo['ls_score'] : 0;
  458. $lsScore = isset($commission['ls_score']) ? $commission['ls_score'] : 0;
  459. if ($userId > 0 && $userInfo && $lsScore > 0) {
  460. $updateData = [
  461. 'ls_score' => DB::raw("ls_score + {$lsScore}"),
  462. 'ls_score_total' => DB::raw("ls_score_total + {$lsScore}"),
  463. 'update_time' => time()
  464. ];
  465. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  466. DB::rollBack();
  467. $this->error = '绿色积分奖励结算错误';
  468. RedisService::clear("{$cacheKey}lock");
  469. return false;
  470. }
  471. $data = [
  472. 'user_id' => $userId,
  473. 'store_id' => 0,
  474. 'source_order_no' => $orderNo,
  475. 'user_type' => 1,
  476. 'account_type' => 4, // 绿色积分
  477. 'type' => 8,
  478. 'bonus_type' => 4,
  479. 'money' => $lsScore,
  480. 'after_money' => moneyFormat($userLsScore + $lsScore, 2),
  481. 'date' => date('Y-m-d'),
  482. 'create_time' => time(),
  483. 'remark' => '绿色积分奖励',
  484. 'remark1' => "用户ID:{$orderUserId}购买商品绿色积分奖励",
  485. 'status' => 1,
  486. 'mark' => 1
  487. ];
  488. $logs[] = $data;
  489. }
  490. // TODO 数字资产和底池
  491. $property = isset($commission['property']) ? $commission['property'] : 0;
  492. $userProperty = isset($userInfo['property']) ? $userInfo['property'] : 0;
  493. if ($userId > 0 && $userInfo && $property > 0) {
  494. $updateData = [
  495. 'property' => DB::raw("property + {$property}"),
  496. 'property_total' => DB::raw("property_total + {$property}"),
  497. 'update_time' => time()
  498. ];
  499. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  500. DB::rollBack();
  501. $this->error = '数字资产奖励结算错误';
  502. RedisService::clear("{$cacheKey}lock");
  503. return false;
  504. }
  505. $data = [
  506. 'user_id' => $userId,
  507. 'store_id' => 0,
  508. 'source_order_no' => $orderNo,
  509. 'user_type' => 1,
  510. 'account_type' => 2, // 数字资产
  511. 'type' => 8,
  512. 'bonus_type' => 5,
  513. 'money' => $property,
  514. 'after_money' => moneyFormat($userProperty + $property, 2),
  515. 'date' => date('Y-m-d'),
  516. 'create_time' => time(),
  517. 'remark' => '数字资产奖励',
  518. 'remark1' => "用户ID:{$orderUserId}购买商品数字资产奖励",
  519. 'status' => 1,
  520. 'mark' => 1
  521. ];
  522. $logs[] = $data;
  523. }
  524. // 平台底池结算
  525. $ptAccount = PtAccountModel::where(['mark' => 1])->first();
  526. $accountId = isset($ptAccount['id']) ? $ptAccount['id'] : 0;
  527. $poolTotal = isset($ptAccount['pool_total']) ? $ptAccount['pool_total'] : 0;
  528. $ptPool = isset($commission['pt_pool']) ? $commission['pt_pool'] : 0;
  529. if ($ptAccount && ($ptPool > 0 || $property > 0)) {
  530. $updateData = [
  531. 'pool_total' => DB::raw("pool_total + {$ptPool}"),
  532. 'today_pool' => DB::raw("today_pool + {$ptPool}"),
  533. 'today_property' => DB::raw("today_property + {$property}"),
  534. 'update_time' => time()
  535. ];
  536. if (!PtAccountModel::where(['id' => $accountId])->update($updateData)) {
  537. DB::rollBack();
  538. $this->error = '平台底池和资产结算错误';
  539. RedisService::clear("{$cacheKey}lock");
  540. return false;
  541. }
  542. // 底池明细
  543. if ($ptPool > 0) {
  544. $data = [
  545. 'user_id' => 0,
  546. 'store_id' => 0,
  547. 'source_order_no' => $orderNo,
  548. 'user_type' => 0,
  549. 'account_type' => 5, // 平台账户
  550. 'type' => 10,
  551. 'bonus_type' => 6,
  552. 'money' => $ptPool,
  553. 'after_money' => moneyFormat($poolTotal + $ptPool, 2),
  554. 'date' => date('Y-m-d'),
  555. 'create_time' => time(),
  556. 'remark' => '返回底池',
  557. 'remark1' => "用户ID:{$orderUserId}购买商品返回底池",
  558. 'status' => 1,
  559. 'mark' => 1
  560. ];
  561. $logs[] = $data;
  562. }
  563. }
  564. }
  565. // TODO 明细入表
  566. RedisService::set("{$cacheKey}logs", $logs, 3600);
  567. if ($logs && !AccountLogModel::insert($logs)) {
  568. DB::rollBack();
  569. $this->error = '奖励明细结算处理错误';
  570. RedisService::clear("{$cacheKey}lock");
  571. return false;
  572. }
  573. // 订单结算状态更新
  574. if(!OrderCommissionModel::where(['id'=>$commissionId])->update(['status'=>1,'arrival_at'=>date('Y-m-d H:i:s'),'update_time'=>time()])){
  575. DB::rollBack();
  576. $this->error = '订单结算状态更新失败';
  577. return false;
  578. }
  579. DB::commit();
  580. $this->error = '订单结算完成';
  581. RedisService::clear("{$cacheKey}lock");
  582. RedisService::set("{$cacheKey}result", $info, 3600);
  583. $result = ['id' => $orderId, 'user_id' => $orderUserId, 'logs' => $logs, 'commission' => $commission];
  584. RedisService::set("caches:settles:{$orderId}_success", $result, 7200);
  585. return $result;
  586. } catch (\Exception $exception){
  587. RedisService::set("caches:settles:{$orderId}_error", ['error'=>$exception->getMessage(),'trace'=>$exception->getTrace()], 7200);
  588. return false;
  589. }
  590. }
  591. /**
  592. * 更新用户等级和下级团队购买礼包人数
  593. * @param $userId
  594. * @param $points
  595. * @return bool
  596. */
  597. public function updateUserData($userId, $points)
  598. {
  599. // 上级用户复购参数更新
  600. $points = $points ? explode(',', $points) : [];
  601. $points = array_filter($points);
  602. $points = array_reverse($points);
  603. $rewardLayer = ConfigService::make()->getConfigByCode('business_reward_layer', 6);
  604. $points = array_splice($points, 0, $rewardLayer);
  605. if ($points) {
  606. // 累计购买人数
  607. MemberModel::where(['bonus_status'=>1,'mark' => 1])->whereIn('id', $points)
  608. ->update(['buy_count' => DB::raw("buy_count + 1"), 'update_time' => time()]);
  609. // 购买人数满N层暂停收益
  610. $buyCount = pow(2, $rewardLayer + 1) - 2;
  611. MemberModel::where('buy_count', '>=', $buyCount)
  612. ->where(['bonus_status'=>1])
  613. ->whereIn('id', $points)
  614. ->update(['buy_count' => 0, 'buy_type' => DB::raw("buy_type+1"), 'bonus_status' => 2, 'update_time' => time()]);
  615. }
  616. // 用户升级
  617. $userInfo = MemberModel::with(['parent'])->where(['id' => $userId, 'mark' => 1])
  618. ->select(['id', 'member_level', 'parent_id', 'status'])
  619. ->first();
  620. $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : [];
  621. $parent = isset($userInfo['parent']) ? $userInfo['parent'] : [];
  622. $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
  623. $parentLevel = isset($parent['member_level']) ? $parent['member_level'] : 0;
  624. $upperLevel = 0;
  625. $parentUpperLevel = 0;
  626. $levelList = MemberLevelService::make()->getListByLevel();
  627. $orderCount = OrderModel::from('orders as a')
  628. ->distinct('user_id')
  629. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  630. ->where('a.status', '>', 1)
  631. ->where(['a.main_no' => '', 'a.type' => 2, 'a.user_id' => $userId, 'a.pay_status' => 20])
  632. ->count();
  633. $parentOrderCount = 0;
  634. if ($parentId > 0) {
  635. $parentOrderCount = OrderModel::from('orders as a')
  636. ->distinct('user_id')
  637. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  638. ->where('a.status', '>', 1)
  639. ->where(['a.main_no' => '', 'a.type' => 2, 'b.parent_id' => $parentId, 'a.pay_status' => 20])
  640. ->count();
  641. }
  642. // 升级检查
  643. if($orderCount||$parentOrderCount){
  644. foreach ($levelList as $item) {
  645. $level = isset($item['id']) ? $item['id'] : 0;
  646. $upperCount = isset($item['upper_count']) ? $item['upper_count'] : 0;
  647. if ($orderCount >= $upperCount && $level > $memberLevel) {
  648. $upperLevel = $level;
  649. }
  650. if ($parentOrderCount >= $upperCount && $level > $parentLevel) {
  651. $parentUpperLevel = $level;
  652. }
  653. }
  654. }
  655. if ($upperLevel > 0) {
  656. MemberModel::where(['id' => $userId])->update(['member_level' => $upperLevel, 'update_time' => time()]);
  657. }
  658. if ($parentUpperLevel > 0) {
  659. MemberModel::where(['id' => $parentId])->update(['member_level' => $parentUpperLevel, 'update_time' => time()]);
  660. }
  661. $this->error = $upperLevel || $parentUpperLevel? '等级更新成功':'没有等级可更新';
  662. return ['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];
  663. }
  664. /**
  665. * 新人注册奖励
  666. * @param $userId
  667. * @return array|bool
  668. */
  669. public function registerReward($userId)
  670. {
  671. $rewardOpen = ConfigService::make()->getConfigByCode('register_award_coupon', 0);
  672. $rewardCouponId = ConfigService::make()->getConfigByCode('register_reward_coupon_id', 0);
  673. if ($rewardCouponId <= 0 || $rewardOpen != 1) {
  674. $this->error = '未开启或配置注册优惠券奖励';
  675. return true;
  676. }
  677. $couponInfo = CouponModel::where(['id' => $rewardCouponId, 'mark' => 1])
  678. ->first();
  679. $status = isset($couponInfo['status']) ? $couponInfo['status'] : 0;
  680. if (empty($couponInfo) || $status != 1) {
  681. $this->error = '配置的注册奖励优惠券不存在或无效';
  682. return true;
  683. }
  684. $data = [
  685. 'coupon_id' => $rewardCouponId,
  686. 'user_id' => $userId,
  687. 'store_id' => isset($couponInfo['store_id']) ? $couponInfo['store_id'] : 0,
  688. 'name' => isset($couponInfo['name']) ? $couponInfo['name'] : '',
  689. 'coupon_type' => isset($couponInfo['coupon_type']) ? $couponInfo['coupon_type'] : 20,
  690. 'reduce_price' => isset($couponInfo['reduce_price']) ? $couponInfo['reduce_price'] : 0,
  691. 'discount' => isset($couponInfo['discount']) ? $couponInfo['discount'] : 0,
  692. 'min_price' => isset($couponInfo['min_price']) ? $couponInfo['min_price'] : 0,
  693. 'expire_day' => isset($couponInfo['expire_day']) ? $couponInfo['expire_day'] : 0,
  694. 'start_time' => isset($couponInfo['start_time']) ? $couponInfo['start_time'] : 0,
  695. 'end_time' => isset($couponInfo['end_time']) ? $couponInfo['end_time'] : 0,
  696. 'goods_ids' => isset($couponInfo['goods_ids']) && $couponInfo['goods_ids'] ? $couponInfo['goods_ids'] : '',
  697. 'create_time' => time(),
  698. 'status' => 1,
  699. ];
  700. if (!$id = MemberCouponModel::insertGetId($data)) {
  701. $this->error = '奖励注册优惠券失败';
  702. return true;
  703. }
  704. $data['id'] = $id;
  705. $data['discount'] = floatval($data['discount']);
  706. $data['reduce_price'] = floatval($data['reduce_price']);
  707. unset($data['create_time']);
  708. unset($data['status']);
  709. unset($data['goods_ids']);
  710. $this->error = '奖励注册优惠券成功';
  711. return $data;
  712. }
  713. }