package com.palmnest.service.estateserve.impl;

import java.util.HashMap;
import java.util.List;
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.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.bean.Page;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.Pages;
import com.palmnest.dao.estateserve.ServicePackDao;
import com.palmnest.model.estateserve.ProductCategories;
import com.palmnest.service.estateserve.ServicePackManager;
import com.palmnest.service.estateserve.ServiceTypeInfoManager;
import com.palmnest.service.impl.GenericManagerImpl;

@Service("servicePackManager")
public class ServicePackManagerImpl extends GenericManagerImpl<Object, Integer> implements ServicePackManager {

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

	private ServicePackDao servicePackDao;

	@Autowired
	private ServiceTypeInfoManager serviceTypeInfoManager;


	@Autowired
	public void setServicePackDao(ServicePackDao servicePackDao) {
		this.servicePackDao = servicePackDao;
		this.dao = servicePackDao;
	}

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

	@Override
	public JSONObject deleteServicePack(String id, String isEnabled) {
		JSONObject json = new JSONObject();
		try {
			//如果被关联了，则不能被删除
			int count = servicePackDao.findServicePackDetailsId(Integer.valueOf(id));
			if (count > 0) {
				json.put("code", -1);
				json.put("msg", "该服务项已经被关联，请先删除关联的物品，删除失败！");
				return json;
			}
			int result = servicePackDao.updateServicePack(Integer.valueOf(id), isEnabled);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "删除成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("deleteServicePack occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "删除失败！");
		return json;
	}

	@Override
	public JSONObject deletePicture(String id) {
		JSONObject json = new JSONObject();
		try {
			int result = servicePackDao.deletePicture(id);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "操作成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("deletePicture occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "操作失败！");
		return json;
	}


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

