Taskorderguquan.php 51 KB

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