Taskorderlianc.php 31 KB

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