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.WxMsgProcessorDao;
import cn.hmsoft.weixin.data.model.WxMsgProcessor;

@Service
public class WxMsgProcessorService extends FrameService {

	@Autowired
	private WxMsgProcessorDao daoProcessor;

	public void editProcessor(int wx_id, WxMsgProcessor processor, FrameOptr optr) {
		FrameAssertUtil.checkParam(wx_id, processor.getMsg_type(), processor.getProcess_class(),
				processor.getRun_thread());

		String sql = "select * from WX_MSG_PROCESSOR where wx_id=? and msg_type=?";
		WxMsgProcessor wp = this.daoProcessor.findBySql(WxMsgProcessor.class, sql, wx_id, processor.getMsg_type());

		sql = "update WX_MSG_PROCESSOR set process_class=?,process_param=?,process_desc=?,run_thread=? where wx_id=? and msg_type=?";
		if (wp == null) {
			this.daoProcessor.insert(processor);
		} else {
			this.daoProcessor.updateBySql(sql, processor.getProcess_class(), processor.getProcess_param(),
					processor.getProcess_desc(), processor.getRun_thread(), wx_id, processor.getMsg_type());
		}
	}

	public void addProcessor(int wx_id, WxMsgProcessor processor, FrameOptr optr) {
		FrameAssertUtil.checkParam(wx_id, processor.getMsg_type(), processor.getProcess_class(),
				processor.getRun_thread());

		String sql = "select * from WX_MSG_PROCESSOR where wx_id=? and msg_type=?";
		WxMsgProcessor wp = this.daoProcessor.findBySql(WxMsgProcessor.class, sql, wx_id, processor.getMsg_type());
		FrameAssertUtil.isNull(wp, "该消息处理器已经配置，请勿重复配置！");
		processor.setWx_id(wx_id);
		this.daoProcessor.insert(processor);
	}

	public void delProcessor(int wx_id, String msg_type, FrameOptr optr) {
		String sql = "delete from WX_MSG_PROCESSOR where wx_id=? and msg_type=?";
		this.daoProcessor.updateBySql(sql, wx_id, msg_type);
	}

}
