package com.palmnest.dao.estateserve.impl;

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

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;

import com.palmnest.application.Constants;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.dao.estateserve.ShoppingMallDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.MallManagement;

/**
 * 商城数据层实现类
 *
 * @version 1.0
 * @authr xiemin
 */
@Repository("shoppingMallDao")
public class ShoppingMallDaoHibernate extends GenericDaoHibernate<Object, Integer> implements ShoppingMallDao {

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

	@SuppressWarnings({"unchecked"})
	public Pagenation<Map<String, String>> queryMallList(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 = Constants.LIMIT_PAGESIZE;
			StringBuffer sbf = new StringBuffer();
			sbf.append("select m.id,m.name mallName,m.addressCode,m.urlKey,o.name orgName,m.isEnabled ")
					.append(" from tb_mall_management m left join tb_organization o on o.id = m.organizationId where m.isEnabled = 1");
			if (null != modelMap.get("mallName") && !modelMap.get("mallName").equals("")) {
				sbf.append(" and m.name like '%").append(modelMap.get("mallName") + "%'");
			}
			if (null != modelMap.get("orgName") && !modelMap.get("orgName").equals("")) {
				sbf.append(" and o.name like '%").append(modelMap.get("orgName") + "%'");
			}

			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) {
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public int addMallUrl(ModelMap modelMap) throws Exception {
		MallManagement mm = new MallManagement();
		try {
			if (null != modelMap.get("id") && !modelMap.get("id").equals("")) {
				mm.setId(Integer.valueOf(String.valueOf(modelMap.get("id"))));
			}
			mm.setAddressCode(String.valueOf(modelMap.get("addressCode")));
			mm.setIsEnabled(ElementConstant.YESORNO_1);
			mm.setCreateTime(DateUtils.formatDate(new Date()));
			mm.setCreator(String.valueOf(modelMap.get("userName")));
			mm.setName(String.valueOf(modelMap.get("mallName")));
			mm.setOrganizationId(Integer.valueOf(String.valueOf(modelMap.get("organizationId"))));
			mm.setUrl(String.valueOf(modelMap.get("url")));
			mm.setUrlKey(String.valueOf(modelMap.get("urlKey")));
			getSession().saveOrUpdate(mm);
			return mm.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> queryDetails(String id) {
		StringBuilder sbSql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		try {
			sbSql.append("select m.id,o.id organizationId, m.name mallName,m.addressCode,m.urlKey,o.name orgName,m.isEnabled,m.url ")
					.append(" from tb_mall_management m left join tb_organization o on o.id = m.organizationId where m.isEnabled = 1")
					.append(" and m.id = ").append(id);
			Query query = getSession().createSQLQuery(sbSql.toString());

			map = (Map<String, String>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;

	}

	@Override
	public String queryAddressCode(String organizationId) {
		StringBuilder sbSql = new StringBuilder();
		String addressCode = null;
		try {
			sbSql.append("select addressCode from tb_mall_managemen where organizationId=").append(organizationId);
			Query query = getSession().createSQLQuery(sbSql.toString());

			Object obj = query.uniqueResult();

			if (null != obj && !obj.equals("")) {
				addressCode = String.valueOf(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return addressCode;

	}

	/*
	@Override
	public int updateImge(String id, String isEnabled) throws Exception
	{
		StringBuffer sb = new StringBuffer();
		sb = sb.append("update tb_picture_info 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 (DataAccessException e) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
		return result;

	}
*/
	@Override
	public int mallDelete(String id, String isEnabled) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb = sb.append("update tb_mall_management 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) {
			e.printStackTrace();
			throw new Exception("系统异常");
		}
		return result;
	}

	@Override
	public List<Map<String, String>> findByNameList(String orgName) {
		try {
			StringBuffer sqlStr = new StringBuffer();
			if (StringUtils.isNotEmpty(orgName)) {
				sqlStr.append("select id,name from tb_organization where name like '%").append(orgName).append("%' and isenabled=1");
			} else {
				sqlStr.append("select id,name from tb_organization where isenabled=1").toString();
			}
			Query query = getSession().createSQLQuery(sqlStr.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(0).setMaxResults(10);
			@SuppressWarnings("unchecked")
			List<Map<String, String>> list = query.list();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> queryUrlKey(String urlKey) {
		StringBuilder sbSql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		try {
			sbSql.append("select url,addressCode from tb_mall_management where urlKey ='").append(urlKey + "'");
			Query query = getSession().createSQLQuery(sbSql.toString());

			map = (Map<String, String>) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

}
