SettleService.php 34 KB

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