Taskorderlianc.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 Taskorderlianc
  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. // 定义分红金额
  240. $weekRecord = $this->getTeamPvByTime($startTime, $endTime, $removeUserIdList);
  241. // 分红金额 - 红包金额
  242. $redEnvelope = $this->getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList);
  243. $weekRecord -= $redEnvelope;
  244. if ($weekRecord <= 0) {
  245. return json('无效分红金额');
  246. }
  247. if (!is_null($maxAllocationAmount)) {
  248. if ($weekRecord > $maxAllocationAmount) {
  249. $weekRecord = $maxAllocationAmount;
  250. }
  251. }
  252. $originator_award = round(($weekRecord * 0.05), 2);
  253. // foreach($list as $k=>$v){
  254. // var_dump($v);
  255. // }
  256. // return;
  257. $sum_money = Db::name("old_user_lianc")->sum("number");
  258. $lianc_list = Db::name("old_user_lianc")->order("id desc")->field("id,number,title")->select()->toArray();
  259. $lianc_list_arr = array_column($lianc_list, null, 'id');
  260. $list = Db::name("user")
  261. ->where("originator_id_old", ">", 0)
  262. ->where("status", 1)
  263. ->field("uid,originator_id_old")
  264. ->select()
  265. ->toArray();
  266. $user_arr = [];
  267. foreach ($list as $key => $value) {
  268. # code...
  269. $user_arr[$value['originator_id_old']]['people'][] = $value['uid'];
  270. $user_arr[$value['originator_id_old']]['money'] = $lianc_list_arr[$value['originator_id_old']]['number'];
  271. $user_arr[$value['originator_id_old']]['title'] = $lianc_list_arr[$value['originator_id_old']]['title'];
  272. $user_arr[$value['originator_id_old']]['id'] = $lianc_list_arr[$value['originator_id_old']]['id'];
  273. }
  274. //处理计算权重 和平均分配的金额
  275. $total_num = 0;
  276. $count_num = 0;
  277. foreach ($user_arr as $key => $value) {
  278. $user_arr[$key]['people_count'] = count($user_arr[$key]['people']);
  279. # code...
  280. // floor(($weekRecord * ($value['profit_rate'] / 100)) * 100) / 100;
  281. // $total_num += round($value['money'] * count($value['people']) / 1000, 2);
  282. $total_num += floor(($value['money'] * count($value['people'])) * 100) / 100;
  283. if (isset($user_arr[$key]['total_num'])) {
  284. $user_arr[$key]['total_num'] += floor(($value['money'] * count($value['people'])) * 100) / 100;
  285. } else {
  286. $user_arr[$key]['total_num'] = 0;
  287. }
  288. $count_num += count($value['people']);
  289. }
  290. foreach ($user_arr as $key => $value) {
  291. # code...
  292. // $rate = round((count($value['people']) * $value['money']) / $total_num, 2) / 1000;
  293. $rate = floor(($value['money'] * count($value['people']) / $total_num) * 100) / 100;
  294. $user_arr[$key]['rate'] = $rate;
  295. $distribution_money = floor($originator_award * $rate * 100) / 100;
  296. $user_arr[$key]['distribution_money'] = $distribution_money;
  297. // $user_arr[$key]['people_award'] = round($distribution_money / count($value['people']), 2);//可分配佣金
  298. $user_arr[$key]['people_award'] = floor($distribution_money / count($value['people']) * 100) / 100;//可分配佣金
  299. }
  300. $count = 0;
  301. foreach ($user_arr as $key => $value) {
  302. $distribution_money = $value['distribution_money'];
  303. # code...
  304. foreach ($value['people'] as $k => $v) {
  305. # code...
  306. $award = $value['people_award'];
  307. //90给佣金,10给复购金
  308. $yj_90 = $award * 0.9;
  309. $fg_10 = $award * 0.1;
  310. if ($isTest == 0) {
  311. Db::name("user")->where("uid", $v)->inc("brokerage_price", $yj_90)->save();
  312. $res = Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save();
  313. Db::name("user")->where("uid", $v)->inc("amount_old", $yj_90)->save();//不用冻结金额和提现额度来限制佣金发放
  314. $res3 = Db::name("user")->where("uid", $v)->inc("fugou", $fg_10)->save();
  315. $res = true;
  316. if ($res) {
  317. $$count = $count + 1;
  318. $this->change_user_log($v, 'amount_old', 1, $yj_90, "relation_profits", "联创分红入账" . "-" . $value['title']);
  319. //入账
  320. $week_data = [];
  321. $week_data['user_id'] = $v;
  322. $week_data['user_lianc_id'] = $value['id'];
  323. $week_data['money'] = $yj_90;
  324. $week_data['remark'] = "联创分红入账主动执行" . $yj_90;
  325. $week_data['all_record'] = $originator_award;
  326. $week_data['all_amount'] = $distribution_money;
  327. $week_data['create_time'] = time();
  328. Db::name("old_user_originator")->insert($week_data);
  329. $userdatassss = db::name("user")->where("uid", $v)->find();
  330. $datas_log = [
  331. 'note' => "名称:" . $userdatassss['nickname'] . " 用户:" . $v . " 新增联创佣金:" . $yj_90,
  332. 'create_time' => date('Y-m-d H:i:s')
  333. ];
  334. Db::name('test_log')->insert($datas_log); //数据存到2.0数据表中
  335. $this->bill_user_log111111($v, "联创分佣", $yj_90, "联创明细入账", 12);//股权分佣明细2.0系统
  336. $this->fugou_user_log($v, $fg_10, 16, 1, 1);//复购金明细入账
  337. } else {
  338. // ..
  339. }
  340. }
  341. }
  342. }
  343. return json($user_arr);
  344. }
  345. //添加复购金
  346. public function fugou_user_log($uid, $num, $order_id, $type_inc_des, $status = -1)
  347. {
  348. $fugou_data = Db::name('user')->where("uid", $uid)->find();
  349. $bill = [
  350. 'uid' => $uid,
  351. 'number' => $num,//数量
  352. 'status' => $status,//复购金状态1-有效 0-失效 -1待确认
  353. 'mark' => "复购金",//备注
  354. 'desc' => "10%作为复购金",//描述
  355. 'order_id' => $order_id,//订单id
  356. 'surplus' => 0,//剩余
  357. 'order_type' => 11,//关联订单ID
  358. 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗
  359. 'settlement_time' => date('Y-m-d H:i:s'),//添加时间,
  360. 'addtime' => time(),
  361. ];
  362. Db::name('user_sign_fugou')->insert($bill);
  363. }
  364. //添加平台分佣2.0系统收益明细日志(养老金金额)
  365. public function bill_user_log111111($uid, $name, $num, $mark, $comm)
  366. {
  367. $con_data = Db::name('user')->where("uid", $uid)->find();
  368. //如果是会员专区就即刻到账
  369. // Db::name('user')->where("uid",$con_data['uid'])->update(['brokerage_price'=> $con_data['brokerage_price'] + $num]);
  370. $bill = [
  371. 'uid' => $uid,
  372. 'link_id' => 0,//关联订单id
  373. 'pm' => 1,//0:支出,1:获得
  374. 'title' => $name,//账单标题
  375. 'category' => 'now_money',//明细种类
  376. 'type' => 'commission',//明细类型
  377. 'number' => $num,//明细数字
  378. 'balance' => 0,//剩余
  379. 'mark' => $mark,//备注
  380. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  381. 'status' => 1,//0待确定,1有效,-1无效
  382. 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  383. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  384. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  385. 'order_sn' => 0,//订单号
  386. 'tripartite' => 0,//
  387. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  388. 'mer_id' => 0,//商户id
  389. 'source' => 0,//1线下 0线上
  390. 'order_type' => 1,//1自营 2 第三方 订单类型
  391. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  392. 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入
  393. 'take_time' => '',//结算时间
  394. 'district_id' => '',//
  395. 'street_id' => '',//
  396. 'month' => '',//月份
  397. ];
  398. Db::name('user_bill')->insert($bill);
  399. }
  400. //上周
  401. public function getWeekStartEnd()
  402. {
  403. $now = time();
  404. // 计算上周日 00:00 的时间戳
  405. $last_sunday_start = strtotime('last Sunday', $now);
  406. // 计算这周一 00:00 的时间戳
  407. $this_monday_start = strtotime('this Monday', $last_sunday_start);
  408. // 上周日 23:59:59 的时间戳,即这周一 00:00
  409. $last_sunday_end = $this_monday_start - 1;
  410. // 上周一 00:00 的时间戳,即上周日 24:00
  411. $last_monday_start = $this_monday_start - 604800;
  412. return [$last_monday_start, $last_sunday_end];
  413. }
  414. //本周
  415. // public function getWeekStartEnd()
  416. // {
  417. // $now = time();
  418. // // 计算本周一 00:00 的时间戳
  419. // $monday_start = strtotime('last Monday', $now);
  420. // if (date('w', $now) == 1) {
  421. // // 如果今天是周一,直接取今天的 00:00
  422. // $monday_start = strtotime('today', $now);
  423. // }
  424. // // 计算本周日 23:59:59 的时间戳
  425. // $sunday_end = strtotime('next Sunday', $now) + 86399;
  426. // return [$monday_start,$sunday_end];
  427. // }
  428. public function change_user_log($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0)
  429. {
  430. $transition = [
  431. 'pv' => 1,
  432. "gb" => 2,
  433. "df" => 3,
  434. "vr" => 4,
  435. "bt" => 5,
  436. "withdraw_quota" => 6,
  437. "unsettled_pension" => 7,
  438. "amount_old" => 8
  439. ];
  440. $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find();
  441. if (empty($routineInfo)) {
  442. return false;
  443. }
  444. $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0;
  445. if ($appoint_money > 0) {
  446. $alter_money = $appoint_money;
  447. } else {
  448. //再去查询变更前的 金额
  449. $alter_money = Db::name("user")->where('uid', $user_id)->value($type);
  450. }
  451. if ($change_status == 1) {
  452. //新增前
  453. $alter_money = $alter_money - $money;
  454. } else if ($change_status == 2) {
  455. //减少前
  456. $alter_money = $alter_money + $money;
  457. }
  458. if ($alter_money < 0) {
  459. $alter_money = 0;
  460. }
  461. $inser_data = [];
  462. $inser_data['user_id'] = $user_id;
  463. $inser_data['type'] = $transition[$type];
  464. $inser_data['change_status'] = $change_status;
  465. $inser_data['money'] = $money;
  466. $inser_data['routine_log_of_key_id'] = $key_id;
  467. $inser_data['remark'] = $remark;
  468. $inser_data['is_admin'] = $is_admin;
  469. $inser_data['create_time'] = time();
  470. $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0;
  471. Db::name("old_user_log")->insert($inser_data);
  472. }
  473. /**
  474. * 释放待发放佣金
  475. */
  476. public function releaseTobeAmount($user_id, $amount, $output)
  477. {
  478. $userInfo = Db::name("user")->find($user_id);
  479. if (empty($userInfo)) {
  480. return false;
  481. }
  482. $withdraw_quota = empty($userInfo['withdraw_quota_old']) ? 0 : $userInfo['withdraw_quota_old'];
  483. $old_amount = empty($userInfo['tobe_amount_old']) ? 0 : $userInfo['tobe_amount_old'];
  484. Db::name("user")->where("uid", $user_id)->save(['tobe_amount_old' => 0]);
  485. $amount += $old_amount;
  486. $inc_amount = 0;
  487. $inc_tobe_amount = 0;
  488. $dec_withdraw_quota = 0;
  489. if (($amount - $withdraw_quota) > 0) {
  490. $diff_money = $amount - $withdraw_quota;
  491. $inc_amount = $withdraw_quota;
  492. $dec_withdraw_quota = $withdraw_quota;
  493. $inc_tobe_amount = $diff_money;
  494. } elseif (($amount - $withdraw_quota) == 0) {
  495. $inc_amount = $withdraw_quota;
  496. $dec_withdraw_quota = $withdraw_quota;
  497. } else {
  498. // <0的情况
  499. $diff_money = $withdraw_quota - $amount;
  500. $inc_amount = $amount;
  501. $dec_withdraw_quota = $amount;
  502. }
  503. if ($inc_tobe_amount != 0) {
  504. $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();
  505. } else {
  506. $res = Db::name("user")->where("uid", $user_id)->inc("amount_old", $inc_amount)->fetchSql()->dec("withdraw_quota_old", $dec_withdraw_quota)->save();
  507. }
  508. //记录日志 提现额度消费
  509. if ($dec_withdraw_quota > 0) {
  510. $this->change_user_log($user_id, 'withdraw_quota', 2, $dec_withdraw_quota, "conversion_tobe_amount", "佣金额度兑换佣金");
  511. }
  512. }
  513. public function getTime($timePeriod)
  514. {
  515. // 定义时间区间
  516. $now = time();
  517. $startTime = '';
  518. $endTime = '';
  519. if ($timePeriod == 'last_week') {
  520. // 上周的时间区间
  521. $startTime = date('Y-m-d', strtotime('monday last week', $now)); // 上周星期一的开始时间
  522. $endTime = date('Y-m-d', strtotime('sunday last week', $now)); // 上周星期日的结束时间
  523. } elseif ($timePeriod == 'this_week') {
  524. // 这周的时间区间
  525. $startTime = date('Y-m-d', strtotime('monday this week', $now)); // 这周星期一的开始时间
  526. $endTime = date('Y-m-d', strtotime('sunday this week', $now)); // 这周星期日的结束时间
  527. }
  528. // return ['startTime' => '2025-03-10', 'endTime' => '2025-03-23'];
  529. return ['startTime' => $startTime, 'endTime' => $endTime];
  530. }
  531. public function getTeamPvByTime($startTime, $endTime, $removeUserIdList = [])
  532. {
  533. // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
  534. $orders = Db::name("store_order")
  535. ->where('mer_id', 496)
  536. ->whereNotIn('uid', $removeUserIdList)
  537. ->whereIn('status', [0, 1, 2, 3])
  538. ->whereIn("total_price", [1180, 11800, 10620, 2360, 9440])
  539. ->whereBetween("pay_time", [$startTime, $endTime])
  540. ->whereNotNull("pay_time") // 排除 pay_time 为 NULL 的订单
  541. ->select();
  542. // 计算总业绩
  543. $totalPv = 0;
  544. foreach ($orders as $order) {
  545. switch ($order['total_price']) {
  546. case 1180:
  547. $totalPv += 700;
  548. break;
  549. case 11800:
  550. $totalPv += 7000;
  551. break;
  552. case 10620:
  553. $totalPv += 6300;
  554. break;
  555. case 2360:
  556. $totalPv += 1400;
  557. break;
  558. case 9440:
  559. $totalPv += 5600;
  560. break;
  561. }
  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. }