Taskorderlianc.php 31 KB

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