Taskorderguquan.php 51 KB

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