GiftExchangeService.java 7.87 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package jp.agentec.sinaburocast.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.seasar.extension.jdbc.AutoSelect;
import org.seasar.extension.jdbc.where.SimpleWhere;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;
import org.seasar.framework.util.StringUtil;

15 16 17 18 19
import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.entity.GiftExchange;
import jp.agentec.sinaburocast.form.admin.enquete.GiftExchangeSearchForm;

Kim Gyeongeun committed
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
@Component(instance=InstanceType.SINGLETON)
public class GiftExchangeService extends AbstractService<GiftExchange> {

	private final Logger logger = Logger.getLogger(getClass());
	
	private static final String ID_SEQ_NAME = "gift_exchange_id_seq";
	/**
	 * IDを発行して、登録する。
	 *
	 */
	public int insertGiftExchange(GiftExchange giftExchange, String insId) {
		giftExchange.giftExchangeId = getSeqNextVal(Integer.class, ID_SEQ_NAME);
		return super.insert(giftExchange, insId);
	}

	public GiftExchange findById(Integer giftExchangeId) {
		return select().id(giftExchangeId).getSingleResult();
	}


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

	public List<GiftExchange> findBymemberIdList(Integer memberId) {
		return select().where(new SimpleWhere().eq("memberId", memberId)).getResultList();
	}

	public List<GiftExchange> findBymemberId(Integer memberId) {
		return select()
				.where(new SimpleWhere().eq("memberId", memberId))
				.innerJoin("gift")
				.orderBy("applyDate")
				.getResultList();
	}

	public Integer getSumRequiredPointByMemberId(Integer memberId){
		List<GiftExchange> giftExchangeList = findBymemberId(memberId);
		int sumRequiredPoint = 0;
		
		for(GiftExchange giftExchange : giftExchangeList){
			sumRequiredPoint += giftExchange.gift.requiredPoint;
		}

		return sumRequiredPoint;
	}

	/**
	 * ポイント交換申請情報取得(画面用)
	 * @param giftExchangeSearchForm
	 * @return ArrayList<GiftExchange>
	 */
	public ArrayList<GiftExchange> findAllByPaging(GiftExchangeSearchForm giftExchangeSearchForm){

		//件数取得
		AutoSelect<GiftExchange> counter = this.getAutoSelectForFindAllByPaging(giftExchangeSearchForm);
		giftExchangeSearchForm.setPaging(null, giftExchangeSearchForm.pageNo, counter.getCount());

		//結果取得
		return (ArrayList<GiftExchange>)this.getAutoSelectForFindAllByPaging(giftExchangeSearchForm).orderBy("applyDate,memberId")
				.offset(giftExchangeSearchForm.getOffSet(""))
				.limit(giftExchangeSearchForm.getLimit(""))
				.getResultList();
	}

	/**
	 * ポイント交換申請情報を検索するための、AutoSelectを返す。
	 * @param giftExchangeSearchForm
	 * @return AutoSelect<GiftExchange>
	 */
	private AutoSelect<GiftExchange> getAutoSelectForFindAllByPaging(GiftExchangeSearchForm giftExchangeSearchForm){

		Date fromDay = null;
		Date toDay = null;

		if (!StringUtil.isBlank(giftExchangeSearchForm.fromDay)){
			//fromDay =SinaburoUtil.stringToDateSlash(giftExchangeSearchForm.fromDay);
			fromDay=SinaburoUtil.convertStringToTimestamp(giftExchangeSearchForm.fromDay,"yyyy-MM-dd");
		}
		if (!StringUtil.isBlank(giftExchangeSearchForm.toDay)){
			//toDay =SinaburoUtil.getAddDate(SinaburoUtil.stringToDateSlash(giftExchangeSearchForm.toDay),1);
			toDay=SinaburoUtil.convertStringToTimestamp(SinaburoUtil.getNextDay(giftExchangeSearchForm.toDay),"yyyy-MM-dd");
		}
Kim Gyeongeun committed
103 104 105 106 107 108 109 110 111
		SimpleWhere where = new SimpleWhere().ge("applyDate", fromDay)
				.lt("applyDate", toDay)
				.eq("member.validFlg", SinaburoConstant.MemberValidFlg.VALID)
				.eq("member.delFlg", SinaburoConstant.MemberDelFlg.NOT_DEL);
		// 区分(エコボ:1、ICHICO:2)
		if (giftExchangeSearchForm.giftType != null) {
			where.eq("gift.giftType", giftExchangeSearchForm.giftType);
		}
		return select().innerJoin("member").innerJoin("member.prefecture").innerJoin("gift").where(where);
Kim Gyeongeun committed
112 113 114 115 116 117 118 119 120 121
	}

