ShopHotel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\api\controller\shop;
  3. use app\common\controller\Api;
  4. use app\common\model\HotelModel;
  5. use app\common\model\OfflineShopModel;
  6. use app\common\model\OfflineTypeModel;
  7. use app\utils\DataUtil;
  8. use think\Db;
  9. /**
  10. * 示例接口
  11. */
  12. class ShopHotel extends Api
  13. {
  14. protected $noNeedLogin = [''];
  15. protected $noNeedRight = ['*'];
  16. public function home()
  17. {
  18. $user_id = $this->auth->id;
  19. $model = new HotelModel();
  20. $info = $model->getDetail(
  21. params: ['user_id' => $user_id],
  22. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  23. );
  24. if (!$info){
  25. return $this->error('未开通门店');
  26. }
  27. // 待核销金额
  28. $wait_use = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>2])->field('sum(total_amount - back_amount) as money')->find();
  29. // 已核销
  30. $used = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>3])->field('sum(total_amount - back_amount) as money')->find();
  31. // 已退款
  32. $refund = Db::name('bill')->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order','status'=>4])->field('sum(total_amount - back_amount) as money')->find();
  33. $statistics = [
  34. 'wait_use' => $wait_use['money'] ?? 0,
  35. 'used' => $used['money'] ?? 0,
  36. 'refund' => $refund['money'] ?? 0,
  37. ];
  38. return $this->success('success',[
  39. 'info' => $info,
  40. 'statistics' => $statistics
  41. ]);
  42. }
  43. // 交易统计
  44. public function statistics()
  45. {
  46. $params = $this->request->param();
  47. $user_id = $this->auth->id;
  48. if (empty($params['type'])){
  49. $params['type'] = 1;
  50. }
  51. $model = new HotelModel();
  52. $info = $model->getDetail(
  53. params: ['user_id' => $user_id],
  54. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  55. );
  56. if (!$info){
  57. return $this->error('未开通门店');
  58. }
  59. switch ($params['type']){
  60. case 1:
  61. // 今天
  62. $createtimeBetween = [
  63. strtotime(date('Y-m-d 00:00:00')),
  64. strtotime(date('Y-m-d 23:59:59')),
  65. ];
  66. break;
  67. case 2:
  68. // 昨天
  69. $createtimeBetween = [
  70. strtotime(date('Y-m-d 00:00:00', strtotime('-1 day'))),
  71. strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))),
  72. ];
  73. break;
  74. case 3:
  75. // 本月
  76. $createtimeBetween = [
  77. strtotime(date('Y-m-01 00:00:00')),
  78. strtotime(date('Y-m-d 23:59:59')),
  79. ];
  80. break;
  81. default:
  82. $createtimeBetween = [
  83. strtotime(date('Y-m-d 00:00:00'),strtotime($params['date'])),
  84. strtotime(date('Y-m-d 23:59:59'),strtotime($params['date'])),
  85. ];
  86. break;
  87. }
  88. $wait_use = Db::name('bill')
  89. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order'])
  90. ->whereIn('status',[1,2,3])
  91. ->whereBetween('createtime',$createtimeBetween)
  92. ->field('sum(total_amount - back_amount) as money,count(id) as num')
  93. ->find();
  94. return $this->success('success',[
  95. 'money' => $wait_use['money'] ?? 0,
  96. 'num' => $used['num'] ?? 0,
  97. ]);
  98. }
  99. // 趋势图
  100. public function trend()
  101. {
  102. $user_id = $this->auth->id;
  103. $model = new HotelModel();
  104. $info = $model->getDetail(
  105. params: ['user_id' => $user_id],
  106. select: ['id','invite_id','user_id','name','image','images','back_rate','address']
  107. );
  108. if (!$info){
  109. return $this->error('未开通门店');
  110. }
  111. // 七日数据走势统计
  112. $createtimeBetween = [
  113. strtotime(date('Y-m-d 00:00:00', strtotime('-6 day'))),
  114. strtotime(date('Y-m-d 23:59:59')),
  115. ];
  116. $dates = [];
  117. for ($i = 6; $i >= 0; $i--){
  118. $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
  119. }
  120. $wait_use = Db::name('bill')
  121. ->field('DATE(FROM_UNIXTIME(createtime)) as order_date,sum(total_amount - back_amount) as money,count(id) as num')
  122. ->where(['shop_id'=>$info['id'],'table_name'=>'hotel_order'])
  123. ->whereIn('status',[1,2,3])
  124. ->whereBetween('createtime',$createtimeBetween)
  125. ->group('order_date')
  126. ->order('order_date','asc')
  127. ->select();
  128. $num = [];
  129. $money = [];
  130. foreach ($dates as $key=>$date){
  131. $num[$key] = 0;
  132. $money[$key] = '0';
  133. foreach ($wait_use as $k=>$v){
  134. if ($date == $v['order_date']){
  135. $num[$key] = $v['num'];
  136. $money[$key] = $v['money'];
  137. }
  138. }
  139. $dates[$key] = date('m/d',strtotime($date));
  140. }
  141. return $this->success('success',[
  142. 'money' => $money,
  143. 'num' => $num,
  144. 'dates' => $dates
  145. ]);
  146. }
  147. }