package com.hh.search.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.hh.base.BaseAction;
import com.hh.common.CommonUtil;
import com.hh.common.Constant;
import com.hh.common.service.CommonService;
import com.hh.search.service.AdvancedSearchService;
import com.hh.search.service.QuickSearchService;
import com.hh.vo.ClickStatisticVo;
import com.hh.vo.DictionaryVo;
import com.hh.vo.Form;
import com.hh.vo.ImageVo;
import com.hh.vo.StatisticVo;
import com.hh.vo.UserVo;
import com.hh.vo.WebsiteVo;

/**
 * 高级检索Action
 * 
 * @author user
 * 
 */
public class AdvancedSearchAction extends BaseAction {

	/**
	 * 运算符数据
	 */
	private List<DictionaryVo> operators;

	/**
	 * 文档类型数据
	 */
	private List<DictionaryVo> documentTypes;

	/**
	 * 高级查询SERVICE
	 */
	private AdvancedSearchService advancedSearchService;

	/**
	 * 快速查询SERVICE
	 */
	private QuickSearchService quickService;

	/**
	 * 公共SERVICE
	 */
	private CommonService commonService;

	/**
	 * 上传文件
	 */
	private File image;

	/**
	 * 上传文件名
	 */
	private String imageFileName;

	/**
	 * 上传文件类型
	 */
	private String imageContentType;

	/**
	 * 返回的匹配网页结果
	 */
	private List<WebsiteVo> webSiteList;

	/**
	 * 返回的匹配图片结果
	 */
	private List<ImageVo> imageList;

	/**
	 * 关键字
	 */
	private String keyWords;

	/**
	 * 第一次检索出来的网页ID链
	 */
	private String webIds;

	/**
	 * 扩展查询项1
	 */
	private List<Map<String, Object>> expansionList1;

	/**
	 * 扩展查询项2
	 */
	private List<Map<String, Object>> expansionList2;

	/**
	 * 跳转高级检索页面
	 * 
	 * @return
	 */
	public String goAdvancedSearch() {
		operators = commonService.queryDictionaryByList("operator");
		documentTypes = commonService.queryDictionaryByList("document_type");
		return SUCCESS;
	}

	/**
	 * 图片查询
	 * 
	 * @return
	 */
	public String imageQuery() {
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		UserVo user = (UserVo) session.get(Constant.USER);
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			fos = new FileOutputStream(realpath + "/" + imageFileName);
			fis = new FileInputStream(image);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		ImageVo imageVo = advancedSearchService.queryImageInfo(imageFileName);
		if (imageVo != null) {
			imageList = advancedSearchService.queryImageByImage(imageVo);
			webSiteList = advancedSearchService.queryWebsitByImage(imageVo);
		}
		return SUCCESS;
	}

	/**
	 * 文本查询
	 * 
	 * @return
	 */
	public String textQuery() {
		operators = commonService.queryDictionaryByList("operator");
		documentTypes = commonService.queryDictionaryByList("document_type");
		UserVo user = (UserVo) session.get(Constant.USER);
		this.form.setUser_id(user.getId());
		webSiteList = advancedSearchService.queryWebsiteByText(this.form);
		imageList = advancedSearchService.queryImageByText(this.form);
		session.put(Constant.WEB_CLICK_STATISTIC,
				CommonUtil.createClickStatisticVo());
		return SUCCESS;
	}

	/**
	 * academic paper retrieval“那块在显示结果的时候只需要有webpage，不需要有image
	 * 
	 * @return
	 */
	public String academicQuery() {
		operators = commonService.queryDictionaryByList("operator");
		documentTypes = commonService.queryDictionaryByList("document_type");
		UserVo user = (UserVo) session.get(Constant.USER);
		this.form.setUser_id(user.getId());
		webSiteList = advancedSearchService.queryWebsiteByText(this.form);
		return SUCCESS;
	}

	/**
	 * ”non- academic paper retrieval“那块在显示结果的时候webpage和image都得有
	 * 
	 * @return
	 */
	public String academicQuery2() {
		operators = commonService.queryDictionaryByList("operator");
		documentTypes = commonService.queryDictionaryByList("document_type");
		UserVo user = (UserVo) session.get(Constant.USER);
		this.form.setUser_id(user.getId());
		webSiteList = advancedSearchService.queryWebsiteByText(this.form);
		imageList = advancedSearchService.queryImageByText(this.form);
		return SUCCESS;
	}

