Taskorderguquan.php 54 KB

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