Taskorderlianc.php 31 KB

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