package org.ccpit.business.visaAgencyManage;



import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import net.sf.json.JSONArray;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.ccpit.base.controller.Convert;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageBo;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.logManage.LogInfoService;
import org.ccpit.base.user.User;
import org.ccpit.base.utils.DateConvert;
import org.ccpit.base.utils.StringUtil;
import org.ccpit.base.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Transactional
@Service
public class VisaAgencyService {
	@Autowired
	private VisaAgencyDao visaAgencyDao;
	@Autowired
	private LogInfoService logInfoService;

	public boolean addOrUpdateVisaAgency(VisaAgency visaAgency, String operate,
                                         String visaAgencyId, User user, HttpServletRequest request) {
		boolean flag = false;
		if ("add".equals(operate)) {
			visaAgency.setCreateTime(new Date());
			visaAgency.setCreator(user);
			flag = visaAgencyDao.save(visaAgency);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "新增签证机构数据信息");
			}
		} else if ("edit".equals(operate)) {
			VisaAgency visaAgency1 = queryVisaAgencyById(Long.parseLong(visaAgencyId));
			PageUtil.setterValues(visaAgency, visaAgency1);
			visaAgency1.setModifyTime(new Date());
			visaAgency1.setModifyBy(user);
			flag = visaAgencyDao.update(visaAgency1);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "修改签证机构数据信息");
			}
		}
		return flag;
	}

	public boolean deleteVisaAgencyById(String jsonIds) {
		JSONArray jsonArray = JSONArray.fromObject(jsonIds);
		Object[] ids = jsonArray.toArray();
		boolean flag = false;
		for (Object metadataId : ids) {
			VisaAgency visaAgency = visaAgencyDao.queryById(Long.valueOf(String
					.valueOf(metadataId)));
			visaAgency.setDeleted(true);
			flag = visaAgencyDao.update(visaAgency);
		}
		return flag;
	}

	public VisaAgency queryVisaAgencyById(long id) {
		return visaAgencyDao.queryById(id);
	}
	
	public boolean queryVisaAgencyByTitle(String visaAgencyName) {
		if (null != visaAgencyName && !"".equals(visaAgencyName)) {
			try {
				visaAgencyName = java.net.URLDecoder.decode(visaAgencyName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<VisaAgency> visaAgencyList = visaAgencyDao.query(
				"from VisaAgency where visaAgencyName = '" + visaAgencyName + "'", null);
		if (null != visaAgencyList && visaAgencyList.size() > 0) {
			return true;
		}
		return false;
	}
	
	public Page<VisaAgency> queryVisaAgencys(PageRequest pageRequest, String visaAgencyName) {
		StringBuffer hql = new StringBuffer("from VisaAgency where 1=1");
		if (visaAgencyName != null && !"".equals(visaAgencyName)) {
			hql.append(" and visaAgencyName like '%" + visaAgencyName + "%'");
		}
		hql.append(" order by createTime desc");
		return visaAgencyDao.findPage(pageRequest, hql.toString(), null);
	}

	public Map<String, Object> convertToMap(VisaAgency visaAgency) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (visaAgency == null) {
			return map;
		}
		map.put("id", visaAgency.getId());
		map.put("visaAgencyName", visaAgency.getVisaAgencyName());
		map.put("province", visaAgency.getProvince());
		map.put("city", visaAgency.getCity());
		map.put("contactPerson", visaAgency.getContactPerson());
		map.put("websiteUrl", visaAgency.getWebsiteUrl());
		map.put("address", visaAgency.getAddress());
		map.put("phone", visaAgency.getPhone());
		map.put("createTime",
				StringUtil.DATE_FORMATE.format(visaAgency.getCreateTime()));
		map.put("creator", visaAgency.getCreator().getUsername());
		return map;
	}

	public PageBo<VisaAgency> convert(Page<VisaAgency> page) {
		return new PageBo<VisaAgency>(page, new Convert<VisaAgency>() {
			@Override
			public Map<String, Object> convert(VisaAgency obj) {
				return convertToMap(obj);
			}
		});
	}

	public List<Field> createVisaAgencysIndex(VisaAgency visaAgency) {
		List<Field> fields = new ArrayList<Field>();
		Field id = new Field("id", "n" + visaAgency.getId(), Store.YES,
				Index.NOT_ANALYZED);
		fields.add(id);
		Field visaAgencyName = new Field("visaAgencyName", visaAgency.getVisaAgencyName(), Store.YES,
				Index.ANALYZED);
		fields.add(visaAgencyName);
		Field createTime = new Field("createTime",
				DateConvert.convertDateWithoutHms(visaAgency.getCreateTime()),
				Store.YES, Index.NO);
		fields.add(createTime);
		return fields;
	}
}