	/**
	 * 跳转扩展查询
	 * 
	 * @return
	 */
	public String goExpansionQuery() {
		operators = commonService.queryDictionaryByList("operator");
		UserVo user = (UserVo) session.get(Constant.USER);
		documentTypes = commonService.queryDictionaryByList("document_type");
		ClickStatisticVo cvo = (ClickStatisticVo) session
				.get(Constant.WEB_CLICK_STATISTIC);
		List<StatisticVo> svoList = cvo.getList();
		if (svoList != null && svoList.size() > 0) {
			StatisticVo vo1 = svoList.get(0);
			double rate1 = vo1.getRate();
			StatisticVo vo2 = null;
			double rate2 = 0;
			if (svoList.size() > 1) {
				vo2 = svoList.get(1);
				rate2 = vo2.getRate();
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", user.getId());
			if (vo2 != null) {
				if ((rate1 >= 0.6 && rate2 < 0.3)
						|| (rate1 >= 0.4 && rate1 <= 0.6 && rate2 < 0.1)
						|| rate2 == 0) {// 只扩展term1
					map.put("term1", vo1.getId());
					map.put("term1_rate", vo1.getRate());
				} else if ((rate2 >= 0.6 && rate1 < 0.3)
						|| (rate2 >= 0.4 && rate2 <= 0.6 && rate1 < 0.1)
						|| rate1 == 0) {// 只扩展term2
					map.put("term2", vo2.getId());
					map.put("term2_rate", vo2.getRate());
				} else {// term1 term2都扩展
					map.put("term1", vo1.getId());
					map.put("term2", vo2.getId());
					map.put("term1_rate", vo1.getRate());
					map.put("term2_rate", vo2.getRate());
				}
			} else {
				map.put("term1", vo1.getId());
				map.put("term1_rate", vo1.getRate());
			}
			map.put("word", form.getWord());
			expansionList1 = advancedSearchService.queryExpansionList(map);
			if (CommonUtil.isNotNull(form.getWord2_select())
					&& !"3".equals(form.getWord2_select())
					&& CommonUtil.isNotNull(form.getWord2())) {
				map.put("word", form.getWord2());
				expansionList2 = advancedSearchService.queryExpansionList(map);
			}
		}
		return SUCCESS;
	}

	/**
	 * 扩展查询
	 * 
	 * @return
	 */
	public String expansionQuery() {
		operators = commonService.queryDictionaryByList("operator");
		documentTypes = commonService.queryDictionaryByList("document_type");
		UserVo user = (UserVo) session.get(Constant.USER);
		this.form.setUser_id(user.getId());
		webSiteList = advancedSearchService.queryWebsiteByText(this.form);
		imageList = advancedSearchService.queryImageByText(this.form);
		String[] words = this.form.getWord_exs();
		if (words != null) {
			for (String word : words) {
				webSiteList.addAll(quickService.queryWebsitResults(word));
				imageList.addAll(quickService.queryImageResults(word));
			}
		}
		String[] words2 = this.form.getWord2_exs();
		if (words2 != null) {
			for (String word : words2) {
				webSiteList.addAll(quickService.queryWebsitResults(word));
				imageList.addAll(quickService.queryImageResults(word));
			}
		}
		return SUCCESS;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public List<DictionaryVo> getOperators() {
		return operators;
	}

	public void setOperators(List<DictionaryVo> operators) {
		this.operators = operators;
	}

	public List<DictionaryVo> getDocumentTypes() {
		return documentTypes;
	}

	public void setDocumentTypes(List<DictionaryVo> documentTypes) {
		this.documentTypes = documentTypes;
	}

	public AdvancedSearchService getAdvancedSearchService() {
		return advancedSearchService;
	}

	public void setAdvancedSearchService(
			AdvancedSearchService advancedSearchService) {
		this.advancedSearchService = advancedSearchService;
	}

	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public List<WebsiteVo> getWebSiteList() {
		return webSiteList;
	}

	public void setWebSiteList(List<WebsiteVo> webSiteList) {
		this.webSiteList = webSiteList;
	}

	public List<ImageVo> getImageList() {
		return imageList;
	}

	public void setImageList(List<ImageVo> imageList) {
		this.imageList = imageList;
	}

	public String getKeyWords() {
		return keyWords;
	}

	public void setKeyWords(String keyWords) {
		this.keyWords = keyWords;
	}

	public String getWebIds() {
		return webIds;
	}

	public void setWebIds(String webIds) {
		this.webIds = webIds;
	}

	public List<Map<String, Object>> getExpansionList1() {
		return expansionList1;
	}

	public void setExpansionList1(List<Map<String, Object>> expansionList1) {
		this.expansionList1 = expansionList1;
	}

	public List<Map<String, Object>> getExpansionList2() {
		return expansionList2;
	}

	public void setExpansionList2(List<Map<String, Object>> expansionList2) {
		this.expansionList2 = expansionList2;
	}

	public QuickSearchService getQuickService() {
		return quickService;
	}

	public void setQuickService(QuickSearchService quickService) {
		this.quickService = quickService;
	}

}
