Taskorderguquan.php 50 KB

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