package com.palmnest.dao.wechat.impl;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.Pages;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.wechat.SignContractDao;
import com.palmnest.model.estateserve.*;
import com.palmnest.model.property.VirtualArchitecture;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 签约管理微信端数据实现类
 *
 * @author xiemin
 */
@Slf4j
@Repository("signContractDao")
public class SignContractDaoImpl extends GenericDaoHibernate<SignContract, Integer>
		implements SignContractDao {

	public SignContractDaoImpl() {
		super(SignContract.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VirtualArchitecture> findVirtualArchitectureList(String virtualName,
																 Integer organizationId, String type) {
		List<VirtualArchitecture> list = new ArrayList<VirtualArchitecture>();
		StringBuffer              sbf  = new StringBuffer();

		if (StringUtils.isNotEmpty(virtualName)) {
			sbf.append("select * from tb_build where isEnabled = 1 and organizationId = ")
					.append(organizationId).append(" and type = ").append(type)
					.append(" and name like '%").append(virtualName + "%'");
		} else {
			sbf.append("select * from tb_virtualArchitecture where isEnabled = 1 and organizationId = ").append(organizationId).append(" and type = ").append(type);
		}

		try {

			Query query = getSession().createSQLQuery(sbf.toString()).addEntity(VirtualArchitecture.class).setFirstResult(0).setMaxResults(10);
			list = query.list();

		} catch (Exception e) {
			log.error("findVirtualArchitectureList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}

		return list;
	}

	@Override
	public VirtualArchitecture findVirtualArchitecture(String name,
													   Integer organizationId, String type) {

		VirtualArchitecture virtualArchitecture = new VirtualArchitecture();
		StringBuffer        sbf                 = new StringBuffer();

		sbf.append("select * from tb_virtualArchitecture where isEnabled = 1 and organizationId = ").append(organizationId)
				.append(" and type = ").append(type)
				.append(" and name ='").append(name + "'");

		try {
			Query query = getSession().createSQLQuery(sbf.toString()).addEntity(VirtualArchitecture.class);
			virtualArchitecture = (VirtualArchitecture) query.uniqueResult();
		} catch (Exception e) {
			log.error("findVirtualArchitecture occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return virtualArchitecture;
	}

	@Override
	public int addVirtualArchitecture(Map<String, String> map) throws Exception {
		VirtualArchitecture va = new VirtualArchitecture();
		try {
			va.setName(map.get("name"));
			va.setType(ElementConstant.YESORNO_1);
			va.setIsEnabled(map.get("isEnabled"));
			va.setParentCode(ElementConstant.YESORNO_0);
			va.setAddress(map.get("address"));
			va.setOrganizationId(Integer.valueOf(map.get("organizationId")));
			va.setPhone(map.get("phone"));
			va.setCustomerNumber(map.get("customerNumber"));
			va.setEmployees(Integer.valueOf(map.get("employees")));
			va.setContacts(map.get("contacts"));
			va.setCreator(map.get("userName"));
			va.setCreateTime(new Date());
			getSession().save(va);
			return va.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
	}

	@Override
	public int addSignContract(Map<String, String> map) throws Exception {
		SignContract sc = new SignContract();
		try {
			if (null != map.get("signContractId") && !map.get("signContractId").equals("") && (null == map.get("renewalContractId") || map.get("renewalContractId").equals(""))) {
				sc = this.get(Integer.valueOf(map.get("signContractId")));
			} else if ((null != map.get("signContractId") && !map.get("signContractId").equals("")) && (null != map.get("renewalContractId") && !map.get("renewalContractId").equals("")) && !map.get("renewalContractId").equals(map.get("signContractId"))) {
				sc = this.get(Integer.valueOf(map.get("signContractId")));
			}
			sc.setOrganizationId(Integer.valueOf(map.get("organizationId")));
			sc.setVirtualArchitectureId(Integer.valueOf(map.get("virtualArchitectureId")));
			sc.setPaymentMethod(map.get("paymentType"));
			sc.setServiceTypeInCode(map.get("code"));
			sc.setServiceTypeInfoId(Integer.valueOf(map.get("serviceId")));
			sc.setStatus(Integer.valueOf(map.get("status")));
			sc.setEffectiveTime(DateUtils.getDate(new Date(), DateUtils.YYYY_MM_DD_MM_HH_SS));
			sc.setIsEnabled(ElementConstant.YESORNO_0);
			sc.setStartTime(map.get("startTime"));
			sc.setEndTime(map.get("endTime"));
			sc.setSignUserType(map.get("signUserType"));
			sc.setRemarks(map.get("remarks"));
			sc.setStaffId(map.get("staffId"));
			if ((null == map.get("signContractId") || map.get("signContractId").equals("")) && (null == map.get("renewalContractId") || map.get("renewalContractId").equals(""))) {
				sc.setCreator(map.get("userName"));
				sc.setCreateTime(DateUtils.getDate(new Date(), DateUtils.YYYY_MM_DD_MM_HH_SS));
				getSession().save(sc);
			} else if ((null != map.get("signContractId") && !map.get("signContractId").equals("")) && (null != map.get("renewalContractId") && !map.get("renewalContractId").equals("")) && map.get("renewalContractId").equals(map.get("signContractId"))) {
				sc.setCreator(map.get("userName"));
				sc.setCreateTime(DateUtils.getDate(new Date(), DateUtils.YYYY_MM_DD_MM_HH_SS));
				getSession().save(sc);
			} else {
				sc.setModifier(map.get("userName"));
				sc.setModifyTime(DateUtils.getDate(new Date(), DateUtils.YYYY_MM_DD_MM_HH_SS));
				getSession().update(sc);
			}

			return sc.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
	}

	@Override
	public int addSignBasics(Map<String, String> map) throws Exception {
		SignBasics sbc = new SignBasics();
		try {
			sbc.setBasicsId(Integer.valueOf(map.get("basicsId")));
			sbc.setNumber(Integer.valueOf(map.get("number")));
			sbc.setSignId(map.get("signId"));
			sbc.setTotalAmount(BigDecimal.valueOf(Double.valueOf(map.get("totalAmount"))));
			sbc.setCreateTime(new Date());
			sbc.setCreator(map.get("userName"));
			getSession().save(sbc);
			return sbc.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
	}

	@Override
	public int addProductSign(Map<String, String> map) throws Exception {
		ProductSign ps = new ProductSign();
		try {
			ps.setSignContractId(Integer.valueOf(map.get("signContractId")));
			ps.setServicePackDetailsId(Integer.valueOf(map.get("servicePackDetailsId")));
			getSession().save(ps);
			return ps.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
	}

	@Override
	public List<Map<String, Object>> getSignContractListByVirId(int virtualId) {
		StringBuffer strSQL = new StringBuffer("SELECT t1.ID,t2.serviceName,t1.serviceTypeInCode,t1.serviceTypeInfoId,t1.autograph,t1.paymentMethod,t1.`status`,t1.prepayment,t1.effectiveTime,t1.startTime,t1.endTime,t3.`name` as staffName,t1.staffId,t1.createTime,t1.autograph");
		strSQL.append(" FROM tb_sign_contract t1");
		strSQL.append(" INNER JOIN tb_servicetypeinfo t2 on t1.serviceTypeInfoId=t2.ID");
		strSQL.append(" LEFT JOIN tb_staff t3 ON t1.staffId = t3.ID");
		strSQL.append(" WHERE t1.isEnabled = '1' and t1.virtualarchitectureId=" + virtualId);
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public DepositItemInformationForm queryDepositItem(String dtId) {
		DepositItemInformationForm depositItemInformationForm = new DepositItemInformationForm();
		StringBuffer               sbf                        = new StringBuffer();
		sbf.append("select * from tb_depositItem_information_form where isEnabled = 1 and id = ").append(dtId);

		try {
			Query query = getSession().createSQLQuery(sbf.toString()).addEntity(DepositItemInformationForm.class);
			depositItemInformationForm = (DepositItemInformationForm) query.uniqueResult();
		} catch (Exception e) {
			log.error("queryDepositItem occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return depositItemInformationForm;
	}

	@Override
	public int updateSignContract(String id, String imageUrl) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb = sb.append("update tb_sign_contract as a set a.autograph ='").append(imageUrl).append("',a.isEnabled = 1").append(" where a.id=").append(id);
		int result = -1;
		try {
			Query query = getSession().createSQLQuery(sb.toString());
			result = query.executeUpdate();
		} catch (Exception e) {
			log.info("updateSignContract：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}
		return result;
	}

	@Override
	public int updateSignContractStatus(String id, String status) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb = sb.append("update tb_sign_contract as a set a.status ='").append(status).append("' where a.id=").append(id);
		int result = -1;
		try {
			Query query = getSession().createSQLQuery(sb.toString());
			result = query.executeUpdate();
		} catch (Exception e) {
			log.info("updateSignContract：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}
		return result;
	}

	@Override
	public int updateRenewalContract(String id, String isRenewalContract) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb = sb.append("update tb_sign_contract as a set a.isRenewalContract ='").append(isRenewalContract).append("' where a.id=").append(id);
		int result = -1;
		try {
			Query query = getSession().createSQLQuery(sb.toString());
			result = query.executeUpdate();
		} catch (Exception e) {
			log.info("updateSignContract：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}
		return result;
	}

	@Override
	public int addSignServiceInvoice(Map<String, String> map) throws Exception {
		SignServiceInvoice ssi = new SignServiceInvoice();
		try {
			String type = "";
			if (null != map.get("signServiceInvoiceId") && !map.get("signServiceInvoiceId").equals("") && map.get("type").equals("1")) {
				ssi = (SignServiceInvoice) this.findSignServiceInvoice(Integer.valueOf(map.get("signServiceInvoiceId")));
				type = ssi.getType() + "";
			}
			if (null != map.get("signServiceInvoiceId") && !map.get("signServiceInvoiceId").equals("") && map.get("type").equals("2")) {
				ssi = (SignServiceInvoice) this.findSignServiceInvoice(Integer.valueOf(map.get("signServiceInvoiceId")));
				type = ssi.getType() + "";
			}
			ssi.setOrganizationId(Integer.valueOf(map.get("organizationId")));
			ssi.setVirtualarchitectureId(Integer.valueOf(map.get("virtualArchitectureId")));
			ssi.setCreator(map.get("userName"));
			ssi.setType(Integer.valueOf(map.get("type")));
			ssi.setRaise(map.get("raise"));
			ssi.setIdentificationNumber(map.get("identificationNumber"));
			if (null != map.get("mobile") && !map.get("mobile").equals("")) {
				ssi.setMobile(map.get("mobile"));
			}
			if (null != map.get("address") && !map.get("address").equals("")) {
				ssi.setAddress(map.get("address"));
			}
			if (null != map.get("bank") && !map.get("bank").equals("")) {
				ssi.setBank(map.get("bank"));
			}
			if (null != map.get("bankCard") && !map.get("bankCard").equals("")) {
				ssi.setBankCard(map.get("bankCard"));
			}
			if (null != map.get("signServiceInvoiceId") && !map.get("signServiceInvoiceId").equals("") && map.get("type").equals(type)) {
				ssi.setModifier(map.get("userName"));
				ssi.setModifyTime(new Date());
				getSession().update(ssi);
			} else {
				ssi.setIsEnabled(ElementConstant.YESORNO_1);
				ssi.setCreateTime(new Date());
				getSession().save(ssi);
			}
			return ssi.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	@Override
	public List<Map<String, Object>> getSignContractListBySignid(String signid) throws Exception {
		try {
			String strSQL = "SELECT t2.id,t2.signid,t2.basicsId,t2.number,t2.totalAmount from tb_sign_contract t1"
					+ " inner JOIN tb_sign_basics t2 on t1.ID=t2.signid and t2.isEnabled=1"
					+ " where t1.isEnabled=1  and t1.ID=" + signid;
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("getSignContractListBySignid occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public SignBasics getSignBasicsObj(Integer id) {
		try {
			String           strHQL = "from SignBasics where id=? and isEnabled=?";
			List<SignBasics> list   = this.getSession().createQuery(strHQL).setInteger(0, id).setString(1, "1").list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			log.error("getSignBasicsObj occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public Map<String, Object> getIsSignBasics(String signId, String basicsId, Integer id) {
		try {
			String strSQL = "SELECT * from tb_sign_basics t1 where t1.isEnabled=1 and t1.basicsId=" + basicsId + " and t1.signId=" + signId;
			if (id != null) {
				strSQL += " and t1.id!=" + id;
			}
			List<Map<String, Object>> listMap = getJdbcTemplate().queryForList(strSQL);
			if (listMap != null && listMap.size() > 0) {
				return listMap.get(0);
			}
		} catch (Exception e) {
			log.error("getIsSignBasics occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public SignServiceInvoice getSignServiceInvoiceObj(Integer virtualId,
													   String type) {
		try {
			String                   strHQL = "from SignServiceInvoice where virtualarchitectureId=? and type=? and isEnabled=?";
			List<SignServiceInvoice> list   = this.getSession().createQuery(strHQL).setInteger(0, virtualId).setString(1, type).setString(2, "1").list();
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			log.error("getSignServiceInvoiceObj occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public SignServiceInvoice findSignServiceInvoice(Integer id) {
		SignServiceInvoice ssi = new SignServiceInvoice();
		try {
			String sql   = "select * from tb_sign_service_invoice where id=" + id + " and isEnabled=1";
			Query  query = getSession().createSQLQuery(sql).addEntity(SignServiceInvoice.class);
			ssi = (SignServiceInvoice) query.uniqueResult();
		} catch (Exception e) {
			log.error("findSignServiceInvoice occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return ssi;
	}

	@Override
	public List<Map<String, Object>> getIntentionalGoods(Integer id) {
		try {
			String strSQL = "SELECT t3.`name`,t3.price from tb_sign_contract t1"
					+ " INNER JOIN tb_product_sign t2 on t1.ID=t2.signContractId"
					+ " inner JOIN tb_service_pack_details t3 on t3.ID=t2.servicePackDetailsId and t3.isEnabled=1"
					+ " where t1.isEnabled=1 and t1.ID=" + id;
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("getIntentionalGoods occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pages<Map<String, String>> findContract(Map<String, String> map) {

		List<Map<String, String>>  list = new ArrayList<Map<String, String>>();
		Pages<Map<String, String>> page = new Pages<Map<String, String>>();
		try {
			String sbSql = "select sc.id,v.customerNumber,v.name,sp.serviceName,sc.serviceTypeInfoId,date_format(sc.effectiveTime,'%Y-%m-%d') effectiveTime,paymentMethod,sc.status" +
					",date_format(sc.startTime,'%Y-%m-%d') startTime,date_format(sc.endTime,'%Y-%m-%d') endTime,sc.virtualarchitectureId,sc.organizationId,sc.isRenewalContract" +
					" from tb_sign_contract sc left join tb_virtualarchitecture v on v.id = sc.virtualarchitectureId" +
					" left join tb_servicetypeinfo sp on sc.serviceTypeInfoId=sp.id where sc.isEnabled = 1 ";
			if (null != map.get("userId") && !map.get("userId").equals("")) {
				sbSql += " and sc.staffId=" + map.get("userId");
			}
			if (null != map.get("status") && !map.get("status").equals("")) {
				sbSql += " and sc.status = " + map.get("status");
			}
			if (null != map.get("isRenewalContract") && !map.get("isRenewalContract").equals("")) {
				sbSql += " and sc.isRenewalContract = " + map.get("isRenewalContract");
			}
			if (null != map.get("virtualArchitectureId") && !map.get("virtualArchitectureId").equals("")) {
				sbSql += " and sc.virtualArchitectureId = " + map.get("virtualArchitectureId");
			}
			sbSql += " order by sc.createTime desc";
			Query query = getSession().createSQLQuery(sbSql);
			int   count = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list().size();

			int pageNo   = Integer.valueOf(map.get("pageNo") + "");
			int pageSize = Integer.valueOf(map.get("pageSize") + "");

			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult((pageNo - 1) * pageSize)
					.setMaxResults(pageSize).list();

			page.setPageList(list);
			page.setTotalCount(count);
			page.setCurPage(pageNo);
			page.setPageSize(pageSize);
			return page;
		} catch (Exception e) {
			log.error("findContract occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return page;
	}

	@Override
	public String findCustomerNumberMaxId(String organizationId) {
		String customerNumber = "";
		String sql            = "select customerNumber from tb_virtualarchitecture where customerNumber <>'' and customerNumber is not null and organizationId =" + organizationId + " order by id desc limit 1";
		try {
			Query  query  = getSession().createSQLQuery(sql);
			Object result = query.uniqueResult();
			if (null != result && !result.equals("")) {
				customerNumber = String.valueOf(result);
			}
		} catch (Exception e) {
			log.error("findCustomerNumberMaxId occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return customerNumber;
	}

	@Override
	public List<Map<String, Object>> getStaffByOrganztion(Integer userId) {
		try {
			String strSQL = "SELECT t2.organizationId,t3.`name` from tb_staff t1"
					+ " inner join tr_staff_org t2 on t1.ID=t2.staffId"
					+ " inner join tb_organization t3 on t3.ID=t2.organizationId and t3.isEnabled=1"
					+ " where t1.isEnabled=1 and t1.`status`=1 and t1.id=" + userId
					+ " GROUP BY t2.organizationId";
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("getStaffByOrganztion occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<SignServiceInvoice> getSignServiceInvoiceList(Integer virtualId) {
		try {
			String                   strHQL = "from SignServiceInvoice where virtualarchitectureId=?  and isEnabled=?";
			List<SignServiceInvoice> list   = this.getSession().createQuery(strHQL).setInteger(0, virtualId).setString(1, "1").list();
			return list;
		} catch (Exception e) {
			log.error("getSignServiceInvoiceList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getSignContractList(Integer virtualId) {
		try {
			String strSQL = "SELECT DATE_FORMAT(t1.createTime,'%Y-%m-%d ') createTime,t1.ID,(SELECT itemname from tb_dictionary_item where groupid="
					+ "(SELECT id from tb_dictionary_group where groupcode='paymentTypeGroup') and itemcode=t1.paymentMethod) paymentName,"
					+ "t2.serviceIcon,t2.serviceName,DATE_FORMAT(t1.startTime,'%Y-%m-%d ') startTime,DATE_FORMAT(t1.endTime,'%Y-%m-%d ') endTime from tb_sign_contract t1"
					+ " inner JOIN tb_servicetypeinfo t2 on t1.serviceTypeInfoId=t2.id and t2.isEnabled=1"
					+ " where t1.`status`=1 and t1.isEnabled=1 and t1.virtualarchitectureId=" + virtualId;
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("getSignContractList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getSignBasicsList(String id) throws Exception {
		try {
			String strSQL = "SELECT t3.deposName,t2.id,t2.signid,t2.basicsId,t2.number,t2.totalAmount from tb_sign_contract t1"
					+ " inner JOIN tb_sign_basics t2 on t1.ID=t2.signid and t2.isEnabled=1"
					+ " inner join tb_deposititem_information_form t3 on t3.id=t2.basicsId"
					+ " where t1.isEnabled=1  and t1.ID=" + id;
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("getSignContractListBySignid occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public int deleteSignBasics(String basicsId, int signId) throws Exception {
		int result = -1;
		try {
			String sql   = " delete from tb_sign_basics where basicsId = " + basicsId + " and signId = " + signId;
			Query  query = getSession().createSQLQuery(sql.toString());
			result = query.executeUpdate();
			return result;
		} catch (Exception e) {
			log.error("deleteSignBasics occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return result;
	}

	@Override
	public int deleteProductSign(int signContractId, String servicePackDetailsId) throws Exception {
		int result = -1;
		try {
			String sql   = " delete from tb_product_sign where signContractId = " + signContractId + " and servicePackDetailsId = " + servicePackDetailsId;
			Query  query = getSession().createSQLQuery(sql.toString());
			result = query.executeUpdate();
			return result;
		} catch (Exception e) {
			log.error("deleteProductSign occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> findProductSign(String id) throws Exception {
		try {
			String strSQL = "select spd.id,spd.name,spd.price from tb_service_pack_details spd left join tb_product_sign ps on spd.id = ps.servicePackDetailsId where ps.signContractId = " + id;
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("findProductSign occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> findSignBasics(String signContractId) throws Exception {
		try {
			String strSQL = "select sb.number,sb.totalAmount,dif.deposName name,sb.basicsId,dif.price from tb_sign_basics sb left join tb_deposititem_information_form dif on sb.basicsId = dif.id where sb.signid = " + signContractId;
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			log.error("findProductSign occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public SignContract findSignContract(String signContractId) {
		SignContract sc  = new SignContract();
		StringBuffer sbf = new StringBuffer();
		sbf.append("select id,organizationId,virtualArchitectureId,status,autograph,paymentMethod,serviceTypeInfoId,serviceTypeInCode,prepayment,pack,effectiveTime,date_format(startTime,'%Y-%m-%d') startTime,")
				.append("date_format(endTime,'%Y-%m-%d') endTime,signUserType,remarks,staffId,creator,createTime,modifier,modifyTime,isEnabled from tb_sign_contract where isEnabled = 1 and id = ").append(signContractId);

		try {
			Query query = getSession().createSQLQuery(sbf.toString()).addEntity(SignContract.class);
			sc = (SignContract) query.uniqueResult();
		} catch (Exception e) {
			log.error("findSignContract occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return sc;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SignContract> findSignContractList() throws Exception {
		List<SignContract> list = new ArrayList<SignContract>();
		String             sql  = "select * from tb_sign_contract where isEnabled = 1 and endTime <= now()";
		try {
			Query query = getSession().createSQLQuery(sql).addEntity(SignContract.class).setFirstResult(0).setMaxResults(10);
			list = query.list();
		} catch (Exception e) {
			log.error("findSignContractList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return list;
	}


	@Override
	public SignContract findSignContract(Map<String, String> map) {
		SignContract sc  = new SignContract();
		StringBuffer sbf = new StringBuffer();
		sbf.append("select * from tb_sign_contract where status = 1");
		if (null != map.get("organizationId") && !map.get("organizationId").equals("")) {
			sbf.append(" and organizationId = ").append(map.get("organizationId"));
		}
		if (null != map.get("virtualArchitectureId") && !map.get("virtualArchitectureId").equals("")) {
			sbf.append(" and virtualarchitectureId = ").append(map.get("virtualArchitectureId"));
		}
		if (null != map.get("serviceId") && !map.get("serviceId").equals("")) {
			sbf.append(" and serviceTypeInfoId = ").append(map.get("serviceId"));
		}
		/*if(null != map.get("staffId") && !map.get("staffId").equals("")){
			sbf.append(" and staffId = ").append(map.get("staffId"));
		}*/
		if (null != map.get("isEnabled") && !map.get("isEnabled").equals("")) {
			sbf.append(" and isEnabled = ").append(map.get("isEnabled"));
		}
		sbf.append(" limit 1");
		try {
			Query query = getSession().createSQLQuery(sbf.toString()).addEntity(SignContract.class);
			sc = (SignContract) query.uniqueResult();
		} catch (Exception e) {
			log.error("findSignContract occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return sc;
	}

}
