TaskPartnerProfits.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. namespace app\controller\api\store\merchant;
  3. use think\facade\Db;
  4. class TaskPartnerProfits
  5. {
  6. public function getTeamPvByTime($timePeriod = 'last_week')
  7. {
  8. // 定义时间区间
  9. $startTime = '';
  10. $endTime = '';
  11. if ($timePeriod == 'last_week') {
  12. // 上周的时间区间
  13. $startTime = '2025-03-17 00:00:00'; // 上周星期一的开始时间
  14. $endTime = '2025-03-23 23:59:59'; // 上周星期日的结束时间
  15. } elseif ($timePeriod == 'this_week') {
  16. // 这周的时间区间
  17. $startTime = '2025-02-17 00:00:00'; // 这周星期一的开始时间
  18. $endTime = '2025-02-23 23:59:59'; // 这周星期日的结束时间
  19. } else {
  20. return "Invalid time period specified.";
  21. }
  22. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  23. $orders = Db::name("store_order")
  24. ->whereIn('status', [0, 1, 2, 3])
  25. ->whereIn("total_price", [1180, 11800, 10620, 2360, 9440])
  26. ->whereBetween("pay_time", [$startTime, $endTime])
  27. ->whereNotNull("pay_time") // 排除 pay_time 为 NULL 的订单
  28. ->select();
  29. // 计算总业绩
  30. $totalPv = 0;
  31. foreach ($orders as $order) {
  32. switch ($order['total_price']) {
  33. case 1180:
  34. $totalPv += 700;
  35. break;
  36. case 11800:
  37. $totalPv += 7000;
  38. break;
  39. case 10620:
  40. $totalPv += 6300;
  41. break;
  42. case 2360:
  43. $totalPv += 1400;
  44. break;
  45. case 9440:
  46. $totalPv += 5600;
  47. break;
  48. }
  49. }
  50. return $totalPv;
  51. }
  52. public function partnerProfits()
  53. {
  54. // $a = $this->getSpreadUserUidAccountListByUidList(1);
  55. // return json($a);
  56. // 定义分红金额
  57. $weekRecord = $this->getTeamPvByTime('last_week');
  58. // 1、获取用户星级信息
  59. //$star_list = Db::name("user_star")->order("statement_all_num desc")->select()->toArray();//旧星级等级
  60. $starList = Db::name("old_user_starlevel")
  61. ->whereIn('title', ['初级', '中级', '高级'])
  62. ->order("statement_all_num desc")
  63. ->select()
  64. ->toArray();//星级等级
  65. // 2、循环星级信息 并分组归类
  66. $salesArr = array_column($starList, null, 'title');
  67. // 3、获取 分红用户 信息 (`user_group` tinyint(1) DEFAULT '1' COMMENT '1消费者 2,推广业务员 3,业务经理 4初级合伙人5中级合伙人 6高级合伙人 7.懂事',)
  68. $list = Db::name("user")
  69. ->where("user_group", "IN", [4, 5, 6])
  70. ->where("status", "<>", 0)
  71. ->select()
  72. ->toArray();
  73. // 4、将用户装载到 星级等级 分红数据中
  74. foreach ($list as $key => $value) {
  75. if ($value['user_group'] == 4) {
  76. $salesArr['初级']['divider_people'][] = $value['uid'];
  77. } else if ($value['user_group'] == 5) {
  78. $salesArr['中级']['divider_people'][] = $value['uid'];
  79. } else if ($value['user_group'] == 6) {
  80. $salesArr['高级']['divider_people'][] = $value['uid'];
  81. }
  82. }
  83. // 5、移除特定人员
  84. // 要移动的ID数组
  85. $ids_to_remove_from_junior = array(12, 69, 144); // 从初级中移除的ID
  86. $ids_to_remove_from_intermediate = array(49); // 从中级中移除的ID
  87. // 查找初级并获取其divider_people
  88. $junior = &$salesArr['初级']['divider_people']; // 使用引用,以便直接修改数组
  89. // 查找中级并获取其divider_people
  90. $intermediate = &$salesArr['中级']['divider_people']; // 使用引用,以便直接修改数组
  91. // 从初级的divider_people中移除特定的ID
  92. foreach ($ids_to_remove_from_junior as $id) {
  93. $key = array_search($id, $junior);
  94. if ($key !== false) {
  95. unset($junior[$key]); // 移除初级中的特定ID
  96. }
  97. }
  98. // 从中级的divider_people中移除特定的ID
  99. foreach ($ids_to_remove_from_intermediate as $id) {
  100. $key = array_search($id, $intermediate);
  101. if ($key !== false) {
  102. unset($intermediate[$key]); // 移除中级中的特定ID
  103. }
  104. }
  105. // 重置数组的索引,因为unset会留下空位
  106. $salesArr['初级']['divider_people'] = array_values($junior);
  107. $salesArr['中级']['divider_people'] = array_values($intermediate);
  108. // 6、执行分开发放
  109. $partnerProfitsPvListByUid = [];
  110. foreach ($salesArr as $key => $value) {
  111. // 6.1 计算可分红金额
  112. //比如这个周的总业绩是 8400
  113. //周总业绩(8400) * 合伙人级别的分佣比例(15%) = 可分红的佣金(1260)
  114. // $salesArr[$key]['fs_money_nums'] = round($weekRecord * $value['profit_rate'], 2);
  115. $salesArr[$key]['fs_money_nums'] = floor(($weekRecord * ($value['profit_rate'] / 100)) * 100) / 100;
  116. //可分红的业绩(分为两半,各50%,一部分是分给双联盟比例,一部分是小业绩比例)
  117. //把本周的业绩(8400) * 合伙人级别的分佣比例(15%) * 0.5 = 630(可分红佣金的 50%)
  118. // $salesArr[$key]['fs_money'] = round($salesArr[$key]['fs_money_nums'] / 2, 2);
  119. $salesArr[$key]['fs_money'] = floor(($salesArr[$key]['fs_money_nums'] / 2) * 100) / 100;
  120. // 6.2 计算所有人的 PV值
  121. $salesArr[$key]['maxValueAll'] = 0;
  122. $salesArr[$key]['sumOfOthersAll'] = 0;
  123. $salesArr[$key]['dividerPeoplePv'] = [];
  124. foreach ($value['divider_people'] as $k => $v) {
  125. // 6.2.1 查出分红人的所有部门 即 下级用户
  126. $spreadUserUidList = $this->getSpreadUserUidListByUidList([$v]);
  127. if (empty($spreadUserUidList[$v])) {
  128. continue;
  129. }
  130. // 6.2.2 获取当前分红人的所有部门 内的下级成员,递归获取至最低一级
  131. $subordinatesBatch = $this->getSubordinatesBatch($spreadUserUidList[$v]);
  132. // 6.2.3 获取所有部门(下线)的PV值
  133. foreach ($subordinatesBatch as $k1 => $v1) {
  134. // 获取当前下线的PV值
  135. $pv = $this->getPv(array_merge($v1, [$k1]));
  136. if (!empty($pv)) {
  137. $partnerProfitsPvListByUid[$v][$k1] = $pv;
  138. }
  139. }
  140. // 6.2.4 将PV值进行记录 以便后续计算
  141. if (!empty($partnerProfitsPvListByUid[$v])) {
  142. // 获取数组中的最大值
  143. $maxValue = max($partnerProfitsPvListByUid[$v]);
  144. // 计算总和
  145. $totalSum = array_sum($partnerProfitsPvListByUid[$v]);
  146. // 计算除最大值外其余值的和
  147. $sumOfOthers = $totalSum - $maxValue;
  148. $salesArr[$key]['dividerPeoplePv'][$v]['maxValue'] = $maxValue;
  149. $salesArr[$key]['dividerPeoplePv'][$v]['sumOfOthers'] = $sumOfOthers;
  150. $salesArr[$key]['maxValueAll'] += $maxValue;
  151. $salesArr[$key]['sumOfOthersAll'] += $sumOfOthers;
  152. }
  153. }
  154. }
  155. // 7、执行分红
  156. $testRecord = [];
  157. foreach ($salesArr as $key => $value) {
  158. $amount = $value['fs_money'];//可分红的总佣金,分红佣金的一半,中级合伙61.95
  159. $fsMoneyNums = $value['fs_money_nums'];//中级合伙人级别,可分红的总佣金
  160. $maxValueAll = $value['maxValueAll'];//当前级别合伙人下面所有的双联盟业绩
  161. $sumOfOthersAll = $value['sumOfOthersAll'];//当前级别合伙人下面所有的小区(业绩小的部分)总业绩
  162. if (count($value['divider_people']) <= 0) {
  163. continue;
  164. }
  165. //每人分红金额
  166. foreach ($value['divider_people'] as $v) {
  167. //个人小区(佣金少的一部分) 权重 计算权重
  168. //返回是数组,下标0是最小的值(业绩小的),下标1是最大的值(业绩大的)
  169. $userPvSmallNum = $value['dividerPeoplePv'][$v]['sumOfOthers'] ?? 0;
  170. // $myMixMoney = round($amount * ($userPvSmallNum / $sumOfOthersAll), 2);
  171. $myMixMoney = floor(($amount * (floor(($userPvSmallNum / $sumOfOthersAll) * 100) / 100)) * 100) / 100;
  172. //个人双区总佣金 权重 计算
  173. $myBill = $value['dividerPeoplePv'][$v]['maxValue'] ?? 0;
  174. //中级合伙人。userid=17可以分到双区可分红的金额
  175. //可分红的佣金 * (中级合伙人个人的双区总业绩 / 当前等级所有合伙人的双联盟业绩) = 当前用户可以获取到的分红佣金(双区联盟总业绩加权佣金),287742 * (61.95/431032)=41.36
  176. // $myWeightMoney = round($amount * ($myBill / $maxValueAll), 2);
  177. $myWeightMoney = floor(($amount * (floor((($myBill + $userPvSmallNum) / ($maxValueAll + $sumOfOthersAll)) * 100) / 100)) * 100) / 100;
  178. $salesArr[$key]['dividerPeoplePv'][$v]['myWeightMoney'] = $myWeightMoney;
  179. $salesArr[$key]['dividerPeoplePv'][$v]['myMixMoney'] = $myMixMoney;
  180. $salesArr[$key]['dividerPeoplePv'][$v]['maxValueAll'] = $maxValueAll;
  181. $salesArr[$key]['dividerPeoplePv'][$v]['sumOfOthersAll'] = $sumOfOthersAll;
  182. $salesArr[$key]['dividerPeoplePv'][$v]['weekRecord'] = $weekRecord;
  183. # code...
  184. $allAward = $myWeightMoney + $myMixMoney;
  185. //90%给佣金,10给复购金
  186. $yj_90 = $allAward * 0.9;
  187. $fg_10 = $allAward * 0.1;
  188. // $userdatassss = db::name("old_user_jbp")->where("id", $v)->find();
  189. // $datas_log = [
  190. // 'note' => "名称:" . $userdatassss['nick_name'] . " 用户:" . $v . " 双联盟总业绩:" . $maxValueAll . " 我的双区业绩:" . $myBill . " 小区总业绩:" . $sumOfOthersAll . " 我的业绩小的:" . $userPvSmallNum[0] . " 可分红佣金:" . $amount . " 可分红总佣金:" . $fsMoneyNums . " 小区业绩可分红:" . $myMixMoney . " 大区业绩可分红:" . $myWeightMoney . " 实际分得:" . $yj_90,
  191. // 'create_time' => date('Y-m-d H:i:s')
  192. // ];
  193. // $con_data = Db::name('test_log')->insert($datas_log); //数据存到2.0数据表中
  194. //
  195. // # code...
  196. // $res = Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save();
  197. //
  198. // Db::name("user")->where("uid", $v)->inc("amount_old", $yj_90)->save();//不用冻结金额和提现额度来限制佣金发放
  199. // Db::name("user")->where("uid", $v)->inc("brokerage_price", $yj_90)->save();
  200. //
  201. // Db::name("user")->where("uid", $v)->inc("fugou", $fg_10)->save();
  202. //
  203. //
  204. // //if($res) {
  205. // $this->change_user_log($v, 'amount_old', 1, $yj_90, "member_award", "星级分红入账" . "-" . $value['title']);//释放佣金
  206. // //入账星级分红记录表
  207. // $week_data = [];
  208. // $week_data['user_id'] = $v;
  209. // $week_data['user_star_id'] = $key;
  210. // $week_data['award_num'] = $myWeightMoney;
  211. // $week_data['award_num_sm'] = $myMixMoney;
  212. // $week_data['remark'] = "星级分红主动执行:" . $yj_90;
  213. // $week_data['week_record'] = $weekRecord;
  214. // $week_data['all_amount'] = $fsMoneyNums;
  215. // $week_data['radio'] = 2;
  216. // $week_data['owner_bill'] = $maxValueAll; //总流水金额
  217. // $week_data['my_bill'] = $myBill; //我的流水
  218. // $week_data['create_time'] = time();
  219. //
  220. // Db::name("old_user_weekaward")->insert($week_data);
  221. //
  222. // $this->bill_user_log111111($v, "合伙人分佣", $yj_90, "合伙人分佣明细入账", 11);//联创分佣明细2.0系统
  223. // $this->fugou_user_log($v, $fg_10, 16, 1, 1);//复购金明细入账
  224. }
  225. }
  226. return json($salesArr);
  227. }
  228. /**
  229. * 获取该会员的所有下级会员
  230. */
  231. public function getSpreadUserUidListByUidList(array $uidList)
  232. {
  233. $son_ids = Db::name("user")
  234. ->whereIn("spread_uid", $uidList)
  235. ->field("uid,spread_uid")
  236. ->select()
  237. ->toArray();
  238. $new_array_ids = [];
  239. foreach ($son_ids as $key => $value) {
  240. $new_array_ids[$value['spread_uid']][] = $value['uid'];
  241. }
  242. return $new_array_ids;
  243. }
  244. /**
  245. * 递归查询下级用户并分组
  246. *
  247. * @param array $uids 初始用户ID数组
  248. * @return array 分组后的下级用户ID数组
  249. */
  250. public function getSubordinates(array $uids): array
  251. {
  252. $result = [];
  253. foreach ($uids as $uid) {
  254. $result[$uid] = $this->getSubordinatesRecursively($uid);
  255. }
  256. return $result;
  257. }
  258. /**
  259. * 获取该会员的所有下级会员
  260. */
  261. public function getSpreadUserUidAccountListByUidList($uid, $account = '')
  262. {
  263. $son_ids = Db::name("user")
  264. ->where("spread_uid", $uid)
  265. ->field("uid,spread_uid,account")
  266. ->select()
  267. ->toArray();
  268. $new_array_ids = [];
  269. foreach ($son_ids as $key => $value) {
  270. $childList = $this->getSpreadUserUidAccountListByUidList($value['uid'], $value['account']);
  271. $new_array_ids[] = [
  272. '当前用户ID' => $value['uid'],
  273. '当前用户的上级用户ID' => $value['spread_uid'],
  274. '用户账号(手机号)' => $value['account'],
  275. '当前用户的下级用户列表' => $childList
  276. ];
  277. }
  278. return $new_array_ids;
  279. }
  280. /**
  281. * 递归查询单个用户的下级用户
  282. *
  283. * @param int $uid 用户ID
  284. * @param array $subordinates 用于存储下级用户ID的数组(内部使用, 递归传递)
  285. * @return array 下级用户ID数组
  286. */
  287. private function getSubordinatesRecursively(int $uid, array &$subordinates = []): array
  288. {
  289. $subUidList = Db::name("user")
  290. ->where("spread_uid", $uid) // 直接使用 $uid, 不需要 whereIn
  291. ->field("uid") //只需要查询uid
  292. ->select()
  293. ->toArray();
  294. if (!empty($subUidList)) {
  295. foreach ($subUidList as $subUser) {
  296. $subUid = $subUser['uid'];
  297. $subordinates[] = $subUid; // 添加到下级用户数组
  298. $this->getSubordinatesRecursively($subUid, $subordinates); // 递归查询下级的下级
  299. }
  300. }
  301. return $subordinates;
  302. }
  303. /**
  304. * 递归获取所有下级ID(不分组)
  305. * @param int $uid
  306. * @param array $allSubordinates
  307. * @return array
  308. */
  309. public function getAllSubordinates(int $uid, array &$allSubordinates = []): array
  310. {
  311. $subUidList = Db::name("user")
  312. ->where("spread_uid", $uid)
  313. ->column("uid"); // 使用 column 方法直接获取 uid 数组
  314. if (!empty($subUidList)) {
  315. foreach ($subUidList as $subUid) {
  316. $allSubordinates[] = $subUid;
  317. $this->getAllSubordinates($subUid, $allSubordinates);
  318. }
  319. }
  320. return $allSubordinates;
  321. }
  322. /**
  323. * 更高效的获取所有下级 (使用循环代替递归, 避免栈溢出)
  324. * @param array $uids
  325. * @return array
  326. */
  327. public function getAllSubordinatesIterative(array $uids): array
  328. {
  329. $result = [];
  330. foreach ($uids as $uid) {
  331. $result[$uid] = [];
  332. $queue = [$uid]; // 使用队列来存储待处理的 UID
  333. $processed = []; // 记录已经处理过的
  334. while (!empty($queue)) {
  335. $currentUid = array_shift($queue); // 从队列头部取出 UID
  336. if (in_array($currentUid, $processed)) {
  337. continue; // 已经处理过
  338. }
  339. $processed[] = $currentUid;
  340. $subUidList = Db::name("user")
  341. ->where("spread_uid", $currentUid)
  342. ->column("uid");
  343. if (!empty($subUidList)) {
  344. $result[$uid] = array_merge($result[$uid], $subUidList); //合并
  345. $queue = array_merge($queue, $subUidList); // 将新找到的下级 UID 加入队列尾部
  346. }
  347. }
  348. $result[$uid] = array_unique($result[$uid]);//去重
  349. }
  350. return $result;
  351. }
  352. /**
  353. * 获取多个用户的所有下级, 返回分组后的数组. (更高效, 使用循环, 减少数据库查询次数)
  354. * @param array $uids
  355. * @return array 形如 ['A1' => [A11, A12, ...], 'A2' => [...]]
  356. */
  357. public function getSubordinatesBatch(array $uids): array
  358. {
  359. $result = [];
  360. $allSubordinates = [];
  361. $queue = $uids;
  362. $processed = [];
  363. //1. 找出所有的下级ID (不去重)
  364. while (!empty($queue)) {
  365. $currentUids = array_splice($queue, 0, 500); // 一次最多处理500个,防止in太多. 可以根据实际情况调整.
  366. $currentUids = array_diff($currentUids, $processed); //去除已经查询过的
  367. if (empty($currentUids)) continue; //没有新的需要处理的
  368. $processed = array_merge($processed, $currentUids);//记录
  369. $subUidList = Db::name("user")
  370. ->whereIn("spread_uid", $currentUids)
  371. ->field("uid,spread_uid") // 需要查询 spread_uid 用于后续分组
  372. ->select()
  373. ->toArray();
  374. if (!empty($subUidList)) {
  375. $allSubordinates = array_merge($allSubordinates, $subUidList); //所有下级
  376. $nextLevelUids = array_column($subUidList, 'uid');
  377. $queue = array_merge($queue, $nextLevelUids); //放到队列中
  378. }
  379. }
  380. //2. 分组
  381. foreach ($uids as $uid) {
  382. $result[$uid] = []; // 初始化每个用户的下级数组
  383. }
  384. $allSubordinatesMap = [];
  385. foreach ($allSubordinates as $item) {
  386. $allSubordinatesMap[$item['spread_uid']][] = $item['uid'];
  387. }
  388. //3. 递归的将查到的下级进行分组
  389. $group = function ($spreadUid) use (&$group, &$allSubordinatesMap, &$result) {
  390. if (isset($allSubordinatesMap[$spreadUid])) {
  391. foreach ($allSubordinatesMap[$spreadUid] as $subUid) {
  392. if (isset($result[$spreadUid])) {
  393. $result[$spreadUid][] = $subUid; //直接添加到结果中
  394. } else { //不属于直接上级, 查找上级的上级
  395. foreach ($result as $rootUid => &$subordinates) {
  396. if (in_array($spreadUid, $subordinates) || $rootUid == $spreadUid) {
  397. $subordinates[] = $subUid;
  398. break;
  399. }
  400. }
  401. }
  402. $group($subUid); //递归下一层
  403. }
  404. }
  405. };
  406. foreach ($uids as $uid) {
  407. $group($uid);
  408. }
  409. // 4. 去重 (如果需要)
  410. foreach ($result as $key => $value) {
  411. $result[$key] = array_values(array_unique($value));
  412. }
  413. return $result;
  414. }
  415. // 获取用户PV值
  416. public function getPv($userIdList)
  417. {
  418. // 基础查询构建器
  419. $user_team_amount = Db::name("old_user_of_goods")
  420. ->whereIn("user_id", $userIdList)
  421. ->where('status', 1)
  422. ->where('money', 1180)
  423. ->select();
  424. //$money1180Records = $this->old_user_of_goods->where('status', 1)->where('money', 1180)->select();
  425. // 计算 status 为 1 且 money 为 1180 的记录总和(减去相应值后)
  426. $money1180Sum = 0;
  427. foreach ($user_team_amount as $record) {
  428. $money1180Sum += $record['money'] - 300 - 180;
  429. }
  430. // 获取 status 为 1 且 money 为 11800 的记录集合
  431. $money11800Records = Db::name("old_user_of_goods")
  432. ->whereIn("user_id", $userIdList)
  433. ->where('status', 1)
  434. ->where('money', 11800)
  435. ->select();
  436. // 计算 status 为 1 且 money 为 11800 的记录总和(减去相应值后)
  437. $money11800Sum = 0;
  438. foreach ($money11800Records as $record) {
  439. $money11800Sum += $record['money'] - 3000 - 1800;
  440. }
  441. $money10620Records = Db::name("old_user_of_goods")
  442. ->whereIn("user_id", $userIdList)
  443. ->where('status', 1)
  444. ->where('money', 10620)
  445. ->select();
  446. // 计算 status 为 1 且 money 为 10620 的记录总和(减去相应值后)
  447. $money10620Sum = 0;
  448. foreach ($money10620Records as $record) {
  449. $money10620Sum += $record['money'] - 2700 - 1620;
  450. }
  451. $money9440Records = Db::name("old_user_of_goods")
  452. ->whereIn("user_id", $userIdList)
  453. ->where('status', 1)
  454. ->where('money', 9440)
  455. ->select();
  456. // 计算 status 为 1 且 money 为 9440 的记录总和(减去相应值后)
  457. $money9440Sum = 0;
  458. foreach ($money9440Records as $record) {
  459. $money9440Sum += $record['money'] - 2400 - 1440;
  460. }
  461. $money2360Records = Db::name("old_user_of_goods")
  462. ->whereIn("user_id", $userIdList)
  463. ->where('status', 1)
  464. ->where('money', 2360)
  465. ->select();
  466. // 计算 status 为 1 且 money 为 2360 的记录总和(减去相应值后)
  467. $money2360Sum = 0;
  468. foreach ($money2360Records as $record) {
  469. $money2360Sum += $record['money'] - 600 - 360;
  470. }
  471. // 计算除去特定值后的总和
  472. $otherMoneyRecords = [];
  473. //$otherMoneyRecords = $this->old_user_of_goods->where('status', 1)->whereNotIn('money', [1180, 11800])->select();
  474. $otherMoneyRecords = Db::name("old_user_of_goods")
  475. ->whereIn("user_id", $userIdList)
  476. ->whereNotIn('money', [1180, 11800, 10620, 9440, 2360])
  477. ->select();
  478. $otherMoneySum = 0;
  479. foreach ($otherMoneyRecords as $record) {
  480. $otherMoneySum += $record['money'];
  481. }
  482. return $money1180Sum + $money11800Sum + $money10620Sum + $money9440Sum + $money2360Sum + (floor($otherMoneySum * 70) / 100);
  483. }
  484. //发放佣金 示例: $this->change_user_log($v,'amount',1,$all_award,"member_award","星级分红入账"."-".$value['title']);//释放佣金
  485. function change_user_log($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0)
  486. {
  487. $transition = [
  488. 'pv' => 1,
  489. "gb" => 2,
  490. "df" => 3,
  491. "vr" => 4,
  492. "bt" => 5,
  493. "withdraw_quota_old" => 6,//提现
  494. "unsettled_pension" => 7,
  495. "amount_old" => 8//发放佣金
  496. ];
  497. $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find();
  498. if (empty($routineInfo)) {
  499. return false;
  500. }
  501. $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0;
  502. if ($appoint_money > 0) {
  503. $alter_money = $appoint_money;
  504. } else {
  505. //再去查询变更前的 金额
  506. $alter_money = Db::name("user")->where('uid', $user_id)->value($type);
  507. }
  508. if ($change_status == 1) {
  509. //新增前
  510. $alter_money = $alter_money - $money;
  511. } else if ($change_status == 2) {
  512. //减少前
  513. $alter_money = $alter_money + $money;
  514. }
  515. if ($alter_money < 0) {
  516. $alter_money = 0;
  517. }
  518. $inser_data = [];
  519. $inser_data['user_id'] = $user_id;
  520. $inser_data['type'] = $transition[$type];
  521. $inser_data['change_status'] = $change_status;
  522. $inser_data['money'] = $money;
  523. $inser_data['routine_log_of_key_id'] = $key_id;
  524. $inser_data['remark'] = $remark;
  525. $inser_data['is_admin'] = $is_admin;
  526. $inser_data['create_time'] = time();
  527. $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0;
  528. Db::name("old_user_log")->insert($inser_data);
  529. }
  530. //添加平台分佣2.0系统收益明细日志(养老金金额)
  531. public function bill_user_log111111($uid, $name, $num, $mark, $comm)
  532. {
  533. $con_data = Db::name('user')->where("uid", $uid)->find();
  534. //如果是会员专区就即刻到账
  535. // Db::name('user')->where("uid",$con_data['uid'])->update(['brokerage_price'=> $con_data['brokerage_price'] + $num]);
  536. $bill = [
  537. 'uid' => $uid,
  538. 'link_id' => 0,//关联订单id
  539. 'pm' => 1,//0:支出,1:获得
  540. 'title' => $name,//账单标题
  541. 'category' => 'now_money',//明细种类
  542. 'type' => 'commission',//明细类型
  543. 'number' => $num,//明细数字
  544. 'balance' => 0,//剩余
  545. 'mark' => $mark,//备注
  546. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  547. 'status' => 1,//0待确定,1有效,-1无效
  548. 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  549. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  550. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  551. 'order_sn' => 0,//订单号
  552. 'tripartite' => 0,//
  553. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  554. 'mer_id' => 0,//商户id
  555. 'source' => 0,//1线下 0线上
  556. 'order_type' => 1,//1自营 2 第三方 订单类型
  557. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  558. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  559. 'take_time' => '',//结算时间
  560. 'district_id' => '',//
  561. 'street_id' => '',//
  562. 'month' => '',//月份
  563. ];
  564. Db::name('user_bill')->insert($bill);
  565. }
  566. //添加复购金
  567. public function fugou_user_log($uid, $num, $order_id, $type_inc_des, $status = -1)
  568. {
  569. $fugou_data = Db::name('user')->where("uid", $uid)->find();
  570. $bill = [
  571. 'uid' => $uid,
  572. 'number' => $num,//数量
  573. 'status' => $status,//复购金状态1-有效 0-失效 -1待确认
  574. 'mark' => "复购金",//备注
  575. 'desc' => "10%复购金",//描述
  576. 'order_id' => $order_id,//订单id
  577. 'surplus' => 0,//剩余
  578. 'order_type' => 11,//关联订单ID
  579. 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗
  580. 'settlement_time' => date('Y-m-d H:i:s'),//添加时间,
  581. 'addtime' => time(),
  582. ];
  583. Db::name('user_sign_fugou')->insert($bill);
  584. }
  585. }