Demo.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\utils\RedisUtil;
  6. /**
  7. * 示例接口
  8. */
  9. class Demo extends Api
  10. {
  11. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  12. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  13. //如果接口已经设置无需登录,那也就无需鉴权了
  14. //
  15. // 无需登录的接口,*表示全部
  16. protected $noNeedLogin = ['*'];
  17. // 无需鉴权的接口,*表示全部
  18. protected $noNeedRight = ['test2'];
  19. /**
  20. * 测试方法
  21. *
  22. * @ApiTitle (测试名称)
  23. * @ApiSummary (测试描述信息)
  24. * @ApiMethod (POST)
  25. * @ApiRoute (/api/demo/test/id/{id}/name/{name})
  26. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  27. * @ApiParams (name="id", type="integer", required=true, description="会员ID")
  28. * @ApiParams (name="name", type="string", required=true, description="用户名")
  29. * @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
  30. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  31. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  32. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  33. * @ApiReturn ({
  34. 'code':'1',
  35. 'msg':'返回成功'
  36. })
  37. */
  38. public function test()
  39. {
  40. $total_amount = 1000; //消费额
  41. $back_rate = 10; //商家比例
  42. $user_id = 1; //消费者id
  43. $shop_id = 1; //门店id
  44. $this->task_bill($total_amount,$back_rate,$user_id,$shop_id,'hotel_order');
  45. }
  46. /**
  47. * 40期限 数据模拟
  48. * @return void
  49. */
  50. public function fenhong40()
  51. {
  52. $qi = 80;// 期数
  53. $jishu = rand(100,99999);// 最初基数
  54. $mqzz = 1.15;// 每期上涨金额倍数
  55. $z = 0;
  56. echo "<style>table{border-collapse: collapse;border:1px solid #ccc;font-size: 14px;margin: 0 auto} tbody{font-size: 12px} th,td{padding: 2px} td{text-align: right} tr:hover{background: #ebf1e7} .bg{background: #e5f5dd}</style>";
  57. echo "<table border='1px'><thead>
  58. <tr>
  59. <th width='50'>期数</th>
  60. <th width='100'>消费(元)</th>
  61. <th width='100'>让利(元)</th>
  62. <th width='100'>商家5倍积分</th>
  63. <th width='100'>用户5倍积分</th>
  64. <th width='110'>发放40%(元)</th>
  65. <th width='100'>商家得</th>
  66. <th width='100'>用户得</th>
  67. <th width='100'>累计总得</th>
  68. </tr>
  69. </thead><tbody>";
  70. // $arr = [];
  71. for ($i = 1; $i <= $qi; $i++) {
  72. $sj5b = bcmul($jishu, 5,2);
  73. $ff40 = bcmul($jishu,'0.01',2);
  74. $ff402 = bcdiv($ff40,'2',2);
  75. $xf = bcmul($jishu,'10',2);
  76. $z = bcadd($z,$ff402,2);
  77. $class = $i%2 == 1 ? 'bg' : '';
  78. echo "<tr class='{$class}'><td style='text-align: center'>{$i}</td><td>{$xf}</td><td>{$jishu}</td><td>{$sj5b}</td><td>{$sj5b}</td><td>{$ff40}</td><td>{$ff402}</td><td>{$ff402}</td><td>{$z}</td></tr>";
  79. //$arr[] = "第{$i}期————消费:{$xf}元—————让利:{$jishu}元—————商家5倍积分:{$sj5b}—————用户5倍积分:{$sj5b}—————发放40%1期:{$ff40}—————商家得:{$ff402}—————用户得:{$ff402}————总得:{$z}";
  80. $jishu = bcmul($jishu,$mqzz,2);
  81. }
  82. echo "</tbody></table>";
  83. }
  84. /*
  85. * 订单额,返利比例
  86. * $total_amount : 订单金额 ,需要计算的订单金额
  87. * $back_rate : 各店铺设置的返佣比例 3-20%
  88. * $user_id : 下单用户id
  89. * $shop_id : 店铺id
  90. * $table_name : hotel_order , hotel_canteen_order , university_event_apply , offline_shop_order
  91. * */
  92. public function task_bill($total_amount,$back_rate,$user_id,$shop_id,$table_name)
  93. {
  94. //3-20%
  95. $amount = bcdiv(bcmul($total_amount,$back_rate,4),100,2);
  96. //5倍
  97. // $back_double = config('site.back_double');
  98. // $amount = bcmul($amount,$back_double,2);
  99. //商务 + 锁客 + 网体 + 省代 + 市代 + 区代 = 40% (大约40%,具体看多少看各级配置)
  100. //商务 发给门店的上级 收益
  101. $shangwu_rate = config('site.back_shangwu');
  102. $shangwu_amount = bcdiv(bcmul($amount,$shangwu_rate,4),100,2);
  103. $shop_info = (new \app\common\business\PaymentBusiness)->getShopInfo($table_name,$shop_id);
  104. if($shop_info && $shop_info['invite_id'] && $shangwu_amount > 0){
  105. //发钱给 $shop_info['invite_id']
  106. }
  107. //锁客 发给消费者的上级 收益
  108. $suoke_rate = config('site.back_suoke');
  109. $suoke_amount = bcdiv(bcmul($amount,$suoke_rate,4),100,2);
  110. $suoke_invite_id = Db::name('user')->where('id',$user_id)->value('invite_id');
  111. if($suoke_invite_id && $suoke_amount > 0){
  112. //发钱给 $suoke_invite_id
  113. }
  114. //网体 业务员逻辑 按月发 收益
  115. //省代,市代,区代 三个代理 都按月发 收益, 放到代理月辅助表
  116. $map_agent = [
  117. 'month_date' => date('Ym'),
  118. 'province_id' => $shop_info['province_id'],
  119. 'city_id' => $shop_info['city_id'],
  120. 'district_id' => $shop_info['district_id'],
  121. ];
  122. $agent_month = Db::name('agent_month')->where($map_agent)->order('id asc')->find();
  123. if(empty($agent_month)){
  124. $map_agent['back_amount'] = $amount;
  125. $map_agent['updatetime'] = time();
  126. $map_agent['status'] = 0;
  127. Db::name('agent_month')->insertGetId($map_agent);
  128. }else{
  129. Db::name('agent_month')->where('id',$agent_month['id'])->update([
  130. 'updatetime' => time(),
  131. 'back_amount' => bcadd($agent_month['back_amount'],$amount,2)
  132. ]);
  133. }
  134. }
  135. //业务员奖
  136. public function yewuyuan(){
  137. $user_id = 1;
  138. $last_month = date('Ym',strtotime(date('Y-m-01')) - 86400);
  139. //我直推线下商家的总数量。
  140. $invite_count = Db::name('offline_shop')->where('invite_id',$user_id)->count();
  141. //我上个月直推线下商家的总数量
  142. $invite_count_mon = Db::name('offline_shop')->where('invite_id',$user_id)->whereTime('back_time','last month')->count();
  143. //我的个人业绩。我所有直推线下商家的3-20%的让利总额
  144. $invite_amount = Db::name('bill')->where('shop_invite_id',$user_id)->where('table_name','offline_shop_order')->where('back_status','neq',0)
  145. ->whereTime('back_time','last month')->sum('back_amount');
  146. //我团队业绩
  147. $invite_uids = $this->get_all_down_uids($user_id);
  148. $invite_amount_tuandui = Db::name('bill')->where('shop_invite_id','IN',$invite_uids)->where('table_name','offline_shop_order')->where('back_status','neq',0)
  149. ->whereTime('back_time','last month')->sum('back_amount');
  150. //dd($last_month,$invite_count,$invite_count_mon,$invite_amount,$invite_amount_tuandui);
  151. //补贴/提成
  152. $butie = 0; //补贴
  153. $yeji = 0; //个人业绩提成
  154. $yingxiao_level = 0;//用户的营销等级
  155. //各级的条件
  156. $level_list = Db::name('yingxiao_level')->order('id asc')->select();
  157. foreach($level_list as $level){
  158. if($level['type'] == 1 || 1==1){
  159. //营销经理
  160. if(1 == 1 || $invite_amount_tuandui >= $level['invite_amount_tuandui'] && $invite_amount >= $level['invite_amount'] && $invite_count >= $level['invite_count']){
  161. $butie = $level['butie'];
  162. $ticheng_rate = $level['ticheng_rate'];
  163. $yeji = bcdiv(bcmul($invite_amount,$ticheng_rate,4),100,2);
  164. $yingxiao_level = $level['id'];
  165. //公司分红(a%平分,b%业绩加权)
  166. $redis_key_a = $last_month . '_yingxiao_' . $yingxiao_level . '_a';
  167. RedisUtil::getInstance($redis_key_a)->lPush($user_id);
  168. //redis左推入当前用户
  169. $redis_key_b = $last_month . '_yingxiao_' . $yingxiao_level . '_b';
  170. RedisUtil::getInstance($redis_key_b)->lPush(json_encode([
  171. 'user_id' => $user_id,'invite_amount_tuandui' => $invite_amount_tuandui,
  172. ]));
  173. break;
  174. }
  175. }else{
  176. //营销员
  177. if($invite_amount >= 60000 && $invite_count_mon >= 5){
  178. $butie = $level['butie'];
  179. $ticheng_rate = $level['ticheng_rate'];
  180. $yeji = bcdiv(bcmul($invite_amount,$ticheng_rate,4),100,2);
  181. $yingxiao_level = $level['id'];
  182. break;
  183. }
  184. }
  185. }
  186. //dd($butie,$yeji,$yingxiao_level);
  187. if($yingxiao_level != 0){
  188. if($butie > 0){
  189. //给 $user_id 发 补贴
  190. }
  191. if($yeji > 0){
  192. //给 $user_id 发 个人业绩
  193. }
  194. }
  195. }
  196. //获取我的团队用户ids
  197. private function get_all_down_uids($user_id){
  198. return $user_id;
  199. }
  200. //业务员 a%评分,b%业绩加权
  201. public function yewuyuan_ab(){
  202. //上个月的公司分红1% 。酒店,餐厅,老年大学活动,线下,商城,所有让利总额,作为基数
  203. $back_amount_sum = Db::name('bill')->whereTime('back_time','last month')->where('back_status','neq',0)->sum('back_amount');
  204. if($back_amount_sum <= 0){
  205. echo '基数为0结束';exit;
  206. return true;
  207. }
  208. dump('总基数'.$back_amount_sum);
  209. $yingxiao_level = Db::name('yingxiao_level')->where('id','IN','1,2')->column('id,fenhong,pingfen,jiaquan');
  210. //百分之一
  211. $base_1 = bcdiv(bcmul($back_amount_sum,$yingxiao_level[1]['fenhong'],4),100,4);
  212. $base_2 = bcdiv(bcmul($back_amount_sum,$yingxiao_level[2]['fenhong'],4),100,4);
  213. dump('百分之一'.$base_1);
  214. dump('百分之一'.$base_2);
  215. //两个ab基数
  216. $back_amount_1_a = bcdiv(bcmul($base_1,$yingxiao_level[1]['pingfen'],4),100,4);
  217. $back_amount_1_b = bcdiv(bcmul($base_1,$yingxiao_level[1]['jiaquan'],4),100,4);
  218. $back_amount_2_a = bcdiv(bcmul($base_2,$yingxiao_level[2]['pingfen'],4),100,4);
  219. $back_amount_2_b = bcdiv(bcmul($base_2,$yingxiao_level[2]['jiaquan'],4),100,4);
  220. dd($base_1,$base_2,$back_amount_1_a,$back_amount_1_b,$back_amount_2_a,$back_amount_2_b);
  221. //两个ab奖
  222. $last_month = date('Ym',strtotime(date('Y-m-01')) - 86400);
  223. $redis_key_1_a = $last_month . '_yingxiao_1_a';
  224. $redis_key_1_b = $last_month . '_yingxiao_1_b';
  225. $redis_key_2_a = $last_month . '_yingxiao_2_a';
  226. $redis_key_2_b = $last_month . '_yingxiao_2_b';
  227. $list_1_a = RedisUtil::getInstance($redis_key_1_a)->LRANGE();
  228. $list_1_b = RedisUtil::getInstance($redis_key_1_b)->LRANGE();
  229. $list_2_a = RedisUtil::getInstance($redis_key_2_a)->LRANGE();
  230. $list_2_b = RedisUtil::getInstance($redis_key_2_b)->LRANGE();
  231. if(!empty($list_1_a)){
  232. $price_1_a = bcdiv($back_amount_1_a,count($list_1_a),2);
  233. foreach($list_1_a as $item_1_a){
  234. dump($item_1_a);
  235. dump($price_1_a);
  236. //给用户id:$a1发钱
  237. //
  238. }
  239. }
  240. dump($list_1_b);
  241. if(!empty($list_1_b)){
  242. //总额
  243. foreach($list_1_b as $k_1_b => $v_1_b){
  244. $list_1_b[$k_1_b] = json_decode($v_1_b,true);
  245. }
  246. $total_1_b = array_sum(array_column($list_1_b,'invite_amount_tuandui'));
  247. dump($total_1_b);
  248. foreach($list_1_b as $v_1_b){
  249. dump($v_1_b['user_id']);
  250. $price_1_b = bcmul($back_amount_1_b,bcdiv($v_1_b['invite_amount_tuandui'],$total_1_b,4),2);
  251. dump($price_1_b);
  252. }
  253. }
  254. if(!empty($list_2_a)){
  255. $price_2_a = bcdiv($back_amount_2_a,count($list_2_a),2);
  256. foreach($list_2_a as $item_2_a){
  257. dump($item_2_a);
  258. dump($price_2_a);
  259. //给用户id:$a2发钱
  260. //
  261. }
  262. }
  263. dump($list_2_b);
  264. if(!empty($list_2_b)){
  265. //总额
  266. foreach($list_2_b as $k_2_b => $v_2_b){
  267. $list_2_b[$k_2_b] = json_decode($v_2_b,true);
  268. }
  269. $total_2_b = array_sum(array_column($list_2_b,'invite_amount_tuandui'));
  270. dump($total_2_b);
  271. foreach($list_2_b as $v_2_b){
  272. dump($v_2_b['user_id']);
  273. $price_2_b = bcmul($back_amount_2_b,bcdiv($v_2_b['invite_amount_tuandui'],$total_2_b,4),2);
  274. dump($price_2_b);
  275. }
  276. }
  277. }
  278. //每月初执行,跑代理辅助表,把上个月的数据全部推送到 代理队列
  279. public function task_agent(){
  280. $last_month = date('Ym',strtotime(date('Y-m-01')) - 86400); //上个月一号,202502
  281. $agent_month = Db::name('agent_month')->where('month_date',$last_month)->where('status',0)->column('id');
  282. //推送到代理job
  283. //修改状态
  284. Db::name('agent_month')->where('id','IN',$agent_month)->update([
  285. 'status' => 1,
  286. 'updatetime' => time(),
  287. ]);
  288. }
  289. //代理job,跑完代理队列里的每一条
  290. public function job_agent($agent_month_id){
  291. $agent_month = Db::name('agent_month')->where('id',$agent_month_id)->find();
  292. if($agent_month['status'] != 1){
  293. //结束
  294. return;
  295. }
  296. $area = Db::name('shopro_data_area')
  297. ->where('id','IN',[$agent_month['province_id'],$agent_month['city_id'],$agent_month['district_id']])
  298. ->where('user_id','>',0)
  299. ->where('back_rate','>',0)
  300. ->select();
  301. if(empty($area)){
  302. //结束
  303. return;
  304. }
  305. //省市县执行三次
  306. foreach($area as $key => $agent){
  307. $agent_money = bcdiv(bcmul($agent_month['back_amount'],$agent['back_rate'],2),100,2);
  308. //发钱
  309. if($agent_money > 0){
  310. $agent['user_id'];
  311. }
  312. }
  313. //状态改为已发放
  314. Db::name('agent_month')->where('id',$agent_month_id)->update([
  315. 'status' => '2',
  316. 'exec_time' => time(),
  317. ]);
  318. }
  319. //注册时设置推荐人,顺带修改网体
  320. //已经放到注册里了
  321. public function set_invite($invite_id = 13){
  322. $intro = Db::name('user')->where('id',$invite_id)->field('id,invite_id,intro_level,intro_ids')->find();
  323. if(!$intro) {
  324. echo '无效推荐人';
  325. }
  326. $data_reg['invite_id'] = $intro['id']; //推荐人id
  327. $data_reg['intro_ids'] = $intro['intro_ids'] ? ($intro['intro_ids'].','.$intro['id']) : $intro['id']; //所有上级
  328. if(!empty($data_reg['intro_ids'])) {
  329. $data_reg['intro_ids_new'] = ','.$data_reg['intro_ids'].','; //便于查询所有下级
  330. }
  331. $data_reg['intro_level'] = $intro['intro_level'] + 1; //推荐网层数
  332. //修改用户
  333. Db::name('user')->where('id',$this->auth->id)->update($data_reg);
  334. //更新直推数和团队数
  335. model('Intro')->addIntroNum($intro['id'], 1, 1);
  336. echo '结束';
  337. }
  338. /**
  339. * 1000为起点,循环40次15%累加,入库
  340. *
  341. */
  342. public function test2()
  343. {
  344. echo 111;exit;
  345. $amount = 1000;
  346. $rate = 15;
  347. $data = [];
  348. for($i=1;$i<=40;$i++){
  349. $data[] = [
  350. 'amount' => $amount,
  351. ];
  352. $amount = bcdiv(bcmul($amount,$rate,2),100,2);
  353. }
  354. dump($data);
  355. // Db::name('back_base')->insertAll($data);
  356. }
  357. /**
  358. * 需要登录且需要验证有相应组的权限
  359. *
  360. */
  361. public function test3()
  362. {
  363. //$this->success('返回成功', ['action' => 'test3']);
  364. $data = [
  365. 'order_paidnum' => ['inc',1],
  366. 'order_total_amount' => ['inc',11.25],
  367. 'goods_sales' => ['inc',5],
  368. ];
  369. Db::name('live_room_log')->where('id',23)->update($data);
  370. }
  371. }