package com.palmnest.service.estateserve.impl;

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

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.palmnest.application.Constants;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.FileUploadUtils;
import com.palmnest.dao.estateserve.ServiceProjectAllocationDao;
import com.palmnest.dao.estateserve.ServiceTypeInfoDao;
import com.palmnest.dao.estateserve.impl.ServiceTypeInfoDaoImpl;
import com.palmnest.model.estateserve.ServiceProjectAllocation;
import com.palmnest.model.estateserve.ServiceTypeInfo;
import com.palmnest.service.estateserve.ServiceTypeInfoManager;
import com.palmnest.service.impl.GenericManagerImpl;

@Service("serviceTypeInfoManager")
public class ServiceTypeInfoManagerImpl extends GenericManagerImpl<ServiceTypeInfo, Integer> implements ServiceTypeInfoManager {

	private Logger LOG = LoggerFactory.getLogger(ServiceTypeInfoDaoImpl.class);

	private ServiceTypeInfoDao serviceTypeInfoDao;

	@Autowired
	public void setServiceTypeInfoDao(ServiceTypeInfoDao serviceTypeInfoDao) {
		this.serviceTypeInfoDao = serviceTypeInfoDao;
		this.dao = serviceTypeInfoDao;
	}

	@Autowired
	private ServiceProjectAllocationDao serviceProjectAllocationDao;


	@Override
	public Page serviceTypeInfoPage(Integer pageNo, Integer pageSize,
									String whereSql) throws Exception {
		try {
			return serviceTypeInfoDao.serviceTypeInfoPage(pageNo, pageSize, whereSql);
		} catch (Exception e) {
			LOG.error("service find ServiceTypeInfoPage error{}", e);
		}
		return null;
	}

