Demo.php 16 KB

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