<?php

/**
 * @author darkredz
 */

class SoftController extends DooController {

	public $webPath="http://cld.smartcost.com.cn/upload/emailAnnex/";

	function __construct() {
		
	}

	function longleTest(){
		$this->render ( "/test", array() );
	}
	
	function categoryApi(){
		Doo::loadModel ( 'L_category' );
		$L_category = new L_category ();
		$category=$L_category->getCategoryBySoft();
		header ( 'Content-Type:text/html;charset=utf-8' );
		echo json_encode(array('status'=>1,'categoryList'=>$category),JSON_UNESCAPED_UNICODE);die;
	}

	function productApi(){
		Doo::loadModel ( 'product' );
		$product = new product ();
		$product=$product->getProudct();
		header ( 'Content-Type:text/html;charset=utf-8' );
		echo json_encode(array('status'=>1,'productList'=>$product),JSON_UNESCAPED_UNICODE);die;
	}
	
	function addLockApi(){

		$longleList=isset($_POST['longle'])?$_POST['longle']:"";

        header ( 'Content-Type:text/html;charset=utf-8' );
		if($longleList!=""){
			Doo::loadModel ( 'longle' );
			Doo::loadModel ( 'longle_log' );

			//$exlArray=$this->_format_excel_to_array(DOO::conf()->SITE_PATH."upload/".iconv("UTF-8", "GB2312", $path));
            //file_put_contents ( "protected/config/longle.bak", $longleList );

			$longleList=json_decode($longleList,TRUE);

			if (!is_array($longleList)){
				echo json_encode(array('status'=>2,'msg'=>'锁数据不正确'),JSON_UNESCAPED_UNICODE);die;
			}

			//数组格式化，新增字段需要在这新增
			$longleList=$this->array_unique_fb($longleList);

			//$category=$L_category->getCategoryById($cid);
			try {
			Doo::db()->beginTransaction();
			foreach ($longleList as $key=>$value){

				//锁记录
				$longle = new longle ();
				$longle->key_num=$value['key_num'];
				$longle->product=$value['product'];
				$longle->make_day=$value['make_day'];
				$longle->alloted_time=$value['alloted_time'];
				$longle->status=1;
				$longle->statusT="生成";
				$longle->cid=$value['cid'];
				$longle->category=$value['title'];
				$longle->SerialNumber=$value['serialnumber'];
				$longle->version=$value['version'];
				$longle->licences=$value['licences'];
				$lid=$longle->insert();

				//锁日志记录
				$longle_log=new longle_log();
				$longle_log->lid=$lid;
				$longle_log->status=1;
				$longle_log->statusT="生成";
				$longle_log->operator='软件生成';
				$longle_log->product=$longle->product;
				$longle_log->category='总部';
				$longle_log->dateline=date("Y-m-d");
				$id=$longle_log->insert();

			}
			//记录日常行为
			Doo::loadModel("action_log");
			$action_log=new action_log();
			$action_log->action='总部<b href="javascript:void(0)"  >软件</b>生成<a href="javascript:void(0)" onclick="showLook(\''.$longleList[0]['key_num'].'\')">'.$longleList[0]['key_num'].'</a>等<b>'.count($longleList).'</b>个锁';
			$action_log->sid=43;
			$action_log->cid=12;
			$action_log->class=$action_log->iconGenerate;
			$action_log->updatetime=date("Y-m-d");
			$action_log->time=date("H:i");
			$action_log->status=11;

			$action_log->insert();
			Doo::db()->commit();
			
			
			} catch (Exception $e) {
				echo json_encode(array('status'=>2,'msg'=>'数据录入发生异常'),JSON_UNESCAPED_UNICODE);
				//print $e->getMessage();
				die;
			}
			echo json_encode(array('status'=>1,'msg'=>'入库'.count($longleList).'个'),JSON_UNESCAPED_UNICODE);die;
		}else{
			echo json_encode(array('status'=>2,'msg'=>'请求参数不正确或者为空'),JSON_UNESCAPED_UNICODE);die;
		}
	}

