TaskZeroLineOrderLianc.php 29 KB

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