Demo.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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('create_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('createtime','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('createtime','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_a_' . $yingxiao_level;
  129. RedisUtil::getInstance($redis_key_a)->lPush($user_id);
  130. //redis左推入当前用户
  131. $redis_key_b = $last_month . '_yingxiao_b_' . $yingxiao_level;
  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. private function get_all_down_uids($user_id){
  159. return $user_id;
  160. }
  161. //每月初执行,跑代理辅助表,把上个月的数据全部推送到 代理队列
  162. public function task_agent(){
  163. $last_month = date('Ym',strtotime(date('Y-m-01')) - 86400); //上个月一号,202502
  164. $agent_month = Db::name('agent_month')->where('month_date',$last_month)->where('status',0)->column('id');
  165. //推送到代理job
  166. //修改状态
  167. Db::name('agent_month')->where('id','IN',$agent_month)->update([
  168. 'status' => 1,
  169. 'updatetime' => time(),
  170. ]);
  171. }
  172. //代理job,跑完代理队列里的每一条
  173. public function job_agent($agent_month_id){
  174. $agent_month = Db::name('agent_month')->where('id',$agent_month_id)->find();
  175. if($agent_month['status'] != 1){
  176. //结束
  177. return;
  178. }
  179. $area = Db::name('shopro_data_area')
  180. ->where('id','IN',[$agent_month['province_id'],$agent_month['city_id'],$agent_month['district_id']])
  181. ->where('user_id','>',0)
  182. ->where('back_rate','>',0)
  183. ->select();
  184. if(empty($area)){
  185. //结束
  186. return;
  187. }
  188. //省市县执行三次
  189. foreach($area as $key => $agent){
  190. $agent_money = bcdiv(bcmul($agent_month['back_amount'],$agent['back_rate'],2),100,2);
  191. //发钱
  192. if($agent_money > 0){
  193. $agent['user_id'];
  194. }
  195. }
  196. //状态改为已发放
  197. Db::name('agent_month')->where('id',$agent_month_id)->update([
  198. 'status' => '2',
  199. 'exec_time' => time(),
  200. ]);
  201. }
  202. /**
  203. * 需要登录的接口
  204. *
  205. */
  206. public function test2()
  207. {
  208. echo 111;exit;
  209. $amount = 1000;
  210. $rate = 15;
  211. $data = [];
  212. for($i=1;$i<=40;$i++){
  213. $data[] = [
  214. 'amount' => $amount,
  215. ];
  216. $amount = bcdiv(bcmul($amount,$rate,2),100,2);
  217. }
  218. dump($data);
  219. // Db::name('back_base')->insertAll($data);
  220. }
  221. /**
  222. * 需要登录且需要验证有相应组的权限
  223. *
  224. */
  225. public function test3()
  226. {
  227. //$this->success('返回成功', ['action' => 'test3']);
  228. $data = [
  229. 'order_paidnum' => ['inc',1],
  230. 'order_total_amount' => ['inc',11.25],
  231. 'goods_sales' => ['inc',5],
  232. ];
  233. Db::name('live_room_log')->where('id',23)->update($data);
  234. }
  235. }