Demo.php 15 KB

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