Demo.php 14 KB

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