Gmstore.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * 商家管理店铺
  4. */
  5. namespace app\ais\controller\api\v1;
  6. use app\ais\controller\api\Base;
  7. use app\ais\model\AisStore;
  8. use app\ais\model\AisStoreBill;
  9. use app\ais\model\AisStoreGroup;
  10. use app\ais\model\AisStoreUnion;
  11. use app\ais\model\AisCouponUser;
  12. use app\ais\model\AisOrder;
  13. use app\ais\model\AisCoupon;
  14. use think\helper\Time;
  15. use filter\Filter;
  16. class Gmstore extends Base{
  17. protected $store;
  18. public function initialize() {
  19. parent::initialize();
  20. $this->isUserAuth();
  21. $this->store = AisStore::manageStore($this->user->id);
  22. if(empty($this->store)){
  23. exit(json_encode(['code' => 403,'msg'=>'无法找到该商家']));
  24. }
  25. }
  26. /**
  27. * 申请入驻商家/修改商家信息
  28. */
  29. public function edit(){
  30. if (request()->isPost()) {
  31. $param = [
  32. 'name' => $this->request->param('name/s'),
  33. 'address' => $this->request->param('address/s'),
  34. 'telphone' => $this->request->param('telphone/s'),
  35. 'mch_id' => $this->request->param('mch_id/d',''),
  36. 'latitude' => $this->request->param('latitude/s'),
  37. 'longitude' => $this->request->param('longitude/s'),
  38. 'imgs' => $this->request->param('imgs/s','[]','htmlspecialchars_decode'),
  39. ];
  40. $this->apiSign($param);
  41. $validate = $this->validate($param,'Store.gmedit');
  42. if(true !== $validate){
  43. return enjson(403,$validate);
  44. }
  45. $imgs = Filter::filter_escape(json_decode($param['imgs'],true));
  46. if(!empty($imgs)){
  47. $data['img'] = $imgs[0];
  48. }
  49. $data['name'] = $param['name'];
  50. $data['telphone'] = $param['telphone'];
  51. $data['mch_id'] = $param['mch_id'];
  52. $data['address'] = $param['address'];
  53. $data['latitude'] = $param['latitude'];
  54. $data['longitude'] = $param['longitude'];
  55. $rel = AisStore::where(['id' => $this->store->id])->update($data);
  56. if($rel){
  57. return enjson(200,"成功");
  58. }
  59. return enjson(403,"失败");
  60. }
  61. }
  62. //读取商家信息
  63. public function read(){
  64. $this->apiSign();
  65. return enjson(200,$this->store);
  66. }
  67. //商家数据统计
  68. public function statis(){
  69. $this->apiSign();
  70. list($start, $end) = Time::yesterday();
  71. $amount = AisOrder::where([['store_id','=', $this->store->id],['state', '=',1],['update_time', '>=',$start],['update_time', '<=',$end]])->sum('price');
  72. $coupon = AisCoupon::where(['store_id' => $this->store->id])->count(); //已领取
  73. $couponuser = AisCouponUser::where(['store_id' => $this->store->id])->where('parent_store_id','>',0)->count(); //引流
  74. $from = AisCouponUser::where([['store_id', '=', $this->store->id],['parent_store_id', '>',0],['money', '>',0],['is_end', '=',1]])->count(); //引流
  75. return enjson(200,['store'=>$this->store,'amount' => $amount,'coupon'=>$coupon,'couponuser' => $couponuser,'from'=> $from]);
  76. }
  77. //商家收入
  78. public function bill(){
  79. $param = [
  80. 'page' => $this->request->param('page/d',1),
  81. 'store_id' => $this->request->param('store_id'),
  82. 'store_chain_id' => $this->request->param('store_chain_id'),
  83. 'fixed_date' => $this->request->param('fixed_date'),
  84. 'times' => $this->request->param('times','[]','htmlspecialchars_decode'),
  85. ];
  86. $this->apiSign($param);
  87. //判断是修改还是创建
  88. $store = AisStore::manageStore($this->user->id);
  89. if (!$store) {
  90. return enjson(404);
  91. }
  92. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  93. $condition[] = ['store_id', '=',$this->store->id];
  94. if($param['store_chain_id']){
  95. $condition[] = ['store_chain_id', '=',$param['store_chain_id']];
  96. }
  97. if($param['fixed_date'] == 3){
  98. $times = Filter::filter_escape(json_decode($param['times'],true));
  99. if(!empty($times['startime']) || !empty($times['endtime'])){
  100. $start = strtotime($times['startime'].'00:00:00');
  101. $end = strtotime($times['endtime'].'23:59:59');
  102. if($start >= $end){
  103. return enjson(403,'开启日期禁止大于结束日期');
  104. }
  105. $condition[] = ['update_time', '>=', $start];
  106. $condition[] = ['update_time', '<=', $end];
  107. }
  108. }else{
  109. switch ($param['fixed_date']) {
  110. case 1:
  111. list($start, $end) = Time::yesterday();
  112. break;
  113. case 2:
  114. list($start, $end) = Time::month();
  115. break;
  116. default:
  117. list($start, $end) = Time::today();
  118. break;
  119. }
  120. $condition[] = ['update_time', '>=', $start];
  121. $condition[] = ['update_time', '<=', $end];
  122. }
  123. $rel = AisStoreBill::withAttr('update_time', function ($value, $data) {
  124. return date('Y-m-d H:i',$value);
  125. })->with(['user'=> function($query) {
  126. $query->field('id,face,nickname');
  127. }])->where($condition)->order('id desc')->page($param['page'],20)->select();
  128. if (empty($rel)) {
  129. return enjson(204);
  130. }
  131. $amount['inc'] = 0;
  132. $amount['dec'] = 0;
  133. $amount['order'] = 0;
  134. if($param['page'] == 1){
  135. $amount['inc'] = AisStoreBill::where($condition)->where('money','>',0)->sum('money');
  136. $amount['dec'] = AisStoreBill::where($condition)->where('money','<',0)->sum('money');
  137. $amount['order'] = AisOrder::where($condition)->where('state','=',1)->sum('price');
  138. }
  139. return enjson(200,['bill' => $rel,'amount' => $amount]);
  140. }
  141. }