ExecelPHP类 简单导入功能 tp3.1.3

jerry thinkphp 2015年11月19日 收藏
ExecelPHP类 简单导入功能 tp3.1.3
  1. public function import($file =''){
  2.         
  3.         vendor('Excel.PHPExcel');
  4.         vendor('Excel.PHPExcel.IOFactory');
  5.         vendor('Excel.PHPExcel.Reader.Excel5');
  6.                 
  7.         $objReader = new PHPExcel_Reader_Excel5;
  8.         
  9.         $objPHPExcel = $objReader->load($file);
  10.                 
  11.         $sheet = $objPHPExcel->getSheet(0);
  12.         $highestRow = $sheet->getHighestRow();
  13.         $highestColumn = $sheet->getHighestColumn();
  14.         
  15.         $count = 0;
  16.         $s_count = 0;
  17.         $t_count = 0;
  18.         $b_count = 0;
  19.         $m_count = 0;
  20.         $string = '';
  21.         
  22.         for($j =2; $j <= $highestRow; $j ++){
  23.             
  24.             $haipin_id = trim($objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue()); //海品编号
  25.             $goods_name = trim($objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue()); //商品名称
  26.             $common_name = trim($objPHPExcel->getActiveSheet()->getCell("E".$j)->getValue()); //通用名
  27.             $license_number = trim($objPHPExcel->getActiveSheet()->getCell("F".$j)->getValue()); //批准文号
  28.             $spec = trim($objPHPExcel->getActiveSheet()->getCell("G".$j)->getValue()); //规格
  29.             $manufacturer = trim($objPHPExcel->getActiveSheet()->getCell("Q".$j)->getValue()); //厂家
  30.             $brand = trim($objPHPExcel->getActiveSheet()->getCell("R".$j)->getValue()); //品牌
  31.             $type = trim($objPHPExcel->getActiveSheet()->getCell("S".$j)->getValue()); //类型
  32.             $cate = trim($objPHPExcel->getActiveSheet()->getCell("U".$j)->getValue()); //分类
  33.             $keywords = trim($objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue()); //关键词
  34.             $effect = trim($objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue()); //功效
  35.             $is_drug = trim($objPHPExcel->getActiveSheet()->getCell("H".$j)->getValue()); //处方药
  36.             $drug_description = trim($objPHPExcel->getActiveSheet()->getCell("I".$j)->getValue()); //处方药说明
  37.             $form = trim($objPHPExcel->getActiveSheet()->getCell("J".$j)->getValue()); //剂型
  38.             $material = trim($objPHPExcel->getActiveSheet()->getCell("L".$j)->getValue()); //原料
  39.             $leading_effect = trim($objPHPExcel->getActiveSheet()->getCell("M".$j)->getValue()); //作用
  40.             $crowd = trim($objPHPExcel->getActiveSheet()->getCell("N".$j)->getValue()); //适用人群
  41.             $instructions = trim($objPHPExcel->getActiveSheet()->getCell("P".$j)->getValue()); //用法用量
  42.             $is_import = trim($objPHPExcel->getActiveSheet()->getCell("T".$j)->getValue()); //是否进口
  43.             $department = trim($objPHPExcel->getActiveSheet()->getCell("V".$j)->getValue()); //部门名称
  44.             $haipin = trim($objPHPExcel->getActiveSheet()->getCell("W".$j)->getValue());  //海品药店
  45.             $zhimeilan = trim($objPHPExcel->getActiveSheet()->getCell("X".$j)->getValue());  //植玫兰
  46.             $off_line = trim($objPHPExcel->getActiveSheet()->getCell("Y".$j)->getValue()); //线下
  47.             
  48.             $goods_data = array();
  49.             $empty_field = 0;
  50.             
  51.             if(empty($cate)){
  52.                 continue;
  53.             }else{
  54.     
  55.                 $cate_id = A('Admin/Cate')->check_cate_exists($cate);
  56.                 
  57.                 if(!$cate_id){
  58.                     $string .= $cate. "分类不存在分类列表,请先输入.<br/> ";
  59.                     continue;
  60.                 }else{
  61.                     $goods_data['cate_id'] = (int)$cate_id;
  62.                 }
  63.             }
  64.             
  65.             if(empty($department)){
  66.                 $goods_data['department_id'] = 0;
  67.             }else{
  68.                 
  69.                 $department_id = $this->check_department_exists($department);
  70.                 
  71.                 if(!$department_id){
  72.                     $goods_data['department_id'] = 0;
  73.                 }else{
  74.                     $goods_data['department_id'] = (int)$department_id;
  75.                 }
  76.             }
  77.             
  78.             if(empty($brand)){
  79.                 //continue;
  80.                 $goods_data['brand_id'] = 0;
  81.                 $empty_field = 1;
  82.                 
  83.             }else{
  84.                 
  85.                 $brand_id = A('Admin/Brand')->check_brand_exists($brand);
  86.                 
  87.                 if(!$brand_id){
  88.                     $data = array(
  89.                         'brand_name' => $brand,
  90.                     );
  91.                     $insert_brand_id = M('Brand')->add($data);
  92.                     if($insert_brand_id !== false){
  93.                         $b_count ++;
  94.                         $goods_data['brand_id'] = $insert_brand_id;
  95.                     }else{
  96.                         continue;
  97.                     }
  98.                 }else{
  99.                     $goods_data['brand_id'] = $brand_id;
  100.                 }
  101.             }
  102.             
  103.             if(empty($type)){
  104.                 //continue;
  105.                 $goods_data['type_id'] = 0;
  106.             }else{
  107.                 
  108.                 $type_id = A('Admin/GoodsType')->check_type_exists($type);
  109.                 
  110.                 if(!$type_id){
  111.                     $data = array(
  112.                         'type_name' => $type,
  113.                     );
  114.                     $insert_type_id = M('Goods_type')->add($data);
  115.                     if($insert_type_id !== false){
  116.                         $t_count ++;
  117.                         $goods_data['type_id'] = $insert_type_id;
  118.                     }else{
  119.                         continue;
  120.                     }
  121.                 }else{
  122.                     $goods_data['type_id'] = $type_id;
  123.                 }
  124.             }
  125.             
  126.             if(empty($spec)){
  127.                 //continue;
  128.                 $goods_data['spec_id'] = 0;
  129.                 $empty_field = 1;
  130.             }else{
  131.                 
  132.                 $spec_id = A('Admin/GoodsSpec')->check_goods_spec_exists($spec);
  133.                 
  134.                 if(!$spec_id){
  135.                     $data = array(
  136.                         'spec_name' => $spec,
  137.                     );
  138.                     $insert_spec_id = M('Goods_spec')->add($data);
  139.                     if($insert_spec_id !== false){
  140.                         $s_count ++;
  141.                         $goods_data['spec_id'] = $insert_spec_id;
  142.                     }else{
  143.                         continue;
  144.                     }
  145.                 }else{
  146.                     $goods_data['spec_id'] = $spec_id;
  147.                 }
  148.                 
  149.             }
  150.             
  151.             if(empty($manufacturer)){
  152.                 //continue;
  153.                 $goods_data['manufacturer_id'] = 0;
  154.                 $empty_field = 1;
  155.             }else{
  156.                 
  157.                 $manufacturer_id = A('Admin/Manufacturer')->check_manufacturer_exists($manufacturer);
  158.                 
  159.                 if(!$manufacturer_id){
  160.                     $data = array(
  161.                         'manufacturer_name' => $manufacturer,
  162.                     );
  163.                     $insert_manufacturer_id = M('Manufacturer')->add($data);
  164.                     if($insert_manufacturer_id !== false){
  165.                         $m_count ++;
  166.                         $goods_data['manufacturer_id'] = $insert_manufacturer_id;
  167.                     }else{
  168.                         continue;
  169.                     }
  170.                 }else{
  171.                     $goods_data['manufacturer_id'] = $manufacturer_id;
  172.                 }
  173.             }
  174.             
  175.             $goods_data['is_drug'] = array_search(strtoupper($is_drug), C(IS_DRUG));
  176.             if(empty($goods_data['is_drug'])){
  177.                 $goods_data['is_drug'] = 0;
  178.                 $empty_field = 1;
  179.             }

  180.             $goods_id = $this->is_exist_goods($goods_name, $goods_data['brand_id'], $goods_data['is_drug'], $goods_data['spec_id'], $goods_data['manufacturer_id']);
  181.                     
  182.             if(!$goods_id){
  183.                 
  184.                 $goods_data['haipin_id'] = $haipin_id;
  185.                 $goods_data['goods_name'] = $goods_name;
  186.                 $goods_data['common_name'] = $common_name;
  187.                 $goods_data['license_number'] = $license_number;
  188.                 $goods_data['keywords'] = $keywords;
  189.                 $goods_data['effect'] = $effect;
  190.                 //$goods_data['is_drug'] = array_search(strtoupper($is_drug), C(IS_DRUG));
  191.                 $goods_data['drug_description'] = $drug_description;
  192.                 $goods_data['form'] = $form;
  193.                 $goods_data['material'] = $material;
  194.                 $goods_data['leading_effect'] = $leading_effect;
  195.                 $goods_data['crowd'] = $crowd;
  196.                 $goods_data['instructions'] = $instructions;
  197.                 $goods_data['is_import'] = empty($is_import)? 0: 1;
  198.                 $goods_data['add_time'] = time();
  199.                 $goods_data['user_id'] = session('uid');

  200.                 $goods_model = M('Goods');
  201.                 $goods_model->startTrans();
  202.                 $goods_id = $goods_model->add($goods_data); 
  203.                 if($goods_id !== false){
  204.                     
  205.                     $g_new_goods_sn = '';
  206.                     $g_new_goods_sn .= str_pad((string)$goods_data['cate_id'], 1, "0" ,STR_PAD_LEFT);
  207.                     $g_new_goods_sn .= str_pad((string)$goods_id, 7, "0" ,STR_PAD_LEFT);
  208.                     $goods_model->save(array('new_goods_sn'=>$g_new_goods_sn, 'goods_id'=>$goods_id));
  209.                     
  210.                     //处理网站和商品关系
  211.                     if($haipin){
  212.                         $s_data = array(
  213.                             'site_id' =>2,
  214.                             'goods_id' =>$goods_id,
  215.                             'type' =>1,
  216.                         );
  217.                         $this->deal_with_goods_to_site($s_data);
  218.                     }
  219.                     if($zhimeilan){
  220.                         $s_data = array(
  221.                             'site_id' =>3,
  222.                             'goods_id' =>$goods_id,
  223.                             'type' =>1,
  224.                         );
  225.                         $this->deal_with_goods_to_site($s_data);
  226.                     }
  227.                     if($off_line){
  228.                         $s_data = array(
  229.                             'site_id' =>4,
  230.                             'goods_id' =>$goods_id,
  231.                             'type' =>1,
  232.                         );
  233.                         $this->deal_with_goods_to_site($s_data);
  234.                     }
  235.                     
  236.                     if(!$empty_field){
  237.                         $goods_sn = '';
  238.                         $cate_res = M('Cate')->field('alias_name')->where(array('cate_name'=>$cate))->find();
  239.                         $goods_sn .= substr($cate_res['alias_name'], 0, 2);
  240.                         $goods_sn .= str_pad((string)$goods_data['brand_id'], 5, "0" ,STR_PAD_LEFT);
  241.                         $goods_sn .= str_pad((string)$goods_data['is_drug'], 3, "0" ,STR_PAD_LEFT);
  242.                         $goods_sn .= str_pad((string)$goods_id, 5, "0" ,STR_PAD_LEFT);
  243.                         $goods_sn .= str_pad((string)$goods_data['manufacturer_id'], 5, "0" ,STR_PAD_LEFT);
  244.                         $goods_sn .= str_pad((string)$goods_data['spec_id'], 5, "0" ,STR_PAD_LEFT);
  245.                         $data = array(
  246.                             'goods_id' => $goods_id,
  247.                             'goods_sn' => $goods_sn,
  248.                         );
  249.                         $res = $goods_model->save($data);
  250.                         
  251.                         if($res !== false){
  252.                             $goods_model->commit();
  253.                         }else{
  254.                             $goods_model->rollback();    
  255.                         }
  256.                         
  257.                     }else{
  258.                         $goods_model->commit();
  259.                     }
  260.                     
  261.                     $count ++;
  262.                     
  263.                 }else{
  264.                     $goods_model->rollback();
  265.                     $string .= $haipin_id.'/'.$goods_name. "录入失败.<br/> ";
  266.                 }
  267.                 
  268.             }else{
  269.                 $string .= $haipin_id.'/'.$goods_name. "已经存在.<br/> ";
  270.             }
  271.         }
  272.         
  273.         $o_string = '';
  274.         $o_string .= '本次操作录入'. $count. '个产品, ';
  275.         $o_string .= '录入'. $s_count. '个规格, ';
  276.         $o_string .= '录入'. $t_count. '个类型, ';
  277.         $o_string .= '录入'. $b_count. '个品牌, ';
  278.         $o_string .= '录入'. $m_count. "个厂家.<br/><br/> ";
  279.         
  280.         return '<div style="font-size;12px">'.$o_string . $string.'</div>';
  281.     }