Taskorderguquan.php 52 KB

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