package org.ccpit.business.coApplication;

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 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;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class CoApplicationInfoService {

	@Autowired
	private CoApplicationInfoDao coApplicationInfoDao;
	@Autowired
	private LogInfoService logInfoService;


	public boolean addOrUpdateCoApplicationInfo(CoApplicationInfo coApplicationInfo, String operate,
												String coApplicationInfoId, User user, HttpServletRequest request) {
		boolean flag = false;
		if ("add".equals(operate)) {
			coApplicationInfo.setCreateTime(new Date());
			coApplicationInfo.setCreator(user);
			flag = coApplicationInfoDao.save(coApplicationInfo);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "新增CO数据信息");
			}
		} else if ("edit".equals(operate)) {
			CoApplicationInfo coApplicationInfo1 = queryCoApplicationInfoById(Long.parseLong(coApplicationInfoId));
			PageUtil.setterValues(coApplicationInfo, coApplicationInfo1);
			coApplicationInfo1.setModifyTime(new Date());
			coApplicationInfo1.setModifyBy(user);
			flag = coApplicationInfoDao.update(coApplicationInfo1);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "修改CO数据信息");
			}
		}
		return flag;
	}
	public boolean addCoApplicationInfo(CoApplicationInfo coApplicationInfo, 
			 HttpServletRequest request,User user) {
		User u = new User();
		u.setId(1);
		boolean flag = false;
			coApplicationInfo.setCreateTime(new Date());
			if(user!=null){
				coApplicationInfo.setCreator(user);
			}else{
				coApplicationInfo.setCreator(u);
			}
			
			flag = coApplicationInfoDao.save(coApplicationInfo);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "新增CO数据信息");
			}
		
		return flag;
	}
	
	public boolean deleteCoApplicationInfoById(String jsonIds) {
		JSONArray jsonArray = JSONArray.fromObject(jsonIds);
		Object[] ids = jsonArray.toArray();
		boolean flag = false;
		for (Object metadataId : ids) {
			CoApplicationInfo coApplicationInfo = coApplicationInfoDao.queryById(Long.valueOf(String
					.valueOf(metadataId)));
			coApplicationInfo.setDeleted(true);
			flag = coApplicationInfoDao.update(coApplicationInfo);
		}
		return flag;
	}

	public CoApplicationInfo queryCoApplicationInfoById(long id) {
		return coApplicationInfoDao.queryById(id);
	}

	public boolean queryCoApplicationInfoByData(String ccpitCode,String registNumber) {
		if (null != ccpitCode && !"".equals(ccpitCode)&&null != registNumber && !"".equals(registNumber)) {
			try {
				ccpitCode = java.net.URLDecoder.decode(ccpitCode, "UTF-8");
				registNumber = java.net.URLDecoder.decode(registNumber, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<CoApplicationInfo> coApplicationInfoList = coApplicationInfoDao.query(
				"from CoApplicationInfo where ccpitCode = '" + ccpitCode + "' and registNumber='" + registNumber +"'", null);
		if (null != coApplicationInfoList && coApplicationInfoList.size() > 0) {
			return true;
		}
		return false;
	}
	
	public Page<CoApplicationInfo> queryCoApplicationInfos(PageRequest pageRequest, String ccpitCode,
														   String registNumber) {
		StringBuffer hql = new StringBuffer("from CoApplicationInfo where 1=1");
		if (ccpitCode != null && !"".equals(ccpitCode)) {
			hql.append(" and ccpitCode like '%" + ccpitCode + "%'");
		}
		if (registNumber != null && !"".equals(registNumber)) {
			hql.append(" and registNumber like '%"+registNumber+"%'" );
		}
		hql.append(" order by createTime desc");
		return coApplicationInfoDao.findPage(pageRequest, hql.toString(), null);
	}

	public Map<String, Object> convertToMap(CoApplicationInfo coApplicationInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (coApplicationInfo == null) {
			return map;
		}
		map.put("id", coApplicationInfo.getId());
		map.put("ccpitCode", coApplicationInfo.getCcpitCode());
		map.put("registNumber", coApplicationInfo.getRegistNumber());
		map.put("companyName", coApplicationInfo.getCompanyName());
		map.put("linkMan", coApplicationInfo.getLinkMan());
		map.put("linkPhone", coApplicationInfo.getLinkPhone());
		map.put("email", coApplicationInfo.getEmail());
		map.put("reason", coApplicationInfo.getReason());
		map.put("infotatus", 0 == coApplicationInfo.getInfotatus() ? "审核中" : (1 == coApplicationInfo.getInfotatus() ?"审核未过":"审核通过"));
		map.put("createTime",
				StringUtil.DATE_FORMATE.format(coApplicationInfo.getCreateTime()));
		map.put("clickVolume", coApplicationInfo.getClickVolume());
		map.put("creator", coApplicationInfo.getCreator().getUsername());

		return map;
	}

	public PageBo<CoApplicationInfo> convert(Page<CoApplicationInfo> page) {
		return new PageBo<CoApplicationInfo>(page, new Convert<CoApplicationInfo>() {
			@Override
			public Map<String, Object> convert(CoApplicationInfo obj) {
				return convertToMap(obj);
			}
		});
	}
	
	public void addClickVolume (CoApplicationInfo coApplicationInfo) {
		coApplicationInfo.setClickVolume((coApplicationInfo.getClickVolume())+1);
		coApplicationInfoDao.save(coApplicationInfo);
	}

	public List<Field> createCoApplicationInfosIndex(CoApplicationInfo coApplicationInfo) {
		List<Field> fields = new ArrayList<Field>();
		Field id = new Field("id", "n" + coApplicationInfo.getId(), Store.YES,
				Index.NOT_ANALYZED);
		fields.add(id);
		Field title = new Field("ccpitCode", coApplicationInfo.getCcpitCode(), Store.YES,
				Index.ANALYZED);
		fields.add(title);
		Field createTime = new Field("createTime",
				DateConvert.convertDateWithoutHms(coApplicationInfo.getCreateTime()),
				Store.YES, Index.NO);
		fields.add(createTime);
		return fields;
	}
}
