/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.icss.hit.struts.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.icss.hit.bean.MessageSaveBean;
import com.icss.hit.bean.interfaces.MessageDao;
import com.icss.hit.hibernate.vo.Message;
import com.icss.hit.hibernate.vo.MessageReceivers;
import com.icss.hit.hibernate.vo.ReceiverInfo;
import com.icss.hit.hibernate.vo.SysUser;
import com.icss.hit.struts.form.DraftMessageForm;
import com.icss.hit.struts.form.NewUserMessageForm;

/** 
 * ÐÞ¸Ä²Ý¸åÏäÀïÃæµÄÐÅÏ¢£¬ÊÇ·¢ËÍ»¹ÊÇ±£´æ
 * Creation date: 08-08-2009
 * @author ÕÔÓ±Éê
 * XDoclet definition:
 * @struts.action path="/messageAfterAlter" name="draftMessageForm" input="/draftMessage.do" scope="request" validate="true"
 */
public class MessageAfterAlterAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewUserMessageForm newUserMessageForm = (NewUserMessageForm) form;// TODO Auto-generated method stub
		//ÓÃ»§ID
		long uid = 1;
		if( request.getSession().getAttribute("UserId") != null ){
			uid = Long.parseLong(request.getSession().getAttribute("UserId").toString());
		}else{
			return mapping.findForward("NullLogin");
		}
		
		long mesID = -2;
		long ReciverID = -2;
		Long result;
		String riBox = "0";
		SysUser user = new SysUser();
		user.setSuId(uid);
		Date date = new Date();
		// ÀàÐÍ
		String optype = newUserMessageForm.getType();
		// ÓÃ»§ID¼¯ºÏ
		String userIds = newUserMessageForm.getIdArray();
		// ±êÌâ
		String mesTitle = newUserMessageForm.getMessageTitle();
		// ÄÚÈÝ
		String mesContent = newUserMessageForm.getMessagecontent();
		
		long MesID = -1;
		try{
			MesID = Long.parseLong(newUserMessageForm.getMesID());
		}
		catch(Exception ex)
		{
			MesID = -1;
			ex.getStackTrace();
		}
		if(MesID == -1)return mapping.findForward("messageAfterAlter.fail");
		System.out.println(MesID);
		//¶ÔÓ¦µÄMesIDÉ¾³ýÐÅÏ¢
		MessageDao messageAlter = new MessageSaveBean();
		//System.out.println(MesID);
		if(!messageAlter.deleteDraftMessage(MesID))return mapping.findForward("messageAfterAlter.fail");
		
		
		
		
		
		//É¾³ýºóÌí¼Ó£¡£¡£¡
		String[] Users;           
		Users = userIds.split(",");                        //·Ö³öÀ´µÄÓÃ»§ID
		System.out.println(userIds);
		//ÉèÖÃÐÅÏ¢µÄ»ù±¾ÐÅÏ¢
		Message myMessage = new Message();
		myMessage.setMsTitle(mesTitle);
		myMessage.setMsContent(mesContent);
		myMessage.setMsSendtime(date);
		myMessage.setSysUser(user);
		myMessage.setMsDelete("0");
		//System.out.println(userIds);
		//MessageDao messageSave = new MessageSaveBean();
		//±êÊ¾ÐÂ½¨ËùÊôÐÅÏä£¬1Îª·¢¼þÏä£¬2Îª²Ý¸åÏä,3ÎªÊÕ¼þÏä
		if(optype.equals("send"))
		{
			System.out.println("send");
			//´¢´æÔÚ·¢¼þÏäÖÐ£¬µ±ÓÃ»§Ïë·¢ËÍÓÊ¼þÊÇ£¬·ÅÈë·¢¼þÏä£¬×¢ÒâµÄÊÇÓÊ¼þµÄ·¢ËÍÈËµÄÓÊÏäÐ´µÄÊÇ·¢¼þÏä£¬ËùÓÐ½ÓÊÕÈËµÄÓÊÏäÐ´µÄÊÇÊÕ¼þÏä
			riBox = "3";
			myMessage.setMsBox("1");
			
		}
		else if(optype.equals("save"))
		{	
			System.out.println("save");
			//´¢´æÔÚ²Ý¸åÏä£¬µ±ÓÃ»§Ïë±£´æ²Ý¸åµÄÊ±ºò£¬·ÅÈë²Ý¸åÏä£¬×¢ÒâµÄÊÇÓÊ¼þµÄ·¢ËÍÈËµÄÓÊÏäÐ´µÄÊÇ²Ý¸åÏä£¬ËùÓÐ½ÓÊÕÈËµÄÓÊÏäÐ´µÄÊÇ²Ý¸åÏä
			riBox = "2";
			myMessage.setMsBox("2");
		}
		mesID  = messageAlter.saveReceiveMessage(myMessage); //±£´æÐÅÏ¢ÓÃµÄ£¬×¢ÒâÁË°¡°¡°¡°¡£¡£¡£¡£¡
		if(mesID == -1)return mapping.findForward("messageAfterAlter.fail");
		
		//ÒÔÏÂÊÇÊäÈëÐÅÏ¢µÄÊÕ¼þÈË£¬ºÍÊÕ¼þÈËµÄËùÊôÓÊÏä
		for(int i = 0;i < Users.length; i++)
		{
			ReceiverInfo receiver = new ReceiverInfo();
			//ÉèÖÃÊÕ¼þÈËÐÅÏ¢ÀïÃæÏà¹ØÁªµÄÐÅÏ¢µÄIDºÅ
			Message message = new Message();
			message.setMsId(mesID);
			receiver.setMessage(message);
			//ÉèÖÃ½ÓÊÕÈËµÄÓÊÏä
			receiver.setRiBox(riBox);
			//ÉèÖÃÎ´¶Á
			receiver.setRiRead("0");
			//ÉèÖÃÎ´É¾³ý
			receiver.setRiDelete("0");
			//ÉèÖÃÓÃ»§µÄID
			SysUser sysUser = new SysUser();
			Long id;
			try
			{
				id = Long.parseLong(Users[i]);
			}
			catch(Exception ex)
			{
				ex.getStackTrace();
				return mapping.findForward("messageAfterAlter.fail");
			}
			sysUser.setSuId(id);
			receiver.setSysUser(sysUser);
			//±£´æÓÃ»§µÄÄÚÈÝ
			result = messageAlter.saveReceiveUsers(receiver);
			if(result == -1)return mapping.findForward("messageAfterAlter.fail");
		}
		return mapping.findForward("messageAfterAlter.succussd");
	}
}