zoukankan      html  css  js  c++  java
  • 序列号导出到csv的实现

        //导出到csv
        public function exportCsvByIds($ids){
            header("Content-type:text/html;charset=utf-8");
            $license = M('license');
            $where ="id in($ids) and status=1 ";
            //获取序列号列表
            $list = $license->field('id,license,sku_id,if_sku,version_id,store_id,user_id,create_time as generate_time,concat(concat(left(effective_start,10),"~"),left(effective_end,10)) as effective,IFNULL(act_status,1) as act_status,if(act_status=1,"待审核",review_time) as review_time,IFNULL(use_status,1) as use_status,if(use_status=1,"未使用",use_time) as use_time')       
                    ->where($where)
                    ->order('id desc')
                    ->select();
            if(!empty($list)){
                //商品sku_id
                $sku_ids=array_unique(array_column($list,'sku_id'));
                //蜂店版本id
                $version_ids=array_unique(array_column($list,'version_id'));
                //蜂店id
                $store_ids=array_unique(array_column($list,'store_id'));
                //用户id
                $user_ids=array_unique(array_column($list,'user_id'));
    
                //获取商品信息
                $goods_where['sku_id']=array('in',$sku_ids);
                $goods_list=M('goods_sku')->field('sku_id,title')->where($goods_where)->select();
                foreach($goods_list as $k=>$v){
                    $goods_info[$v['sku_id']]=$v['title'];
                }
                //获取蜂店版本信息
                $version_where['id']=array('in',$version_ids);
                $version_list=M('store_version')->field('id,name')->where($version_where)->select();
                foreach($version_list as $k=>$v){
                    $version_info[$v['id']]=$v['name'];
                }
                //获取用户手机号
                $user_where['user_id']=array('in',$user_ids);
                $user_list=M('user')->field('user_id,username')->where($user_where)->select();
                foreach($user_list as $k=>$v){
                    $user_info[$v['user_id']]=$v['username'];
                }
                //获取店铺名称
                $store_where['id']=array('in',$store_ids);
                $store_list=M('store')->field('id,user_id,name')->where($store_where)->select();
                foreach($store_list as $k=>$v){
                    $store_info[$v['user_id']]=$v['name'];
                }
                //获取蜂店服务时间
                $store_extend_where['user_id']=array('in',$user_ids);
                $store_extend_list=M('store_extend')->field('user_id,store_id,add_time as start_time,end_time,IFNULL(active_status,1) as active_status,active_time')->where($store_extend_where)->select();
                foreach($store_extend_list as $k=>$v){
                    $store_extend_info[$v['user_id']]['start']=$v['start_time'];
                    $store_extend_info[$v['user_id']]['end']=$v['end_time'];
                    $store_extend_info[$v['user_id']]['active_status']=$v['active_status'];
                    $store_extend_info[$v['user_id']]['active_time']=$v['active_time'];
                }
                //序列号列表拼装数据
                foreach($list as $k=>$v){
                    if($v['if_sku']==1){
                        $list[$k]['goods_title']="无赠品";
                    }else{
                        $list[$k]['goods_title']=$goods_info[$v['sku_id']];
                    }
                    if(empty($v['user_id'])){
                        $list[$k]['store_name']=null;
                    }else{
                        $list[$k]['store_name']=$store_info[$v['user_id']];
                    }
                    $list[$k]['store_version']=$version_info[$v['version_id']];
                    $list[$k]['phone']=$user_info[$v['user_id']];
                    $list[$k]['start_time']=$store_extend_info[$v['user_id']]['start'];
                    $list[$k]['end_time']=$store_extend_info[$v['user_id']]['end'];
                    $active_status=$store_extend_info[$v['user_id']]['active_status'];
                    if($active_status==1){
                        $active_status="未激活";
                    }else{
                        $active_status=$store_extend_info[$v['user_id']]['active_time'];
                    }
                    if(empty($active_status)){
                        $active_status="未激活";
                    }
                    $list[$k]['active_time']=$active_status;
                    unset($list[$k]['sku_id']);
                    unset($list[$k]['if_sku']);
                    unset($list[$k]['version_id']);
                    unset($list[$k]['store_id']);
                    unset($list[$k]['user_id']);
                }
            }else{
                $list=array();
            }
            $public=D("Public");
            $string=$public->getutf8("序列号,蜂店版本,生成时间,有效期,审核时间,使用时间,激活时间,店铺名称,店主手机号,店铺服务时间,赠品")."
    ";
            //状态转变为中文
            foreach ($list as $k=>$v){
                 $t3=null;
                 if($v['start_time']==null && $v['end_time'] !=null){
                    $t3=$v['end_time'];
                 }
                 if($v['start_time'] !=null && $v['end_time']==null){
                    $t3=$v['start_time'];
                 }
                 if($v['start_time'] !=null && $v['end_time'] !=null){
                    $t3=$v['start_time']."~".$v['end_time'];
                 }
                 $string .=$public->getutf8($v['license'])."	,".$public->getutf8($v['store_version'])."	,".$public->getutf8($v['generate_time'])."	,".$public->getutf8($v['effective'])."	,".$public->getutf8($v['review_time'])."	,".$public->getutf8($v['use_time'])."	,".$public->getutf8($v['active_time'])."	,".$public->getutf8($v['store_name'])."	,".$public->getutf8($v['phone'])."	,".$public->getutf8($t3)."	,".$public->getutf8($v['goods_title'])."
    ";
            }
             $fileName=date('YmdHis').rand(1000, 9999).".csv";//文件名
             $path='./Public/Data/Tmall/License/';
             if(!is_dir($path)){
                 mkdir($path,0777);
             }
             $sh=file_put_contents($path.$fileName, $string);
             //根据id获取渠道id
             $re2=$license->alias("A")->field('cate_id')->where($where)->find();
             if(!$sh){
                 $params=array(
                     'message'=>$_SESSION['user']['true_name'].":导出序列号到csv失败,".$sh,
                     'userid'=>$_SESSION['user']['personnel_code'],
                     'created'=>date('Y-m-d H:i:s')
                 );
                 $res=$this->writeLog('log_error',$params);   
                 if(!$res){
                     return 101;
                 }
                 return 102;
             }
             $params=array(
                'message'=>$_SESSION['user']['true_name'].":导出序列号[".$ids."]到EXCEL",
                'create_time'=>date('Y-m-d H:i:s',time()),
                'create_id'=>$_SESSION['user']['personnel_code'],
                'cate_id'=>$re2['cate_id']
             );
             $this->writeLog('license_log',$params);
             $base_url="http://".$_SERVER['SERVER_NAME'];
             return array('path'=>$base_url.trim($path,'.').$fileName);
        }
  • 相关阅读:
    使用pandas的get_dummies对类目型的特征因子化
    关于RandomForestRegressor,补全null数值
    关于train_test_split和cross_val_score交叉检验
    关于seaborn
    正态分布
    单下划线或双下划线的意义
    MFC中关于运行时类信息及动态创建对象的两个宏的意义(转)
    DPDK
    根据结构体成员地址得到结构体入口地址,内核代码
    多线程频繁写全局变量导致性能降低
  • 原文地址:https://www.cnblogs.com/zouke1220/p/9109924.html
Copyright © 2011-2022 走看看