package com.palmnest.service.estateserve.impl;

import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.estateserve.ScPicandtextdetailstempDao;
import com.palmnest.dao.estateserve.ScReplykeywordsDao;
import com.palmnest.dao.estateserve.ScTextpicvoicetempDao;
import com.palmnest.model.estateserve.Picandtextdetailstemp;
import com.palmnest.model.estateserve.Replykeywords;
import com.palmnest.model.estateserve.Textpicvoicetemp;
import com.palmnest.service.estateserve.ScReplykeywordsManager;
import com.palmnest.service.impl.GenericManagerImpl;
@Slf4j
@Service("scReplykeywordsManager")
public class ScReplykeywordsManagerImpl extends GenericManagerImpl<Replykeywords, Integer> implements ScReplykeywordsManager {

	private ScReplykeywordsDao         scReplykeywordsDao;
	private ScPicandtextdetailstempDao scPicandtextdetailstempDao;
	private ScTextpicvoicetempDao      scTextpicvoicetempDao;

	@Autowired
	public void setScTextpicvoicetempDao(ScTextpicvoicetempDao scTextpicvoicetempDao) {
		this.scTextpicvoicetempDao = scTextpicvoicetempDao;
	}

	@Autowired
	public void setScPicandtextdetailstempscPicandtextdetailstempDao(
			ScPicandtextdetailstempDao scPicandtextdetailstempDao) {
		this.scPicandtextdetailstempDao = scPicandtextdetailstempDao;
	}

	@Autowired
	public void setScReplykeywordsDao(ScReplykeywordsDao scReplykeywordsDao) {
		this.scReplykeywordsDao = scReplykeywordsDao;
	}

	public Replykeywords getReplykeywordsById(Integer kid) {
		return this.scReplykeywordsDao.getReplykeywordsById(kid);
	}

	@Override
	public Page getReplykeywords(int pageno, int pagesize, String developId, String name, String state) {
		return scReplykeywordsDao.getReplykeywords(pageno, pagesize, developId, name, state);
	}

	@Transactional
	@Override
	public void deleteTemplate(final Integer... ids) {
		scReplykeywordsDao.delReplykeywords(ids);
		scPicandtextdetailstempDao.delPicandtextdetailstemp(ids);
		scTextpicvoicetempDao.delTextpicvoicetemp(ids);
	}

	@Transactional
	@Override
	public boolean saveScTextpicvoicetemp(Replykeywords replykeywords, String text) {
		try {
			if (replykeywords != null && replykeywords.getReplyKeywordsId() != null) {
				//删除原来的
				scTextpicvoicetempDao.delTextpicvoicetempByKeywordsId(replykeywords.getReplyKeywordsId());
			}
			Replykeywords new_Ojb = scReplykeywordsDao.save(replykeywords);
			Textpicvoicetemp textpicvoicetemp = new Textpicvoicetemp();
			textpicvoicetemp.setText(text);
			textpicvoicetemp.setReplyType("1");//1文字 2图片 3语音
			textpicvoicetemp.setReplyId("0");//文字
			textpicvoicetemp.setReplyKeywordsId(new_Ojb.getReplyKeywordsId());
			textpicvoicetemp.setCreateTime(new Date());
			scTextpicvoicetempDao.save(textpicvoicetemp);
			return true;
		} catch (Exception e) {
			log.error("scReplykeywordsManager saveScTextpicvoicetemp error{}", e);
			return false;
		}
	}

	@Transactional
	@Override
	public boolean saveScPicandtexttemp(Replykeywords replykeywords, List<Picandtextdetailstemp> ls) {
		try {
			if (replykeywords != null) {
				//删除原来的
				scPicandtextdetailstempDao.delPicandtextdetailstemp(replykeywords.getReplyKeywordsId());
			}
			Replykeywords new_Ojb = scReplykeywordsDao.save(replykeywords);
			for (Picandtextdetailstemp scPicandtextdetailstemp : ls) {
				scPicandtextdetailstemp.setReplyKeywordsId(new_Ojb.getReplyKeywordsId());
				scPicandtextdetailstempDao.save(scPicandtextdetailstemp);
			}
			return true;
		} catch (Exception e) {
			log.error("scReplykeywordsManager saveScPicandtexttemp error{}", e);
			return false;
		}
	}
}