	function postEmail($elid){
		
		if (!empty($elid)){
			
				
			    $url = 'http://sendcloud.sohu.com/webapi/mail.send.json';
			
			   
			     $param = array('api_user' => 'cldmail',
			            'api_key' => 'cDO1GjtY1seH',
			    		'to'=>'cpthought@vip.qq.com',
			            'from' =>'cpthought@vip.qq.com',
			            'fromname' => '软件导入CLD锁接口',
			            'subject' => '软件导入CLD锁接口通知',
			            'html' => $elid);

			    $post_data = http_build_query($param);

			    $ch = curl_init();
			    curl_setopt($ch, CURLOPT_POST, 1);
			    curl_setopt($ch, CURLOPT_URL,$url);
			    curl_setopt($ch, CURLOPT_HEADER, false); //设定是否输出页面内容
				curl_setopt($ch, CURLOPT_NOBODY, false);
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
				
			    $result =curl_exec($ch);
			    curl_close($ch);

		}
	}
	
	
	function array_unique_fb($array2D){
		 foreach ($array2D as $k=>$v){
  $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
  $temp[$k]=$v;
 }
 $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组 
 foreach ($temp as $k => $v){
  $array=explode(',',$v); //再将拆开的数组重新组装
  //下面的索引根据自己的情况进行修改即可
  $temp2[$k]['key_num'] =$array[0];
  $temp2[$k]['serialnumber'] =$array[1];
  $temp2[$k]['product'] =$array[2];
  $temp2[$k]['make_day'] =$array[3];
  
  $temp2[$k]['alloted_time'] =$array[4];
  $temp2[$k]['version'] =$array[5];
  $temp2[$k]['cid'] =$array[6];
  $temp2[$k]['title'] =$array[7];
     $temp2[$k]['licences'] =$array[8];
 }
 

 
 return $temp2;
	}
	
	
	
	/**
	 * 格式化excel文件为数组
	 * @param unknown_type $file_url
	 * @param unknown_type $mcid
	 */
	function _format_excel_to_array($file_url = "") {
		if (! file_exists ( $file_url ))
			return array ();


		Doo::loadClass ( 'PHPExcel' );

		$PHPExcel = new PHPExcel ();

		$PHPReader = new PHPExcel_Reader_Excel2007 ();

		if (! $PHPReader->canRead ( $file_url )) {
			$PHPReader = new PHPExcel_Reader_Excel5 ();
			if (! $PHPReader->canRead ( $file_url )) {

				echo 'no Excel';
				return;
			}
		}

		$PHPExcel = $PHPReader->load ( $file_url );
		/**读取excel文件中的第一个工作表*/
		$currentSheet = $PHPExcel->getSheet ( 0 );
		/**取得最大的列号*/
		$allColumn = $currentSheet->getHighestColumn ();
		/**取得一共有多少行*/
		$allRow = $currentSheet->getHighestRow ();

		$excel_array = array ();

		for($currentRow = 2; $currentRow <= $allRow; $currentRow ++) {

			$excel_column = array ();

			//后期改进
			if($allColumn=='AM')
				$allColumn='Z';

			/**从第A列开始输出*/
			for($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn ++) {

				$val = $currentSheet->getCellByColumnAndRow ( ord ( $currentColumn ) - 65, $currentRow )->getValue ();

				/**ord()将字符转为十进制数 iconv ( 'utf-8', 'gb2312',*/
				$val = "'" . addslashes ( $val ) . "'";
					if ($currentColumn == 'A')
						$excel_column ['key_num'] = $val;
					elseif ($currentColumn == 'C')
						$excel_column ['product'] = $val;
					elseif ($currentColumn == 'D'){

						$val=str_replace("'","",$val);

						$excel_column ['make_day'] = $this->excelTime($val);
					}elseif($currentColumn == 'E'){
						$val=str_replace("'","",$val);

						$excel_column ['alloted_time'] = $this->excelTime($val);
					}elseif($currentColumn == 'F'){
						$val=str_replace("'","",$val);

						$excel_column ['version'] = $val;
					}elseif ($currentColumn == 'B'){
						$val=str_replace("'","",$val);
						$excel_column ['SerialNumber'] = $val;
					}

			}
			array_push ( $excel_array, $excel_column );
		}

		return $excel_array;
	}


}

?>