Users.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. namespace app\controller\api\entropybarter;
  3. use alipay\aop\AlipayConfig;
  4. use alipay\aop\AopCertClient;
  5. use alipay\aop\request\AlipayTradePrecreateRequest;
  6. use alipay\aop\request\AlipayFundTransUniTransferRequest;
  7. use Endroid\QrCode\QrCode;
  8. use think\db\Where;
  9. use think\Exception;
  10. use think\facade\Cache;
  11. use think\facade\Db;
  12. class Users
  13. {
  14. /**12171
  15. * @param $order
  16. * @param $type 1 线上 2线下
  17. * 订单
  18. * 修改用户 类型 比例 完成
  19. */
  20. public function order($order, $type)
  21. {
  22. try {
  23. // Db::name('log')->insert(['data'=>'user._______'.json_encode($order)]);
  24. $mer_id=$order['mer_id'];
  25. // Db::name('log')->insert(['data'=>'商户id0'.$mer_id]);
  26. if($mer_id=='290'){
  27. return $this->pay_orrder_290($order,$type);
  28. }elseif($mer_id=='236'){
  29. // Db::name('log')->insert(['data'=>'商户id1'.$mer_id]);
  30. try {
  31. $order_236=new Order_236();
  32. // Db::name('log')->insert(['data'=>'商户id2'.json_encode($order_236)]);
  33. return $order_236-> order($order, $type);
  34. }catch (\Exception $e) {
  35. Db::name('log')->insert(['data'=>'地宝订单支付出错:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  36. }
  37. }
  38. }catch (\Exception $e) {
  39. Db::name('log')->insert(['data'=>'地宝订单支付出错lll:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  40. }
  41. $user=Db::name('entropy_barter_user_'.$mer_id)->where('uid',$order['uid'])->find();
  42. $update=[];
  43. $update_config=[];
  44. //增加消费总额/积分
  45. $weizhi=strpos($order['money'],'.');
  46. if($weizhi){
  47. $money=substr($order['money'],'0',$weizhi);
  48. }else{
  49. $money=$order['money'];
  50. }
  51. $update_user=[
  52. 'cumulative_money'=>bcadd($order['money'],$user['cumulative_money'],2),
  53. ];
  54. // if($mer_id=='276'||$mer_id=='236'){
  55. // if($order['money']>=1){
  56. // if($user['first_consumption_one']==1){
  57. // if($type==2){
  58. //
  59. // $bill=[
  60. // 'from_id'=>'0',
  61. // 'number'=>100,
  62. // 'to_id'=>$user['eb_id'],
  63. // 'type'=>'1',
  64. // 'order_no'=>$order['order_no'],
  65. // 'remarks'=>'首次消费满1元赠送100积分',
  66. // 'pm'=>1,
  67. // 'status'=>2,
  68. // 'on_line'=>2,
  69. // 'mer_id'=>$mer_id
  70. // ];
  71. // $this->eb_user_bill($bill);
  72. // Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$user['eb_id'])->inc('integral',100)->update();
  73. // Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$user['eb_id'])->inc('first_consumption_one',1)->update();
  74. //
  75. // }else{
  76. //
  77. // $bill=[
  78. // 'from_id'=>'0',
  79. // 'number'=>100,
  80. // 'to_id'=>$user['eb_id'],
  81. // 'type'=>'1',
  82. // 'order_no'=>$order['order_no'],
  83. // 'remarks'=>'首次消费满1元赠送100积分',
  84. // 'pm'=>1,
  85. // 'status'=>1,
  86. // 'on_line'=>1,
  87. // 'mer_id'=>$mer_id
  88. // ];
  89. // $this->eb_user_bill($bill);
  90. // Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$user['eb_id'])->inc('first_consumption_one',1)->update();
  91. // }
  92. //
  93. // }
  94. // }
  95. // }
  96. if($user['type']==1){
  97. if($update_user['cumulative_money']>=6000){
  98. $update['integral']='6000';
  99. }
  100. }
  101. $reward_one=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_one')->find();
  102. if($update_user['cumulative_money']>=$reward_one||$mer_id=='236'){
  103. if($type==2){
  104. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$user['eb_id'])->update($update_user);
  105. $bill=[
  106. 'from_id'=>'0',
  107. 'number'=>$money,
  108. 'to_id'=>$user['eb_id'],
  109. 'type'=>'1',
  110. 'order_no'=>$order['order_no'],
  111. 'remarks'=>'消费得积分',
  112. 'pm'=>1,
  113. 'status'=>2,
  114. 'on_line'=>2,
  115. 'mer_id'=>$mer_id
  116. ];
  117. $this->eb_user_bill($bill);
  118. }else{
  119. $bill=[
  120. 'from_id'=>'0',
  121. 'number'=>$money,
  122. 'to_id'=>$user['eb_id'],
  123. 'type'=>'1',
  124. 'order_no'=>$order['order_no'],
  125. 'remarks'=>'消费得积分',
  126. 'pm'=>1,
  127. 'status'=>1,
  128. 'on_line'=>1,
  129. 'mer_id'=>$mer_id
  130. ];
  131. $this->eb_user_bill($bill);
  132. }
  133. }
  134. $eb_user=Db::name('entropy_barter_user_'.$mer_id)->where('uid',$order['uid'])->find();
  135. $reward_two=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_two')->find();
  136. $reward_three=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_three')->find();
  137. $reward_four=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_four')->find();
  138. // Db::name('log')->insert(['data'=>'这是我总共的消费'.$eb_user['cumulative_money']]);
  139. // Db::name('log')->insert(['data'=>'1'.json_encode($reward_one)]);
  140. // Db::name('log')->insert(['data'=>'2'.json_encode($reward_two)]);
  141. // Db::name('log')->insert(['data'=>'3'.json_encode($reward_three)]);
  142. // Db::name('log')->insert(['data'=>'4'.json_encode($reward_four)]);
  143. if($eb_user['cumulative_money']>=$reward_one['value'] and $eb_user['cumulative_money']<$reward_two['value']){
  144. $update['type']=2;
  145. $update_config['value']=$reward_one['id'];
  146. }elseif ($eb_user['cumulative_money']>=$reward_two['value'] and $eb_user['cumulative_money']<$reward_three['value']){
  147. $update['type']=3;
  148. $update_config['value']=$reward_two['id'];
  149. }elseif ($eb_user['cumulative_money']>=$reward_three['value'] and $eb_user['cumulative_money']<$reward_four['value']){
  150. $update['type']=4;
  151. $update_config['value']=$reward_three['id'];
  152. }elseif ($eb_user['cumulative_money']>=$reward_four['value']){
  153. $update['type']=5;
  154. $update_config['value']=$reward_four['id'];
  155. }
  156. //修改此用户佣金比例
  157. if($type==2){
  158. Db::name('entropy_barter_config_user_'.$mer_id)->where('eb_id',$user['eb_id'])->update($update_config);
  159. //修改此用户类型 2 会员 3经理 4 总监 5 总裁
  160. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$user['eb_id'])->update($update);
  161. }
  162. //是否有上级
  163. if($user['top_eb_id']){
  164. $top_user=Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$user['top_eb_id'])->find();
  165. $wallet=bcmul($order['money'],bcmul(30,0.01,2),2);
  166. if($top_user['integral']>=$wallet){
  167. if($type==2){
  168. $bill=[
  169. 'from_id'=>'0',
  170. 'number'=>$wallet,
  171. 'to_id'=>$top_user['eb_id'],
  172. 'type'=>'2',
  173. 'order_no'=>$order['order_no'],
  174. 'remarks'=>'直推佣金',
  175. 'pm'=>1,
  176. 'status'=>2,
  177. 'on_line'=>2,
  178. 'mer_id'=>$mer_id
  179. ];
  180. $this->eb_user_bill($bill);
  181. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$top_user['eb_id'])->inc('wallet',$wallet)->update();
  182. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$top_user['eb_id'])->dec('integral',$wallet)->update();
  183. }else{
  184. $bill=[
  185. 'from_id'=>'0',
  186. 'number'=>$wallet,
  187. 'to_id'=>$top_user['eb_id'],
  188. 'type'=>'2',
  189. 'order_no'=>$order['order_no'],
  190. 'remarks'=>'直推佣金',
  191. 'pm'=>1,
  192. 'status'=>1,
  193. 'on_line'=>1,
  194. 'mer_id'=>$mer_id
  195. ];
  196. $this->eb_user_bill($bill);
  197. }
  198. }
  199. $top_level=Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$top_user['top_eb_id'])->find();
  200. if($top_level){
  201. $table_name='entropy_barter_config_'.$mer_id;
  202. $cproportion=Db::name('entropy_barter_config_user_'.$mer_id)
  203. ->alias('ebcu')
  204. ->leftJoin($table_name .' ebc','ebcu.value=ebc.id')
  205. ->where('eb_id',$top_level['eb_id'])
  206. ->field('ebc.proportion')
  207. ->find();
  208. $integral=bcmul($money,bcmul($cproportion['proportion'],'0.01',2),2);
  209. if($integral>0){
  210. if($type==2) {
  211. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$top_level['eb_id'])->inc('integral',$integral)->update();
  212. $bill=[
  213. 'from_id'=>'0',
  214. 'number'=>$integral,
  215. 'to_id'=>$top_level['eb_id'],
  216. 'type'=>'2',
  217. 'order_no'=>$order['order_no'],
  218. 'remarks'=>'团队积分奖励',
  219. 'pm'=>1,
  220. 'status'=>2,
  221. 'on_line'=>2,
  222. 'mer_id'=>$mer_id
  223. ];
  224. $this->eb_user_bill($bill);
  225. }else{
  226. $bill=[
  227. 'from_id'=>'0',
  228. 'number'=>$integral,
  229. 'to_id'=>$top_level['eb_id'],
  230. 'type'=>'1',
  231. 'order_no'=>$order['order_no'],
  232. 'remarks'=>'团队积分奖励',
  233. 'pm'=>1,
  234. 'status'=>1,
  235. 'on_line'=>1,
  236. 'mer_id'=>$mer_id
  237. ];
  238. $this->eb_user_bill($bill);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. //账单记录
  245. public function eb_user_bill($bill)
  246. {
  247. $insert=[
  248. 'from_id'=>$bill['from_id'],
  249. 'number'=>$bill['number'],
  250. 'to_id'=>$bill['to_id'],
  251. 'type'=>$bill['type'],
  252. 'order_no'=>$bill['order_no'],
  253. 'remarks'=>$bill['remarks'],
  254. 'pm'=>$bill['pm'],
  255. 'ctime'=>time(),
  256. 'on_line'=>$bill['on_line'],
  257. 'status'=>$bill['status'],
  258. 'pm2_msg'=>$bill['pm2_msg']??'',
  259. 'mer_id'=>$bill['mer_id'],
  260. 'commission_scale'=>$bill['commission_scale']??0,
  261. 'commission_type'=>$bill['commission_type']??0,
  262. 'to_user_type'=>$bill['to_user_type']??'',
  263. 'to_user_type_1'=>$bill['to_user_type_1']??'',
  264. ];
  265. // Db::name('log')->insert(['data'=>json_encode($insert)]);
  266. Db::name('entropy_barter_bill')->insert($insert);
  267. }
  268. //确认收货
  269. public function take($order_no)
  270. {
  271. // Db::name('log')->insert(['data'=>'易熵易物我走进来了']);
  272. try {
  273. $mer_id=Db::name('store_order')->where('order_sn',$order_no)->value('mer_id');
  274. if($mer_id=='290'){
  275. $this->take_290($order_no);
  276. return '';
  277. }
  278. $data=Db::name('entropy_barter_bill')->where('mer_id',$mer_id)->where('order_no',$order_no)->select();
  279. $order_data=Db::name('store_order')->where('order_sn',$order_no)->find();
  280. if($order_data['pay_price']>=1){
  281. $true=true;
  282. }else{
  283. $true=false;
  284. }
  285. // Db::name('log')->insert(['data'=>'数据---'.json_encode($data)]);
  286. if($data->isEmpty()){
  287. return '12';
  288. }
  289. $data=$data->toArray();
  290. foreach ($data as $k=>$v){
  291. // if($true){
  292. // Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$v['to_id'])->inc('integral',100)->update();
  293. // }
  294. $true=false;
  295. if($v['status']=='1'){
  296. if($v['type']==2){
  297. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$v['to_id'])->inc('wallet',$v['number'])->update();
  298. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$v['to_id'])->dec('integral',$v['number'])->update();
  299. Db::name('entropy_barter_bill')->where('id',$v['id'])->update(['status'=>2]);
  300. }elseif ($v['type']==1){
  301. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$v['to_id'])->inc('integral',$v['number'])->update();
  302. Db::name('entropy_barter_bill')->where('id',$v['id'])->update(['status'=>2]);
  303. }
  304. }
  305. }
  306. $uid=Db::name('store_order')->where('order_sn',$order_no)->value('uid');
  307. $eb_user=Db::name('entropy_barter_user_'.$mer_id)->where('uid',$uid)->find();
  308. $reward_one=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_one')->find();
  309. $reward_two=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_two')->find();
  310. $reward_three=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_three')->find();
  311. $reward_four=Db::name('entropy_barter_config_'.$mer_id)->where('key','reward_four')->find();
  312. // Db::name('log')->insert(['data'=>'这是我总共的消费'.$eb_user['cumulative_money']]);
  313. // Db::name('log')->insert(['data'=>'1'.json_encode($reward_one)]);
  314. // Db::name('log')->insert(['data'=>'2'.json_encode($reward_two)]);
  315. // Db::name('log')->insert(['data'=>'3'.json_encode($reward_three)]);
  316. // Db::name('log')->insert(['data'=>'4'.json_encode($reward_four)]);
  317. $update_config=[];
  318. $update=[];
  319. if($eb_user['cumulative_money']>=$reward_one['value'] and $eb_user['cumulative_money']<$reward_two['value']){
  320. $update['type']=2;
  321. $update_config['value']=$reward_one['id'];
  322. }elseif ($eb_user['cumulative_money']>=$reward_two['value'] and $eb_user['cumulative_money']<$reward_three['value']){
  323. $update['type']=3;
  324. $update_config['value']=$reward_two['id'];
  325. }elseif ($eb_user['cumulative_money']>=$reward_three['value'] and $eb_user['cumulative_money']<$reward_four['value']){
  326. $update['type']=4;
  327. $update_config['value']=$reward_three['id'];
  328. }elseif ($eb_user['cumulative_money']>=$reward_four['value']){
  329. $update['type']=5;
  330. $update_config['value']=$reward_four['id'];
  331. }
  332. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$eb_user['eb_id'])->update(['cumulative_money'=>bcadd($eb_user['cumulative_money'],$order_data['pay_price'],2)]);
  333. //修改此用户佣金比例
  334. Db::name('entropy_barter_config_user_'.$mer_id)->where('eb_id',$eb_user['eb_id'])->update($update_config);
  335. //修改此用户类型 2 会员 3经理 4 总监 5 总裁
  336. Db::name('entropy_barter_user_'.$mer_id)->where('eb_id',$eb_user['eb_id'])->update($update);
  337. }catch (Exception $exception){
  338. Db::name("log")->insert(array('data'=>$exception->getFile().':'.$exception->getLine().'【'.$exception->getMessage().'】'));//日志记录
  339. }
  340. }
  341. //国腾名医单独处理
  342. public function pay_orrder_290($order,$type)
  343. {
  344. $uid=$order['uid'];
  345. $table_name='entropy_barter_user_290';
  346. $order=Db::name('store_order')->where('order_sn',$order['order_no'])->find();
  347. $order['order_no']=$order['order_sn'];
  348. $product=DB::name('store_cart')->where('cart_id',$order['cart_id'])->value('product_id');
  349. $imUserData=Db::name($table_name)->where('uid',$uid)->find();
  350. $top_data=Db::name($table_name)->where('eb_id',$imUserData['top_eb_id'])->find();
  351. if ($product=='362'||$product=='364'||$product=='363') {
  352. Db::name('store_order')->where('order_sn',$order['order_no'])->update(['is_split_to_tomorrow'=>1,'status'=>3]);
  353. if ($product == '362') {
  354. Db::name($table_name)->where('eb_id', $imUserData['eb_id'])->update(['type' => 1, 'pay_goods_type' => 1,'pay_status'=>1]);
  355. } else {
  356. Db::name($table_name)->where('eb_id', $imUserData['eb_id'])->update(['type' => 2, 'pay_goods_type' => 2,'pay_status'=>1]);
  357. }
  358. }
  359. /**
  360. * 奖励模式开始
  361. */
  362. Db::name('user_bill')->where('order_sn',$order['order_no'])->where('uid',$order['uid'])->where('source',0)->update(['status'=>1]);
  363. $order['money']=$order['pay_price'];
  364. $eb_one=$this->one_jiangli($top_data,$order,$table_name,$type);
  365. if(!$eb_one){
  366. if($imUserData['pay_status']!=1){
  367. $this->take_290($order['order_no'],1);
  368. }else{
  369. // Db::name('log')->insert(['data'=>'已购买过合伙人商品或者创始人商品']);
  370. }
  371. return false;
  372. }
  373. //one_jiangli 推荐奖给了"它"
  374. $eb_two=$this->two_jiangli($top_data,$order,$table_name,$type);
  375. if(!$eb_two){
  376. if($imUserData['pay_status']!=1){
  377. $this->take_290($order['order_no'],1);
  378. }else{
  379. // Db::name('log')->insert(['data'=>'已购买过合伙人商品或者创始人商品']);
  380. }
  381. return false;
  382. }
  383. //two_jiangli 推荐奖给了"它"
  384. $eb_three=$this->three_jiangli($eb_two,$order,$table_name,$type);
  385. if(!$eb_three){
  386. if($imUserData['pay_status']!=1){
  387. $this->take_290($order['order_no'],1);
  388. }else{
  389. // Db::name('log')->insert(['data'=>'已购买过合伙人商品或者创始人商品']);
  390. }
  391. return false;
  392. }
  393. //three_jiangli 推荐奖给了"它"
  394. if($eb_one['eb_id']==$eb_two['eb_id']){
  395. $eb_four=$this->four_jiangli($eb_three,$order,$table_name,$type);
  396. }
  397. /**
  398. * 奖励模式结束
  399. */
  400. }
  401. //推荐奖
  402. public function one_jiangli($top_data,$order,$table_name,$type)
  403. {
  404. // dd($top_data['pay_status']==1&&$top_data['type']>2);
  405. if($top_data['pay_status']==1&&$top_data['type']>2){
  406. $proportion=$this->reward_proportion($top_data['type']);
  407. $money=bcmul($order['money'],"0.3",2);
  408. $msg=$proportion['msg'].':推荐奖励';
  409. $bill_data=[
  410. 'from_id'=>0,
  411. 'to_id'=>$top_data['eb_id'],
  412. 'type'=>2,
  413. 'order_no'=>$order['order_no'],
  414. 'remarks'=>$msg,
  415. 'ctime'=>time(),
  416. 'number'=>$money,//商业合伙人或联合创始人奖励为30%
  417. 'pm'=>1,
  418. 'status'=>'2',
  419. 'on_line'=>$type,
  420. 'mer_id'=>290,
  421. 'commission_type'=>1,
  422. 'commission_scale'=>30,
  423. 'to_user_type'=>1,
  424. 'to_user_type_1'=>$proportion['type'],
  425. ];
  426. //增加账单记录
  427. $this->eb_user_bill($bill_data);
  428. Db::name($table_name)->where('eb_id',$top_data['eb_id'])->inc('wallet',$money)->update();
  429. Db::name($table_name)->where('eb_id',$top_data['eb_id'])->inc('ranking_money',$money)->update();
  430. $data=[
  431. 'type'=>$top_data['type'],
  432. 'eb_id'=>$top_data['eb_id']
  433. ];
  434. }
  435. else{
  436. // if($top_data['type']>=1){
  437. // $money=bcmul($order['money'],"0.3",2);
  438. // Db::name($table_name)->where('eb_id',$top_data['eb_id'])->inc('precipitate',$money)->update();
  439. // $data=[
  440. // 'type'=>$top_data['type'],
  441. // 'eb_id'=>$top_data['eb_id']
  442. // ];
  443. // }
  444. $data=[12313];
  445. }
  446. return $data;
  447. }
  448. //一级奖励
  449. public function two_jiangli($top_data,$order,$table_name,$type)
  450. {
  451. // dd($top_data['type']>2);
  452. if($top_data['type']>2){
  453. $proportion=$this->reward_proportion($top_data['type']);
  454. $money=bcmul($order['money'],$proportion['proportion'],2);
  455. $msg=$proportion['msg'].':管理奖励';
  456. $bill_data=[
  457. 'from_id'=>0,
  458. 'to_id'=>$top_data['eb_id'],
  459. 'type'=>2,
  460. 'order_no'=>$order['order_no'],
  461. 'remarks'=>$msg,
  462. 'ctime'=>time(),
  463. 'number'=>$money,//商业合伙人或联合创始人奖励为30%
  464. 'pm'=>1,
  465. 'status'=>'2',
  466. 'on_line'=>$type,
  467. 'mer_id'=>290,
  468. 'commission_type'=>1,
  469. 'commission_scale'=>$proportion['proportion']*100,
  470. 'to_user_type'=>2,
  471. 'to_user_type_1'=>$proportion['type'],
  472. ];
  473. //增加账单记录
  474. $this->eb_user_bill($bill_data);
  475. Db::name($table_name)->where('eb_id',$top_data['eb_id'])->inc('wallet',$money)->update();
  476. Db::name($table_name)->where('eb_id',$top_data['eb_id'])->inc('ranking_money',$money)->update();
  477. $data=[
  478. 'eb_id'=>$top_data['eb_id'],
  479. 'type'=>$top_data['type']
  480. ];
  481. }else{
  482. $top_1_id=$this->recursion_top($top_data['top_eb_id'],'0');
  483. if($top_1_id==0){
  484. return false;
  485. }
  486. $top_1_data=Db::name($table_name)->where('eb_id',$top_1_id)->find();
  487. $proportion=$this->reward_proportion($top_1_data['type']);
  488. $money=bcmul($order['money'],$proportion['proportion'],2);
  489. $msg=$proportion['msg'].':管理奖励';
  490. $bill_data=[
  491. 'from_id'=>0,
  492. 'to_id'=>$top_1_data['eb_id'],
  493. 'type'=>2,
  494. 'order_no'=>$order['order_no'],
  495. 'remarks'=>$msg,
  496. 'ctime'=>time(),
  497. 'number'=>$money,//商业合伙人或联合创始人奖励为30%
  498. 'pm'=>1,
  499. 'status'=>'2',
  500. 'on_line'=>$type,
  501. 'mer_id'=>290,
  502. 'commission_type'=>1,
  503. 'commission_scale'=>$proportion['proportion']*100,
  504. 'to_user_type'=>2,
  505. 'to_user_type_1'=>$proportion['type'],
  506. ];
  507. //增加账单记录
  508. $this->eb_user_bill($bill_data);
  509. Db::name($table_name)->where('eb_id',$top_1_data['eb_id'])->inc('wallet',$money)->update();
  510. Db::name($table_name)->where('eb_id',$top_1_data['eb_id'])->inc('ranking_money',$money)->update();
  511. $data=[
  512. 'type'=>$top_1_data['type'],
  513. 'eb_id'=>$top_1_data['eb_id']
  514. ];
  515. }
  516. return $data;
  517. }
  518. //二级奖励
  519. public function three_jiangli($top_data,$order,$table_name,$type)
  520. {
  521. $top=$top_data;
  522. $top_data=Db::name($table_name)->where('eb_id',$top['eb_id'])->find();
  523. $top_top_data=Db::name($table_name)->where('eb_id',$top_data['top_eb_id'])->find();
  524. //// dd($top_top_data>2);
  525. // dd($top['type']==$top_top_data['type']||$top['type']>$top_top_data['type']);
  526. if($top_top_data['type']>2){
  527. if($top['type']==$top_top_data['type']||$top['type']>$top_top_data['type']){
  528. $proportion=$this->reward_proportion($top_top_data['type']);
  529. $proportion['proportion']='0.02';
  530. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,3);
  531. }elseif($top['type']<$top_top_data['type']){
  532. if($top_top_data['type']==4){
  533. $proportion=$this->reward_proportion($top_top_data['type']);
  534. $proportion['proportion']='0.06';
  535. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,3);
  536. }elseif($top_top_data['type']==5){
  537. if($top['type']==3){
  538. $proportion=$this->reward_proportion($top_top_data['type']);
  539. $proportion['proportion']='15';
  540. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,3);
  541. return false;
  542. }else{
  543. $proportion=$this->reward_proportion($top_top_data['type']);
  544. $proportion['proportion']='0.09';
  545. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,3);
  546. }
  547. }elseif($top_top_data['type']==3){
  548. $proportion=$this->reward_proportion($top_top_data['type']);
  549. $proportion['proportion']='0.03';
  550. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,3);
  551. }
  552. }
  553. $data=[
  554. 'eb_id'=>$top_top_data['eb_id'],
  555. 'type'=>$top_top_data['type']
  556. ];
  557. }else{
  558. $top_1_id=$this->recursion_top($top_data['top_eb_id'],'0');
  559. if($top_1_id==0){
  560. return false;
  561. }
  562. $top_1_data=Db::name($table_name)->where('eb_id',$top_1_id)->find();
  563. if($top['type']==$top_1_data['type']||$top['type']>$top_1_data['type']){
  564. $proportion=$this->reward_proportion($top_1_data['type']);
  565. $proportion['proportion']='0.02';
  566. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_1_data['eb_id'],$table_name,$proportion,3);
  567. }elseif($top['type']<$top_1_data['type']){
  568. if($top_1_data['type']==4){
  569. $proportion=$this->reward_proportion($top_1_data['type']);
  570. $proportion['proportion']='0.06';
  571. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_1_data['eb_id'],$table_name,$proportion,3);
  572. }elseif($top_1_data['type']==5){
  573. $proportion=$this->reward_proportion($top_1_data['type']);
  574. $proportion['proportion']='0.09';
  575. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_1_data['eb_id'],$table_name,$proportion,3);
  576. }elseif($top_1_data['type']==3){
  577. $proportion=$this->reward_proportion($top_1_data['type']);
  578. $proportion['proportion']='0.02';
  579. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,3);
  580. }
  581. }
  582. $data=[
  583. 'eb_id'=>$top_1_data['eb_id'],
  584. 'type'=>$top_1_data['type']
  585. ];
  586. }
  587. return $data;
  588. }
  589. //三级奖励
  590. public function four_jiangli($top_data,$order,$table_name,$type)
  591. {
  592. $top=$top_data;
  593. $top_data=Db::name($table_name)->where('eb_id',$top['eb_id'])->find();
  594. $top_top_data=Db::name($table_name)->where('eb_id',$top_data['top_eb_id'])->find();
  595. if(!$top_top_data){
  596. return false;
  597. }
  598. if($top_top_data['type']>2){
  599. if($top['type']<$top_top_data['type']){
  600. if($top_top_data['type']==4){
  601. $proportion=$this->reward_proportion($type);
  602. $proportion['proportion']='0.06';
  603. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,4);
  604. }elseif($top_top_data['type']==5){
  605. $proportion=$this->reward_proportion($type);
  606. $proportion['proportion']='0.09';
  607. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,4);
  608. }elseif($top_top_data['type']==3){
  609. $proportion=$this->reward_proportion($type);
  610. $proportion['proportion']='0.03';
  611. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_top_data['eb_id'],$table_name,$proportion,4);
  612. }
  613. }
  614. $data=[
  615. 'eb_id'=>$top_top_data['eb_id'],
  616. 'type'=>$top_top_data['type']
  617. ];
  618. }else{
  619. $top_1_id=$this->recursion_top($top_data['top_eb_id'],'0');
  620. if($top_1_id==0){
  621. return false;
  622. }
  623. $top_1_data=Db::name($table_name)->where('eb_id',$top_1_id)->find();
  624. if($top['type']<$top_1_data['type']){
  625. if($top_1_data['type']==4){
  626. $proportion=$this->reward_proportion($top_1_data['type']);
  627. $proportion['proportion']='0.06';
  628. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_1_data['eb_id'],$table_name,$proportion,4);
  629. $data=[
  630. 'eb_id'=>$top_1_data['eb_id'],
  631. 'type'=>$top_1_data['type']
  632. ];
  633. }elseif($top_1_data['type']==5){
  634. $proportion=$this->reward_proportion($top_1_data['type']);
  635. $proportion['proportion']='0.09';
  636. $this->jiangli_cl($type,$order['money'],$order['order_no'],$top_1_data['eb_id'],$table_name,$proportion,4);
  637. $data=[
  638. 'eb_id'=>$top_1_data['eb_id'],
  639. 'type'=>$top_1_data['type']
  640. ];
  641. }else{
  642. return false;
  643. }
  644. }else{
  645. return false;
  646. }
  647. }
  648. return $data;
  649. }
  650. public function jiangli_cl($type,$money,$order_no,$eb_id,$table_name,$proportion,$to_user_type)
  651. {
  652. $money=bcmul($money,$proportion['proportion'],2);
  653. $msg=$proportion['msg'].':管理奖励';
  654. $bill_data=[
  655. 'from_id'=>0,
  656. 'to_id'=>$eb_id,
  657. 'type'=>2,
  658. 'order_no'=>$order_no,
  659. 'remarks'=>$msg,
  660. 'ctime'=>time(),
  661. 'number'=>$money,//商业合伙人或联合创始人奖励为30%
  662. 'pm'=>1,
  663. 'status'=>'2',
  664. 'on_line'=>$type,
  665. 'mer_id'=>290,
  666. 'commission_type'=>1,
  667. 'commission_scale'=>$proportion['proportion']*100,
  668. 'to_user_type'=>$to_user_type,
  669. 'to_user_type_1'=>$proportion['type'],
  670. ];
  671. // dd($bill_data);
  672. //增加账单记录
  673. $this->eb_user_bill($bill_data);
  674. Db::name($table_name)->where('eb_id',$eb_id)->inc('wallet',$money)->update();
  675. Db::name($table_name)->where('eb_id',$eb_id)->inc('ranking_money',$money)->update();
  676. }
  677. //获取奖励比例
  678. public function reward_proportion($type)
  679. {
  680. switch ($type){
  681. case '3':
  682. $proportion=[
  683. "proportion"=>'0.03',
  684. 'msg'=>'经理',
  685. 'type'=>3,
  686. ];
  687. break;
  688. case '4':
  689. $proportion=[
  690. "proportion"=>'0.09',
  691. 'msg'=>'总监',
  692. 'type'=>4,
  693. ];
  694. break;
  695. case '5':
  696. $proportion=[
  697. "proportion"=>'0.18',
  698. 'msg'=>'总裁',
  699. 'type'=>5,
  700. ];
  701. break;
  702. default:
  703. case '2':
  704. case '1':
  705. $proportion=[
  706. "proportion"=>'0',
  707. 'msg'=>'其它',
  708. 'type'=>0,
  709. ];
  710. }
  711. return $proportion;
  712. }
  713. //国腾名医确认收货单独处理
  714. public function take_290($order_no,$type=2)
  715. {
  716. Db::name('log')->insert(['data'=>'290进来了5']);
  717. if($type==1){
  718. $store_table_name='store_order';
  719. $eb_table_name='entropy_barter_user_290';
  720. $orde_data=Db::name($store_table_name)->where('order_sn',$order_no)->find();
  721. $eb_data=Db::name($eb_table_name)->where('uid',$orde_data['uid'])->find();
  722. // Db::name('user_bill')->where('order_sn',$order_no)->where('uid',$orde_data['uid'])->where('source',0)->update(['status'=>1]);
  723. // $mer_id=Db::name('store_order')->where('order_sn',$order_no)->value('mer_id');
  724. // $data=Db::name('entropy_barter_bill')->where('mer_id',$mer_id)->where('order_no',$order_no)->select();
  725. // foreach ($data as $k=>$v){
  726. // if($v['status']=='1'){
  727. // if($v['type']==2){
  728. // Db::name($eb_table_name)->where('eb_id',$v['to_id'])->inc('wallet',$v['number'])->update();
  729. // Db::name($eb_table_name)->where('eb_id',$v['to_id'])->dec('integral',$v['number'])->update();
  730. // Db::name('entropy_barter_bill')->where('id',$v['id'])->update(['status'=>2]);
  731. // }elseif ($v['type']==1){
  732. // Db::name($eb_table_name)->where('eb_id',$v['to_id'])->inc('integral',$v['number'])->update();
  733. // Db::name('entropy_barter_bill')->where('id',$v['id'])->update(['status'=>2]);
  734. // }
  735. // }
  736. // }
  737. //特殊的地方开始处理了
  738. //如果是特殊商品可以接着执行
  739. $product_id=Db::name('store_cart')->where('cart_id',$orde_data['cart_id'])->value('product_id');
  740. if($product_id==''){
  741. return '';
  742. }elseif ($product_id=='362'||$product_id=='364'||$product_id=='363'){
  743. // Db::name('store_order')->where('order_sn',$order_no)->update(['is_split_to_tomorrow'=>1,'status'=>3]);
  744. //查找上级是否可升级 $eb_data['top_eb_id']--上级ID
  745. // if($product_id=='362'){
  746. // Db::name($eb_table_name)->where('eb_id',$eb_data['eb_id'])->update(['type'=>2,'pay_goods_type'=>2]);
  747. // }else{
  748. // Db::name($eb_table_name)->where('eb_id',$eb_data['eb_id'])->update(['type'=>1]);
  749. //
  750. // }
  751. $top_data=Db::name($eb_table_name)->where('eb_id',$eb_data['top_eb_id'])->find();
  752. if($top_data['type']!=0){
  753. Db::name('log')->insert(['data'=>'上级是会员']);
  754. $this->recursion($top_data['eb_id']);
  755. }else{
  756. Db::name('log')->insert(['data'=>'上级不是会员'.$top_data['eb_id']]);
  757. }
  758. }
  759. }
  760. }
  761. public function recursion($eb_id)
  762. {
  763. try {
  764. $eb_table_name='entropy_barter_user_290';//1111
  765. $eb_data=Db::name($eb_table_name)->where('eb_id',$eb_id)->find();
  766. if($eb_data['type']==1){//一和二是同级别
  767. $eb_data['type']=2;
  768. }
  769. if($eb_data['type']==0){
  770. return '';
  771. }
  772. Db::name('guoteng_famous_doctor_relation')->insert(['uid'=>$eb_data['uid'],'type'=>$eb_data['type'],'create_time'=>date('Y-m-d H:i:s')]);
  773. $top_upgrade_num=Db::name('guoteng_famous_doctor_relation')->where('type',$eb_data['type'])->where('uid',$eb_data['uid'])->count('id');
  774. if($top_upgrade_num>=3){//如果该等级的数量大于3则升级
  775. if($eb_data['type']==5){
  776. return '已达到最高级别';
  777. }
  778. Db::name($eb_table_name)->where('uid',$eb_data['uid'])->update(['type'=>$eb_data['type']+1]);
  779. if($eb_data['type']+1==3){
  780. // $money=Db::name($eb_table_name)->where('uid',$eb_data['uid'])->value('precipitate');
  781. $money=systemConfig('manager_reward');
  782. $bill_data=[
  783. 'from_id'=>0,
  784. 'to_id'=>$eb_data['eb_id'],
  785. 'type'=>2,
  786. 'order_no'=>'0000000000000000',
  787. 'remarks'=>'升级经理奖励',
  788. 'ctime'=>time(),
  789. 'number'=>$money,//商业合伙人或联合创始人奖励为30%
  790. 'pm'=>1,
  791. 'status'=>'2',
  792. 'on_line'=>1,
  793. 'mer_id'=>290,
  794. 'commission_type'=>1,
  795. 'commission_scale'=>0,
  796. ];
  797. //增加账单记录
  798. $this->eb_user_bill($bill_data);
  799. Db::name($eb_table_name)->where('eb_id',$eb_data['eb_id'])->inc('wallet',$money)->update();
  800. Db::name($eb_table_name)->where('eb_id',$eb_data['eb_id'])->inc('ranking_money',$money)->update();
  801. }
  802. if($eb_data['top_eb_id']!=0){
  803. $this->recursion($eb_data['top_eb_id']);
  804. }else{
  805. return '成功1';
  806. }
  807. }else{
  808. return '小于3';
  809. }
  810. return '成功2';
  811. }catch (\Exception $e) {
  812. Db::name('log')->insert(['data'=>'订单支付出错:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  813. }
  814. }
  815. //查找上级递归循环
  816. public function recursion_top($eb_id,$my_type)
  817. {
  818. $data=Db::name('entropy_barter_user_290')->where('eb_id',$eb_id)->field('top_eb_id,eb_id,type,pay_status')->find();
  819. if(!$data){
  820. return 0;
  821. }
  822. if($data['pay_status']==1&&$data['type']>2){
  823. // return $data['eb_id'];
  824. return $data['eb_id'];
  825. }elseif($data['pay_status']!=1||$data['type']<2){
  826. return $this->recursion_top($data['top_eb_id'],$my_type);
  827. }
  828. }
  829. }