package com.palmnest.dao.estateserve.impl;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.application.bean.Pagenation;
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.estateserve.ServicePackDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.ProductCategories;
import com.palmnest.model.estateserve.ServicePackDetails;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 服务项管理实现DAO
 *
 * @author xiemin
 */
@Repository("servicePackDao")
public class ServicePackDaoImpl extends GenericDaoHibernate<Object, Integer>
		implements ServicePackDao {

	private Logger LOG = Logger.getLogger(ServicePackDaoImpl.class);

	public ServicePackDaoImpl() {
		super(Object.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagenation<Map<String, String>> queryServicePackList(ModelMap modelMap) throws Exception {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			int pageNo   = Integer.valueOf(String.valueOf(modelMap.get("pageNo")));
			int pageSize = Integer.valueOf(String.valueOf(modelMap.get("pageSize")));

			StringBuffer sbf = new StringBuffer();

			sbf.append("select id,name,date_format(createTime,'%Y-%m-%d %H:%i:%s') createTime,serviceImg,isDeposit,paymentMethod")
					.append(" from tb_service_pack where isEnabled = 1");
			if (null != modelMap.get("name") && !modelMap.get("name").equals("")) {
				sbf.append(" and name like '%").append(modelMap.get("name") + "%'");
			}
			if (null != modelMap.get("startTime") && !modelMap.get("startTime").equals("")) {
				sbf.append(" and createTime >= '").append(modelMap.get("startTime") + "'");
			}
			if (null != modelMap.get("endTime") && !modelMap.get("endTime").equals("")) {
				sbf.append(" and createTime <= '").append(modelMap.get("endTime") + "'");
			}
			if (null != modelMap.get("type") && !modelMap.get("type").equals("")) {
				sbf.append(" and type = ").append(modelMap.get("type"));
			}
			if (null != modelMap.get("isDeposit") && !modelMap.get("isDeposit").equals("")) {
				sbf.append(" and isDeposit = '").append(modelMap.get("isDeposit") + "'");
			}
			if (null != modelMap.get("paymentMethod") && !modelMap.get("paymentMethod").equals("")) {
				sbf.append(" and paymentMethod = ").append(modelMap.get("paymentMethod"));
			}

			sbf.append(" order by createTime desc ");
			Query query = getSession().createSQLQuery(sbf.toString());
			int   count = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list().size();
			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
			return new Pagenation<Map<String, String>>(list, pageNo, count, pageSize);
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	@Override
	public String findByName(String name) throws Exception {
		String sql    = "select name from tb_service_pack where name = '" + name + "'";
		Object result = "";
		try {
			Query query = getSession().createSQLQuery(sql);
			result = query.uniqueResult();

			if (null != result && !result.equals("")) {
				return result.toString();
			}
		} catch (Exception e) {
			LOG.info("findByName：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}

		return null;
	}

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

	@SuppressWarnings("unchecked")
	@Override
	public Pages<Map<String, String>> queryServicePackDetailsList(ModelMap modelMap) throws Exception {

		List<Map<String, String>>  list = new ArrayList<Map<String, String>>();
		Pages<Map<String, String>> page = new Pages<Map<String, String>>();

		try {
			int pageNo   = Integer.valueOf(String.valueOf(modelMap.get("pageNo")));
			int pageSize = Integer.valueOf(String.valueOf(modelMap.get("pageSize")));

			StringBuffer sbf = new StringBuffer();
			sbf.append("select spd.id,spd.name,date_format(spd.createTime,'%Y-%m-%d %H:%i:%s') createTime,spd.price,spd.logoPath,st.serviceName servicePackName,st.id servicePackId,")
					.append("pc.name serviceCategoriesName,pc.id serviceCategoriesId from tb_service_pack_details spd")
					.append(" left join tb_product_categories pc on pc.id = spd.typeId left join  tb_servicetypeinfo st on st.id = pc.serviceId where spd.isEnabled = 1");
			if (null != modelMap.get("name") && !modelMap.get("name").equals("")) {
				sbf.append(" and spd.name like '%").append(modelMap.get("name") + "%'");
			}
			if (null != modelMap.get("startTime") && !modelMap.get("startTime").equals("")) {
				sbf.append(" and spd.createTime >= '").append(modelMap.get("startTime") + "'");
			}
			if (null != modelMap.get("endTime") && !modelMap.get("endTime").equals("")) {
				sbf.append(" and spd.createTime <= '").append(modelMap.get("endTime") + "'");
			}
			if (null != modelMap.get("serviceCategoriesId") && !modelMap.get("serviceCategoriesId").equals("")) {
				sbf.append(" and pc.id = '").append(modelMap.get("serviceCategoriesId") + "'");
			}
			if (null != modelMap.get("servicePackId") && !modelMap.get("servicePackId").equals("")) {
				sbf.append(" and st.id = '").append(modelMap.get("servicePackId") + "'");
			}

			sbf.append(" order by spd.createTime desc ");
			Query query = getSession().createSQLQuery(sbf.toString());
			int   count = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list().size();
			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) {
			throw new Exception(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, String>> queryServiceList() throws Exception {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select id,serviceName name,serviceIcon,paymentType,code from tb_serviceTypeInfo where isEnabled = 1 and isSignContract = 1 ");
			Query query = getSession().createSQLQuery(sbf.toString());
			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, String>> queryServiceList(String serviceId) throws Exception {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select id,serviceName name,serviceIcon,paymentType,code from tb_serviceTypeInfo where isEnabled = 1 and isSignContract = 1");
			if (StringUtils.isNotEmpty(serviceId)) {
				sbf.append(" and id not in(").append(serviceId + ")");
			}
			Query query = getSession().createSQLQuery(sbf.toString());
			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> queryServicePackDetails(String id) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select spd.id,spd.name,date_format(spd.createTime,'%Y-%m-%d %H:%i:%s') createTime,spd.price,spd.logoPath,st.serviceName servicePackName,st.id servicePackId,")
					.append("pc.name serviceCategoriesName,pc.id serviceCategoriesId,pc.typeCode from tb_service_pack_details spd")
					.append(" left join tb_product_categories pc on pc.id = spd.typeId left join tb_servicetypeinfo st on st.id = pc.serviceId where spd.isEnabled = 1").append(" and spd.id = ").append(Integer.valueOf(id));
			Query query = getSession().createSQLQuery(sbf.toString());
			map = (Map<String, String>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return map;
	}

	@Override
	public String findByDetailsName(String name, String belongId) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("select name from tb_service_pack_details where name = '").append(name + "'").append(" and typeId =").append(belongId);
		Object result = "";
		try {
			Query query = getSession().createSQLQuery(sql.toString());
			result = query.uniqueResult();

			if (null != result && !result.equals("")) {
				return result.toString();
			}
		} catch (Exception e) {
			LOG.info("findByDetailsName：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}

		return null;
	}

	@Override
	public int addServicePackDetails(Map<String, String> map) throws Exception {
		ServicePackDetails  sp     = new ServicePackDetails();
		Map<String, String> mapNew = new HashMap<String, String>();
		try {
			if (null != map.get("id") && !map.get("id").equals("")) {
				mapNew = this.queryServicePackDetails(map.get("id"));
				sp.setId(Integer.valueOf(map.get("id")));
				sp.setCreateTime(mapNew.get("createTime"));
			}
			sp.setName(map.get("name"));
			sp.setTypeCode(Integer.valueOf(map.get("typeCode")));
			sp.setTypeId(Integer.valueOf(map.get("typeId")));
			sp.setLogoPath(map.get("logoPath"));
			sp.setIsEnabled(ElementConstant.YESORNO_1);
			sp.setCreator(map.get("adminName"));
			sp.setPrice(BigDecimal.valueOf(Double.valueOf(map.get("price"))));
			if (null != map.get("id") && !map.get("id").equals("")) {
				sp.setModifyTime(DateUtils.formatDate(new Date()));
				sp.setModifier(map.get("adminName"));
				getSession().update(sp);
			} else {
				sp.setCreateTime(DateUtils.formatDate(new Date()));
				getSession().save(sp);
			}
			return sp.getId();
		} catch (Exception e) {
			LOG.info("addServicePackDetails：" + e.getCause() + e.getMessage());
			throw new Exception("系统异常");
		}
	}

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

	@Override
	public int findServicePackDetailsId(int typeId) throws Exception {
		String sql    = "select count(id) from tb_service_pack_details where isEnabled = 1 and typeId = " + typeId;
		Object result = "";
		try {
			Query query = getSession().createSQLQuery(sql);
			result = query.uniqueResult();

			return Integer.valueOf(result.toString());
		} catch (Exception e) {
			LOG.info("findServicePackDetailsId：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}
	}

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

	@SuppressWarnings("unchecked")
	@Override
	public Pages<Map<String, String>> queryProductCategoriesList(
			ModelMap modelMap) throws Exception {

		List<Map<String, String>>  list = new ArrayList<Map<String, String>>();
		Pages<Map<String, String>> page = new Pages<Map<String, String>>();

		try {

			int pageNo   = Integer.valueOf(String.valueOf(modelMap.get("pageNo")));
			int pageSize = Integer.valueOf(String.valueOf(modelMap.get("pageSize")));

			StringBuffer sbf = new StringBuffer();
			sbf.append("select spc.id,spc.name,date_format(spc.createTime,'%Y-%m-%d %H:%i:%s') createTime,spc.typeCode,st.serviceName")
					.append(" from tb_product_categories spc left join tb_serviceTypeInfo st on st.id = spc.serviceId where spc.isEnabled = 1");
			if (null != modelMap.get("name") && !modelMap.get("name").equals("")) {
				sbf.append(" and spc.name like '%").append(modelMap.get("name") + "%'");
			}
			if (null != modelMap.get("startTime") && !modelMap.get("startTime").equals("")) {
				sbf.append(" and spc.createTime >= '").append(modelMap.get("startTime") + "'");
			}
			if (null != modelMap.get("endTime") && !modelMap.get("endTime").equals("")) {
				sbf.append(" and spc.createTime <= '").append(modelMap.get("endTime") + "'");
			}
			if (null != modelMap.get("typeCode") && !modelMap.get("typeCode").equals("")) {
				sbf.append(" and spc.typeCode = '").append(modelMap.get("typeCode") + "'");
			}
			if (null != modelMap.get("serviceId") && !modelMap.get("serviceId").equals("")) {
				sbf.append(" and spc.serviceId = '").append(modelMap.get("serviceId") + "'");
			}

			sbf.append(" order by spc.createTime desc ");
			Query query = getSession().createSQLQuery(sbf.toString());
			int   count = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list().size();
			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) {
			throw new Exception(e);
		}
	}

	@Override
	public String findProductByName(String name, String serviceId) throws Exception {

		StringBuffer sql = new StringBuffer();
		sql.append("select name from tb_product_categories where name = '").append(name + "'").append(" and serviceId =").append(Integer.valueOf(serviceId));
		Object result = "";
		try {
			Query query = getSession().createSQLQuery(sql.toString());
			result = query.uniqueResult();

			if (null != result && !result.equals("")) {
				return result.toString();
			}
		} catch (Exception e) {
			LOG.info("findProductByName：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}

		return null;
	}

	@Override
	public int addProductCategories(Map<String, String> map) throws Exception {

		ProductCategories   pc     = new ProductCategories();
		Map<String, String> mapNew = new HashMap<String, String>();

		try {
			if (null != map.get("id") && !map.get("id").equals("")) {
				mapNew = this.queryProductCategories(map.get("id"));
				pc.setCreateTime(mapNew.get("createTime"));
			}
			pc.setName(map.get("name"));
			pc.setServiceId(Integer.valueOf(map.get("serviceId")));
			pc.setIsEnabled(ElementConstant.YESORNO_1);
			pc.setCreator(map.get("adminName"));
			pc.setCode(map.get("typeCode"));
			pc.setParentCode("0");
			pc.setCreator(map.get("adminName"));
			if (null != map.get("id") && !map.get("id").equals("")) {
				pc.setModifyTime(DateUtils.formatDate(new Date()));
				pc.setModifier(map.get("adminName"));
				pc.setId(Integer.valueOf(map.get("id")));
				getSession().update(pc);
			} else {
				pc.setCreateTime(DateUtils.formatDate(new Date()));
				getSession().save(pc);
			}
			return pc.getId();
		} catch (Exception e) {
			LOG.info("addServicePackDetails：" + e.getCause() + e.getMessage());
			throw new Exception("系统异常");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> queryProductCategories(String id) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select spc.id,spc.name,spc.typeCode,spc.serviceId,st.serviceName,date_format(spc.createTime,'%Y-%m-%d %H:%i:%s')createTime")
					.append(" from tb_product_categories spc left join tb_serviceTypeInfo st on st.id = spc.serviceId where spc.isEnabled = 1").append(" and spc.id = ").append(Integer.valueOf(id));
			Query query = getSession().createSQLQuery(sbf.toString());
			map = (Map<String, String>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return map;
	}

	@Override
	public int updateProductCategories(Integer id, String isEnabled) throws Exception {

		StringBuffer sb = new StringBuffer();

		sb = sb.append("update tb_product_categories as a set a.isEnabled =").append(isEnabled).append(" where a.id=").append(id);
		int result = -1;
		try {
			Query query = getSession().createSQLQuery(sb.toString());
			result = query.executeUpdate();
		} catch (Exception e) {
			LOG.info("updateProductCategories：" + e.getCause() + e.getMessage());
			throw new Exception(e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, String>> queryCategoriesList(String serviceId) throws Exception {

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select id,name,typeCode from tb_product_categories where isEnabled = 1 and serviceId = ").append(serviceId);
			Query query = getSession().createSQLQuery(sbf.toString());
			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, String>> queryServicePackDetailsList(String categoriesId) throws Exception {

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select id,name,logoPath,price from tb_service_pack_details where isEnabled = 1 and typeId = ").append(categoriesId);
			Query query = getSession().createSQLQuery(sbf.toString());
			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, String>> queryServiceDetailsList(String dataIdarr) throws Exception {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select id,name,price from tb_service_pack_details where isEnabled = 1 and id in(").append(dataIdarr + ")");
			Query query = getSession().createSQLQuery(sbf.toString());
			list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> queryServiceDetails(String serviceId) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		try {
			StringBuffer sbf = new StringBuffer();
			sbf.append("select id,serviceName name,serviceIcon,paymentType,code from tb_serviceTypeInfo where isEnabled = 1").append(" and id=").append(serviceId);
			Query query = getSession().createSQLQuery(sbf.toString());
			map = (Map<String, String>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return map;
	}

	@Override
	public ProductCategories getProductCategoriesByServiceCode(
			String serviceCode) {
		String                  strHQL = "from ProductCategories where serviceCode=? and parentCode='0' and isEnabled='1' ";
		List<ProductCategories> list   = getSession().createQuery(strHQL).setString(0, serviceCode).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ProductCategories> queryChildrenCategoriesList(String parentCode) {
		String                  strHQL = "from ProductCategories where parentCode=? and isEnabled='1' order by orderNo asc";
		List<ProductCategories> list   = getSession().createQuery(strHQL).setString(0, parentCode).list();
		return list;
	}

	@Override
	public ProductCategories querProductCategoriesByServiceCode(String serviceCode) {
		String                  strHQL = "from ProductCategories where isEnabled='1' and serviceCode=? ";
		List<ProductCategories> list   = getSession().createQuery(strHQL).setString(0, serviceCode).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Page getProductCategoriesPage(String whereSql, Integer pageSize, Integer pageNo) throws Exception {
		String strSQL = "SELECT  date_format(t1.createTime,'%Y-%m-%d %H:%i') createTimes,t1.*,t2.itemcode,t2.itemname from tb_product_categories t1"
				+ " LEFT JOIN tb_dictionary_item t2 on t1.serviceId=t2.itemcode "
				+ " and t2.groupid=(SELECT t3.id from tb_dictionary_group t3 where  t3.groupcode='signingServiceGroup')";
		if (StringUtils.isNotBlank(whereSql)) {
			strSQL += whereSql;
		}
		return pagedQueryByJdbcTemplate(strSQL, pageNo, pageSize);
	}

	@Override
	public List<Map<String, Object>> getProductCategoriesNodes(String parentCode) throws Exception {
		try {
			String strSQL = "SELECT  date_format(t1.createTime,'%Y-%m-%d %H:%i') createTimes,t1.*,t2.itemcode,t2.itemname from tb_product_categories t1 "
					+ " LEFT JOIN tb_dictionary_item t2 on t1.serviceId=t2.itemcode "
					+ " and t2.groupid=(SELECT t3.id from tb_dictionary_group t3 where  t3.groupcode='signingServiceGroup')"
					+ " where t1.parentCode=" + parentCode + " and t1.isEnabled='1'";
			return getJdbcTemplate().queryForList(strSQL);
		} catch (Exception e) {
			LOG.error("getProductCategoriesNodes error {}" + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public Integer queryProductCategoriesMaxCode() throws Exception {
		try {
			String strSQL = "SELECT MAX(id)+1 code from tb_product_categories";
			return getJdbcTemplate().queryForInt(strSQL);
		} catch (Exception e) {
			LOG.error("queryProductCategoriesMaxCode error {}" + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return 0;
	}

	@Override
	public void updateProductCategoriesServiceId(Integer serviceId, String parentCode)
			throws Exception {
		try {
			String strSQL = "UPDATE tb_product_categories set serviceId=" + serviceId + " where parentCode='" + parentCode + "'";
			this.getJdbcTemplate().execute(strSQL);
		} catch (Exception e) {
			LOG.error("updateProductCategories error {}" + LogExceptionStackUtil.LogExceptionStack(e));
		}

	}

	@Override
	public ProductCategories querProductCategoriesByCode(String code) {
		String                  strHQL = "from ProductCategories where isEnabled='1' and code=? ";
		List<ProductCategories> list   = getSession().createQuery(strHQL).setString(0, code).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ProductCategories querProductCategoriesById(Integer id) {
		String                  strHQL = "from ProductCategories where isEnabled='1' and id=? ";
		List<ProductCategories> list   = getSession().createQuery(strHQL).setInteger(0, id).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean querProductCateoriesIsFlag(Integer id, String serviceCode) {
		String strHQL = "from ProductCategories where isEnabled='1' ";
		if (id != null) {
			strHQL += " and id!=" + id;
		}
		strHQL += "and serviceCode='" + serviceCode + "'";
		List<ProductCategories> list = getSession().createQuery(strHQL).list();
		if (!CollectionUtils.isEmpty(list)) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteProductCategories(String parentCode, String isEnabled)
			throws Exception {

		try {

			String strSQL = "UPDATE tb_product_categories set isEnabled='" + isEnabled + "' where parentCode='" + parentCode + "'";

			this.getJdbcTemplate().execute(strSQL);
		} catch (Exception e) {
			LOG.error("deleteProductCategories error {}" + LogExceptionStackUtil.LogExceptionStack(e));
		}

	}


}