	/**
	 * CSVファイル出力ポイント交換申請情報取得
	 * @param giftExchangeSearchForm
	 * @return String ポイント交換申請情報文字列
	 */
	public String getGiftExchangeForCsv(GiftExchangeSearchForm giftExchangeSearchForm) throws UnsupportedEncodingException, Exception{

		StringBuffer str = new StringBuffer();
122
		str.append("\"申請日\",\"会員ID\",\"氏名\",\"E-mail(メイン)\",\"申請(枚数/ポイント数)\",\"郵便番号\",\"都道府県\",\"市区町村\",\"町名・番地\",\"建物名\",\"会員番号\",\"電話番号\""+"\r\n");
Kim Gyeongeun committed
123 124 125 126 127 128

		List<GiftExchange> giftExchangeList = getGiftExchangeByFromDayAndToDay(giftExchangeSearchForm);

    	for (GiftExchange giftExchange : giftExchangeList) {

    		//申請日
129
    		str.append("\"" + SinaburoUtil.dateToString2(giftExchange.applyDate) + "\",");
Kim Gyeongeun committed
130 131

    		//会員ID
132
    		str.append("\"" + giftExchange.memberId +"\",");
Kim Gyeongeun committed
133 134

    		//氏名
135
    		str.append("\"" + giftExchange.member.firstName + " " + giftExchange.member.lastName + "\",");
Kim Gyeongeun committed
136 137

    		//メールアドレス
138
    		str.append("\"" + replaceNullWithBlank(giftExchange.member.mainPcEmail) +"\",");
Kim Gyeongeun committed
139 140

    		//申請枚数
141 142 143 144 145 146 147
    		String unit = null;
    		if(SinaburoConstant.giftType.ECOBO.equals(giftExchange.gift.giftType)) {
    			unit = "枚";
    		} else {
    			unit = "ポイント";
    		}
    		str.append("\"" + giftExchange.cnt + unit + "\",");
Kim Gyeongeun committed
148 149

    		//郵便番号
150
    		str.append("\"" + giftExchange.member.zipCode + "\",");
Kim Gyeongeun committed
151 152

    		//都道府県
153
    		str.append("\"" + giftExchange.member.prefecture.prefecture + "\",");
Kim Gyeongeun committed
154 155

    		//市区町村
156
    		str.append("\"" + giftExchange.member.cityName + "\",");
Kim Gyeongeun committed
157 158

    		//町名・番地
159
    		str.append("\"" + giftExchange.member.areaName + "\",");
Kim Gyeongeun committed
160 161

    		//建物名
162
    		str.append("\"" + replaceNullWithBlank(giftExchange.member.buildingName) + "\",");
163

164
    		//会員番号
165 166 167 168
    		str.append("\"" + replaceNullWithBlank(giftExchange.member.memberNum) + "\",");
    		
    		//電話番号
    		str.append("\"" + replaceNullWithBlank(giftExchange.member.telno) + "\"");
Kim Gyeongeun committed
169 170 171 172 173 174 175

    		str.append("\r\n");

    	}
    	return str.toString();

	}
176 177 178 179 180 181 182 183 184 185
	
	/**
	 * null値を空白に置き換え
	 */
	private String replaceNullWithBlank(String str) {
		if(str != null) {
			return str;
		}
		return "";
	}
Kim Gyeongeun committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	/**
	 * ポイント交換申請情報取得(CSV出力用)
	 * @param giftExchangeSearchForm
	 * @return List<GiftExchange>
	 */
	private List<GiftExchange> getGiftExchangeByFromDayAndToDay(GiftExchangeSearchForm giftExchangeSearchForm){

		Date fromDay = null;
		Date toDay = null;

		//#30951 「ポイント交換申請結果CSV出力」でエラー画面 対応
		if (!StringUtil.isBlank(giftExchangeSearchForm.fromDay)){
			logger.info("giftExchangeSearchForm.fromDay:" + giftExchangeSearchForm.fromDay);
			//fromDay =SinaburoUtil.stringToDateSlash(giftExchangeSearchForm.fromDay);
			fromDay = SinaburoUtil.convertStringToTimestamp(giftExchangeSearchForm.fromDay,"yyyy-MM-dd");
		}
		if (!StringUtil.isBlank(giftExchangeSearchForm.toDay)){
			logger.info("giftExchangeSearchForm.toDay:" + giftExchangeSearchForm.toDay);
			//toDay =SinaburoUtil.getAddDate(SinaburoUtil.stringToDateSlash(giftExchangeSearchForm.toDay),1);
			toDay = SinaburoUtil.convertStringToTimestamp(SinaburoUtil.getNextDay(giftExchangeSearchForm.toDay),"yyyy-MM-dd");
		}
Kim Gyeongeun committed
208 209 210 211 212 213 214 215 216
		
		SimpleWhere where = new SimpleWhere().ge("applyDate", fromDay)
				.lt("applyDate", toDay)
				.eq("member.validFlg", SinaburoConstant.MemberValidFlg.VALID)
				.eq("member.delFlg", SinaburoConstant.MemberDelFlg.NOT_DEL);
		// 区分(エコボ:1、ICHICO:2)
		if (giftExchangeSearchForm.giftType != null) {
			where.eq("gift.giftType", giftExchangeSearchForm.giftType);
		}
Kim Gyeongeun committed
217

Kim Gyeongeun committed
218
		return select().innerJoin("member").innerJoin("member.prefecture").innerJoin("gift").where(where)
Kim Gyeongeun committed
219 220 221 222 223 224
		.orderBy("applyDate,memberId")
		.getResultList();
	}


}