package com.palmnest.service.estateserve.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.Pages;
import com.palmnest.dao.estateserve.ServicePackBasicsDao;
import com.palmnest.model.estateserve.DepositItemInformationForm;
import com.palmnest.service.estateserve.ServicePackBasicsManager;
import com.palmnest.service.impl.GenericManagerImpl;

@Service("servicePackBasicsManager")
public class ServicePackBasicsManagerImpl extends GenericManagerImpl<Object, Integer> implements ServicePackBasicsManager {

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

	@Autowired
	private ServicePackBasicsDao servicePackBasicsDao;

	@Override
	public Pages<Map<String, String>> queryServicePackBasicsList(ModelMap modelMap) {
		try {
			modelMap.put("pageSize", "10");
			return servicePackBasicsDao.queryServicePackBasicsList(modelMap);
		} catch (Exception e) {
			LOG.error("queryServicePackDetailsList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	public Map<String, String> findDetails(int id) {
		try {
			return servicePackBasicsDao.findDetails(id);
		} catch (Exception e) {
			LOG.error("findDetils occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public JSONObject addServicePackBasics(Map<String, String> map) {
		JSONObject json = new JSONObject();
		try {
			if (map.get("serviceTypeInfoId") == null || map.get("serviceTypeInfoId").equals("")) {
				json.put("code", -1);
				json.put("msg", "请选择服务项！");
				return json;
			}
			if (map.get("name") == null || map.get("name").equals("")) {
				json.put("code", -1);
				json.put("msg", "基础信息名称不能为空！");
				return json;
			}
			String name = "";
			name = servicePackBasicsDao.findByName(map.get("name"));

			//新增
			if ((map.get("id") == null || map.get("id").equals("")) && StringUtils.isNotEmpty(name)) {
				json.put("code", -1);
				json.put("msg", "名称已经存在，请重新填写！");
				return json;
			}

			//修改
			if (map.get("id") != null && !map.get("id").equals("")) {
				DepositItemInformationForm df = servicePackBasicsDao.get(Integer.valueOf(map.get("id")));
				if (null != df && StringUtils.isNotEmpty(name) && !df.getDeposName().equals(name)) {
					json.put("code", -1);
					json.put("msg", "名称已经存在，请重新填写！");
					return json;
				}
			}

			if (map.get("price") == null || map.get("price").equals("")) {
				json.put("code", -1);
				json.put("msg", "押金不能为空！");
				return json;
			}
			int result = servicePackBasicsDao.addServicePackBasics(map);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "操作成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("addServicePackBasics occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "操作失败！");
		return json;
	}

	@Override
	public JSONObject deleteServicePackBasics(String id, String isEnabled) {
		JSONObject json = new JSONObject();
		try {
			int result = servicePackBasicsDao.deleteServicePackBasics(Integer.valueOf(id), isEnabled);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "删除成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("deleteServicePackBasics occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "删除失败！");
		return json;
	}

}
