package com.palmnest.service.notice.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.notice.WxtemplateMessageTypeDao;
import com.palmnest.model.notice.WxtemplateMessageTypeModel;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.notice.WxtemplateMessageTypeManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service("wxtemplateMessageTypeManager")
public class WxtemplateMessageTypeManagerImpl extends GenericManagerImpl<WxtemplateMessageTypeModel, Integer> implements WxtemplateMessageTypeManager {

	private WxtemplateMessageTypeDao wxtemplateMessageTypeDao;

	@Autowired
	public void setWxtemplateMessageTypeDao(
			WxtemplateMessageTypeDao wxtemplateMessageTypeDao) {
		this.wxtemplateMessageTypeDao = wxtemplateMessageTypeDao;
		this.dao = wxtemplateMessageTypeDao;
	}

	/**
	 * longmeizhen
	 */
	@Override
	public Page getTemplateMessageTypeList(String orgIds, int wechatAccountId, int pageSize, int pageNo) throws Exception {
		return wxtemplateMessageTypeDao.getTemplateMessageTypeList(orgIds, wechatAccountId, pageSize, pageNo);
	}

	@Override
	public Map<String, Object> saveOrUpdate(WxtemplateMessageTypeModel wxtemplateMessageTypeModel) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		String isSuccess = "100";

		if (wxtemplateMessageTypeModel.getId() == null || wxtemplateMessageTypeModel.getId() == 0) {

			wxtemplateMessageTypeModel = wxtemplateMessageTypeDao.save(wxtemplateMessageTypeModel);
			if (wxtemplateMessageTypeModel != null) {
				isSuccess = "200";
			}

		} else {
			try {
				wxtemplateMessageTypeDao.updateObj(wxtemplateMessageTypeModel);
				isSuccess = "200";

			} catch (Exception e) {
				log.error(e.getMessage());
				map.put("code", "100");
				map.put("msg", e.getMessage());
				map.put("data", wxtemplateMessageTypeModel);
				throw new Exception();
			}
		}
		map.put("code", isSuccess);
		map.put("msg", "");
		map.put("data", wxtemplateMessageTypeModel);
		return map;
	}

	@Override
	public WxtemplateMessageTypeModel findBy(String templateid) {
		return wxtemplateMessageTypeDao.findBy(templateid);
	}

	@Override
	public List<Map<String, Object>> findByOrgIdAndDevelopId(Integer orgId,
															 String developId) {
		return wxtemplateMessageTypeDao.findByOrgIdAndDevelopId(orgId, developId);
	}
}