	@Override
	public Map<String, Object> saveUpdateServiceTypeInfo(
			ServiceTypeInfo serviceTypeInfo, String actype, String organizationId, String roleGroup) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (serviceTypeInfo.getId() != null) {
				ServiceTypeInfo eidtServiceTypeInfo = serviceTypeInfoDao.get(serviceTypeInfo.getId());
				eidtServiceTypeInfo.setModifier(serviceTypeInfo.getModifier());
				eidtServiceTypeInfo.setModifyTime(serviceTypeInfo.getModifyTime());
				eidtServiceTypeInfo.setCode(serviceTypeInfo.getCode());
				eidtServiceTypeInfo.setServiceName(serviceTypeInfo.getServiceName());
				eidtServiceTypeInfo.setServiceRemark(serviceTypeInfo.getServiceRemark());
				eidtServiceTypeInfo.setSkipAddress(serviceTypeInfo.getSkipAddress());
				eidtServiceTypeInfo.setJumpAddress(serviceTypeInfo.getJumpAddress());
				eidtServiceTypeInfo.setServiceType(serviceTypeInfo.getServiceType());
				eidtServiceTypeInfo.setServiceStatus(serviceTypeInfo.getServiceStatus());
				eidtServiceTypeInfo.setLinkUrlType(serviceTypeInfo.getLinkUrlType());
				eidtServiceTypeInfo.setRegisterStatus(serviceTypeInfo.getRegisterStatus());
				eidtServiceTypeInfo.setPaymentType(serviceTypeInfo.getPaymentType());
				eidtServiceTypeInfo.setDepositType(serviceTypeInfo.getDepositType());
				eidtServiceTypeInfo.setIsSignContract(serviceTypeInfo.getIsSignContract());
				eidtServiceTypeInfo.setServiceCategory(serviceTypeInfo.getServiceCategory());
				eidtServiceTypeInfo.setGrayUrl(serviceTypeInfo.getGrayUrl());
				String delUrl = "";
				String delGrayAppUrl = "";
				String delGrayWechatUrl = "";

				if (StringUtils.isNotBlank(serviceTypeInfo.getServiceIcon()) && !serviceTypeInfo.getServiceIcon().endsWith(".jpeg")) {
					delUrl = Constants.IMAGE_ROOT + eidtServiceTypeInfo.getServiceIcon();
					eidtServiceTypeInfo.setServiceIcon(FileUploadUtils.getBase64ImageUrl(serviceTypeInfo.getServiceIcon()));
				}

				if (StringUtils.isNotBlank(serviceTypeInfo.getGrayAppServiceIcon()) && !serviceTypeInfo.getGrayAppServiceIcon().endsWith(".jpeg")) {
					delGrayAppUrl = Constants.IMAGE_ROOT + eidtServiceTypeInfo.getGrayAppServiceIcon();
					eidtServiceTypeInfo.setGrayAppServiceIcon(FileUploadUtils.getBase64ImageUrl(serviceTypeInfo.getGrayAppServiceIcon()));
				}

				if (StringUtils.isNotBlank(serviceTypeInfo.getGrayWechatServiceIcon()) && !serviceTypeInfo.getGrayWechatServiceIcon().endsWith(".jpeg")) {
					delGrayWechatUrl = Constants.IMAGE_ROOT + eidtServiceTypeInfo.getGrayWechatServiceIcon();
					eidtServiceTypeInfo.setGrayWechatServiceIcon(FileUploadUtils.getBase64ImageUrl(serviceTypeInfo.getGrayWechatServiceIcon()));
				}

				serviceTypeInfoDao.updateObj(eidtServiceTypeInfo);
				FileUploadUtils.deleteFileByPath(delUrl);
				FileUploadUtils.deleteFileByPath(delGrayAppUrl);
				FileUploadUtils.deleteFileByPath(delGrayWechatUrl);
				resultMap.put("flag", true);
				resultMap.put("desc", "更新成功！");
			} else {
				if ("1".equals(serviceTypeInfo.getServiceStatus())) {
					serviceTypeInfo.setProjectId(Integer.parseInt(organizationId));
					serviceTypeInfo.setType("0");
				}
				serviceTypeInfoDao.saveObj(serviceTypeInfo);
				resultMap.put("flag", true);
				resultMap.put("desc", "新增成功！");
			}
		} catch (Exception e) {
			LOG.error("service saveUpdate saveUpdateServiceTypeInfo error{}", e);
			resultMap.put("flag", false);
			resultMap.put("desc", "操作失败！");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> saveUpdateServiceProjectTypeInfoManager(
			String organizationId, String organizationIds, String creator) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<ServiceProjectAllocation> list = serviceProjectAllocationDao.findServiceProjectAllocationList(organizationId);
			String str[] = organizationIds.split(";");
			if (list != null && list.size() > 0) {
				for (String orgId : str) {
					serviceProjectAllocationDao.deleteProjectAllocation(orgId);
					serviceTypeInfoDao.deleteServiceTypeInfo(orgId);
					for (ServiceProjectAllocation serviceProjectAllocation : list) {
						ServiceTypeInfo serviceTypeInfo = null;
						serviceTypeInfo = serviceTypeInfoDao.getServiceTypeInfo(serviceProjectAllocation.getServiceTypeId());
						ServiceTypeInfo serviceTypeInfo2 = null;
						if (serviceTypeInfo != null && serviceTypeInfo.getProjectId() != null) {
							serviceTypeInfo2 = new ServiceTypeInfo();
							serviceTypeInfo2.setCode(serviceTypeInfo.getCode());
							serviceTypeInfo2.setCreateTime(serviceTypeInfo.getCreateTime());
							serviceTypeInfo2.setCreator(serviceTypeInfo.getCreator());
							serviceTypeInfo2.setDepositType(serviceTypeInfo.getDepositType());
							serviceTypeInfo2.setEnterpriseAddress(serviceTypeInfo.getEnterpriseAddress());
							serviceTypeInfo2.setIsEnabled(serviceTypeInfo.getIsEnabled());
							serviceTypeInfo2.setIsSignContract(serviceTypeInfo.getIsSignContract());
							serviceTypeInfo2.setLinkUrlType(serviceTypeInfo.getLinkUrlType());
							serviceTypeInfo2.setModifier(serviceTypeInfo.getModifier());
							serviceTypeInfo2.setModifyTime(serviceTypeInfo.getModifyTime());
							serviceTypeInfo2.setJumpAddress(serviceTypeInfo.getJumpAddress());
							serviceTypeInfo2.setProjectId(Integer.parseInt(orgId));
							serviceTypeInfo2.setPaymentType(serviceTypeInfo.getPaymentType());
							serviceTypeInfo2.setRegisterStatus(serviceTypeInfo.getRegisterStatus());
							serviceTypeInfo2.setServiceIcon(serviceTypeInfo.getServiceIcon());
							serviceTypeInfo2.setServiceName(serviceTypeInfo.getServiceName());
							serviceTypeInfo2.setServiceType(serviceTypeInfo.getServiceType());
							serviceTypeInfo2.setSkipAddress(serviceTypeInfo.getSkipAddress());
							serviceTypeInfo2.setServiceStatus(serviceTypeInfo.getServiceStatus());
							serviceTypeInfo2.setType(serviceTypeInfo.getType());
							serviceTypeInfoDao.saveObj(serviceTypeInfo2);
						}
						ServiceProjectAllocation serviceProjectAllocation2 = new ServiceProjectAllocation();
						if (serviceTypeInfo2 != null) {
							serviceProjectAllocation2.setServiceTypeId(serviceTypeInfo2.getId());
						} else {
							serviceProjectAllocation2.setServiceTypeId(serviceProjectAllocation.getServiceTypeId());
						}
						serviceProjectAllocation2.setCreateTime(new Date());
						serviceProjectAllocation2.setCreator(creator);
						serviceProjectAllocation2.setProjectId(Integer.parseInt(orgId));
						serviceProjectAllocation2.setOrderNo(serviceProjectAllocation.getOrderNo());
//						serviceProjectAllocation2.setRoleGroup(serviceProjectAllocation.getRoleGroup());
						serviceProjectAllocation2.setStatus(serviceProjectAllocation.getStatus());
						serviceProjectAllocation2.setType(serviceProjectAllocation.getType());
						serviceProjectAllocation2.setIsEnabled(serviceProjectAllocation.getIsEnabled());
						serviceProjectAllocationDao.save(serviceProjectAllocation2);
					}
				}
			}
			resultMap.put("flag", true);
			resultMap.put("desc", "处理成功！");
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("service saveUpdate saveUpdateServiceProjectTypeInfoManager error{}", e);
			resultMap.put("flag", false);
			resultMap.put("desc", "处理失败！");
		}
		return resultMap;
	}

	@Override
	public List<ServiceTypeInfo> getServiceTypeInfoByShkipAddress(
			String shkipAddress) {

		return serviceTypeInfoDao.getServiceTypeInfoByShkipAddress(shkipAddress);
	}

	@Override
	public int maxId() {
		return serviceTypeInfoDao.maxId();
	}

	@Override
	public ServiceTypeInfo getServiceTypeInfoByServiceCode(String serviceCode) {
		return serviceTypeInfoDao.getServiceTypeInfoByServiceCode(serviceCode);
	}

}
