TaskZeroLineOrderLianc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/28
  6. */
  7. namespace app\controller\api\store\merchant;
  8. use think\facade\Db;
  9. use think\Request;
  10. class TaskZeroLineOrderLianc
  11. {
  12. //添加推广佣金/养老金日志(养老金金额)
  13. public function bill_user_log($uid, $name, $num, $mark)
  14. {
  15. $con_data = Db::name('user')->where("uid", $uid)->find();
  16. //如果是会员专区就即刻到账
  17. Db::name('user')->where("uid", $con_data['uid'])->update(['brokerage_price' => $con_data['brokerage_price'] + $num]);
  18. $bill = [
  19. 'uid' => $uid,
  20. 'link_id' => 0,//关联订单id
  21. 'pm' => 1,//0:支出,1:获得
  22. 'title' => $name,//账单标题
  23. 'category' => 'now_money',//明细种类
  24. 'type' => 'commission',//明细类型
  25. 'number' => $num,//明细数字
  26. 'balance' => 0,//剩余
  27. 'mark' => $mark,//备注
  28. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  29. 'status' => 1,//0待确定,1有效,-1无效
  30. 'commission_type' => 8,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  31. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  32. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  33. 'order_sn' => 0,//订单号
  34. 'tripartite' => 0,//
  35. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  36. 'mer_id' => 0,//商户id
  37. 'source' => 0,//1线下 0线上
  38. 'order_type' => 1,//1自营 2 第三方 订单类型
  39. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  40. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  41. 'take_time' => '',//结算时间
  42. 'district_id' => '',//
  43. 'street_id' => '',//
  44. 'month' => '',//月份
  45. ];
  46. Db::name('user_bill')->insert($bill);
  47. }
  48. public function lst2(Request $request)
  49. {
  50. $startTime = $request->param('startTime', null);
  51. $endTime = $request->param('endTime', null);
  52. $isTest = $request->param('isTest', 1);
  53. $maxAllocationAmount = $request->param('maxAllocationAmount', null);
  54. // 查询测试人员账号信息
  55. //18973958920
  56. //15186694339
  57. //18833633835
  58. //19525470098
  59. //17756507016
  60. //18226705337
  61. $testAccount = ['18973958920', '15186694339', '18833633835', '19525470098', '17756507016', '18226705337', '17855366921'];
  62. $removeUserList = Db::name("user")
  63. ->whereIn('account', $testAccount)
  64. ->where("status", "<>", 0)
  65. ->where('is_del', '<>', 1)
  66. ->select()
  67. ->toArray();
  68. $removeUserIdList = array_column($removeUserList, 'uid');
  69. if (empty($startTime) || empty($endTime)) {
  70. $timeMap = $this->getTime('last_week');
  71. $startTime = $timeMap['startTime'] . ' 00:00:00';
  72. $endTime = $timeMap['endTime'] . ' 23:59:59';
  73. }
  74. $zeroLineUidList = [17];
  75. $result = [];
  76. foreach ($zeroLineUidList as $uid) {
  77. //查询当前团队的用户列表
  78. $sql = "select getUserLevelId($uid) as uids";
  79. //\think\facade\Log::info("lplpDB".$sql);
  80. try {
  81. $spread_user_list = Db::query($sql);
  82. } catch (\Exception $exception) {
  83. $result[$uid] = $exception->getMessage();
  84. continue;
  85. }
  86. $spread_user_list = array_diff(explode(",", trim($spread_user_list[0]['uids'], '$,')), $removeUserIdList);
  87. if (empty($spread_user_list)) {
  88. $result[$uid] = '无团队成员1';
  89. continue;
  90. }
  91. // 定义分红金额
  92. $performance = $this->getTeamPvByTime($startTime, $endTime, $spread_user_list);
  93. // 分红金额 - 红包金额
  94. // $redEnvelope = $this->getRedEnvelopeByTime($startTime, $endTime, $spread_user_list);
  95. $redEnvelope = 0.00;
  96. $weekRecord = bcsub($performance, $redEnvelope, 2);
  97. if (!is_null($maxAllocationAmount)) {
  98. $weekRecord = $maxAllocationAmount;
  99. }
  100. if ($weekRecord <= 0) {
  101. $result[$uid] = '无效分红金额';
  102. continue;
  103. }
  104. $originator_award = round(($weekRecord * 0.05), 2);
  105. $lianc_list = Db::name("old_user_lianc")->order("id desc")->field("id,number,title")->select()->toArray();
  106. $lianc_list_arr = array_column($lianc_list, null, 'id');
  107. $list = Db::name("user")
  108. ->whereIn('uid', $spread_user_list)
  109. ->where("originator_id_old", ">", 0)
  110. ->where("status", 1)
  111. ->field("uid,originator_id_old")
  112. ->select()
  113. ->toArray();
  114. $user_arr = [];
  115. foreach ($list as $key => $value) {
  116. # code...
  117. $user_arr[$value['originator_id_old']]['people'][] = $value['uid'];
  118. $user_arr[$value['originator_id_old']]['money'] = $lianc_list_arr[$value['originator_id_old']]['number'];
  119. $user_arr[$value['originator_id_old']]['title'] = $lianc_list_arr[$value['originator_id_old']]['title'];
  120. $user_arr[$value['originator_id_old']]['id'] = $lianc_list_arr[$value['originator_id_old']]['id'];
  121. }
  122. //处理计算权重 和平均分配的金额
  123. $total_num = 0;
  124. $count_num = 0;
  125. foreach ($user_arr as $key => $value) {
  126. $user_arr[$key]['people_count'] = count($user_arr[$key]['people']);
  127. # code...
  128. // floor(($weekRecord * ($value['profit_rate'] / 100)) * 100) / 100;
  129. // $total_num += round($value['money'] * count($value['people']) / 1000, 2);
  130. $total_num += floor(($value['money'] * count($value['people'])) * 100) / 100;
  131. if (isset($user_arr[$key]['total_num'])) {
  132. $user_arr[$key]['total_num'] += floor(($value['money'] * count($value['people'])) * 100) / 100;
  133. } else {
  134. $user_arr[$key]['total_num'] = 0;
  135. }
  136. $count_num += count($value['people']);
  137. }
  138. foreach ($user_arr as $key => $value) {
  139. # code...
  140. // $rate = round((count($value['people']) * $value['money']) / $total_num, 2) / 1000;
  141. $rate = floor(($value['money'] * count($value['people']) / $total_num) * 100) / 100;
  142. $user_arr[$key]['rate'] = $rate;
  143. $distribution_money = floor($originator_award * $rate * 100) / 100;
  144. $user_arr[$key]['distribution_money'] = $distribution_money;
  145. // $user_arr[$key]['people_award'] = round($distribution_money / count($value['people']), 2);//可分配佣金
  146. $user_arr[$key]['people_award'] = floor($distribution_money / count($value['people']) * 100) / 100;//可分配佣金
  147. }
  148. $count = 0;
  149. foreach ($user_arr as $key => $value) {
  150. $distribution_money = $value['distribution_money'];
  151. # code...
  152. foreach ($value['people'] as $k => $v) {
  153. # code...
  154. $award = $value['people_award'];
  155. //90给佣金,10给复购金
  156. $yj_90 = $award * 0.9;
  157. $fg_10 = $award * 0.1;
  158. if ($isTest == 0) {
  159. Db::name("user")->where("uid", $v)->inc("brokerage_price", $yj_90)->save();
  160. $res = Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save();
  161. Db::name("user")->where("uid", $v)->inc("amount_old", $yj_90)->save();//不用冻结金额和提现额度来限制佣金发放
  162. $res3 = Db::name("user")->where("uid", $v)->inc("fugou", $fg_10)->save();
  163. $res = true;
  164. if ($res) {
  165. $$count = $count + 1;
  166. $this->change_user_log($v, 'amount_old', 1, $yj_90, "relation_profits", "联创分红入账" . "-" . $value['title']);
  167. //入账
  168. $week_data = [];
  169. $week_data['user_id'] = $v;
  170. $week_data['user_lianc_id'] = $value['id'];
  171. $week_data['money'] = $yj_90;
  172. $week_data['remark'] = "联创分红入账主动执行" . $yj_90;
  173. $week_data['all_record'] = $originator_award;
  174. $week_data['all_amount'] = $distribution_money;
  175. $week_data['create_time'] = time();
  176. Db::name("old_user_originator")->insert($week_data);
  177. $userdatassss = db::name("user")->where("uid", $v)->find();
  178. $datas_log = [
  179. 'note' => "名称:" . $userdatassss['nickname'] . " 用户:" . $v . " 新增联创佣金:" . $yj_90,
  180. 'create_time' => date('Y-m-d H:i:s')
  181. ];
  182. Db::name('test_log')->insert($datas_log); //数据存到2.0数据表中
  183. $this->bill_user_log111111($v, "联创分佣", $yj_90, "联创明细入账", 12);//股权分佣明细2.0系统
  184. $this->fugou_user_log($v, $fg_10, 16, 1, 1);//复购金明细入账
  185. } else {
  186. // ..
  187. }
  188. }
  189. }
  190. }
  191. $result[$uid] = $user_arr;
  192. }
  193. return json($result);
  194. }
  195. //添加复购金
  196. public function fugou_user_log($uid, $num, $order_id, $type_inc_des, $status = -1)
  197. {
  198. $fugou_data = Db::name('user')->where("uid", $uid)->find();
  199. $bill = [
  200. 'uid' => $uid,
  201. 'number' => $num,//数量
  202. 'status' => $status,//复购金状态1-有效 0-失效 -1待确认
  203. 'mark' => "复购金",//备注
  204. 'desc' => "10%作为复购金",//描述
  205. 'order_id' => $order_id,//订单id
  206. 'surplus' => 0,//剩余
  207. 'order_type' => 11,//关联订单ID
  208. 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗
  209. 'settlement_time' => date('Y-m-d H:i:s'),//添加时间,
  210. 'addtime' => time(),
  211. ];
  212. Db::name('user_sign_fugou')->insert($bill);
  213. }
  214. //添加平台分佣2.0系统收益明细日志(养老金金额)
  215. public function bill_user_log111111($uid, $name, $num, $mark, $comm)
  216. {
  217. $con_data = Db::name('user')->where("uid", $uid)->find();
  218. //如果是会员专区就即刻到账
  219. // Db::name('user')->where("uid",$con_data['uid'])->update(['brokerage_price'=> $con_data['brokerage_price'] + $num]);
  220. $bill = [
  221. 'uid' => $uid,
  222. 'link_id' => 0,//关联订单id
  223. 'pm' => 1,//0:支出,1:获得
  224. 'title' => $name,//账单标题
  225. 'category' => 'now_money',//明细种类
  226. 'type' => 'commission',//明细类型
  227. 'number' => $num,//明细数字
  228. 'balance' => 0,//剩余
  229. 'mark' => $mark,//备注
  230. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  231. 'status' => 1,//0待确定,1有效,-1无效
  232. 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  233. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  234. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  235. 'order_sn' => 0,//订单号
  236. 'tripartite' => 0,//
  237. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  238. 'mer_id' => 0,//商户id
  239. 'source' => 0,//1线下 0线上
  240. 'order_type' => 1,//1自营 2 第三方 订单类型
  241. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  242. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  243. 'take_time' => '',//结算时间
  244. 'district_id' => '',//
  245. 'street_id' => '',//
  246. 'month' => '',//月份
  247. ];
  248. Db::name('user_bill')->insert($bill);
  249. }
  250. //上周
  251. public function getWeekStartEnd()
  252. {
  253. $now = time();
  254. // 计算上周日 00:00 的时间戳
  255. $last_sunday_start = strtotime('last Sunday', $now);
  256. // 计算这周一 00:00 的时间戳
  257. $this_monday_start = strtotime('this Monday', $last_sunday_start);
  258. // 上周日 23:59:59 的时间戳,即这周一 00:00
  259. $last_sunday_end = $this_monday_start - 1;
  260. // 上周一 00:00 的时间戳,即上周日 24:00
  261. $last_monday_start = $this_monday_start - 604800;
  262. return [$last_monday_start, $last_sunday_end];
  263. }
  264. //本周
  265. // public function getWeekStartEnd()
  266. // {
  267. // $now = time();
  268. // // 计算本周一 00:00 的时间戳
  269. // $monday_start = strtotime('last Monday', $now);
  270. // if (date('w', $now) == 1) {
  271. // // 如果今天是周一,直接取今天的 00:00
  272. // $monday_start = strtotime('today', $now);
  273. // }
  274. // // 计算本周日 23:59:59 的时间戳
  275. // $sunday_end = strtotime('next Sunday', $now) + 86399;
  276. // return [$monday_start,$sunday_end];
  277. // }
  278. public function change_user_log($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0)
  279. {
  280. $transition = [
  281. 'pv' => 1,
  282. "gb" => 2,
  283. "df" => 3,
  284. "vr" => 4,
  285. "bt" => 5,
  286. "withdraw_quota" => 6,
  287. "unsettled_pension" => 7,
  288. "amount_old" => 8
  289. ];
  290. $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find();
  291. if (empty($routineInfo)) {
  292. return false;
  293. }
  294. $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0;
  295. if ($appoint_money > 0) {
  296. $alter_money = $appoint_money;
  297. } else {
  298. //再去查询变更前的 金额
  299. $alter_money = Db::name("user")->where('uid', $user_id)->value($type);
  300. }
  301. if ($change_status == 1) {
  302. //新增前
  303. $alter_money = $alter_money - $money;
  304. } else if ($change_status == 2) {
  305. //减少前
  306. $alter_money = $alter_money + $money;
  307. }
  308. if ($alter_money < 0) {
  309. $alter_money = 0;
  310. }
  311. $inser_data = [];
  312. $inser_data['user_id'] = $user_id;
  313. $inser_data['type'] = $transition[$type];
  314. $inser_data['change_status'] = $change_status;
  315. $inser_data['money'] = $money;
  316. $inser_data['routine_log_of_key_id'] = $key_id;
  317. $inser_data['remark'] = $remark;
  318. $inser_data['is_admin'] = $is_admin;
  319. $inser_data['create_time'] = time();
  320. $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0;
  321. Db::name("old_user_log")->insert($inser_data);
  322. }
  323. /**
  324. * 释放待发放佣金
  325. */
  326. public function releaseTobeAmount($user_id, $amount, $output)
  327. {
  328. $userInfo = Db::name("user")->find($user_id);
  329. if (empty($userInfo)) {
  330. return false;
  331. }
  332. $withdraw_quota = empty($userInfo['withdraw_quota_old']) ? 0 : $userInfo['withdraw_quota_old'];
  333. $old_amount = empty($userInfo['tobe_amount_old']) ? 0 : $userInfo['tobe_amount_old'];
  334. Db::name("user")->where("uid", $user_id)->save(['tobe_amount_old' => 0]);
  335. $amount += $old_amount;
  336. $inc_amount = 0;
  337. $inc_tobe_amount = 0;
  338. $dec_withdraw_quota = 0;
  339. if (($amount - $withdraw_quota) > 0) {
  340. $diff_money = $amount - $withdraw_quota;
  341. $inc_amount = $withdraw_quota;
  342. $dec_withdraw_quota = $withdraw_quota;
  343. $inc_tobe_amount = $diff_money;
  344. } elseif (($amount - $withdraw_quota) == 0) {
  345. $inc_amount = $withdraw_quota;
  346. $dec_withdraw_quota = $withdraw_quota;
  347. } else {
  348. // <0的情况
  349. $diff_money = $withdraw_quota - $amount;
  350. $inc_amount = $amount;
  351. $dec_withdraw_quota = $amount;
  352. }
  353. if ($inc_tobe_amount != 0) {
  354. $res = Db::name("user")->where("uid", $user_id)->inc("tobe_amount_old", $inc_tobe_amount)->fetchSql()->inc("amount", $inc_amount)->dec("withdraw_quota_old", $dec_withdraw_quota)->save();
  355. } else {
  356. $res = Db::name("user")->where("uid", $user_id)->inc("amount_old", $inc_amount)->fetchSql()->dec("withdraw_quota_old", $dec_withdraw_quota)->save();
  357. }
  358. //记录日志 提现额度消费
  359. if ($dec_withdraw_quota > 0) {
  360. $this->change_user_log($user_id, 'withdraw_quota', 2, $dec_withdraw_quota, "conversion_tobe_amount", "佣金额度兑换佣金");
  361. }
  362. }
  363. public function getTime($timePeriod)
  364. {
  365. // 定义时间区间
  366. $now = time();
  367. $startTime = '';
  368. $endTime = '';
  369. if ($timePeriod == 'last_week') {
  370. // 上周的时间区间
  371. $startTime = date('Y-m-d', strtotime('monday last week', $now)); // 上周星期一的开始时间
  372. $endTime = date('Y-m-d', strtotime('sunday last week', $now)); // 上周星期日的结束时间
  373. } elseif ($timePeriod == 'this_week') {
  374. // 这周的时间区间
  375. $startTime = date('Y-m-d', strtotime('monday this week', $now)); // 这周星期一的开始时间
  376. $endTime = date('Y-m-d', strtotime('sunday this week', $now)); // 这周星期日的结束时间
  377. }
  378. // return ['startTime' => '2025-03-10', 'endTime' => '2025-03-23'];
  379. return ['startTime' => $startTime, 'endTime' => $endTime];
  380. }
  381. public function getTeamPvByTime($startTime, $endTime, $userIdList = [])
  382. {
  383. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  384. // 0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上9话费10线下
  385. $pvLogList = Db::name("user_pv_log")
  386. ->whereIn('uid', $userIdList)
  387. // ->whereIn('order_type', [0])
  388. ->where('status', '=', 1)
  389. ->whereBetween("settlement_time", [$startTime, $endTime])
  390. ->whereNotNull("settlement_time") // 排除 pay_time 为 NULL 的订单
  391. ->select()
  392. ->toArray();
  393. // 计算总业绩
  394. $totalPv = 0;
  395. foreach ($pvLogList as $pvLog) {
  396. $totalPv = bcadd($totalPv, $pvLog['pv'], 2);
  397. }
  398. return $totalPv;
  399. }
  400. public function getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList = [])
  401. {
  402. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  403. $redEnvelopeList = Db::name("user_sign_hongbao")
  404. ->whereNotIn('uid', $removeUserIdList)
  405. ->where('status', 1)
  406. ->whereBetween("settlement_time", [$startTime, $endTime])
  407. ->select()
  408. ->toArray();
  409. return floor(array_sum(array_column($redEnvelopeList, 'number')) * 100) / 100;
  410. }
  411. }