package jp.agentec.sinaburocast.service;

import java.util.List;

import jp.agentec.sinaburocast.entity.AdminNotifyMail;
import jp.agentec.sinaburocast.entity.AdminUser;
import jp.agentec.sinaburocast.entity.MemberNotifyMail;
import jp.agentec.sinaburocast.form.admin.enquete.EnqueteMailSettingForm;

import org.seasar.extension.jdbc.where.SimpleWhere;
import org.seasar.framework.beans.util.Beans;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;

@Component(instance=InstanceType.SINGLETON)
public class AdminNotifyMailService extends AbstractService<AdminNotifyMail> {

	public static final String ID_SEQ_NAME = "admin_notify_mail_id_seq";
	
	public MemberNotifyMailService memberNotifyMailService;

	/**
	 * IDを発行して、登録する。
	 */
	public int insertAdminNotifyMail(AdminNotifyMail adminNotifyMail, String insId) {
		adminNotifyMail.adminNotifyMailId =	getSeqNextVal(Integer.class, ID_SEQ_NAME);
		return super.insert(adminNotifyMail, insId);
	}
	
	
    public AdminNotifyMail findById(Integer adminNotifyMailId) {
        return select().id(adminNotifyMailId).getSingleResult();
    }
    
    public AdminNotifyMail findByEnqueteId(Integer enqueteId) {
        return select().where(new SimpleWhere().eq("enqueteId",enqueteId )).getSingleResult();
    }
    
    

    public List<AdminNotifyMail> findAllOrderById() {
        return select().orderBy("adminNotifyMailId asc").getResultList();
    }
    
    public List<AdminNotifyMail> findAllByEnqueteId(String eid) {
        return select().where(new SimpleWhere().eq("enqueteId",eid)).getResultList();
    }
    
    /**
     * アンケート検索画面で削除ボタンを押した場合。
     * @param eid アンケートID
     * @return
     */
    public int deleteByEnqueteId(String eid){
    	List<AdminNotifyMail> list = findAllByEnqueteId(eid);
    	if(!list.isEmpty()){
    		jdbcManager.deleteBatch(list).execute();
    	}
    	
    	return 0;
    }
    
    /**
     * アンケートメール設定
     * @param enqueteMailSettingForm
     * @param adminUser
     */
    public void registOrUpdate(EnqueteMailSettingForm enqueteMailSettingForm,AdminUser adminUser){
    	
    	/**「回答ユーザ用通知メール設定情報」登録・更新 */
    	MemberNotifyMail memberNotifyMail = new MemberNotifyMail();
		memberNotifyMail.enqueteId = Integer.parseInt(enqueteMailSettingForm.eid);
		memberNotifyMail.notifyMailSendFlg = Integer.parseInt(enqueteMailSettingForm.memberNotifyMailSendFlg);
		memberNotifyMail.notifyMailSendBcc = enqueteMailSettingForm.memberNotifyMailSendBcc;
		memberNotifyMail.notifyMailSendTo = null;;
		memberNotifyMail.subject = enqueteMailSettingForm.memberSubject;
		memberNotifyMail.mailBody = enqueteMailSettingForm.memberMailBody;
		
		MemberNotifyMail memberNotifyMailTmp = memberNotifyMailService.findByEnqueteId(memberNotifyMail.enqueteId);
		if(memberNotifyMailTmp == null){//ない場合は登録
			memberNotifyMailService.insertMemberNotifyMail(memberNotifyMail, adminUser.loginId);
		}else{
			Beans.copy(memberNotifyMail, memberNotifyMailTmp).excludesNull().excludes("version").execute();
			memberNotifyMailService.update(memberNotifyMailTmp, adminUser.loginId);
		}
		
		/**「管理者用通知メール設定情報」登録・更新*/
		AdminNotifyMail adminNotifyMail = new AdminNotifyMail();
		adminNotifyMail.enqueteId = Integer.parseInt(enqueteMailSettingForm.eid);
		adminNotifyMail.notifyMailSendFlg = Integer.parseInt(enqueteMailSettingForm.adminNotifyMailSendFlg);
		adminNotifyMail.notifyMailSendTo = enqueteMailSettingForm.adminNotifyMailSendTo;
		adminNotifyMail.notifyMailSendCc = enqueteMailSettingForm.adminNotifyMailSendCc;		
		adminNotifyMail.subject = enqueteMailSettingForm.adminSubject;
		adminNotifyMail.mailBody = enqueteMailSettingForm.adminMailBody;
		
		AdminNotifyMail adminNotifyMailTmp = this.findByEnqueteId(adminNotifyMail.enqueteId);
		if(adminNotifyMailTmp == null){//ない場合は登録
			this.insertAdminNotifyMail(adminNotifyMail, adminUser.loginId);			
		}else{
			Beans.copy(adminNotifyMail, adminNotifyMailTmp).excludesNull().excludes("version").execute();
			this.update(adminNotifyMailTmp, adminUser.loginId);
		}
    }
    
}