getHttpHost()))->handle(); $notSearchPlugin = [158,161];// 抖音cps、聚推联盟商品不显示 //销售量前10条数据 $goods = Goods::uniacid() ->whereNotIn('plugin_id',$notSearchPlugin) ->orderBy('real_sales','desc') ->offset(0) ->take(10) ->select('id','title','real_sales','created_at') ->get(); //订单数据 $start_today = strtotime(Carbon::now() ->startOfDay() ->format('Y-m-d H:i:s')); $end_today = strtotime(Carbon::now() ->endOfDay() ->format('Y-m-d H:i:s')); //不统计的订单plugin_id $without_count_plugin_id = \app\common\modules\shop\ShopConfig::current() ->get('without_count_order_plugin_id') ?: []; //获取已开启的且不在不统计的订单的插件id $pluginIds = array_column((new \app\backend\modules\order\services\OrderViewService)->getOrderType(),'plugin_id'); $pluginIds = array_filter($pluginIds,function($item) use ($without_count_plugin_id){ return !in_array($item,$without_count_plugin_id); }); $where = [['uniacid',\YunShop::app()->uniacid]]; $orderResult = DB::table('yz_order') ->selectRaw('status, count(status) as total') ->where($where) ->whereIn('plugin_id',$pluginIds) ->whereIn('status',[0,1]) ->groupBy('status') ->get(); foreach($orderResult as $rows){ //待支付订单 if($rows['status'] == 0){ $to_be_paid = $rows['total']; } //待发货订单 if($rows['status'] == 1){ $to_be_shipped = $rows['total']; } } //今日订单数据 $today_order = DB::table('yz_order') ->selectRaw('sum(price) as money , count(id) as total') ->where($where) ->whereIn('plugin_id',$pluginIds) ->whereBetween('created_at',[$start_today,$end_today]) ->whereIn('status',[1,2,3]) ->first(); //会员总数 $member = DB::table('mc_members') ->select([DB::raw('count(1) as total')]) ->where('mc_members.uniacid',\YunShop::app()->uniacid) ->join('yz_member','mc_members.uid','=','yz_member.member_id') ->whereNull('yz_member.deleted_at') ->first(); //=============获取图表数据 $all_data = [ 'goods' => $goods, 'member_count' => $member['total'], 'member_count_icon' => 'icon-fontclass-renshu', 'chart_data' => $this->getOrderData(), 'system_icon' => 'icon-fontclass-deng', 'order' => [ 'to_be_paid' => $to_be_paid ?: 0, 'to_be_shipped' => $to_be_shipped ?: 0, 'today_order_money' => $today_order['money'] ?: 0, 'today_order_count' => $today_order['total'] ?: 0, 'paid_icon' => 'icon-ht_content_tixian', 'shipped_icon' => 'icon-ht_content_order', 'order_money_icon' => 'icon-fontclass-yue (1)', 'order_count_icon' => 'icon-fontclass-shangpindingdan (1)' ], 'entrance' => $this->getEntrance(), 'guide' => $this->getBasicGuidance(), 'withdrawal' => $this->getWithdrawal(), 'visitor' => $this->getVisitor(), 'is_enabled_statistics' => 0, 'order_url' => '', 'sales_url' => '', ]; if(app('plugins')->isEnabled('shop-statistics')){ $all_data['is_enabled_statistics'] = 1; $all_data['order_url'] = yzWebFullUrl('plugin.shop-statistics.backend.order.show'); $all_data['sales_url'] = yzWebFullUrl('plugin.shop-statistics.backend.goods.show'); } return $all_data; } public function survey(){ // dd($this->_allData()); return $this->successJson('成功',$this->_allData()); } private function getOrderData(){ $times = $this->timeRangeItems(); $result = []; foreach($times as $time){ $item['total'] = $this->orderTotals(NULL,'create_time',$time) ?: 0; $item['complete'] = $this->orderTotals(3,'finish_time',$time) ?: 0; $item['deliver_goods'] = $this->orderTotals(2,'send_time',$time) ?: 0; $item['date'] = $time; $result[] = $item; } return $result; } /** * 获取一星期的时间 * @return array */ public function timeRangeItems(){ $result = []; for($i = 6;$i > -1;$i--){ Carbon::now() ->subDay($i) ->format('Y-m-d'); $result[] = Carbon::now() ->subDay($i) ->format('Y-m-d'); } return $result; } private $orderTotals; private function orderTotals($status,$timeField,$date){ if(!isset($this->orderTotals[$timeField])){ $allDate = Order::uniacid() ->getQuery() ->select(DB::raw("count(1) as total, FROM_UNIXTIME(".$timeField.",'%Y-%m-%d') as date_str")) ->whereBetween($timeField,[ Carbon::now() ->subDay(6) ->startOfDay()->timestamp, Carbon::now() ->endOfDay()->timestamp ]) ->groupBy(DB::raw('YEAR(date_str), MONTH(date_str), DAY(date_str)')); if(isset($status)){ $allDate->where('status',$status); } $allDate = $allDate->get(); $this->orderTotals[$timeField] = []; foreach($allDate as $item){ $this->orderTotals[$timeField][$item['date_str']] = $item['total']; } } return $this->orderTotals[$timeField][$date]; } private function getSystemStatus(){ return (new System())->index(); } /** * 基础功能指引 * @return array */ private function getBasicGuidance(){ $guide_list = $this->getGuideList(); foreach($guide_list as $k => &$v){ foreach($v['list'] as $k2 => &$v2){ if(isset($v2['supper_admin']) and $v2['supper_admin'] === 1){ if(!(\YunShop::app()->role === 'founder')){ unset($guide_list[$k]['list'][$k2]); continue; } } $v2['url'] = yzWebFullUrl($v2['route'],$v2['param'] ?: []); $v2['is_enabled'] = 0; if($v2['is_plugin'] == 1){ $route_array = explode('.',$v2['route']); if(app('plugins')->isEnabled($route_array[1])){ $v2['is_enabled'] = 1; } else{ $v2['url'] = ''; } } if(isset($v2['special_param']) and $v2['special_param']){ $v2['url'] .= $v2['special_param']; } unset($v2['route'],$v2['param'],$v2['special_param']); } } $data = [ 'list' => $guide_list ]; return $data; } /** * 主要入口 * @return array */ private function getEntrance(){ $data = [ 'home_url' => yzAppFullUrl('home'), 'home_code' => '', 'more_home_url' => yzWebFullUrl('setting.shop.entry'), 'is_enabled_mini_app' => 0, 'is_mini_app_config' => 0, 'mini_app_code' => '', 'is_enabled_customer_service' => 0, 'customer_service_url' => '', 'is_enabled_contract' => 0, 'is_contract_config' => 0, 'contract_url' => '', ]; // 商城首页二维码 $code = new \app\common\helpers\QrCodeHelper($data['home_url'],'app/public/qr/home/'.\YunShop::app()->uniacid); if($home_code = $code->url()){ $data['home_code'] = $home_code; } else $data['home_code_error'] = '生成网页二维码失败'; // 小程序二维码 if(app('plugins')->isEnabled('min-app')){ $data['is_enabled_mini_app'] = 1; $setting = \Setting::get('plugin.min_app'); if($setting['switch'] and $setting['key'] and $setting['secret']){ $data['is_mini_app_config'] = 1; if(!(($mini_code = Cache::get('survey_mini_app_code')) and file_exists($mini_code))){ // 生成小程序二维码,图片保存到storage目录 $config = [ 'app_id' => $setting['key'], 'secret' => $setting['secret'], ]; $min_url = 'packageG/index/index'; try{ $app = Factory::miniProgram($config); $response = $app->app_code->getUnlimit('scene-value',['page' => $min_url,'width' => 600]); if(is_array($response) && isset($response['errcode'])){ throw new \Exception('生成小程序码失败,'.$response['errcode'].':'.$response['errmsg']); } if($response instanceof \EasyWeChat\Kernel\Http\StreamResponse){ if(config('app.framework') == 'platform'){ $file_path = base_path('storage/app/public/qr/home'); } else{ $file_path = dirname(dirname(base_path())).'/attachment/app/public/qr/home'; } $filename = $response->saveAs($file_path,'home_mini_code_'.\YunShop::app()->uniacid); } if(isset($filename)){ Cache::put('survey_mini_app_code',$file_path.'/'.$filename,60); $data['mini_app_code'] = yz_tomedia($file_path.'/'.$filename); } else{ $data['mini_app_code_error'] = '保存小程序码失败'; } } catch(\Exception $e){ $data['mini_app_code_error'] = '生成小程序码失败,请检查配置'; } } else{ $data['mini_app_code'] = yz_tomedia($mini_code); } } else{ $data['is_mini_app_config'] = 0; } } // 客服登录 if(app('plugins')->isEnabled('yunqian-pc')){ $data['is_enabled_customer_service'] = 1; $domain = request()->getSchemeAndHttpHost(); $data['customer_service_url'] = $domain."/addons/yun_shop/plugins/yunqian-pc/public/index.html?i=".\YunShop::app()->uniacid; if(config('app.framework') == 'platform'){ $data['customer_service_url'] = $domain."/plugins/yunqian-pc/platform/index.html?i=".\YunShop::app()->uniacid; } } // 电子合同管理 if(app('plugins')->isEnabled('yun-sign')){ $data['is_enabled_contract'] = 1; $setting = \Setting::get('plugin.yun-sign'); if($setting['short_url_front']){ $data['is_contract_config'] = 1; $data['contract_url'] = $setting['short_url_front']; } } return $data; } private function deleteCode(){ // 删除旧数据 Setting::uniacid() ->where('group','shop') ->where('key','like','mini_code%') ->delete(); } /** * 提现动态 * @return array */ private function getWithdrawal(){ $list = Withdraw::with([ 'hasOneMember' => function($query){ return $query->select('uid','nickname','avatar'); } ]) ->select('id','member_id','created_at','amounts') ->orderBy('created_at','desc') ->limit(20) ->get(); $data = [ 'url' => yzWebFullUrl('withdraw.records'), 'list' => $list ]; return $data; } /** * 访客播报 * @return array */ private function getVisitor(){ $data = [ 'is_enabled' => 0, 'url' => '', 'list' => [] ]; if(app('plugins')->isEnabled('browse-footprint')){ $data['is_enabled'] = 1; $data['url'] = yzWebFullUrl('plugin.browse-footprint.admin.index.index'); $prefix = app('db')->getTablePrefix(); $uniacid = \YunShop::app()->uniacid; $list = BrowseFootprintModel::select('created_at','port_type','ip','ip_name','member_id','cookie','day','url') ->from(DB::raw("(select * from {$prefix}yz_browse_footprint where uniacid = $uniacid order by id desc limit 5000) as {$prefix}yz_browse_footprint")) ->with([ 'hasOneMember' => function($q){ $q->select('uid','nickname','avatar','realname'); } ]) ->search([]) ->groupBy('cookie','member_id') ->orderBy('created_at','desc') ->orderBy('id','desc') ->paginate(20); // 判断新老访客 $cookie_arr = $list->pluck('cookie') ->toArray(); $member_arr = $list->pluck('member_id') ->toArray(); $merge_arr = array_merge($cookie_arr,$member_arr); $day_arr = BrowseFootprintModel::selectRaw('min(day) as min_day,cookie,member_id') ->from(DB::raw("(select * from {$prefix}yz_browse_footprint where uniacid = $uniacid order by id desc limit 5000) as {$prefix}yz_browse_footprint")) ->where(function($query) use ($merge_arr){ foreach($merge_arr as $item){ if($item == NULL){ continue; } if(is_numeric($item)){ $query->orWhere('member_id',$item); } else{ $query->orWhere('cookie',$item); } } }) ->groupBy('cookie') ->get(); $member_ids = $real_day_arr = []; foreach($day_arr as $item){ if($item->member_id != 0 && in_array($item->member_id,$member_ids)){ $bccomp = bccomp($item->min_day,$real_day_arr[$item->member_id]);//左边大=》1 右边大=》-1 //更小就替换 if(!$bccomp){ $real_day_arr[$item->member_id] = $item->min_day; } continue;//替换中断 } $real_day_arr[$item->member_id] = $item->min_day; $member_ids[] = $item->member_id; } unset($real_day_arr[0]);//去除0的数据 $portion_day_arr = $day_arr->pluck('min_day','cookie'); foreach($list as $key => $item){ $list_data[] = [ 'access_time' => $item->created_at->toDateTimeString(), 'cookie_type' => $this->getCookieType($item->day,$item->cookie,$portion_day_arr,$real_day_arr,$item->member_id), 'name' => $item->hasOneMember->username ?: '', 'avatar_image' => $item->hasOneMember->avatar_image ?: '', 'uid' => $item->hasOneMember->uid ]; } $data['list'] = $list_data ?: []; } return $data; } /** * 判断新老访客 * @param $item_day * @param $item_cookie * @param $portion_day_arr * @param $real_day_arr * @param $item_member_id * @return string */ private function getCookieType($item_day,$item_cookie,$portion_day_arr,$real_day_arr,$item_member_id){ //存在 if(!empty($real_day_arr[$item_member_id])){ $times[$item_cookie] = $real_day_arr[$item_member_id]; } else{ $times[$item_cookie] = $portion_day_arr[$item_cookie]; } return $item_day == $times[$item_cookie] ? 'new' : 'old'; } // 基础功能指引 private function getGuideList(){ $basic_guidance = [ [ 'title' => '基础设置', 'list' => [ [ 'name' => '安装应用', 'route' => 'plugin.plugins-market.Controllers.new-market.show', 'is_plugin' => 1, 'icon' => 'icon-fontclass-anzhuang', 'supper_admin' => 1, ], [ 'name' => '启动应用', 'route' => 'plugins.get-plugin-data', 'is_plugin' => 0, 'icon' => 'icon-fontclass-mendianfenlei', 'supper_admin' => 1, ], [ 'name' => '设置商城信息', 'route' => 'setting.shop.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-mendianguanli' ], [ 'name' => '设置短信', 'route' => 'setting.shop.sms', 'is_plugin' => 0, 'icon' => 'icon-fontclass-yanzhengma' ], [ 'name' => '设置物流查询', 'route' => 'setting.shop.express-info', 'is_plugin' => 0, 'icon' => 'icon-massage_trade_logistics' ], [ 'name' => '添加操作员', 'route' => 'user.user.index', 'is_plugin' => 0, 'icon' => 'icon-massage_client' ], [ 'name' => '前端入口', 'route' => 'setting.shop.entry', 'is_plugin' => 0, 'icon' => 'icon-wj_record' ], [ 'name' => '接入微信公众号', 'route' => 'plugin.wechat.admin.setting.setting', 'is_plugin' => 1, 'icon' => 'icon-fontclass-gongzonghao1' ], [ 'name' => '接入微信小程序', 'route' => 'plugin.min-app.Backend.Controllers.base-set', 'is_plugin' => 1, 'icon' => 'icon-all_smallprogram' ], [ 'name' => '支付方式', 'route' => 'setting.shop.pay', 'is_plugin' => 0, 'icon' => 'icon-fontclass-zhifufangshi' ], [ 'name' => '微信模板消息', 'route' => 'setting.shop.notice', 'is_plugin' => 0, 'icon' => 'icon-xuanchuantuiguang' ] ] ], [ 'title' => '商品与页面', 'list' => [ [ 'name' => '商品分类层级', 'route' => 'setting.shop.category', 'is_plugin' => 0, 'icon' => 'icon-fontclass-fenleicengji' ], [ 'name' => '商品分类设置', 'route' => 'goods.category.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-anzhuang' ], [ 'name' => '商品品牌设置', 'route' => 'goods.brand.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-shouchu' ], [ 'name' => '配送模板设置', 'route' => 'goods.dispatch.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-jinhuo' ], [ 'name' => '发布商品', 'route' => 'goods.goods.create', 'is_plugin' => 0, 'icon' => 'icon-fontclass-tuanduifenxiaoshang' ], [ 'name' => '商品助手', 'route' => 'plugin.goods-assistant.admin.import.taobao', 'is_plugin' => 1, 'icon' => 'icon-fontclass-mendianshangpin' ], [ 'name' => '装修页面', 'route' => 'plugin.decorate.admin.page.get-list', 'is_plugin' => 1, 'param' => [ 'i' => \YunShop::app()->uniacid ], 'special_param' => '#/home', 'icon' => 'icon-energetank_vipcard' ], [ 'name' => '设置底部导航', 'route' => 'plugin.decorate.admin.page.get-list', 'is_plugin' => 1, 'param' => [ 'i' => \YunShop::app()->uniacid ], 'special_param' => '#/bottom_navigation_list', 'icon' => 'icon-fontclass-dibudaohang' ], [ 'name' => '选择页面模板', 'route' => 'plugin.decorate.admin.page.get-list', 'is_plugin' => 1, 'param' => [ 'i' => \YunShop::app()->uniacid ], 'special_param' => '#/diy_template_manage', 'icon' => 'icon-ht_operation_add' ], [ 'name' => '模板市场', 'route' => 'plugin.decorate.admin.decorate-diy.index', 'is_plugin' => 1, 'icon' => 'icon-ht_content_apply' ], [ 'name' => '自定义文字', 'route' => 'setting.lang.index', 'is_plugin' => 0, 'icon' => 'icon-ht_textstyle_blod' ] ] ], [ 'title' => '会员基础功能', 'list' => [ [ 'name' => '手机验证码登录', 'route' => 'setting.shop.member', 'is_plugin' => 0, 'icon' => 'icon-fontclass-yanzhengma' ], [ 'name' => '强制绑定手机号', 'route' => 'setting.shop.member', 'is_plugin' => 0, 'icon' => 'icon-fontclass-shoujihao' ], [ 'name' => '会员等级设置', 'route' => 'setting.shop.member', 'is_plugin' => 0, 'icon' => 'icon-huiyuanguanli-copy' ], [ 'name' => '邀请码设置', 'route' => 'setting.shop.member', 'is_plugin' => 0, 'icon' => 'icon-fontclass-yaoqingma' ], [ 'name' => '会员注册资料', 'route' => 'setting.form.index', 'is_plugin' => 0, 'icon' => 'icon-dingdan1' ], [ 'name' => '会员注册协议/页面', 'route' => 'setting.shop.register', 'is_plugin' => 0, 'icon' => 'icon-fontclass-shouyintaidingdan' ], [ 'name' => '会员等级管理', 'route' => 'member.member-level.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-huiyuandengji' ], [ 'name' => '关系链设置', 'route' => 'member.member-relation.index', 'is_plugin' => 0, 'icon' => 'icon-all_link' ], [ 'name' => '企业微信管理', 'route' => 'plugin.work-wechat-platform.admin.crop.index', 'is_plugin' => 1, 'icon' => 'icon-fontclass-qiyeweix' ], [ 'name' => '导入会员', 'route' => 'plugin.import-members.Backend.Modules.Member.Controllers.page', 'is_plugin' => 1, 'icon' => 'icon-fontclass-fenxiangjiang' ], [ 'name' => '会员等级同步', 'route' => 'plugin.level-cogradient.admin.set.index', 'is_plugin' => 1, 'icon' => 'icon-fontclass-huiyuantongbu' ], [ 'name' => '会员标签', 'route' => 'plugin.member-tags.Backend.controllers.tag.index', 'is_plugin' => 1, 'icon' => 'icon-goods-class' ], ] ], [ 'title' => '订单与财务', 'list' => [ [ 'name' => '自动关闭未付款订单', 'route' => 'setting.shop.trade', 'is_plugin' => 0, 'icon' => 'icon-fontclass-wenzhangzhongxin' ], [ 'name' => '自动收货', 'route' => 'setting.shop.trade', 'is_plugin' => 0, 'icon' => 'icon-fontclass-shouhuo' ], [ 'name' => '交易设置(退款)', 'route' => 'setting.shop.trade', 'is_plugin' => 0, 'icon' => 'icon-fontclass-fanli' ], [ 'name' => '发票设置', 'route' => 'setting.shop.trade', 'is_plugin' => 0, 'icon' => 'icon-fontclass-fufeijilu' ], [ 'name' => '支付协议', 'route' => 'setting.shop.trade', 'is_plugin' => 0, 'icon' => 'icon-fontclass-shouquanshu' ], [ 'name' => '商城电子合同', 'route' => 'plugin.shop-esign.admin.set.index', 'is_plugin' => 1, 'icon' => 'icon-yq_hetongzhongxin' ], [ 'name' => '支付密码', 'route' => 'password.setting.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-mima' ], [ 'name' => '批量发货', 'route' => 'order.batch-send.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-fahuo' ], [ 'name' => '快递助手', 'route' => 'plugin.exhelper.admin.print-once.search', 'is_plugin' => 1, 'icon' => 'icon-ht_content_goods' ], [ 'name' => '提现设置', 'route' => 'finance.withdraw-set.see', 'is_plugin' => 0, 'icon' => 'icon-fontclass-shezhi2' ], [ 'name' => '提现记录', 'route' => 'withdraw.records', 'is_plugin' => 0, 'icon' => 'icon-fontclass-huodongshuoming' ], [ 'name' => '批量充值', 'route' => 'excelRecharge.page.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-yue' ], ] ], [ 'title' => '常用营销活动', 'list' => [ [ 'name' => '优惠券', 'route' => 'coupon.coupon.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-youhuiq' ], [ 'name' => '满额减', 'route' => 'enoughReduce.index.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-huiyuankaquanrukou' ], [ 'name' => '满额包邮', 'route' => 'enoughReduce.index.index', 'is_plugin' => 0, 'icon' => 'icon-mane01' ], [ 'name' => '余额设置', 'route' => 'finance.balance-set.see', 'is_plugin' => 0, 'icon' => 'icon-fontclass-tichengmingxi' ], [ 'name' => '积分设置', 'route' => 'finance.point-set.index', 'is_plugin' => 0, 'icon' => 'icon-fontclass-lanniujifen' ], [ 'name' => '推广海报', 'route' => 'plugin.new-poster.admin.poster.index', 'is_plugin' => 1, 'icon' => 'icon-jk_haibao' ], [ 'name' => '每日签到', 'route' => 'plugin.sign.Backend.Modules.Sign.Controllers.sign', 'is_plugin' => 1, 'icon' => 'icon-daqia' ], [ 'name' => '幸运大抽奖', 'route' => 'plugin.lucky-draw.admin.controllers.activity.index', 'is_plugin' => 1, 'icon' => 'icon-fontclass-choujiang' ], [ 'name' => '素材中心', 'route' => 'plugin.material-center.admin.material.index', 'is_plugin' => 1, 'icon' => 'icon-ht_show_formicon' ], [ 'name' => '新人奖', 'route' => 'plugin.new-member-prize.admin.controllers.activity.index', 'is_plugin' => 1, 'icon' => 'icon-gudingjiangli' ], [ 'name' => '定金阶梯团', 'route' => 'plugin.deposit-ladder.backend.activity.show', 'is_plugin' => 1, 'icon' => 'icon-fontclass-jieti' ], [ 'name' => '短视频', 'route' => 'plugin.video-share.admin.set', 'is_plugin' => 1, 'icon' => 'icon-fontclass-ship' ], ] ] ]; return $basic_guidance; } /******** 数据库初始化 ***************************************/ public function sqlInit(){ // 不清除数据的表 $notCleanUp = [ 'ims_yz_address', 'ims_yz_admin_users', 'ims_yz_deduction', 'ims_yz_dispatch_type', 'ims_yz_flow', 'ims_yz_pay_type', 'ims_yz_pay_type_group', 'ims_yz_plugins_meal', 'ims_yz_plugins_meal_platform', 'ims_yz_site_setting', 'ims_yz_status', 'ims_yz_street', 'ims_yz_sys_msg_type', 'ims_yz_system_setting', 'ims_yz_virtual_coin', // 分销商相关 'ims_yz_agent_level', // 商品相关 'ims_yz_category', 'ims_yz_diyform_order', 'ims_yz_goods', 'ims_yz_goods_advertising', 'ims_yz_goods_category', 'ims_yz_goods_commission', 'ims_yz_goods_contact_tel', 'ims_yz_goods_coupon', 'ims_yz_goods_discount', 'ims_yz_goods_dispatch', 'ims_yz_goods_div_from', 'ims_yz_goods_image_link', 'ims_yz_goods_limitbuy', 'ims_yz_goods_option', 'ims_yz_goods_point_activity', 'ims_yz_goods_privilege', 'ims_yz_goods_sale', 'ims_yz_goods_service', 'ims_yz_goods_share', 'ims_yz_goods_shareholder_dividend', 'ims_yz_goods_small_code_url', 'ims_yz_goods_spec', 'ims_yz_goods_spec_item', 'ims_yz_goods_team_dividend', 'ims_yz_goods_video', 'ims_yz_invite_page', 'ims_yz_point_mall_goods', 'ims_yz_red_packet_goods', 'ims_yz_goods_area_dividend', 'ims_yz_goods_weight_value', // 附件相关 'ims_yz_core_attachment', 'ims_yz_core_attachment_tags', // 装修相关 'ims_yz_decorate', 'ims_yz_decorate_advertising', 'ims_yz_decorate_footer', 'ims_yz_decorate_templet', 'ims_yz_diyform_type', // 其他 | 系统相关 'ims_yz_order_invoice', 'ims_yz_setting', 'ims_yz_uniacid_app', 'ims_yz_options', 'ims_yz_app_user', 'ims_failed_jobs', 'ims_migrations', 'ims_yz_wechat_rule', 'ims_yz_wechat_rule_keyword', 'ims_yz_withdraw_rich_text', 'ims_yz_help_center_content', 'ims_yz_rich_text', 'ims_yz_wechat_menu', 'ims_yz_user_role', 'ims_yz_users_profile', 'ims_yz_role', 'ims_yz_permission', 'ims_yz_coupon', 'ims_yz_goods_rebate', 'ims_yz_goods_cultural_order_fund_log', // 经销商相关 'ims_yz_team_dividend_level', 'ims_yz_team_dividend_level_redpack', 'ims_yz_team_dividend_level_upgrade', // 文章相关 'ims_yz_plugin_article', 'ims_yz_plugin_article_category', 'ims_yz_plugin_article_log', // 红包相关 'ims_yz_red_packet_logs', // 海报相关 'ims_yz_new_poster', 'ims_yz_new_poster_supplement', // 用户相关 'ims_yz_member_relation', // 微社区 'ims_yz_micro_communities_category', 'ims_yz_micro_communities_set', // 视频分享 'ims_yz_video_share_category', // 文创空间 'ims_yz_goods_cultural_space', ]; // 获取全部数据库列表 $tables = $this->getAllTable(); // 获取存在数据的表 $hasTable = $this->getHasDataTable($tables,$notCleanUp); debug($hasTable); // 域名替换 // $this->domainNameReplacement($tables,[]); // debug("结束"); // 执行清除操作 // foreach($tables as $name){ // if(!in_array($name,$notCleanUp)){ // // 判断:是否操作uniacid=0的数据 存在则执行删除存在,不存在则执行清除操作 // $tableName = str_replace('ims_','',$name); // if($this->isHasZeroData($name)){ // // echo $name."=有","
"; // $columns = \DB::select('SHOW COLUMNS FROM '.$name); // $fields = array_column($columns,'Field'); // $primaryKey = $fields[0]; // $max = \DB::table($tableName)->where('uniacid',0)->max($primaryKey); // \DB::table($tableName)->where('uniacid','>',0)->delete(); // // \DB::select('alter table '.$name.' AUTO_INCREMENT='.$max); // }else{ // \DB::select('TRUNCATE TABLE '.$name.';'); // } // } // } debug("处理完成"); } // 数据库处理 - 获取全部数据库 private function getAllTable(){ $database = \DB::select('SELECT DATABASE() as database_name')[0]['database_name']; $tables = \DB::select('SHOW TABLES'); return array_column($tables,'Tables_in_'.$database); } // 数据库处理 - 获取存在数据的表 private function getHasDataTable($tables,$notCleanUp){ $tableList = []; foreach($tables as $name){ if(!in_array($name,$notCleanUp)){ $tableName = str_replace('ims_','',$name); $table = \DB::table($tableName); if($this->isHasZeroData($name)) $table->where('uniacid','>',0); $count = $table->count(); if($count > 0) $tableList[] = $name; } } return $tableList; } // 数据库处理 - 判断当前表是否存在uniacid=0的数据 private function isHasZeroData($name){ $columns = \DB::select('SHOW COLUMNS FROM '.$name); $fields = array_column($columns,'Field'); // 是否存在指定字段 if(!in_array('uniacid',$fields)) return false; // 是否存在uniacid=0的数据 $tableName = str_replace('ims_','',$name); $count = \DB::table($tableName)->where('uniacid',0)->count(); return $count > 0; } // 数据库处理 - 域名替换 - 开始处理 private function domainNameReplacement($tables,$notCleanUp){ $tableList = []; foreach($tables as $tableName){ if(!in_array($tableName,$notCleanUp)){ echo "{$tableName}\n\r"; // 获取字段信息 $allFieldDesc = \DB::select("show columns from {$tableName}"); $allField = array_column($allFieldDesc,'Field'); // if($this->isHasSpecifyData($allField,$name)) $tableList[] = $name; $res = $this->isHasSpecifyData($allField,$tableName); echo $res ? "存在\n\r" : "不存在\n\r"; if($res){ // 批量替换 $query = "UPDATE {$tableName} SET "; $handleFieldNum = 0; foreach($allFieldDesc as $fieldInfo){ if(strpos($fieldInfo['Type'], 'int') !== false || $fieldInfo['Key'] == 'PRI') continue; $fieldName = $fieldInfo['Field']; $query .= "`{$fieldName}` = REPLACE(`{$fieldName}`,'testbzt.cdlfjy.com','shuizhi.test.cdlfjy.com'),"; ++$handleFieldNum; } $query = trim($query); $query = trim($query,','); $query = trim($query); echo "SQL({$handleFieldNum}):{$query}\n"; if($handleFieldNum > 0) \DB::select($query); } echo "\n\n\n"; } } return $tableList; } // 数据库处理 - 域名替换 - 生成sql private function isHasSpecifyData($allField,$tableName){ $query = "select * from {$tableName} where ("; foreach($allField as $fieldName){ $query .= "`{$fieldName}` like '%testbzt.cdlfjy.com%' OR "; } $query = trim($query); $query = trim($query,'OR'); $query = trim($query) . ")"; $result = \DB::select($query); if($result) return true; return false; } }