Taskorderguquan.php 52 KB

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