Taskorderguquan.php 51 KB

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