Group.php 9.0 KB

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