Group.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 行业分类
  7. */
  8. namespace app\ais\controller\store;
  9. use app\ais\controller\Common;
  10. use app\ais\model\AisConfig;
  11. use app\ais\model\AisOrder;
  12. use app\ais\model\AisStoreGroup;
  13. use app\ais\model\AisStore;
  14. use app\ais\model\AisStoreUnion;
  15. use think\Db;
  16. use think\facade\Request;
  17. use think\helper\Time;
  18. class Group extends Common{
  19. /**
  20. * 列表
  21. */
  22. public function index(){
  23. $view['lists'] = AisStoreGroup::where(['member_miniapp_id' => $this->member_miniapp_id])->order('id desc')->paginate(20);
  24. $view['pathMaps'] = [['name'=>'商圏管理','url'=> url("group/index")]];
  25. return $this->fetch()->assign($view);
  26. }
  27. /**
  28. * 已绑定商圏列表
  29. */
  30. public function store(int $id){
  31. $view['quan_id'] = $id;
  32. $view['lists'] = AisStore::where($this->mini_program)->whereIn('id',AisStoreUnion::where($this->mini_program)->where(['group_id' => $id])->column('store_id'))->order('id desc')->paginate(20);
  33. $view['pathMaps'] = [['name'=>'商圏管理','url'=> url("group/index")],['name'=>'商圈商户','url'=> url("group/storeindex",['id' => $id])]];
  34. return $this->fetch()->assign($view);
  35. }
  36. /**
  37. * 商圏列表
  38. */
  39. public function selectStore(int $quan_id){
  40. $condition = [];
  41. $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
  42. if(request()->isAjax()){
  43. $ids = $this->request->param('ids');
  44. if(empty($ids)){
  45. return json(['code'=>0,'msg'=>'请选择要入住商圏的好店']);
  46. }
  47. $list = AisStore::where($condition)->whereIn('id',$ids)->whereNotIn('id',AisStoreUnion::where($this->mini_program)->where(['group_id'=>$quan_id,'type' => 0])->whereIn('store_id',$ids)->column('store_id'))->select();
  48. foreach ($list as $info){
  49. $config = AisConfig::getConfig($this->member_miniapp_id);
  50. $member = AisStoreUnion::where(['member_miniapp_id' => $this->member_miniapp_id,'group_id' => $quan_id,'type' => 0])->select();
  51. if(!empty($config->group_member) && count($member) >=$config->group_member){
  52. return enjson(403,'商圈只能有'.$config->group_member.'个成员');
  53. }
  54. $size = AisStoreUnion::where(['member_miniapp_id' => $this->member_miniapp_id,'store_id' => $info->id,'type' => 0])->select();
  55. if(!empty($config->group_join) && count($size) >=$config->group_join){
  56. return enjson(403,'只能加入'.$config->group_join.'个商圈');
  57. }
  58. $union = AisStoreUnion::where(['member_miniapp_id'=>$this->member_miniapp_id,'store_id' => $info->id,'group_id' => $quan_id,'type' => 1])->find();
  59. if($union){
  60. $union->delete();
  61. }
  62. AisStoreUnion::create(['member_miniapp_id'=>$this->member_miniapp_id,'store_id' => $info->id,'group_id' => $quan_id,'type' => 0,'create_time' => time(),'update_time' => time()]);
  63. }
  64. return json(['code'=>200,'msg'=>'好店入住商圏成功','data' =>[]]);
  65. }else{
  66. $keyword = $this->request->param('keyword');
  67. if(!empty($keyword)) {
  68. $condition[] = ['name', 'like', '%' . $keyword . '%'];
  69. }
  70. $view['lists'] = AisStore::where($condition)->whereNotIn('id',AisStoreUnion::where($this->mini_program)->where(['group_id'=>$quan_id,'type' => 0])->column('store_id'))->order('id desc')->paginate(20);
  71. $view['keyword'] = $keyword;
  72. $view['quan_id'] = $quan_id;
  73. return $this->fetch()->assign($view);
  74. }
  75. }
  76. //编辑
  77. public function edit(){
  78. if(request()->isAjax()){
  79. $param = [
  80. 'id' => $this->request->param('id/d', 0),
  81. 'title' => $this->request->param('title/s'),
  82. 'face' => $this->request->param('face/s'),
  83. 'content' => $this->request->param('content/s'),
  84. 'uid' => $this->request->param('uid/d'),
  85. ];
  86. $validate = $this->validate($param,'Group.edit');
  87. if(true !== $validate){
  88. return json(['code'=>0,'msg'=>$validate]);
  89. }
  90. $result = AisStoreGroup::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $param['id']])->find();
  91. if($result){
  92. $result->title = $param['title'];
  93. $result->content = $param['content'];
  94. $result->uid = $param['uid'];
  95. $result->face = $param['face'];
  96. $result->save();
  97. }else{
  98. $info = AisStoreGroup::where(['member_miniapp_id' => $this->member_miniapp_id,'uid' => $param['uid']])->find();
  99. if($info){
  100. return json(['code'=>403,'msg'=>'只能管理一个商圈']);
  101. }
  102. $param['member_miniapp_id'] = $this->member_miniapp_id;
  103. AisStoreGroup::create($param);
  104. }
  105. return json(['code'=>200,'url'=>url('group/index'),'msg'=>'操作成功']);
  106. }else{
  107. $view['info']= AisStoreGroup::where(['id' => $this->request->param('id/d',0),'member_miniapp_id' => $this->member_miniapp_id])->find();
  108. return $this->fetch()->assign($view);
  109. }
  110. }
  111. //删除商圈
  112. public function delete(int $id){
  113. $store = AisStoreGroup::where(['member_miniapp_id' => $this->member_miniapp_id])->whereIn('id',AisStoreUnion::where($this->mini_program)->where(['group_id' => $id,'type' => 0])->column('group_id'))->find();
  114. if($store){
  115. return json(['code'=>403,'msg'=>'删除失败,商圏中还包含店铺']);
  116. }
  117. $result = AisStoreGroup::destroy($id);
  118. if($result){
  119. return json(['code'=>200,'msg'=>'操作成功']);
  120. }else{
  121. return json(['code'=>403,'msg'=>'删除失败']);
  122. }
  123. }
  124. //从商圏中删除好店
  125. public function deleteStore(int $id,$quan_id){
  126. $result = AisStoreUnion::destroy(['store_id' => $id,'group_id' => $quan_id]);
  127. if($result){
  128. return json(['code'=>200,'msg'=>'操作成功']);
  129. }
  130. return json(['code'=>403,'msg'=>'删除失败']);
  131. }
  132. /**
  133. * 商圏用户订单
  134. * @access public
  135. */
  136. public function order(int $state = 1,int $group_id){
  137. $condition = [];
  138. $condition[] = ['member_miniapp_id', '=',$this->member_miniapp_id];
  139. $condition[] = ['group_id', '=', $group_id];
  140. $time = Request::param('time/d',0);
  141. if($time){
  142. switch ($time) {
  143. case 2:
  144. list($start, $end) = Time::yesterday();
  145. break;
  146. case 30:
  147. list($start, $end) = Time::month();
  148. break;
  149. case 60:
  150. list($start, $end) = Time::lastMonth();
  151. break;
  152. default:
  153. list($start, $end) = Time::today();
  154. break;
  155. }
  156. $condition[] = ['paid_time','>=',$start];
  157. $condition[] = ['paid_time','<=',$end];
  158. }
  159. $starttime = Request::param('starttime/s');
  160. if($starttime){
  161. $condition[] = ['paid_time','>=',strtotime($starttime)];
  162. }
  163. $endtime = Request::param('endtime/s');
  164. if($endtime){
  165. $condition[] = ['paid_time','<=',strtotime($endtime)];
  166. }
  167. $view['orders'] = AisOrder::where(['state' => $state ? 1 : 0])->whereIn('store_id',AisStoreUnion::where($condition)->column('store_id'))->order('id desc')->paginate(20, false, ['query' => ['time' => $time, 'starttime' => $starttime, 'endtime' => $endtime, 'group_id' => $group_id]]);
  168. $view['order_num'] = AisOrder::where(['state' => 1])->whereIn('store_id',AisStoreUnion::where($condition)->column('store_id'))->count();
  169. $view['user_amount'] = AisOrder::where(['state' => 1])->whereIn('store_id',AisStoreUnion::where($condition)->column('store_id'))->sum('amount');
  170. $view['order_amount'] = AisOrder::where(['state' => 1])->whereIn('store_id',AisStoreUnion::where($condition)->column('store_id'))->sum('price');
  171. $view['coupon_price'] = money($view['user_amount'] - $view['order_amount']);
  172. $view['time'] = $time;
  173. $view['starttime'] = $starttime;
  174. $view['endtime'] = $endtime;
  175. $view['group_id'] = $group_id;
  176. return view()->assign($view);
  177. }
  178. }