	@Override
	public List<Map<String, String>> queryServiceList() {
		try {
			return servicePackDao.queryServiceList();
		} catch (Exception e) {
			LOG.error("queryServiceList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<Map<String, String>> queryServiceList(String serviceId) {
		try {
			return servicePackDao.queryServiceList(serviceId);
		} catch (Exception e) {
			LOG.error("queryServiceList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public Map<String, String> queryServicePackDetails(String id) {
		try {
			return servicePackDao.queryServicePackDetails(id);
		} catch (Exception e) {
			LOG.error("queryServicePackDetails occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public JSONObject addServicePackDetails(Map<String, String> map) {
		JSONObject json = new JSONObject();
		try {
			if (map.get("name") == null || map.get("name").equals("")) {
				json.put("code", -1);
				json.put("msg", "物品名称不能为空！");
				return json;
			}
			if (map.get("typeId") == null || map.get("typeId").equals("")) {
				json.put("code", -1);
				json.put("msg", "请选择服务项类型！");
				return json;
			}
			String name = "";
			name = servicePackDao.findByDetailsName(map.get("name"), map.get("typeId"));

			//新增
			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("")) {
				Map<String, String> sp = servicePackDao.queryServicePackDetails(map.get("id"));
				if (null != sp && StringUtils.isNotEmpty(name) && !sp.get("name").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;
			}
			if (map.get("logoPath") == null || map.get("logoPath").equals("")) {
				json.put("code", -1);
				json.put("msg", "商品图片不能为空！");
				return json;
			}
			int result = servicePackDao.addServicePackDetails(map);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "操作成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("addServicePackDetails occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "操作失败！");
		return json;
	}

	@Override
	public JSONObject deleteServicePackDetails(String id, String isEnabled) {
		JSONObject json = new JSONObject();
		try {
			int result = servicePackDao.deleteServicePackDetails(id, isEnabled);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "操作成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("deleteServicePackDetails occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}

		json.put("code", -1);
		json.put("msg", "操作失败！");
		return json;
	}

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

	@Override
	public JSONObject addProductCategories(Map<String, String> map) {
		JSONObject json = new JSONObject();
		try {
			if (map.get("name") == null || map.get("name").equals("")) {
				json.put("code", -1);
				json.put("msg", "商品类型名称不能为空！");
				return json;
			}

			if (map.get("serviceId") == null || map.get("serviceId").equals("")) {
				json.put("code", -1);
				json.put("msg", "请选择服务项类型！");
				return json;
			}

			if (map.get("typeCode") == null || map.get("typeCode").equals("")) {
				json.put("code", -1);
				json.put("msg", "商品类型编码不能为空！");
				return json;
			}

			String name = "";
			name = servicePackDao.findProductByName(map.get("name"), map.get("serviceId"));

			//新增
			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("")) {
				Map<String, String> mapProduct = new HashMap<String, String>();
				mapProduct = servicePackDao.queryProductCategories(map.get("id"));
				if (null != mapProduct && StringUtils.isNotEmpty(name) && !mapProduct.get("name").equals(name)) {
					json.put("code", -1);
					json.put("msg", "商品分类名称已经存在，请重新填写！");
					return json;
				}
			}
			int result = servicePackDao.addProductCategories(map);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "操作成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("addServicePack occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "操作失败！");
		return json;
	}

	@Override
	public Map<String, String> queryProductCategories(String id) {
		try {
			return servicePackDao.queryProductCategories(id);
		} catch (Exception e) {
			LOG.error("queryProductCategories occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public JSONObject deleteProductCategories(String id, String isEnabled) {
		JSONObject json = new JSONObject();
		try {
			//如果被关联了，则不能被删除
			int count = servicePackDao.findServicePackDetailsId(Integer.valueOf(id));
			if (count > 0) {
				json.put("code", -1);
				json.put("msg", "该商品类型已经被关联，请先删除关联的物品，删除失败！");
				return json;
			}
			int result = servicePackDao.updateProductCategories(Integer.valueOf(id), isEnabled);
			if (result > 0) {
				json.put("code", 0);
				json.put("msg", "删除成功！");
				return json;
			}
		} catch (Exception e) {
			LOG.error("deleteProductCategories occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		json.put("code", -1);
		json.put("msg", "删除失败！");
		return json;
	}

	@Override
	public List<Map<String, String>> queryCategoriesList(String serviceId) {
		try {
			return servicePackDao.queryCategoriesList(serviceId);
		} catch (Exception e) {
			LOG.error("queryCategoriesList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<Map<String, String>> queryServicePackDetailsList(String categoriesId) {
		try {
			return servicePackDao.queryServicePackDetailsList(categoriesId);
		} catch (Exception e) {
			LOG.error("queryServicePackDetailsList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<Map<String, String>> queryServiceDetailsList(String dataIdarr) {
		try {
			String jsonArray = StringUtils.join(JSONArray.parseArray(dataIdarr), ",");
			return servicePackDao.queryServiceDetailsList(jsonArray);
		} catch (Exception e) {
			LOG.error("queryServiceDetailsList occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public Map<String, String> queryServiceDetails(String serviceId) {
		try {
			return servicePackDao.queryServiceDetails(serviceId);
		} catch (Exception e) {
			LOG.error("queryServiceDetails occur error." + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return null;
	}

	@Override
	public List<ProductCategories> queryCategoriesListByServiceCode(
			String serviceCode) {
		ProductCategories categories = servicePackDao.getProductCategoriesByServiceCode(serviceCode);
		if (categories != null) {
			return servicePackDao.queryChildrenCategoriesList(categories.getCode());
		}
		return null;
	}

	@Override
	public ProductCategories querProductCategoriesByServiceCode(String serviceCode) {
		return servicePackDao.querProductCategoriesByServiceCode(serviceCode);
	}

	@Override
	public Page getProductCategoriesPage(String whereSql, Integer pageSize,
										 Integer pageNo) throws Exception {
		return servicePackDao.getProductCategoriesPage(whereSql, pageSize, pageNo);
	}

	@Override
	public List<Map<String, Object>> getProductCategoriesNodes(String parentCode)
			throws Exception {
		return servicePackDao.getProductCategoriesNodes(parentCode);
	}

	@Override
	public Map<String, Object> saveUpdateProductCategories(
			ProductCategories productCategories)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Boolean flag = servicePackDao.querProductCateoriesIsFlag(productCategories.getId(), productCategories.getServiceCode());
			if (flag) {
				resultMap.put("flag", false);
				resultMap.put("desc", "该服务编码已存在");
				return resultMap;
			}
			if (productCategories.getId() != null) {
				servicePackDao.updateObj(productCategories);
				resultMap.put("flag", true);
				resultMap.put("desc", "编辑成功！");

				if ("0".equals(productCategories.getParentCode())) {
					servicePackDao.updateProductCategoriesServiceId(productCategories.getServiceId(), productCategories.getCode());
				} else {
					productCategories = servicePackDao.querProductCategoriesByCode(productCategories.getParentCode());
					servicePackDao.updateProductCategories(productCategories.getServiceId(), productCategories.getCode());
				}

			} else {
				Integer code = servicePackDao.queryProductCategoriesMaxCode();
				if (code == 0) {
					resultMap.put("flag", false);
					resultMap.put("desc", "获取code出错");
					return resultMap;
				}
				productCategories.setCode(code + "");
				if (!"0".equals(productCategories.getParentCode())) {
					ProductCategories ps = servicePackDao.querProductCategoriesByCode(productCategories.getParentCode());
					productCategories.setServiceId(ps.getServiceId());
				}
				servicePackDao.save(productCategories);
				resultMap.put("flag", true);
				resultMap.put("desc", "新增成功！");
			}
		} catch (Exception e) {
			LOG.error("saveUpdateProductCategories error{}" + LogExceptionStackUtil.LogExceptionStack(e));
			resultMap.put("flag", false);
			if (productCategories.getId() != null) {
				resultMap.put("desc", "修改失败");
			} else {
				resultMap.put("desc", "新增失败");
			}
		}
		return resultMap;
	}

	@Override
	public ProductCategories querProductCategoriesById(Integer id) throws Exception {
		return servicePackDao.querProductCategoriesById(id);
	}

	@Override
	public String delProductCategories(ProductCategories productCategories)
			throws Exception {
		try {
			if ("0".equals(productCategories.getParentCode())) {
				servicePackDao.deleteProductCategories(productCategories.getCode(), "0");
			}
			productCategories.setIsEnabled("0");
			servicePackDao.updateObj(productCategories);
			return "success";
		} catch (Exception e) {
			LOG.error("delProductCategories error{}" + LogExceptionStackUtil.LogExceptionStack(e));
		}
		return "error";
	}
}
