package cn.hmsoft.weixin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.service.FrameService;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.weixin.data.dao.WxMsgReplyDao;
import cn.hmsoft.weixin.data.model.WxMsgReply;

@Service
public class WxMsgReplyService extends FrameService {

	@Autowired
	private WxMsgReplyDao daoReply;

	public void addMsgReply(int wx_id, WxMsgReply msg, FrameOptr optr) {
		FrameAssertUtil.checkParam(msg.getMsg_name(), msg.getMsg_content());
		String sql = "select * from wx_msg_reply where wx_id=? and msg_name=?";
		WxMsgReply mReply = this.daoReply.findBySql(WxMsgReply.class, sql, wx_id, msg.getMsg_name());
		FrameAssertUtil.isNull(mReply, "关键字[" + msg.getMsg_name() + "]已经存在");
		this.daoReply.insert(msg);

	}

	public void editMsgReply(int wx_id, WxMsgReply msg, FrameOptr optr) {
		FrameAssertUtil.checkParam(msg.getMsg_name(), msg.getMsg_content());
		WxMsgReply wp = this.daoReply.find(msg.getMsg_id());
		FrameAssertUtil.isNotNull(wp, "基础数据错误：关键字不存在");
		// 去服务器请求
		String sql = "select * from wx_msg_reply where wx_id=? and msg_name=? and msg_id!=?";
		FrameAssertUtil.isNull(this.daoReply.findBySql(sql, wx_id, msg.getMsg_name(), msg.getMsg_id()),
				"关键字[" + msg.getMsg_name() + "]已经存在");
		this.daoReply.update(msg);

	}

	public void delMsgReply(int wx_id, int msg_id, FrameOptr optr) {
		String sql = "delete from wx_msg_reply where wx_id=? and msg_id=?";
		this.daoReply.updateBySql(sql, wx_id, msg_id);

	}

}
