<?php

namespace wstmart\admin\model;

/**

 * ============================================================================

 * 图片空间业务处理

 */

use wstmart\common\model\Aliyunoss;

use think\Db;

class Images extends Base{

	/**

	 * 获取图片空间概况

	 */

	public function summary(){

		$rs = Db::name('images')->where(['dataFlag'=>1])->field('fromTable,isUse,sum(imgSize) imgSize')->group('fromTable,isUse')

		        ->order('fromTable asc')->select();

		//获取目录名称

		$rs2 = Db::name('datas')->where(['catId'=>3])->field('dataName,dataVal')->select();

		$imagesMap = [];

		foreach ($rs2 as $key =>$v){

			$imagesMap[$v['dataVal']] = $v['dataName'];

		}

	    $images = [];

		foreach ($rs as $key =>$v){

			if(!isset($images[$v['fromTable']]))$images[$v['fromTable']] = ['directory'=>'','data'=>['0'=>0,'1'=>0]];

			if(isset($imagesMap[$v['fromTable']]))$images[$v['fromTable']]['directory'] = $imagesMap[$v['fromTable']];

		    $images[$v['fromTable']]['data'][$v['isUse']] = round($v['imgSize']/1024/1024,2);

		}

		$maxSize = 0;

		foreach ($images as $key =>$v){

			$size = (float)$v['data']['0']+(float)$v['data']['1'];

			if($maxSize<$size)$maxSize = $size;

		}

		$images['_WSTSummary_'] = $maxSize;

		return $images;

	}

	/**

	 * 获取记录

	 */

	public function pageQuery(){

		$key = input('keyword');

		$isUse = (int)input('isUse');

		$where = ['fromTable'=>$key,'a.dataFlag'=>1];

		if($isUse !=-1)$where['isUse'] = $isUse;

		$page = $this->alias('a')->join('__USERS__ u','a.ownId=u.userId and fromType=0','left')

		            ->join('__SHOPS__ s','s.userId=u.userId','left')

		            ->join('__STAFFS__ sf','sf.staffId=a.ownId','left')

		            ->where($where)->field('a.imgId,u.loginName,u.userType,fromType,sf.loginName loginName2,s.shopName,imgPath,imgSize,isUse,a.createTime')

		            ->order('a.imgId desc')->paginate(input('post.limit/d'))->toArray();

		foreach ($page['Rows'] as $key => $v){

			if($v['fromType']==1){

				$page['Rows'][$key]['loginName'] = $v['loginName2'];

			}

			$page['Rows'][$key]['imgSize'] = round($v['imgSize']/1024/1024,2);

			unset($page['Rows'][$key]['loginName2']);

		}

		return $page;

	}

	/**

	 * 删除图片

	 */

	public function del(){

		$id = (int)input('id');

		$image = $this->where('imgId',$id)->find();

		$rs = $this->where('imgId',$id)->update(['dataFlag'=>-1]);

		if(false !== $rs){

			$m = WSTConf('CONF.wstMobileImgSuffix');

			$timgPath =  str_replace('.','_thumb.',$image['imgPath']);

			$mimgPath =  str_replace('.',$m.'.',$image['imgPath']);

	        $mtimgPath = str_replace('.',$m.'_thumb.',$image['imgPath']);

	        

			if(file_exists(WSTRootPath()."/".$image['imgPath']))unlink(WSTRootPath()."/".$image['imgPath']); 

			if(file_exists(WSTRootPath()."/".$timgPath))unlink(WSTRootPath()."/".$timgPath); 

			if(file_exists(WSTRootPath()."/".$mimgPath))unlink(WSTRootPath()."/".$mimgPath); 

			if(file_exists(WSTRootPath()."/".$mtimgPath))unlink(WSTRootPath()."/".$mtimgPath);

			//添加删除oss上的图片 mark 20180608

			$imgurl = getImgUrl().$image['imgPath'];

			if(file_exists_oss($image['imgPath']))model('Aliyunoss')->del($image['imgPath']);

			return WSTReturn("删除成功", 1);

		}

		return WSTReturn("删除失败");		

	}

	//添加判断文件是否存在 mark 20180608 by zl

	// public function file_exists_oss($object){

	// 	$imgurl = getImgUrl().$object;

	// 	$ch = curl_init();

	// 	    $timeout = 10;

	// 	    curl_setopt ($ch, CURLOPT_URL, $imgurl);

	// 	    curl_setopt($ch, CURLOPT_HEADER, 1);

	// 	    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

	// 	    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);



	// 	    $contents = curl_exec($ch);

	// 	if (preg_match("/404/", $contents)){

	//         return false;

	//     }else{

	//     	return true;

	//     }

   

	// 	//说是在windows下可以,LINUX下无论图片在不在都返加TRUE

	// 	// if(file_get_contents($imgurl,0,null,0,1)){

	// 	// 	return true;

	// 	// }else{

	// 	// 	return false;

	// 	// }



	// 	// if(@fopen( $imgurl, 'r' )){

	// 	// 	return true;

	// 	// }else{

	// 	// 	return false;

	// 	// }

		

	 

	// }

}