package com.palmnest.service.estateserve.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.dao.estateserve.ShoppingMallDao;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.estateserve.ShoppingMallManager;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.property.PropertyManager;

/**
 * 商城管理业务实现类
 *
 * @version 1.0
 * @authr xiemin
 */
@Component("shoppingMallManager")
public class ShoppingMallManagerImpl extends GenericManagerImpl<Object, Integer> implements ShoppingMallManager {

	@Autowired
	private ShoppingMallDao shoppingMallDao;
	@Autowired
	private PropertyManager propertyManager;

	public Pagenation<Map<String, String>> queryMallList(ModelMap modelMap) {
		if (modelMap == null || modelMap.size() <= 0) {
			return null;
		}
		modelMap.put("pageSize", "10");
		try {
			return shoppingMallDao.queryMallList(modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
//	@Transactional(rollbackFor = Exception.class)
	public JSONObject addMallUrl(ModelMap modelMap) {
		JSONObject json = new JSONObject();
		try {
			if (modelMap.get("mallName") == null || modelMap.get("mallName").equals("")) {
				json.put("code", "-1");
				json.put("msg", "url名称不能为空");
				return json;
			}
			if (modelMap.get("organizationId") == null || modelMap.get("organizationId").equals("")) {
				json.put("code", "-1");
				json.put("msg", "项目名称不能为空");
				return json;
			}

			TbOrganization organization = propertyManager.getTbOrganizationById(Integer.valueOf(String.valueOf(modelMap.get("organizationId"))));

			if (organization == null || organization.equals("")) {
				json.put("code", "-1");
				json.put("msg", "项目不存在，请重新填写");
				return json;
			}

			if (modelMap.get("url") == null || modelMap.get("url").equals("")) {
				json.put("code", "-1");
				json.put("msg", "url不能为空");
				return json;
			}

//			 String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" ;
//			 Pattern patt = Pattern. compile(regex);
//			 Matcher matcher = patt.matcher(String.valueOf(modelMap.get("url")));
//			 boolean isMatch = matcher.matches();
//			 if(!isMatch){
//			     json.put("code", "-1");
//				 json.put("msg", "您输入的URL地址不正确");
//				 return json;
//			 }
			if (modelMap.get("urlKey") == null || modelMap.get("urlKey").equals("")) {
				json.put("code", "-1");
				json.put("msg", "urlKey不能为空");
				return json;
			}
			if (modelMap.get("addressCode") == null || modelMap.get("addressCode").equals("")) {
				json.put("code", "-1");
				json.put("msg", "addressCode不能为空");
				return json;
			}

			int result = shoppingMallDao.addMallUrl(modelMap);
			if (result > 0) {
				json.put("code", "0");
				json.put("msg", "添加成功");
				return json;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("code", "-1");
		json.put("msg", "系统异常，添加失败");
		return json;
	}

	@Override
	public List<Map<String, String>> findByNameList(String orgName) {
		return shoppingMallDao.findByNameList(orgName);
	}

	@Override
	public Map<String, String> queryDetails(String id) {
		return shoppingMallDao.queryDetails(id);
	}

	@Override
	public JSONObject mallDelete(String id, String status) {
		JSONObject json = new JSONObject();
		try {
			int result = shoppingMallDao.mallDelete(id, status);
			if (result <= 0) {
				json.put("code", "-1");
				json.put("msg", "操作失败");
				return json;
			}

			json.put("code", "0");
			json.put("msg", "操作成功");
			return json;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	@Override
	public Map<String, String> queryUrlKey(String urlKey) {
		return shoppingMallDao.queryUrlKey(urlKey);
	}

	@Override
	public String queryAddressCode(String organizationId) {
		return shoppingMallDao.queryAddressCode(organizationId);
	}

}
