PointDonationService.java 5.49 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
package jp.agentec.sinaburocast.service;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.MessagingException;

import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.common.exception.ExpectedException;
import jp.agentec.sinaburocast.common.exception.SystemException;
import jp.agentec.sinaburocast.common.util.MailUtil;
import jp.agentec.sinaburocast.entity.AdminNotifyMail;
import jp.agentec.sinaburocast.entity.Enquete;
import jp.agentec.sinaburocast.entity.Member;
import jp.agentec.sinaburocast.entity.MemberNotifyMail;
import jp.agentec.sinaburocast.entity.PointDonation;
import jp.agentec.sinaburocast.entity.Question;

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

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

	public PointGetService pointGetService;
	public ReplyService replyService;
	
	public MemberService memberService;
	
	public static final String ID_SEQ_NAME = "point_donation_id_seq";

	/**
	 * IDを発行して、登録する。
	 */
	public int insertPointDonation(PointDonation pointDonation, String insId) {
		pointDonation.pointDonationId =
			getSeqNextVal(Integer.class, ID_SEQ_NAME);
		return super.insert(pointDonation, insId);
	}
	
	
    public PointDonation findById(Integer pointDonationId) {
        return select().id(pointDonationId).getSingleResult();
    }

    public PointDonation findBymemberId(Integer memberId) {
        return select().where(new SimpleWhere().eq("memberId", memberId)).getSingleResult();
    }

    public List<PointDonation> findAllOrderById() {
        return select().orderBy("pointDonationId asc").getResultList();
    }

    public List <PointDonation> findBymemberIdList(Integer memberId) {
        return select().where(new SimpleWhere().eq("memberId", memberId)).getResultList();
    }
    public MemberNotifyMailService memberNotifyMailService;
    public AdminNotifyMailService adminNotifyMailService;
    /**
     * 団体寄付の処理を行う。
     * @param pointDonation 
     * @param loginId 寄付したユーザーのID
     * @throws MessagingException 
     * @throws UnsupportedEncodingException 
     * @throws SystemException 
     * @throws ParseException 
     * @throws ExpectedException 
     */
	public void organizationDonation(PointDonation pointDonation, Member member, Enquete enquete, ArrayList<Question> questionList) throws UnsupportedEncodingException, MessagingException, SystemException, ParseException {
		// 回答を登録する
		replyService.replyRegistLogic(member, questionList, enquete, true);
		
		//ポイント削減する。
		pointGetService.upDownPointLogic(pointDonation.pointNum,pointDonation.memberId,member.loginId);
		
		Member upMember = memberService.findById(member.memberId);
		upMember.pointNum = upMember.pointNum - pointDonation.pointNum;
		if (upMember.pointNum < 0) {
			throw new SystemException("point is below 0. current=" +  upMember.pointNum + " donation=" + pointDonation.pointNum, "E013");
		}
		
		memberService.update(upMember, member.loginId);
		this.insertPointDonation(pointDonation, member.loginId);
		
		//■メール送信
		BeanMap wehreMap = new BeanMap();

		wehreMap.put("enqueteId", enquete.enqueteId);
		wehreMap.put("notifyMailSendFlg", SinaburoConstant.notifyMailSendFlg.SEND);

		//1件のみのはず
		List<MemberNotifyMail> memberNotifyMailList = memberNotifyMailService.findByCondition(wehreMap);
    	for (MemberNotifyMail memberNotifyMail : memberNotifyMailList) {

    		
    		//ユーザーに送信
    		if(StringUtil.isNotBlank(member.pcEmail)){
    			MailUtil.send(member.pcEmail, memberNotifyMail.subject, memberNotifyMail.mailBody);
    		}

    		if(StringUtil.isNotBlank(member.mbEmail)){
    			MailUtil.send(member.mbEmail, memberNotifyMail.subject,memberNotifyMail.mailBody);
    		}

    		//BCCに送信
    		if(StringUtil.isNotBlank(memberNotifyMail.notifyMailSendBcc)){
    			MailUtil.send(memberNotifyMail.notifyMailSendBcc,memberNotifyMail.subject, memberNotifyMail.mailBody);
    		}
    	}
    	
    	List<AdminNotifyMail> adminNotifyMailList = adminNotifyMailService.findByCondition(wehreMap);
    	for (AdminNotifyMail adminNotifyMail : adminNotifyMailList) {
    		
    		
    		if(StringUtil.isNotBlank(adminNotifyMail.notifyMailSendTo)){
    			String [] bccAdress = adminNotifyMail.notifyMailSendTo.split(",");
				for(int i=0;i<bccAdress.length;i++){
					MailUtil.send(bccAdress[i],adminNotifyMail.subject, adminNotifyMail.mailBody);
				}
			}
    		
    		if(StringUtil.isNotBlank(adminNotifyMail.notifyMailSendCc)){
    			String [] bccAdress = adminNotifyMail.notifyMailSendCc.split(",");
				for(int i=0;i<bccAdress.length;i++){
					MailUtil.send(bccAdress[i],adminNotifyMail.subject ,adminNotifyMail.mailBody);
				}
			}
		}
    	member.pointNum = upMember.pointNum; // ポイントを設定する
	}

    /**
     * 指定した団体の寄付ポイントの合計を取得する
     * @param organizationId 団体ID
     */
	public Integer findPointSumByOrganizationId(Integer organizationId){
		return jdbcManager.selectBySql(Integer.class, "select sum(point_num) from t_point_donation where organization_id = ? group by organization_id",organizationId).getSingleResult();
	}
}