Taskorderguquan.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/28
  6. */
  7. namespace app\controller\api\store\merchant;
  8. use app\common\model\user\User;
  9. use think\facade\Db;
  10. use think\Request;
  11. class Taskorderguquan
  12. {
  13. public function assignment()
  14. {
  15. /**
  16. 13231658486周行体300
  17. 13333069689魏惠新300
  18. 15832971815孟晓玮300
  19. 13959100621周金花300
  20. 13701061395李素红100
  21. 15810064711 王 玲 200
  22. 13521438652徐立苹100
  23. 18760167163刘春梅50
  24. 15332507711张传莲50
  25. 15960349661吕丽娣50
  26. 18715313601大饼200
  27. */
  28. $data = [
  29. '13231658486' => 300,
  30. '13333069689' => 300,
  31. '15832971815' => 300,
  32. '13959100621' => 300,
  33. '13701061395' => 100,
  34. '15810064711' => 200,
  35. '13521438652' => 100,
  36. '18760167163' => 50,
  37. '15332507711' => 50,
  38. '15960349661' => 50,
  39. '18715313601' => 200,
  40. ];
  41. $result = Db::name("user")
  42. ->whereIn("account", array_keys($data))
  43. ->where('is_del', 0)
  44. ->field('uid, brokerage_price, account, nickname')
  45. ->select();
  46. $res = [];
  47. foreach ($result as $v) {
  48. $yj_90 = $data[$v['account']] * 0.9;
  49. $fg_10 = $data[$v['account']] * 0.1;
  50. Db::name("user")->where("uid", $v['uid'])->inc("brokerage_price", $yj_90)->save();
  51. Db::name("user")->where("uid", $v['uid'])->inc("total_amount_old", $yj_90)->save();
  52. Db::name("user")->where("uid", $v['uid'])->inc("amount_old", $yj_90)->save();
  53. Db::name("user")->where("uid", $v['uid'])->inc("fugou", $fg_10)->save();
  54. $this->bill_user_log111111($v['uid'], "佣金补贴", $yj_90, "佣金补贴", 8);//股权分佣明细2.0系统
  55. $this->fugou_user_log($v['uid'], $fg_10, 16, 1, 1);//复购金明细入账
  56. $res[] = [
  57. 'uid' => $v['uid'],
  58. 'account' => $v['account'],
  59. 'brokerage_price_pre' => $v['brokerage_price'],
  60. 'brokerage_price' => $data[$v['account']],
  61. 'nickname' => $v['nickname']
  62. ];
  63. }
  64. return json($res);
  65. try {
  66. // 1. 获取所有用户数据(按level_id正序排序保证父级在前)
  67. $users = Db::name('user')
  68. ->where('is_del', '=', 0)
  69. ->where('status', '=', 1)
  70. ->where('user_group', '>=', 2)
  71. ->order('level_id', 'asc')
  72. ->field('uid,account,nickname,level_id,is_del,status,user_group')
  73. ->select()
  74. ->toArray();
  75. // 2. 构建以UID为键的映射表
  76. $userMap = [];
  77. foreach ($users as $user) {
  78. $user['children'] = []; // 初始化子节点
  79. $userMap[$user['uid']] = [
  80. '用户ID' => $user['uid'],
  81. '用户账号' => $user['account'],
  82. '用户昵称' => $user['nickname'],
  83. '用户身份' => $user['user_group'],
  84. '上级用户ID' => $user['level_id'],
  85. '直属下级用户人数' => 0,
  86. '直属下级用户列表' => $user['children'],
  87. ];
  88. }
  89. // 3. 递归构建树形结构
  90. $tree = [];
  91. foreach ($userMap as $uid => &$user) {
  92. $parentId = $user['上级用户ID'];
  93. if ($parentId == 0) {
  94. // 顶级节点直接加入树
  95. $tree[] = &$user;
  96. } elseif (isset($userMap[$parentId])) {
  97. // 非顶级节点加入父节点的children
  98. $userMap[$parentId]['直属下级用户列表'][] = &$user;
  99. $userMap[$parentId]['直属下级用户人数']++;
  100. }
  101. }
  102. unset($user); // 销毁引用
  103. return json($tree);
  104. } catch (\Exception $e) {
  105. // 记录错误日志
  106. return json(['code' => 0, 'msg' => '树结构生成失败']);
  107. }
  108. $result = [
  109. 'oldOrderExistGongXian' => [],
  110. 'orderExistGongXian' => [],
  111. 'oldOrderExistPv' => [],
  112. 'orderExistPv' => [],
  113. 'gongXianDataOldList' => [],
  114. 'gongXianDataList' => [],
  115. 'pvDataOldList' => [],
  116. 'pvDataList' => [],
  117. 'contributeNot' => [],
  118. 'pvNot' => [],
  119. 'userUpdateSummary' => ['success' => 0, 'failed' => 0, 'skipped' => 0], // 优化: 汇总用户更新结果
  120. 'insertGongXianSummary' => ['success' => 0, 'failed' => 0], // 优化: 汇总贡献插入结果
  121. 'insertPvSummary' => ['success' => 0, 'failed' => 0], // 优化: 汇总PV插入结果
  122. 'errors' => [], // 优化: 统一记录错误信息
  123. ];
  124. // --- 数据库查询部分 (保持不变,已优化) ---
  125. // 1. 查询用户
  126. $userList = Db::name("user")
  127. ->where('is_del', 0)
  128. // ->whereIn('uid', [277, 1593])
  129. ->column('uid, spread_uid, contribute, per_contribute, pv', 'uid');
  130. // $userIds = array_keys($userList);
  131. $oldUserList = Db::name("old_user_jbp")
  132. ->column('id, amount');
  133. // 2. 查询老订单
  134. $userOfGoodsListRaw = Db::name("old_user_of_goods")
  135. ->where('status', 1)
  136. // 优化: 如果用户量大,可以考虑只查相关用户的订单
  137. // ->whereIn('user_id', $userIds)
  138. ->field('user_id, money, order_id, create_time')
  139. ->select()
  140. ->toArray();
  141. // 3. 查询新订单
  142. $storeOrderListRaw = Db::name("store_order")
  143. ->whereIn('status', [0, 1, 2, 3])
  144. // 优化: 如果用户量大,可以考虑只查相关用户的订单
  145. // ->whereIn('uid', $userIds)
  146. ->field('uid, pay_price, mer_id, status, order_id, create_time')
  147. ->select()
  148. ->toArray();
  149. // 4. 查询已存在的贡献值记录
  150. $gongXianListRaw = Db::name("user_sign_gongxian")
  151. ->where('status', '<>', -1)
  152. // 优化: 如果记录非常多,可以分批查询或只查相关用户的
  153. // ->whereIn('uid', array_merge($userIds, array_column($userList, 'spread_uid'))) // 查询购买者和潜在推广者
  154. ->field('uid, order_id, number, status, type')
  155. ->select()
  156. ->toArray();
  157. // 5. 查询已存在的PV记录
  158. $pvListRaw = Db::name("user_pv_log")
  159. ->where('status', '<>', -1)
  160. // 优化: 同上
  161. // ->whereIn('uid', $userIds)
  162. ->field('uid, order_id, pv, status')
  163. ->select()
  164. ->toArray();
  165. // --- 数据预处理部分
  166. $existingGongXian = [];
  167. $existingGongXianByUid = [];
  168. foreach ($gongXianListRaw as $gx) {
  169. $existingGongXian[$gx['uid'] . '_' . $gx['order_id']] = ['number' => $gx['number'], 'status' => $gx['status'], 'type' => $gx['type']];
  170. // TODO 目前老订单id 1-900 新订单ID 4868 - 6255 线下扫码订单 6870 - 8733
  171. if ((1 <= $gx['order_id'] && $gx['order_id'] <= 900) || (4868 <= $gx['order_id'] && $gx['order_id'] <= 6869)) {
  172. $existingGongXianByUid[$gx['uid']][] = $gx['order_id'];
  173. }
  174. }
  175. unset($gongXianListRaw);
  176. $existingPv = [];
  177. $existingPvByUid = [];
  178. foreach ($pvListRaw as $pv) {
  179. $existingPv[$pv['uid'] . '_' . $pv['order_id']] = ['pv' => $pv['pv'], 'status' => $pv['status']];
  180. // TODO 目前老订单id 1-900 新订单ID 4868 - 6255 线下扫码订单 6870 - 8733
  181. if ((1 <= $pv['order_id'] && $pv['order_id'] <= 900) || (4868 <= $pv['order_id'] && $pv['order_id'] <= 6869)) {
  182. $existingPvByUid[$pv['uid']][] = $pv['order_id'];
  183. }
  184. }
  185. unset($pvListRaw);
  186. $userOfGoodsByUid = [];
  187. foreach ($userOfGoodsListRaw as $oldOrder) $userOfGoodsByUid[$oldOrder['user_id']][] = $oldOrder;
  188. unset($userOfGoodsListRaw);
  189. $storeOrdersByUid = [];
  190. foreach ($storeOrderListRaw as $order) $storeOrdersByUid[$order['uid']][] = $order;
  191. unset($storeOrderListRaw);
  192. // *** 主逻辑处理与数据收集 ***
  193. $gongXianDataToInsert = []; // 优化: 合并新旧贡献插入列表
  194. $pvDataToInsert = []; // 优化: 合并新旧PV插入列表
  195. $userUpdateData = []; // 优化: 收集所有需要更新的用户数据 [uid => ['contribute'=>float, 'pv'=>float, 'per_contribute'=>float]]
  196. // 初始化所有用户的待更新数据
  197. foreach ($userList as $uid => $user) {
  198. // 预先设置初始值,确保即使没有订单的用户也能被包含在后续可能的更新逻辑中(如果需要)
  199. // 如果用户没有任何订单,他们的 contribute 和 pv 应该保持数据库原始值或根据业务逻辑设为0
  200. // 这里我们先假设如果没有订单,他们的计算值就是0
  201. $userUpdateData[$uid] = [
  202. 'uid' => $uid,
  203. 'contribute' => 0.0,
  204. 'pv' => 0.0,
  205. 'per_contribute' => 0.0,
  206. ];
  207. // 初始化推广者的 per_contribute 累加器
  208. if (!empty($user['spread_uid'])) {
  209. $spreadUid = $user['spread_uid'];
  210. if (!isset($userUpdateData[$spreadUid])) {
  211. if (isset($userList[$spreadUid])) {
  212. if (!isset($userUpdateData[$spreadUid]['per_contribute'])) {
  213. $userUpdateData[$spreadUid]['per_contribute'] = 0.0; // 初始化推广者的累加
  214. }
  215. } else {
  216. // 记录或处理推广者不在当前用户列表的情况
  217. $result['errors'][] = "执行{$uid}时,没找到 推荐人 {$spreadUid}";
  218. // 可以选择跳过给这个推广者增加 per_contribute
  219. }
  220. } else if (!isset($userUpdateData[$spreadUid]['per_contribute'])) {
  221. $userUpdateData[$spreadUid]['per_contribute'] = 0.0; // 初始化推广者的累加
  222. }
  223. }
  224. }
  225. // 遍历用户列表进行计算和数据收集
  226. foreach ($userList as $uid => $user) {
  227. $spreadUid = $user['spread_uid'];
  228. $existingGongXianByUid[$uid] = $existingGongXianByUid[$uid] ?? [];
  229. $existingPvByUid[$uid] = $existingPvByUid[$uid] ?? [];
  230. if (isset($userOfGoodsByUid[$uid])) {
  231. foreach ($userOfGoodsByUid[$uid] as $oldOrder) {
  232. switch ($oldOrder['money']) {
  233. case 1180:
  234. $totalPv = 700.0;
  235. break;
  236. case 11800:
  237. $totalPv = 7000.0;
  238. break;
  239. case 10620:
  240. $totalPv = 6300.0;
  241. break;
  242. case 2360:
  243. $totalPv = 1400.0;
  244. break;
  245. case 9440:
  246. $totalPv = 5600.0;
  247. break;
  248. default:
  249. $totalPv = round($oldOrder['money'] * 0.7, 2);
  250. break;
  251. }
  252. $settlementTimeOld = $oldOrder['create_time'] ? date('Y-m-d H:i:s', $oldOrder['create_time']) : strtotime($user['create_time']);
  253. // --- 收集贡献值插入数据 ---
  254. $orderKey = $uid . '_' . $oldOrder['order_id'];
  255. // 一个人 同一个订单 可能有多条记录 有问题
  256. if (isset($existingGongXian[$orderKey])) {
  257. $result['oldOrderExistGongXian'][] = ['uid' => $uid, 'order_id' => $oldOrder['order_id']];
  258. $existingGongXianByUid[$uid] = array_diff($existingGongXianByUid[$uid], [$oldOrder['order_id']]);
  259. if ($existingGongXian[$orderKey]['status'] == 1) {
  260. if ($existingGongXian[$orderKey]['type'] == 1) {
  261. $userUpdateData[$uid]['contribute'] += $existingGongXian[$orderKey]['number'];
  262. } else if ($existingGongXian[$orderKey]['type'] == 2) {
  263. $userUpdateData[$uid]['contribute'] -= $existingGongXian[$orderKey]['number'];
  264. }
  265. }
  266. if (!empty($spreadUid) && isset($userUpdateData[$spreadUid])) {
  267. $existingGongXianByUid[$spreadUid] = array_diff($existingGongXianByUid[$spreadUid], [$oldOrder['order_id']]);
  268. if (isset($existingGongXian[$spreadUid . '_' . $oldOrder['order_id']])) {
  269. if ($existingGongXian[$spreadUid . '_' . $oldOrder['order_id']]['status'] == 1) {
  270. if ($existingGongXian[$spreadUid . '_' . $oldOrder['order_id']]['type'] == 1) {
  271. $userUpdateData[$spreadUid]['per_contribute'] += $existingGongXian[$spreadUid . '_' . $oldOrder['order_id']]['number'];
  272. } else if ($existingGongXian[$spreadUid . '_' . $oldOrder['order_id']]['type'] == 2) {
  273. $userUpdateData[$spreadUid]['per_contribute'] -= $existingGongXian[$spreadUid . '_' . $oldOrder['order_id']]['number'];
  274. }
  275. }
  276. }
  277. }
  278. } else {
  279. if ($totalPv > 0) {
  280. $userUpdateData[$uid]['contribute'] += $totalPv;
  281. // --- 收集贡献值插入数据 ---
  282. $gongXianBase = [
  283. "number" => $totalPv, "addtime" => strtotime($settlementTimeOld), "status" => 1,
  284. "order_type" => 21, "order_id" => $oldOrder['order_id'],
  285. "desc" => '', "surplus" => 0, "type" => 1, 'settlement_time' => $settlementTimeOld
  286. ];
  287. // 本人
  288. $gongXianDataToInsert[] = array_merge(["uid" => $uid, "mark" => "购买会员专区商品赠送贡献值(1.0)"], $gongXianBase);
  289. $existingGongXianByUid[$uid] = array_diff($existingGongXianByUid[$uid], [$oldOrder['order_id']]);
  290. // 推广人
  291. if (!empty($user['spread_uid'])) {
  292. if (isset($userUpdateData[$spreadUid])) {
  293. $userUpdateData[$spreadUid]['per_contribute'] += $totalPv;
  294. $gongXianDataToInsert[] = array_merge(["uid" => $spreadUid, "mark" => "推广会员专区商品赠送贡献值(1.0)"], $gongXianBase);
  295. $existingGongXianByUid[$spreadUid] = array_diff($existingGongXianByUid[$spreadUid], [$oldOrder['order_id']]);
  296. }
  297. }
  298. }
  299. }
  300. // --- 收集 PV 插入数据 ---
  301. if (isset($existingPv[$orderKey])) {
  302. $result['oldOrderExistPv'][] = ['uid' => $uid, 'order_id' => $oldOrder['order_id']];
  303. if ($existingPv[$orderKey]['status'] == 1) {
  304. $userUpdateData[$uid]['pv'] += $existingPv[$orderKey]['pv'];
  305. }
  306. } else {
  307. $userUpdateData[$uid]['pv'] += $totalPv;
  308. $pvDataToInsert[] = [
  309. "uid" => $uid, "pv" => $totalPv, "addtime" => strtotime($settlementTimeOld), "status" => 1,
  310. "order_type" => 21, "order_id" => $oldOrder['order_id'],
  311. "mark" => "购买会员专区商品赠送PV(1.0)", 'settlement_time' => $settlementTimeOld
  312. ];
  313. }
  314. if (!empty($existingPvByUid[$uid])) {
  315. $existingPvByUid[$uid] = array_diff($existingPvByUid[$uid], [$oldOrder['order_id']]);
  316. }
  317. }
  318. } // end if isset($userOfGoodsByUid[$uid])
  319. // --- 处理新订单 ---
  320. if (isset($storeOrdersByUid[$uid])) {
  321. foreach ($storeOrdersByUid[$uid] as $order) {
  322. $totalPv = 0.0;
  323. // ... (if/else logic and switch case for $totalPv calculation - same as before)
  324. if ($order['mer_id'] == 496) {
  325. switch ($order['pay_price']) {
  326. case 1180:
  327. $totalPv = 700.0;
  328. break;
  329. case 11800:
  330. $totalPv = 7000.0;
  331. break;
  332. case 10620:
  333. $totalPv = 6300.0;
  334. break;
  335. case 2360:
  336. $totalPv = 1400.0;
  337. break;
  338. case 9440:
  339. $totalPv = 5600.0;
  340. break;
  341. default:
  342. $totalPv = round($order['pay_price'] * 0.7, 2);
  343. break;
  344. }
  345. } else if ($order['status'] == 3) {
  346. $totalPv = round($order['pay_price'] * 0.7, 2);
  347. }
  348. // --- 收集贡献值插入数据 ---
  349. $orderKey = $uid . '_' . $order['order_id'];
  350. $settlementTimeNew = $order['create_time'] ?: date('Y-m-d H:i:s', $user['create_time']);
  351. // --- 收集贡献值插入数据 ---
  352. if (isset($existingGongXian[$orderKey])) {
  353. $result['orderExistGongXian'][] = ['uid' => $uid, 'order_id' => $order['order_id']];
  354. $existingGongXianByUid[$uid] = array_diff($existingGongXianByUid[$uid], [$order['order_id']]);
  355. // 累加计算值
  356. if ($existingGongXian[$orderKey]['status'] == 1) {
  357. if ($existingGongXian[$orderKey]['type'] == 1) {
  358. $userUpdateData[$uid]['contribute'] += $existingGongXian[$orderKey]['number'];
  359. } else if ($existingGongXian[$orderKey]['type'] == 2) {
  360. $userUpdateData[$uid]['contribute'] -= $existingGongXian[$orderKey]['number'];
  361. }
  362. }
  363. if (!empty($spreadUid) && isset($userUpdateData[$spreadUid])) {
  364. $existingGongXianByUid[$spreadUid] = array_diff($existingGongXianByUid[$spreadUid], [$order['order_id']]);
  365. if (isset($existingGongXian[$spreadUid . '_' . $order['order_id']])) {
  366. if ($existingGongXian[$spreadUid . '_' . $order['order_id']]['status'] == 1) {
  367. if ($existingGongXian[$spreadUid . '_' . $order['order_id']]['type'] == 1) {
  368. $userUpdateData[$spreadUid]['per_contribute'] += $existingGongXian[$spreadUid . '_' . $order['order_id']]['number'];
  369. } else if ($existingGongXian[$spreadUid . '_' . $order['order_id']]['type'] == 2) {
  370. $userUpdateData[$spreadUid]['per_contribute'] += $existingGongXian[$spreadUid . '_' . $order['order_id']]['number'];
  371. }
  372. }
  373. }
  374. }
  375. } else {
  376. if ($totalPv > 0) {
  377. $userUpdateData[$uid]['contribute'] += $totalPv;
  378. $gongXianBase = [ // 优化: 提取公共字段
  379. "number" => $totalPv, "addtime" => strtotime($settlementTimeNew), "status" => 1,
  380. "order_type" => 22, "order_id" => $order['order_id'],
  381. "desc" => '', "surplus" => 0, "type" => 1, 'settlement_time' => $settlementTimeNew
  382. ];
  383. // 本人
  384. $gongXianDataToInsert[] = array_merge(["uid" => $uid, "mark" => "购买商品赠送贡献值(2.0)"], $gongXianBase);
  385. $existingGongXianByUid[$uid] = array_diff($existingGongXianByUid[$uid], [$order['order_id']]);
  386. // 推广人
  387. if (!empty($user['spread_uid'])) {
  388. $spreadUid = $user['spread_uid'];
  389. if (isset($userUpdateData[$spreadUid])) { // 确保推广者存在
  390. $gongXianDataToInsert[] = array_merge(["uid" => $spreadUid, "mark" => "推广商品赠送贡献值(2.0)"], $gongXianBase);
  391. $userUpdateData[$spreadUid]['per_contribute'] += $totalPv; // 累加推广者业绩
  392. $existingGongXianByUid[$spreadUid] = array_diff($existingGongXianByUid[$spreadUid], [$order['order_id']]);
  393. }
  394. }
  395. }
  396. }
  397. // --- 收集 PV 插入数据 ---
  398. if (isset($existingPv[$orderKey])) {
  399. $result['orderExistPv'][] = ['uid' => $uid, 'order_id' => $order['order_id']];
  400. if ($existingPv[$orderKey]['status'] == 1) {
  401. $userUpdateData[$uid]['pv'] += $existingPv[$orderKey]['pv'];
  402. }
  403. } else {
  404. $userUpdateData[$uid]['pv'] += $totalPv;
  405. $pvDataToInsert[] = [
  406. "uid" => $uid, "pv" => $totalPv, "addtime" => strtotime($settlementTimeNew), "status" => 1,
  407. "order_type" => 22, // 确认 PV log 的 order_type 是否与贡献一致或有区分
  408. "order_id" => $order['order_id'],
  409. "mark" => "购买商品赠送PV(2.0)", // 标记调整为更通用
  410. 'settlement_time' => $settlementTimeNew
  411. ];
  412. }
  413. if (!empty($existingPvByUid[$uid])) {
  414. $existingPvByUid[$uid] = array_diff($existingPvByUid[$uid], [$order['order_id']]);
  415. }
  416. }
  417. }
  418. // end if isset($storeOrdersByUid[$uid])
  419. // --- 比较计算值与原值 (移到循环外处理) ---
  420. // (在此处比较并记录到 $result['contributeNot'] / $result['pvNot'])
  421. // 注意:浮点数比较可能需要容差
  422. $tolerance = 0.001; // 定义一个小的容差值
  423. if (abs($userUpdateData[$uid]['contribute'] - (float)$user['contribute']) > $tolerance) {
  424. $result['contributeNot'][] = [
  425. 'uid' => $uid,
  426. 'contribute_old' => (float)$user['contribute'], // 显式转换
  427. 'contribute_new' => $userUpdateData[$uid]['contribute']
  428. ];
  429. }
  430. if (abs($userUpdateData[$uid]['pv'] - (float)$user['pv']) > $tolerance) {
  431. $result['pvNot'][] = [
  432. 'uid' => $uid,
  433. 'pv_old' => (float)$user['pv'], // 显式转换
  434. 'pv_new' => $userUpdateData[$uid]['pv']
  435. ];
  436. }
  437. // per_contribute 是累加的,不需要与旧值比较,直接更新
  438. } // end foreach ($userList as $uid => $user)
  439. // *** 数据库批量操作 ***
  440. // 使用事务确保数据一致性
  441. Db::startTrans();
  442. try {
  443. $bill = [];
  444. foreach ($oldUserList as $item) {
  445. $bill[] = [
  446. 'uid' => $item['id'],
  447. 'link_id' => 0,//关联订单id
  448. 'pm' => 1,//0:支出,1:获得
  449. 'title' => '佣金',//账单标题
  450. 'category' => 'now_money',//明细种类
  451. 'type' => 'commission',//明细类型
  452. 'number' => $item['amount'],//明细数字
  453. 'balance' => 0,//剩余
  454. 'mark' => '1.0佣金记录',//备注
  455. 'create_time' => '2025-02-04 00:00:00',//添加时间
  456. 'status' => 1,//0待确定,1有效,-1无效
  457. 'commission_type' => 21,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  458. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  459. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  460. 'order_sn' => 0,//订单号
  461. 'tripartite' => 0,//
  462. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  463. 'mer_id' => 0,//商户id
  464. 'source' => 0,//1线下 0线上
  465. 'order_type' => 1,//1自营 2 第三方 订单类型
  466. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  467. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  468. 'take_time' => '',//结算时间
  469. 'district_id' => '',//
  470. 'street_id' => '',//
  471. 'month' => '',//月份
  472. ];
  473. }
  474. $chunkedBill = array_chunk($bill, 500);
  475. foreach ($chunkedBill as $chunk) {
  476. Db::name('user_bill')->insertAll($chunk);
  477. }
  478. if (!empty($existingGongXianByUid)) {
  479. foreach ($existingGongXianByUid as $uid => $orderList) {
  480. if (!empty($orderList)) {
  481. Db::name("user_sign_gongxian")
  482. ->where('uid', $uid)
  483. ->whereIn('order_id', $orderList)
  484. ->update(['status' => 0]);
  485. }
  486. }
  487. }
  488. if (!empty($existingPvByUid)) {
  489. foreach ($existingPvByUid as $uid => $orderList) {
  490. if (!empty($orderList)) {
  491. Db::name("user_pv_log")
  492. ->where('uid', $uid)
  493. ->whereIn('order_id', $orderList)
  494. ->update(['status' => 0]);
  495. }
  496. }
  497. }
  498. // --- 批量插入贡献值 ---
  499. if (!empty($gongXianDataToInsert)) {
  500. // 分块插入,避免单次插入过多数据 (例如一次500条)
  501. $chunkedGongXian = array_chunk($gongXianDataToInsert, 500);
  502. foreach ($chunkedGongXian as $chunk) {
  503. $insertedGongXianCount = Db::name('user_sign_gongxian')->insertAll($chunk);
  504. $result['insertGongXianSummary']['success'] += $insertedGongXianCount;
  505. }
  506. if ($result['insertGongXianSummary']['success'] < count($gongXianDataToInsert)) {
  507. $result['insertGongXianSummary']['failed'] = count($gongXianDataToInsert) - $result['insertGongXianSummary']['success'];
  508. // 可以考虑记录更详细的失败信息,但这比较复杂,需要知道哪些失败了
  509. $result['errors'][] = "GongXian insertion partially failed or failed completely.";
  510. // 根据业务决定是否回滚
  511. // throw new Exception("GongXian insertion failed");
  512. }
  513. } else {
  514. $result['insertGongXianSummary']['skipped'] = true; // 标记为跳过(无数据)
  515. }
  516. // --- 批量插入PV ---
  517. if (!empty($pvDataToInsert)) {
  518. $chunkedPv = array_chunk($pvDataToInsert, 500);
  519. foreach ($chunkedPv as $chunk) {
  520. $insertedPvCount = Db::name('user_pv_log')->insertAll($chunk);
  521. $result['insertPvSummary']['success'] += $insertedPvCount;
  522. }
  523. if ($result['insertPvSummary']['success'] < count($pvDataToInsert)) {
  524. $result['insertPvSummary']['failed'] = count($pvDataToInsert) - $result['insertPvSummary']['success'];
  525. $result['errors'][] = "PV Log insertion partially failed or failed completely.";
  526. // throw new Exception("PV Log insertion failed");
  527. }
  528. } else {
  529. $result['insertPvSummary']['skipped'] = true; // 标记为跳过(无数据)
  530. }
  531. // --- 批量更新用户信息 ---
  532. $usersToUpdate = [];
  533. // 筛选出实际需要更新的用户数据 (值有变化或 per_contribute > 0)
  534. foreach ($userUpdateData as $uid => $updateInfo) {
  535. $originalUser = $userList[$uid]; // 获取原始用户信息
  536. // 检查 contribute 或 pv 是否变化(使用容差),或者 per_contribute 是否需要更新
  537. $needsUpdate = false;
  538. if (abs($updateInfo['contribute'] - (float)$originalUser['contribute']) > $tolerance) $needsUpdate = true;
  539. if (abs($updateInfo['pv'] - (float)$originalUser['pv']) > $tolerance) $needsUpdate = true;
  540. // per_contribute 始终更新,因为它是在现有基础上累加的(除非业务定义不同)
  541. // 如果 per_contribute 数据库设计是直接覆盖,那只有 > 0 时才需要更新
  542. // 假设是覆盖更新,且只有大于0时才需要更新变动(如果一直是0则不用更新)
  543. if ($updateInfo['per_contribute'] > 0.0) { // 只有当推广业绩>0时才触发更新
  544. // 这里需要确认 per_contribute 是覆盖还是累加?
  545. // 如果是数据库累加: Db::name("user")->where('uid', $uid)->inc('per_contribute', $amount)->update(); 需要单独处理
  546. // 如果是覆盖: 直接放入 saveAll
  547. // 假设是覆盖,我们只更新有变化的或per_contribute > 0 的
  548. $needsUpdate = true;
  549. } else {
  550. // 如果 per_contribute 计算结果为0,且 contribute 和 pv 也未变,则不需要更新 per_contribute 字段
  551. unset($updateInfo['per_contribute']); // 移除无需更新的 per_contribute
  552. if (empty($updateInfo)) $needsUpdate = false; // 如果移除后为空,则不更新
  553. }
  554. if ($needsUpdate) {
  555. // 确保只包含需要更新的字段,并且包含主键 'uid'
  556. $finalUpdateData = ['uid' => $uid];
  557. if (isset($updateInfo['contribute'])) $finalUpdateData['contribute'] = $updateInfo['contribute'];
  558. if (isset($updateInfo['pv'])) $finalUpdateData['pv'] = $updateInfo['pv'];
  559. if (isset($updateInfo['per_contribute']) && $updateInfo['per_contribute'] > 0.0) { // 再次确认只更新大于0的推广业绩
  560. $finalUpdateData['per_contribute'] = $updateInfo['per_contribute'];
  561. }
  562. if (count($finalUpdateData) > 1) { // 确保除了uid还有其他字段要更新
  563. $usersToUpdate[] = $finalUpdateData;
  564. } else {
  565. $result['userUpdateSummary']['skipped']++; // 记录跳过的更新(无变化)
  566. }
  567. } else {
  568. $result['userUpdateSummary']['skipped']++; // 记录跳过的更新(无变化)
  569. }
  570. }
  571. if (!empty($usersToUpdate)) {
  572. // 使用 saveAll 进行批量更新 (要求 PHP >= 7.1.8 for MySQL)
  573. // saveAll 会根据主键 'uid' 更新对应记录
  574. $userModel = new User();
  575. $updatedCount = $userModel->saveAll($usersToUpdate); // TP6 支持 saveAll
  576. // 注意: saveAll 返回的是成功保存或更新的 *对象集合* 或 *布尔值*,不是影响的行数。
  577. // 需要检查返回类型并判断成功数量。通常返回集合时 count(集合) 是成功数。
  578. // 如果是 TP5,可能需要循环 update 或构造复杂 SQL。
  579. // 假设 TP6 saveAll 成功会返回包含更新后数据的集合或true,失败是false
  580. if ($updatedCount !== false) {
  581. // 这里需要一种方法来确定实际更新了多少条记录。
  582. // saveAll 的返回值可能需要进一步处理。
  583. // 简单假设:如果返回不是 false,就认为请求中的都成功了(可能不准确)
  584. // 更精确的方法是 saveAll 后再查询一次确认。
  585. // 这里我们暂时认为 saveAll 返回非 false 即为大部分成功
  586. $result['userUpdateSummary']['success'] = count($usersToUpdate); // 乐观估计
  587. } else {
  588. $result['userUpdateSummary']['failed'] = count($usersToUpdate); // 全部失败
  589. $result['errors'][] = "User batch update failed using saveAll.";
  590. // throw new Exception("User batch update failed");
  591. }
  592. }
  593. // 更新统计:失败数等于总数减去成功和跳过的
  594. //$result['userUpdateSummary']['failed'] = count($userList) - $result['userUpdateSummary']['success'] - $result['userUpdateSummary']['skipped'];
  595. // 如果所有操作都成功,提交事务
  596. Db::commit();
  597. } catch
  598. (\Exception $e) {
  599. // 如果任何数据库操作失败,回滚事务
  600. Db::rollback();
  601. // 记录错误信息
  602. $result['errors'][] = "Transaction failed: " . $e->getMessage();
  603. // 可以根据需要将 insert/update 摘要设置为失败
  604. $result['userUpdateSummary']['failed'] = count($usersToUpdate); // 假设更新失败
  605. $result['insertGongXianSummary']['failed'] = count($gongXianDataToInsert); // 假设插入失败
  606. $result['insertPvSummary']['failed'] = count($pvDataToInsert); // 假设插入失败
  607. }
  608. // --- 清理/最终结果赋值 (可选) ---
  609. // $result['gongXianDataOldList'] = $gongXianDataOldList; // 这些已合并到 ToInsert
  610. // $result['gongXianDataList'] = $gongXianDataList;
  611. // $result['pvDataOldList'] = $pvDataOldList;
  612. // $result['pvDataList'] = $pvDataList;
  613. return json($result);
  614. // // 计算总业绩
  615. // $totalPv = [];
  616. // foreach ($user_team_amount as $order) {
  617. // if (!isset($totalPv[$order['user_id']])) {
  618. // $totalPv[$order['user_id']] = 0;
  619. // }
  620. // switch ($order['money']) {
  621. // case 1180:
  622. // $totalPv[$order['user_id']] += 700;
  623. // break;
  624. // case 11800:
  625. // $totalPv[$order['user_id']] += 7000;
  626. // break;
  627. // case 10620:
  628. // $totalPv[$order['user_id']] += 6300;
  629. // break;
  630. // case 2360:
  631. // $totalPv[$order['user_id']] += 1400;
  632. // break;
  633. // case 9440:
  634. // $totalPv[$order['user_id']] += 5600;
  635. // break;
  636. // default:
  637. // if ($order['money'] == 2344.99) {
  638. // break;
  639. // }
  640. // $totalPv[$order['user_id']] += round($order['money'] * 0.7, 2);
  641. // break;
  642. // }
  643. // }
  644. // foreach ($totalPv as $uid => $pv) {
  645. // Db::name("user")->where('is_old', 1)->where("uid", $uid)->update(['pv' => $pv]);
  646. // }
  647. //
  648. // return;
  649. // /**
  650. // * 周金花200(13959100621)
  651. // * 黄金花200(18603401312)
  652. // * 徐立苹100(13521438652)
  653. // * 李素红200(13701061395)
  654. // * 周行体200(13231658486)
  655. // * 刘银霞100(13161909228)
  656. // * 李卫东100(13911601230)
  657. // * 金 波100(13801358713)
  658. // * 王 玲 150(15810064711)
  659. // * 侯玉兰150(15232797965)
  660. // * 吕丽娣100(15960349667)
  661. // * 张传莲100(15332507711)
  662. // * 刘春梅100(18760167163)
  663. // * 付天如100(18863873000)
  664. // * 付绍先100(15324059286)
  665. // * 黄桂林100(15960196876)
  666. // * 温 煌100(18350134089)
  667. // * 谭英先100(13978627072)
  668. // * 陈玉珍100(19858353660)
  669. // * 黄雪云100(18650055445)
  670. // * 黄 玲100(13877193411)
  671. // * 邱佳珍100(13960379098)
  672. // * 隋彩云100(13601071082)
  673. // */
  674. // $data = [
  675. // '13959100621' => 200,
  676. // '18603401312' => 200,
  677. // '13521438652' => 100,
  678. // '13701061395' => 200,
  679. // '13231658486' => 200,
  680. // '13161909228' => 100,
  681. // '13911601230' => 100,
  682. // '13801358713' => 100,
  683. // '15810064711' => 150,
  684. // '15232797965' => 150,
  685. // '15960349667' => 100,
  686. // '15332507711' => 100,
  687. // '18760167163' => 100,
  688. // '18863873000' => 100,
  689. // '15324059286' => 100,
  690. // '15960196876' => 100,
  691. // '18350134089' => 100,
  692. // '13978627072' => 100,
  693. // '19858353660' => 100,
  694. // '18650055445' => 100,
  695. // '13877193411' => 100,
  696. // '13960379098' => 100,
  697. // '13601071082' => 100,
  698. // ];
  699. // $result = Db::name("user")
  700. // ->whereIn("account", array_keys($data))
  701. // ->where('is_del', 0)
  702. // ->field('uid, brokerage_price, account, nickname')
  703. // ->select();
  704. // $res = [];
  705. // foreach ($result as $v) {
  706. // Db::name("user")->where("uid", $v['uid'])->inc("brokerage_price", $data[$v['account']])->save();
  707. // $this->bill_user_log111111($v['uid'], "佣金补贴", $data[$v['account']], "佣金补贴", 8);//股权分佣明细2.0系统
  708. // $res[] = [
  709. // 'uid' => $v['uid'],
  710. // 'account' => $v['account'],
  711. // 'brokerage_price_pre' => $v['brokerage_price'],
  712. // 'brokerage_price' => $data[$v['account']],
  713. // 'nickname' => $v['nickname']
  714. // ];
  715. // }
  716. // return json($res);
  717. }
  718. public function lst6(Request $request)
  719. {
  720. $startTime = $request->param('startTime', null);
  721. $endTime = $request->param('endTime', null);
  722. $isTest = $request->param('isTest', 1);
  723. $maxAllocationAmount = $request->param('maxAllocationAmount', null);
  724. // 查询测试人员账号信息
  725. //18973958920
  726. //15186694339
  727. //18833633835
  728. //19525470098
  729. //17756507016
  730. //18226705337
  731. $testAccount = ['18973958920', '15186694339', '18833633835', '19525470098', '17756507016', '18226705337', '17855366921'];
  732. $removeUserList = Db::name("user")
  733. ->whereIn('account', $testAccount)
  734. ->where("status", "<>", 0)
  735. ->where('is_del', '<>', 1)
  736. ->select()
  737. ->toArray();
  738. $removeUserIdList = array_column($removeUserList, 'uid');
  739. $zeroLineUidList = [1420, 2328];
  740. foreach ($zeroLineUidList as $uid) {
  741. //查询当前团队的用户列表
  742. $sql = "select getUserLevelId($uid) as uids";
  743. //\think\facade\Log::info("lplpDB".$sql);
  744. try {
  745. $spread_user_list = Db::query($sql);
  746. } catch (\Exception $exception) {
  747. return json($exception->getMessage());
  748. }
  749. $removeUserIdList = array_merge(explode(",", trim($spread_user_list[0]['uids'], '$,')), $removeUserIdList);
  750. }
  751. if (empty($startTime) || empty($endTime)) {
  752. $timeMap = $this->getTime('last_week');
  753. $startTime = $timeMap['startTime'] . ' 00:00:00';
  754. $endTime = $timeMap['endTime'] . ' 23:59:59';
  755. }
  756. // 定义分红金额
  757. $weekRecord = $this->getTeamPvByTime($startTime, $endTime, $removeUserIdList);
  758. // 分红金额 - 红包金额
  759. $redEnvelope = $this->getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList);
  760. $weekRecord -= $redEnvelope;
  761. if (!is_null($maxAllocationAmount)) {
  762. $weekRecord = $maxAllocationAmount;
  763. }
  764. if ($weekRecord <= 0) {
  765. return json('无效分红金额');
  766. }
  767. // $stock_comm = round($weekRecord * 0.05 ,2);
  768. $stock_comm = floor(($weekRecord * 0.05) * 100) / 100;
  769. //获取股权分红的人
  770. $GetAllUserPackNum = db::name("old_user_stock")->sum("pack");//计算所有用户的份数总和
  771. $stockuserdatas = db::name("old_user_stock")->select();//获取所有用户
  772. $salesArr = [];
  773. if (count($stockuserdatas) > 0) {
  774. // $one_pack_comm_data = round($stock_comm / $GetAllUserPackNum,2);//获取每份的单佣金
  775. $one_pack_comm_data = floor(($stock_comm / $GetAllUserPackNum) * 100) / 100;
  776. foreach ($stockuserdatas as &$v) {
  777. $salesArr[] = [
  778. 'stock_comm' => $stock_comm,
  779. 'GetAllUserPackNum' => $GetAllUserPackNum,
  780. 'uid' => $v['user_id'],
  781. 'pack' => $v['pack'],
  782. 'one_pack_comm_data' => $one_pack_comm_data,
  783. 'getcomm' => floor(($v['pack'] * $one_pack_comm_data) * 100) / 100
  784. ];
  785. $v['getcomm'] = floor(($v['pack'] * $one_pack_comm_data) * 100) / 100;
  786. $v['allgetcomm'] = $v['allgetcomm'] + $v['getcomm'];
  787. $v['update_time'] = time();
  788. $yj_90 = $v['getcomm'] * 0.9;
  789. $fg_10 = $v['getcomm'] * 0.1;
  790. if ($isTest == 0) {
  791. // // 更新 user_stock 表中的记录
  792. $res = db::name("old_user_stock")->where("id", $v['id'])->update([
  793. 'getcomm' => $v['getcomm'],
  794. 'allgetcomm' => $v['allgetcomm'],
  795. 'update_time' => $v['update_time']
  796. ]);
  797. Db::name("user")->where("uid", $v['user_id'])->inc("brokerage_price", $yj_90)->save();
  798. $res1 = Db::name("user")->where("uid", $v['user_id'])->inc("total_amount_old", $yj_90)->save();
  799. $res2 = Db::name("user")->where("uid", $v['user_id'])->inc("amount_old", $yj_90)->save();
  800. $res3 = Db::name("user")->where("uid", $v['user_id'])->inc("fugou", $fg_10)->save();
  801. $res = true;
  802. if ($res) {
  803. $das = $this->change_user_log($v['user_id'], 'amount_old', 1, $yj_90, "member_award_stock", "股权分红入账" . "-" . $v['pack']);//释放佣金
  804. //入账星级分红记录表
  805. $week_data = [];
  806. $week_data['user_id'] = $v['user_id'];
  807. $week_data['user_star_id'] = 0;
  808. $week_data['award_num'] = 0;
  809. $week_data['award_num_sm'] = 0;
  810. $week_data['remark'] = "股权分红主动执行" . $yj_90;
  811. $week_data['week_record'] = $weekRecord;
  812. $week_data['all_amount'] = $stock_comm;
  813. $week_data['radio'] = 2;
  814. $week_data['owner_bill'] = 0; //总流水金额
  815. $week_data['my_bill'] = 0; //我的流水
  816. $week_data['create_time'] = time();
  817. Db::name("old_user_weekaward")->insert($week_data);
  818. $userdatassss = db::name("old_user_jbp")->where("id", $v['user_id'])->find();
  819. $datas_log = [
  820. 'note' => "名称:" . $userdatassss['nick_name'] . " 用户:" . $v['user_id'] . " 新增股权佣金:" . $yj_90,
  821. 'create_time' => date('Y-m-d H:i:s')
  822. ];
  823. $con_data = Db::name('test_log')->insert($datas_log); //数据存到2.0数据表中
  824. $this->bill_user_log111111($v['user_id'], "股权分佣", $yj_90, "股权明细入账", 13);//股权分佣明细2.0系统
  825. $this->fugou_user_log($v['user_id'], $fg_10, 16, 1, 1);//复购金明细入账
  826. }
  827. }
  828. }
  829. }
  830. return json($salesArr);
  831. //股权分红 end
  832. }
  833. //添加复购金
  834. public function fugou_user_log($uid, $num, $order_id, $type_inc_des, $status = -1)
  835. {
  836. $fugou_data = Db::name('user')->where("uid", $uid)->find();
  837. $bill = [
  838. 'uid' => $uid,
  839. 'number' => $num,//数量
  840. 'status' => $status,//复购金状态1-有效 0-失效 -1待确认
  841. 'mark' => "复购金",//备注
  842. 'desc' => "10%作为复购金",//描述
  843. 'order_id' => $order_id,//订单id
  844. 'surplus' => 0,//剩余
  845. 'order_type' => 11,//关联订单ID
  846. 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗
  847. 'settlement_time' => date('Y-m-d H:i:s'),//添加时间,
  848. 'addtime' => time(),
  849. ];
  850. Db::name('user_sign_fugou')->insert($bill);
  851. }
  852. //添加平台分佣2.0系统收益明细日志(养老金金额)
  853. public function bill_user_log111111($uid, $name, $num, $mark, $comm)
  854. {
  855. $con_data = Db::name('user')->where("uid", $uid)->find();
  856. //如果是会员专区就即刻到账
  857. // Db::name('user')->where("uid",$con_data['uid'])->update(['brokerage_price'=> $con_data['brokerage_price'] + $num]);
  858. $bill = [
  859. 'uid' => $uid,
  860. 'link_id' => 0,//关联订单id
  861. 'pm' => 1,//0:支出,1:获得
  862. 'title' => $name,//账单标题
  863. 'category' => 'now_money',//明细种类
  864. 'type' => 'commission',//明细类型
  865. 'number' => $num,//明细数字
  866. 'balance' => 0,//剩余
  867. 'mark' => $mark,//备注
  868. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  869. 'status' => 1,//0待确定,1有效,-1无效
  870. 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  871. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  872. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  873. 'order_sn' => 0,//订单号
  874. 'tripartite' => 0,//
  875. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  876. 'mer_id' => 0,//商户id
  877. 'source' => 0,//1线下 0线上
  878. 'order_type' => 1,//1自营 2 第三方 订单类型
  879. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  880. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  881. 'take_time' => '',//结算时间
  882. 'district_id' => '',//
  883. 'street_id' => '',//
  884. 'month' => '',//月份
  885. ];
  886. Db::name('user_bill')->insert($bill);
  887. }
  888. //发放佣金 示例: $this->change_user_log($v,'amount',1,$all_award,"member_award","星级分红入账"."-".$value['title']);//释放佣金
  889. function change_user_log($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0)
  890. {
  891. $transition = [
  892. 'pv' => 1,
  893. "gb" => 2,
  894. "df" => 3,
  895. "vr" => 4,
  896. "bt" => 5,
  897. "withdraw_quota_old" => 6,//提现
  898. "unsettled_pension" => 7,
  899. "amount_old" => 8//发放佣金
  900. ];
  901. $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find();
  902. if (empty($routineInfo)) {
  903. return false;
  904. }
  905. $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0;
  906. if ($appoint_money > 0) {
  907. $alter_money = $appoint_money;
  908. } else {
  909. //再去查询变更前的 金额
  910. $alter_money = Db::name("user")->where('uid', $user_id)->value($type);
  911. }
  912. if ($change_status == 1) {
  913. //新增前
  914. $alter_money = $alter_money - $money;
  915. } else if ($change_status == 2) {
  916. //减少前
  917. $alter_money = $alter_money + $money;
  918. }
  919. if ($alter_money < 0) {
  920. $alter_money = 0;
  921. }
  922. $inser_data = [];
  923. $inser_data['user_id'] = $user_id;
  924. $inser_data['type'] = $transition[$type];
  925. $inser_data['change_status'] = $change_status;
  926. $inser_data['money'] = $money;
  927. $inser_data['routine_log_of_key_id'] = $key_id;
  928. $inser_data['remark'] = $remark;
  929. $inser_data['is_admin'] = $is_admin;
  930. $inser_data['create_time'] = time();
  931. $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0;
  932. Db::name("old_user_log")->insert($inser_data);
  933. }
  934. public function getTime($timePeriod)
  935. {
  936. // 定义时间区间
  937. $now = time();
  938. $startTime = '';
  939. $endTime = '';
  940. if ($timePeriod == 'last_week') {
  941. // 上周的时间区间
  942. $startTime = date('Y-m-d', strtotime('monday last week', $now)); // 上周星期一的开始时间
  943. $endTime = date('Y-m-d', strtotime('sunday last week', $now)); // 上周星期日的结束时间
  944. } elseif ($timePeriod == 'this_week') {
  945. // 这周的时间区间
  946. $startTime = date('Y-m-d', strtotime('monday this week', $now)); // 这周星期一的开始时间
  947. $endTime = date('Y-m-d', strtotime('sunday this week', $now)); // 这周星期日的结束时间
  948. }
  949. // return ['startTime' => '2025-03-10', 'endTime' => '2025-03-23'];
  950. return ['startTime' => $startTime, 'endTime' => $endTime];
  951. }
  952. public function getTeamPvByTime($startTime, $endTime, $removeUserIdList = [])
  953. {
  954. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  955. $orders = Db::name("store_order")
  956. ->where('mer_id', 496)
  957. ->whereNotIn('uid', $removeUserIdList)
  958. ->whereIn('status', [0, 1, 2, 3])
  959. ->whereIn("total_price", [1180, 11800, 10620, 2360, 9440])
  960. ->whereBetween("pay_time", [$startTime, $endTime])
  961. ->whereNotNull("pay_time") // 排除 pay_time 为 NULL 的订单
  962. ->select();
  963. // 计算总业绩
  964. $totalPv = 0;
  965. foreach ($orders as $order) {
  966. switch ($order['total_price']) {
  967. case 1180:
  968. $totalPv += 700;
  969. break;
  970. case 11800:
  971. $totalPv += 7000;
  972. break;
  973. case 10620:
  974. $totalPv += 6300;
  975. break;
  976. case 2360:
  977. $totalPv += 1400;
  978. break;
  979. case 9440:
  980. $totalPv += 5600;
  981. break;
  982. }
  983. }
  984. return $totalPv;
  985. }
  986. public function getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList = [])
  987. {
  988. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  989. $redEnvelopeList = Db::name("user_sign_hongbao")
  990. ->whereNotIn('uid', $removeUserIdList)
  991. ->where('status', 1)
  992. ->whereBetween("settlement_time", [$startTime, $endTime])
  993. ->select()
  994. ->toArray();
  995. return floor(array_sum(array_column($redEnvelopeList, 'number')) * 100) / 100;
  996. }
  997. }