Taskorderlianc.php 27 KB

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