Taskorderlianc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. class Taskorderlianc
  10. {
  11. /**
  12. * 会员升级
  13. */
  14. public function member_update($orderInfo, $goods_of_info, $user_id)
  15. {
  16. //
  17. //$user_id = $userInfo['id'];
  18. $order_id = $orderInfo['id'];
  19. //code...
  20. $pay_gb = round($orderInfo['num'] * $orderInfo['gb_num'], 2);
  21. //$pay_vr = round($orderInfo['num'] * $orderInfo['vr_num'],2);
  22. //付款按照700的VR付款 start
  23. $vr_num = 0;
  24. if ($orderInfo['price'] == 1180) {
  25. $vr_num = 700;
  26. } elseif ($orderInfo['price'] == 11800) {
  27. $vr_num = 7000;
  28. } elseif ($orderInfo['price'] == 11800) {
  29. $vr_num = 2100;
  30. } elseif ($orderInfo['price'] == 10620) {
  31. $vr_num = 6300;
  32. }
  33. $pay_gb1 = $vr_num;
  34. $pay_vr1 = $orderInfo['price'];//记录VR值
  35. // $result = Db::name("old_user_jbp")->where("id",$user_id)->dec("vr",$orderInfo['price'])->save();//1.0jbp旧表
  36. // Db::name("user")->where("uid",$user_id)->dec("vr",$orderInfo['price'])->save();//2.0新表
  37. $result = true;
  38. if ($result) {
  39. $invite_id = $goods_of_info['spread_id'];
  40. $zs_ylj = 0; //养老金赋值
  41. if ($goods_of_info['money'] == 1180) {
  42. //修改订单到账状态
  43. $zs_ylj = round(700 * 0.05, 2);//计算的养老金 金额
  44. } elseif ($goods_of_info['money'] == 11800) {
  45. $zs_ylj = round(7000 * 0.05, 2);//计算的养老金 金额
  46. } elseif ($goods_of_info['money'] == 3540) {
  47. $zs_ylj = round(2100 * 0.05, 2);//计算的养老金 金额
  48. } elseif ($goods_of_info['money'] == 10620) {
  49. $zs_ylj = round(6300 * 0.05, 2);//计算的养老金 金额
  50. } elseif ($goods_of_info['money'] == 9440) {
  51. $zs_ylj = round(5600 * 0.05, 2);//八单计算的养老金 金额
  52. } elseif ($goods_of_info['money'] == 2360) {
  53. $zs_ylj = round(1400 * 0.05, 2);//两单计算的养老金 金额
  54. }
  55. $uid = $user_id;
  56. $new_user_id = $uid;
  57. if (empty($new_user_id)) {
  58. abort(0, "当前绑定会员查询错误");
  59. }
  60. $new_nums = Db::name("old_user_of_goods")->where("user_id", $uid)->sum("money");
  61. //增加过后权益对比 获取当前权益等级
  62. $old_level_id = Db::name("old_user_jbp")->where("id", $uid)->find();
  63. $old_level_info = Db::name("old_user_level")->where("id", $old_level_id['user_level_id'])->find();
  64. $level_info = [];
  65. $level_list = Db::name("old_user_level")->order("money desc")->select();
  66. //计算会员等级 升级
  67. foreach ($level_list as $key => $value) {
  68. # code...
  69. if ($new_nums >= $value['money']) {
  70. $level_info = $value;
  71. break;
  72. }
  73. }
  74. $zs_gb = $orderInfo['price'] * 3;
  75. $zs_withdraw_quota = $orderInfo['price'];
  76. if ($old_level_info['id'] == $level_info['id']) {
  77. //未到达升级权益 不改变用户等级
  78. $update_status = false;
  79. } else {
  80. $update_status = true;
  81. $user_level = $level_info['id'];
  82. //var_dump( $user_level);
  83. Db::name("old_user_jbp")->where("id", $new_user_id)->save(['user_level_id' => $user_level]);//1.0jbp旧表
  84. Db::name("user")->where("uid", $new_user_id)->save(['user_level_id_old' => $user_level]);//2.0新表
  85. // todo 同时改变他的权益
  86. }
  87. // Db::name("old_user_jbp")->where("id",$new_user_id)->inc("withdraw_quota",$zs_withdraw_quota)->save();//1.0jbp旧表
  88. // Db::name("user")->where("uid",$new_user_id)->save(['withdraw_quota_old'=>$zs_withdraw_quota]);//2.0新表
  89. // 下发邀请人赠送的GB
  90. // $zs_invite_gb = round($orderInfo['price']*0.3,2);
  91. // //todo
  92. // Db::name("old_user_jbp")->where("id",$invite_id)->inc("gb",$zs_invite_gb)->save();//1.0jbp旧表
  93. // Db::name("user")->where("uid",$invite_id)->inc("score",$zs_invite_gb)->save();//2.0新表
  94. //$old_have_pension = Db::name("old_user_jbp")->where("id",$invite_id)->value("have_pension");
  95. //检查是不是订单价格是1180或者11800,更改VR
  96. if ($goods_of_info['money'] == 1180 || $goods_of_info['money'] == 11800 || $goods_of_info['money'] == 3540 || $goods_of_info['money'] == 10620 || $goods_of_info['money'] == 9440 || $goods_of_info['money'] == 2360) {
  97. //修改订单到账状态
  98. Db::name("old_user_of_goods")->where("order_id", $goods_of_info1['id'])->save(['status' => 1, 'pay_gb' => 0, 'pay_vr' => $pay_vr1, 'pay_bt' => 0, 'create_time' => time()]);
  99. }
  100. }
  101. if ($result) {
  102. //更改订单状态
  103. Db::commit();
  104. $new_user_name = Db::name("old_user_jbp")->where("id", $new_user_id)->value("user_name");
  105. $new_user_name = empty($new_user_name) ? "" : $new_user_name;
  106. //记录日志信息
  107. if ($user_id == $invite_id) {
  108. //报单中心=推荐人 GB记录日志有问题需要传入指定参数去计算
  109. //$money = Db::name("old_user_jbp")->where("id",$user_id)->value("gb");
  110. //$appoint_money = $money - $zs_invite_gb;
  111. if ($goods_of_info['money'] == 1180 || $goods_of_info['money'] == 11800 || $goods_of_info['money'] == 3540 || $goods_of_info['money'] == 10620 || $goods_of_info['money'] == 9440 || $goods_of_info['money'] == 2360) {
  112. $this->change_user_log66($user_id, "vr", 2, $pay_vr1, "declare_expend", '权益消费VR-' . $new_user_name);
  113. }
  114. } else {
  115. if ($goods_of_info['money'] == 1180 || $goods_of_info['money'] == 11800 || $goods_of_info['money'] == 3540 || $goods_of_info['money'] == 10620 || $goods_of_info['money'] == 9440 || $goods_of_info['money'] == 2360) {
  116. $this->change_user_log66($user_id, "vr", 2, $pay_vr1, "declare_expend", '权益消费VR-' . $new_user_name);
  117. }
  118. }
  119. // $this->change_user_log66($new_user_id,'gb',1,$zs_gb,"levle_update_zs","权益升级达标赠送");
  120. $this->change_user_log66($new_user_id, 'withdraw_quota_old', 1, $zs_withdraw_quota, "levle_update_zs", "权益升级达标赠送");
  121. // $this->change_user_log66($invite_id,'gb',1,$zs_invite_gb,"levle_update_zs","邀请-".$new_user_name);
  122. // echo 123;return;
  123. //处理查看是否有待发放佣金
  124. // $zs_withdraw_quota
  125. Db::name("old_user_jbp")->where("id", $new_user_id)->inc("total_withdraw_quota", $zs_withdraw_quota)->save();//1.0jbp旧表
  126. Db::name("user")->where("uid", $new_user_id)->inc("total_withdraw_quota_old", $zs_withdraw_quota)->save();
  127. // $this->releaseTobeAmount($new_user_id,$zs_withdraw_quota);
  128. // $this->success("支付成功",['is_cash'=>0]);
  129. }
  130. Db::rollback();
  131. // $this->error("操作失败");
  132. }
  133. function change_user_log66($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0)
  134. {
  135. $transition = [
  136. 'pv' => 1,
  137. "gb" => 2,
  138. "df" => 3,
  139. "vr" => 4,
  140. "bt" => 5,
  141. "withdraw_quota_old" => 6,
  142. "unsettled_pension" => 7,
  143. "amount_old" => 8
  144. ];
  145. $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find();
  146. if (empty($routineInfo)) {
  147. return false;
  148. }
  149. $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0;
  150. //再去查询变更前的 金额
  151. if ($appoint_money > 0) {
  152. $alter_money = $appoint_money;
  153. } else {
  154. //再去查询变更前的 金额
  155. $alter_money = Db::name("user")->where('uid', $user_id)->value($type);
  156. }
  157. if ($change_status == 1) {
  158. //新增前
  159. $alter_money = $alter_money - $money;
  160. } else if ($change_status == 2) {
  161. //减少前
  162. $alter_money = $alter_money + $money;
  163. }
  164. $inser_data = [];
  165. $inser_data['user_id'] = $user_id;
  166. $inser_data['type'] = $transition[$type];
  167. $inser_data['change_status'] = $change_status;
  168. $inser_data['money'] = $money;
  169. $inser_data['routine_log_of_key_id'] = $key_id;
  170. $inser_data['remark'] = $remark;
  171. $inser_data['is_admin'] = $is_admin;
  172. $inser_data['create_time'] = time();
  173. $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0;
  174. Db::name("old_user_log")->insert($inser_data);
  175. }
  176. //添加推广佣金/养老金日志(养老金金额)
  177. public function bill_user_log($uid, $name, $num, $mark)
  178. {
  179. $con_data = Db::name('user')->where("uid", $uid)->find();
  180. //如果是会员专区就即刻到账
  181. Db::name('user')->where("uid", $con_data['uid'])->update(['brokerage_price' => $con_data['brokerage_price'] + $num]);
  182. $bill = [
  183. 'uid' => $uid,
  184. 'link_id' => 0,//关联订单id
  185. 'pm' => 1,//0:支出,1:获得
  186. 'title' => $name,//账单标题
  187. 'category' => 'now_money',//明细种类
  188. 'type' => 'commission',//明细类型
  189. 'number' => $num,//明细数字
  190. 'balance' => 0,//剩余
  191. 'mark' => $mark,//备注
  192. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  193. 'status' => 1,//0待确定,1有效,-1无效
  194. 'commission_type' => 8,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  195. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  196. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  197. 'order_sn' => 0,//订单号
  198. 'tripartite' => 0,//
  199. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  200. 'mer_id' => 0,//商户id
  201. 'source' => 0,//1线下 0线上
  202. 'order_type' => 1,//1自营 2 第三方 订单类型
  203. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  204. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  205. 'take_time' => '',//结算时间
  206. 'district_id' => '',//
  207. 'street_id' => '',//
  208. 'month' => '',//月份
  209. ];
  210. Db::name('user_bill')->insert($bill);
  211. }
  212. public function lst2()
  213. {
  214. $startTime = '2025-03-10';
  215. $endTime = '2025-03-30';
  216. // 查询测试人员账号信息
  217. //18973958920
  218. //15186694339
  219. //18833633835
  220. //19525470098
  221. //17756507016
  222. //18226705337
  223. $testAccount = ['18973958920', '15186694339', '18833633835', '19525470098', '17756507016', '18226705337'];
  224. $removeUserList = Db::name("user")
  225. ->whereIn('account', $testAccount)
  226. ->where("status", "<>", 0)
  227. ->where('is_del', '<>', 1)
  228. ->select()
  229. ->toArray();
  230. $removeUserIdList = array_column($removeUserList, 'uid');
  231. if (empty($startTime) || empty($endTime)) {
  232. $timeMap = $this->getTime('last_week');
  233. $startTime = $timeMap['startTime'] . ' 00:00:00';
  234. $endTime = $timeMap['endTime'] . ' 23:59:59';
  235. }
  236. // 定义分红金额
  237. $weekRecord = $this->getTeamPvByTime($startTime, $endTime, $removeUserIdList);
  238. // 分红金额 - 红包金额
  239. $redEnvelope = $this->getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList);
  240. $weekRecord -= $redEnvelope;
  241. if ($weekRecord <= 0) {
  242. return json('无效分红金额');
  243. }
  244. $originator_award = round(($weekRecord * 0.05), 2);
  245. // foreach($list as $k=>$v){
  246. // var_dump($v);
  247. // }
  248. // return;
  249. $sum_money = Db::name("old_user_lianc")->sum("number");
  250. $lianc_list = Db::name("old_user_lianc")->order("id desc")->field("id,number,title")->select()->toArray();
  251. $lianc_list_arr = array_column($lianc_list, null, 'id');
  252. $list = Db::name("user")
  253. ->where("originator_id_old", ">", 0)
  254. ->where("status", 1)
  255. ->field("uid,originator_id_old")
  256. ->select()
  257. ->toArray();
  258. $user_arr = [];
  259. foreach ($list as $key => $value) {
  260. # code...
  261. $user_arr[$value['originator_id_old']]['people'][] = $value['uid'];
  262. $user_arr[$value['originator_id_old']]['money'] = $lianc_list_arr[$value['originator_id_old']]['number'];
  263. $user_arr[$value['originator_id_old']]['title'] = $lianc_list_arr[$value['originator_id_old']]['title'];
  264. $user_arr[$value['originator_id_old']]['id'] = $lianc_list_arr[$value['originator_id_old']]['id'];
  265. }
  266. //处理计算权重 和平均分配的金额
  267. $total_num = 0;
  268. $count_num = 0;
  269. foreach ($user_arr as $key => $value) {
  270. $user_arr[$key]['people_count'] = count($user_arr[$key]['people']);
  271. # code...
  272. // floor(($weekRecord * ($value['profit_rate'] / 100)) * 100) / 100;
  273. // $total_num += round($value['money'] * count($value['people']) / 1000, 2);
  274. $total_num += floor(($value['money'] * count($value['people'])) * 100) / 100;
  275. if (isset($user_arr[$key]['total_num'])) {
  276. $user_arr[$key]['total_num'] += floor(($value['money'] * count($value['people'])) * 100) / 100;
  277. } else {
  278. $user_arr[$key]['total_num'] = 0;
  279. }
  280. $count_num += count($value['people']);
  281. }
  282. foreach ($user_arr as $key => $value) {
  283. # code...
  284. // $rate = round((count($value['people']) * $value['money']) / $total_num, 2) / 1000;
  285. $rate = floor(($value['money'] * count($value['people']) / $total_num) * 100) / 100;
  286. $user_arr[$key]['rate'] = $rate;
  287. $distribution_money = floor($originator_award * $rate * 100) / 100;
  288. $user_arr[$key]['distribution_money'] = $distribution_money;
  289. // $user_arr[$key]['people_award'] = round($distribution_money / count($value['people']), 2);//可分配佣金
  290. $user_arr[$key]['people_award'] = floor($distribution_money / count($value['people']) * 100) / 100;//可分配佣金
  291. }
  292. $count = 0;
  293. foreach ($user_arr as $key => $value) {
  294. $distribution_money = $value['distribution_money'];
  295. # code...
  296. foreach ($value['people'] as $k => $v) {
  297. # code...
  298. $award = $value['people_award'];
  299. //90给佣金,10给复购金
  300. $yj_90 = $award * 0.9;
  301. $fg_10 = $award * 0.1;
  302. Db::name("user")->where("uid", $v)->inc("brokerage_price", $yj_90)->save();
  303. $res = Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save();
  304. Db::name("user")->where("uid", $v)->inc("amount_old", $yj_90)->save();//不用冻结金额和提现额度来限制佣金发放
  305. $res3 = Db::name("user")->where("uid", $v)->inc("fugou", $fg_10)->save();
  306. $res = true;
  307. if ($res) {
  308. $$count = $count + 1;
  309. $this->change_user_log($v, 'amount_old', 1, $yj_90, "relation_profits", "联创分红入账" . "-" . $value['title']);
  310. //入账
  311. $week_data = [];
  312. $week_data['user_id'] = $v;
  313. $week_data['user_lianc_id'] = $value['id'];
  314. $week_data['money'] = $yj_90;
  315. $week_data['remark'] = "联创分红入账主动执行" . $yj_90;
  316. $week_data['all_record'] = $originator_award;
  317. $week_data['all_amount'] = $distribution_money;
  318. $week_data['create_time'] = time();
  319. Db::name("old_user_originator")->insert($week_data);
  320. $userdatassss = db::name("user")->where("uid", $v)->find();
  321. $datas_log = [
  322. 'note' => "名称:" . $userdatassss['nickname'] . " 用户:" . $v . " 新增联创佣金:" . $yj_90,
  323. 'create_time' => date('Y-m-d H:i:s')
  324. ];
  325. Db::name('test_log')->insert($datas_log); //数据存到2.0数据表中
  326. $this->bill_user_log111111($v, "联创分佣", $yj_90, "联创明细入账", 12);//股权分佣明细2.0系统
  327. $this->fugou_user_log($v, $fg_10, 16, 1, 1);//复购金明细入账
  328. } else {
  329. // ..
  330. }
  331. }
  332. }
  333. return json($user_arr);
  334. }
  335. //添加复购金
  336. public function fugou_user_log($uid, $num, $order_id, $type_inc_des, $status = -1)
  337. {
  338. $fugou_data = Db::name('user')->where("uid", $uid)->find();
  339. $bill = [
  340. 'uid' => $uid,
  341. 'number' => $num,//数量
  342. 'status' => $status,//复购金状态1-有效 0-失效 -1待确认
  343. 'mark' => "复购金",//备注
  344. 'desc' => "10%作为复购金",//描述
  345. 'order_id' => $order_id,//订单id
  346. 'surplus' => 0,//剩余
  347. 'order_type' => 11,//关联订单ID
  348. 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗
  349. 'settlement_time' => date('Y-m-d H:i:s'),//添加时间,
  350. 'addtime' => time(),
  351. ];
  352. Db::name('user_sign_fugou')->insert($bill);
  353. }
  354. //添加平台分佣2.0系统收益明细日志(养老金金额)
  355. public function bill_user_log111111($uid, $name, $num, $mark, $comm)
  356. {
  357. $con_data = Db::name('user')->where("uid", $uid)->find();
  358. //如果是会员专区就即刻到账
  359. // Db::name('user')->where("uid",$con_data['uid'])->update(['brokerage_price'=> $con_data['brokerage_price'] + $num]);
  360. $bill = [
  361. 'uid' => $uid,
  362. 'link_id' => 0,//关联订单id
  363. 'pm' => 1,//0:支出,1:获得
  364. 'title' => $name,//账单标题
  365. 'category' => 'now_money',//明细种类
  366. 'type' => 'commission',//明细类型
  367. 'number' => $num,//明细数字
  368. 'balance' => 0,//剩余
  369. 'mark' => $mark,//备注
  370. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  371. 'status' => 1,//0待确定,1有效,-1无效
  372. 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  373. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  374. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  375. 'order_sn' => 0,//订单号
  376. 'tripartite' => 0,//
  377. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  378. 'mer_id' => 0,//商户id
  379. 'source' => 0,//1线下 0线上
  380. 'order_type' => 1,//1自营 2 第三方 订单类型
  381. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  382. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  383. 'take_time' => '',//结算时间
  384. 'district_id' => '',//
  385. 'street_id' => '',//
  386. 'month' => '',//月份
  387. ];
  388. Db::name('user_bill')->insert($bill);
  389. }
  390. //上周
  391. public function getWeekStartEnd()
  392. {
  393. $now = time();
  394. // 计算上周日 00:00 的时间戳
  395. $last_sunday_start = strtotime('last Sunday', $now);
  396. // 计算这周一 00:00 的时间戳
  397. $this_monday_start = strtotime('this Monday', $last_sunday_start);
  398. // 上周日 23:59:59 的时间戳,即这周一 00:00
  399. $last_sunday_end = $this_monday_start - 1;
  400. // 上周一 00:00 的时间戳,即上周日 24:00
  401. $last_monday_start = $this_monday_start - 604800;
  402. return [$last_monday_start, $last_sunday_end];
  403. }
  404. //本周
  405. // public function getWeekStartEnd()
  406. // {
  407. // $now = time();
  408. // // 计算本周一 00:00 的时间戳
  409. // $monday_start = strtotime('last Monday', $now);
  410. // if (date('w', $now) == 1) {
  411. // // 如果今天是周一,直接取今天的 00:00
  412. // $monday_start = strtotime('today', $now);
  413. // }
  414. // // 计算本周日 23:59:59 的时间戳
  415. // $sunday_end = strtotime('next Sunday', $now) + 86399;
  416. // return [$monday_start,$sunday_end];
  417. // }
  418. public function change_user_log($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0)
  419. {
  420. $transition = [
  421. 'pv' => 1,
  422. "gb" => 2,
  423. "df" => 3,
  424. "vr" => 4,
  425. "bt" => 5,
  426. "withdraw_quota" => 6,
  427. "unsettled_pension" => 7,
  428. "amount_old" => 8
  429. ];
  430. $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find();
  431. if (empty($routineInfo)) {
  432. return false;
  433. }
  434. $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0;
  435. if ($appoint_money > 0) {
  436. $alter_money = $appoint_money;
  437. } else {
  438. //再去查询变更前的 金额
  439. $alter_money = Db::name("user")->where('uid', $user_id)->value($type);
  440. }
  441. if ($change_status == 1) {
  442. //新增前
  443. $alter_money = $alter_money - $money;
  444. } else if ($change_status == 2) {
  445. //减少前
  446. $alter_money = $alter_money + $money;
  447. }
  448. if ($alter_money < 0) {
  449. $alter_money = 0;
  450. }
  451. $inser_data = [];
  452. $inser_data['user_id'] = $user_id;
  453. $inser_data['type'] = $transition[$type];
  454. $inser_data['change_status'] = $change_status;
  455. $inser_data['money'] = $money;
  456. $inser_data['routine_log_of_key_id'] = $key_id;
  457. $inser_data['remark'] = $remark;
  458. $inser_data['is_admin'] = $is_admin;
  459. $inser_data['create_time'] = time();
  460. $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0;
  461. Db::name("old_user_log")->insert($inser_data);
  462. }
  463. /**
  464. * 释放待发放佣金
  465. */
  466. public function releaseTobeAmount($user_id, $amount, $output)
  467. {
  468. $userInfo = Db::name("user")->find($user_id);
  469. if (empty($userInfo)) {
  470. return false;
  471. }
  472. $withdraw_quota = empty($userInfo['withdraw_quota_old']) ? 0 : $userInfo['withdraw_quota_old'];
  473. $old_amount = empty($userInfo['tobe_amount_old']) ? 0 : $userInfo['tobe_amount_old'];
  474. Db::name("user")->where("uid", $user_id)->save(['tobe_amount_old' => 0]);
  475. $amount += $old_amount;
  476. $inc_amount = 0;
  477. $inc_tobe_amount = 0;
  478. $dec_withdraw_quota = 0;
  479. if (($amount - $withdraw_quota) > 0) {
  480. $diff_money = $amount - $withdraw_quota;
  481. $inc_amount = $withdraw_quota;
  482. $dec_withdraw_quota = $withdraw_quota;
  483. $inc_tobe_amount = $diff_money;
  484. } elseif (($amount - $withdraw_quota) == 0) {
  485. $inc_amount = $withdraw_quota;
  486. $dec_withdraw_quota = $withdraw_quota;
  487. } else {
  488. // <0的情况
  489. $diff_money = $withdraw_quota - $amount;
  490. $inc_amount = $amount;
  491. $dec_withdraw_quota = $amount;
  492. }
  493. if ($inc_tobe_amount != 0) {
  494. $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();
  495. } else {
  496. $res = Db::name("user")->where("uid", $user_id)->inc("amount_old", $inc_amount)->fetchSql()->dec("withdraw_quota_old", $dec_withdraw_quota)->save();
  497. }
  498. //记录日志 提现额度消费
  499. if ($dec_withdraw_quota > 0) {
  500. $this->change_user_log($user_id, 'withdraw_quota', 2, $dec_withdraw_quota, "conversion_tobe_amount", "佣金额度兑换佣金");
  501. }
  502. }
  503. public function getTime($timePeriod)
  504. {
  505. // 定义时间区间
  506. $now = time();
  507. $startTime = '';
  508. $endTime = '';
  509. if ($timePeriod == 'last_week') {
  510. // 上周的时间区间
  511. $startTime = date('Y-m-d', strtotime('monday last week', $now)); // 上周星期一的开始时间
  512. $endTime = date('Y-m-d', strtotime('sunday last week', $now)); // 上周星期日的结束时间
  513. } elseif ($timePeriod == 'this_week') {
  514. // 这周的时间区间
  515. $startTime = date('Y-m-d', strtotime('monday this week', $now)); // 这周星期一的开始时间
  516. $endTime = date('Y-m-d', strtotime('sunday this week', $now)); // 这周星期日的结束时间
  517. }
  518. // return ['startTime' => '2025-03-10', 'endTime' => '2025-03-23'];
  519. return ['startTime' => $startTime, 'endTime' => $endTime];
  520. }
  521. public function getTeamPvByTime($startTime, $endTime, $removeUserIdList = [])
  522. {
  523. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  524. $orders = Db::name("store_order")
  525. ->where('mer_id', 496)
  526. ->whereNotIn('uid', $removeUserIdList)
  527. ->whereIn('status', [0, 1, 2, 3])
  528. ->whereIn("total_price", [1180, 11800, 10620, 2360, 9440])
  529. ->whereBetween("pay_time", [$startTime, $endTime])
  530. ->whereNotNull("pay_time") // 排除 pay_time 为 NULL 的订单
  531. ->select();
  532. // 计算总业绩
  533. $totalPv = 0;
  534. foreach ($orders as $order) {
  535. switch ($order['total_price']) {
  536. case 1180:
  537. $totalPv += 700;
  538. break;
  539. case 11800:
  540. $totalPv += 7000;
  541. break;
  542. case 10620:
  543. $totalPv += 6300;
  544. break;
  545. case 2360:
  546. $totalPv += 1400;
  547. break;
  548. case 9440:
  549. $totalPv += 5600;
  550. break;
  551. }
  552. }
  553. return $totalPv;
  554. }
  555. public function getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList = [])
  556. {
  557. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  558. $redEnvelopeList = Db::name("user_sign_hongbao")
  559. ->whereNotIn('uid', $removeUserIdList)
  560. ->where('status', 1)
  561. ->whereBetween("settlement_time", [$startTime, $endTime])
  562. ->select()
  563. ->toArray();
  564. return floor(array_sum(array_column($redEnvelopeList, 'number')) * 100) / 100;
  565. }
  566. }