MemberMigrationInfoRegistAction.java 11.9 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
/**
 *
 */
package jp.agentec.sinaburocast.action.batch;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.agentec.sinaburocast.action.AbstractAction;
import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.common.util.PropertyUtil;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.csv.MemberMigrationInfoCsv;
import jp.agentec.sinaburocast.csv.PointMigrationInfoCsv;
import jp.agentec.sinaburocast.entity.Member;
import jp.agentec.sinaburocast.entity.PointGet;
import jp.agentec.sinaburocast.form.batch.MemberMigrationInfoRegistForm;
import jp.agentec.sinaburocast.service.MemberService;
import jp.agentec.sinaburocast.service.PointGetService;
import jp.agentec.sinaburocast.service.csv.CsvService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.velocity.tools.generic.DateTool;
import org.seasar.framework.beans.util.BeanMap;
import org.seasar.framework.beans.util.Beans;
import org.seasar.framework.util.FileUtil;
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

/**
 * @author choi-c
 *
 * 会員情報一括登録[マイグレーション用]
 *
 */
public class MemberMigrationInfoRegistAction extends AbstractAction {

	private final Logger logger = Logger.getLogger(getClass());
	
	@ActionForm
	public 	MemberMigrationInfoRegistForm memberMigrationInfoRegistForm;
	
	public CsvService csvService;//<MemberMigrationInfoCsv> <PointMigrationInfoCsv>
	
	public MemberService memberService;
/**
	 * 会員一括登録
	 */
	//@Execute(validator = true, input="/admin/member/memberRegistAll/")
	@Execute(validator = false)
	//@OperationHistoryAno(operationTypeId=SinaburoConstant.OperationType.UPLOAD,target="会員一括登録",detail="アップロード",screenId=SinaburoConstant.screenId.CMM0006)
	public String memberUpload() throws Exception {
		if(StringUtil.isBlank(memberMigrationInfoRegistForm.fileFullPath) 
				){
			logger.fatal("ファイルパスを指定してください。");
			return null;
		}
		File file = new File(memberMigrationInfoRegistForm.fileFullPath);
		if(!file.exists()){
			logger.fatal("ファイルパスが不正です。ファイルパス:"+memberMigrationInfoRegistForm.fileFullPath);
			return null;
		} 

		int result = 0;
		String csvData = null;
		InputStream in = null;

		
		
		try {
			in = new FileInputStream(file);
			//バイナリ形式チェック
            byte[] b = new byte[1];
            while (in.read(b, 0, 1) > 0) {
                if (b[0] == 0) {
                	logger.fatal("バイナリ形式のファイルなので処理を完了します。");
        			return null;
                }
            }
		} finally {
			IOUtils.closeQuietly(in);
		}

		try {
			in = new FileInputStream(file);
			//20170508 SJIS文字化け対応
			csvData = IOUtils.toString(in, "Windows-31J");
		} finally {
			IOUtils.closeQuietly(in);
		}

		StringBuffer isOk   = new StringBuffer("OK");
		//List<MemberInfoCsv> memberCsvList = csvService.parseAll(csvData, MemberInfoCsv.class,isOk);
		List<MemberMigrationInfoCsv> memberCsvList = null;

		try {
			memberCsvList = csvService.parseAll(csvData, MemberMigrationInfoCsv.class,isOk);
		} catch (org.seasar.s2csv.csv.exception.CSVFormatException e) {
			logger.fatal(PropertyUtil.getString("ファイルが不正です。"));
			return null;
		}catch(NumberFormatException e){
			logger.fatal("ファイルが不正です。");
			
			return null;
		}

		if (CollectionUtils.isEmpty(memberCsvList)) {
			logger.fatal("登録対象のデータが存在しないので登録できません。");
			return null;
		}
		String insertUser=PropertyUtil.getString("BATCH_UPDATE");
		for (MemberMigrationInfoCsv memberMigrationInfoCsv : memberCsvList) {
			memberMigrationInfoCsv.insertResult="OK";
			if(StringUtil.isBlank(memberMigrationInfoCsv.pointNum))memberMigrationInfoCsv.pointNum="0";
			Member member = Beans.createAndCopy(Member.class, memberMigrationInfoCsv).execute();
			
			
			try {
				memberService.insert(member,insertUser);
			} catch (Exception e) {
				memberMigrationInfoCsv.insertResult="NG";
			}
			
			//result = memberService.insert(null);
		}
		
		/**結果ファイル作成*/
		String dateStr = new DateTool().format("yyyyMMdd_HHmmss",DateTool.getSystemDate());
		//20170508 SJIS文字化け対応
		String outStr = SinaburoUtil.addDoubleStr(csvService.getContents(memberCsvList, MemberMigrationInfoCsv.class).replace("\"", ""));
		outStr = SinaburoUtil.toSJIS(outStr);
		FileUtil.write(memberMigrationInfoRegistForm.fileFullPath+"_"+dateStr, outStr.getBytes("Windows-31J"));
		//FileUtil.write(memberMigrationInfoRegistForm.fileFullPath+"_"+dateStr, SinaburoUtil.addDoubleStr(csvService.getContents(memberCsvList, MemberMigrationInfoCsv.class).replace("\"", "")).getBytes("Shift_JIS"));
		/**一括登録*/
		//result = memberService.memberAllRegist(memberCsvList, getAdmin());
		logger.info("一括更新件数:"+memberCsvList.size());

		return null;

	}
	
	public PointGetService pointGetService;
	
	@Execute(validator = false)
	//@OperationHistoryAno(operationTypeId=SinaburoConstant.OperationType.UPLOAD,target="会員一括登録",detail="アップロード",screenId=SinaburoConstant.screenId.CMM0006)
	public String pointUpload() throws Exception {
		
		if(StringUtil.isBlank(memberMigrationInfoRegistForm.fileFullPath) 
				){
			logger.fatal("ファイルパスを指定してください。");
			return null;
		}
		File file = new File(memberMigrationInfoRegistForm.fileFullPath);
		if(!file.exists()){
			logger.fatal("ファイルパスが不正です。ファイルパス:"+memberMigrationInfoRegistForm.fileFullPath);
			return null;
		} 

		int result = 0;
		String csvData = null;
		InputStream in = null;

		
		
		try {
			in = new FileInputStream(file);
			//バイナリ形式チェック
            byte[] b = new byte[1];
            while (in.read(b, 0, 1) > 0) {
                if (b[0] == 0) {
                	logger.fatal("バイナリ形式のファイルなので処理を完了します。");
        			return null;
                }
            }
		} finally {
			IOUtils.closeQuietly(in);
		}

		try {
			in = new FileInputStream(file);
			//20170508 SJIS文字化け対応
			csvData = IOUtils.toString(in, "Windows-31J");
			//csvData = IOUtils.toString(in, "Shift_JIS");
		} finally {
			IOUtils.closeQuietly(in);
		}

		StringBuffer isOk   = new StringBuffer("OK");
		//List<MemberInfoCsv> memberCsvList = csvService.parseAll(csvData, MemberInfoCsv.class,isOk);
		List<PointMigrationInfoCsv> pointCsvList = null;

		try {
			pointCsvList = csvService.parseAll(csvData, PointMigrationInfoCsv.class,isOk);
		} catch (org.seasar.s2csv.csv.exception.CSVFormatException e) {
			logger.fatal(PropertyUtil.getString("ファイルが不正です。"));
			return null;
		}catch(NumberFormatException e){
			logger.fatal("ファイルが不正です。");
			
			return null;
		}

		if (CollectionUtils.isEmpty(pointCsvList)) {
			logger.fatal("登録対象のデータが存在しないので登録できません。");
			return null;
		}
		
		String insertUser=PropertyUtil.getString("BATCH_UPDATE");
		ArrayList<Integer> memberIdList = new ArrayList<Integer>(); 
		for (PointMigrationInfoCsv pointMigrationInfoCsv : pointCsvList) {
			pointMigrationInfoCsv.pointGetDate=setvalidatePointDate(pointMigrationInfoCsv.pointGetDateInput);
			
			
			PointGet pointGet = Beans.createAndCopy(PointGet.class, pointMigrationInfoCsv).execute();
			if(!memberIdList.contains(pointMigrationInfoCsv.memberId)){
				memberIdList.add(pointGet.memberId);
			}
			
			if(StringUtil.equals(pointMigrationInfoCsv.okNg, "OK")||pointMigrationInfoCsv.okNg == null){
				pointMigrationInfoCsv.insertResult="OK";
			} else{
				pointMigrationInfoCsv.insertResult="NG";//NGの場合登録しない。
				continue;
			}
			
			//マイナスは登録しない
			if(pointGet.pointGetNum<0){
				pointMigrationInfoCsv.insertResult="NG";//NGの場合登録しない。
				continue;
			}
			
			try {
				pointGetService.insertPointGet(pointGet,insertUser);
			} catch (Exception e) {
				pointMigrationInfoCsv.insertResult="NG";
			}
			
			
		}
		
		
		//PointGetが多い場合MEMBERのPOINT_NUMにあわせる。
		for (Integer fmemberId : memberIdList) {
			BeanMap beanMap =pointGetService.getDefaultPoint(fmemberId);
			if(beanMap != null && StringUtil.equals("P", beanMap.get("mp").toString()) ){
				int herasPoint=(int)((Long)beanMap.get("herasupoint")).doubleValue();//&& beanMap.get()  m.MEMBER_ID ,M.POINT_NUM ,P.zan ppoint, (CASE WHEN M.POINT_NUM > P.ZAN THEN 'M' WHEN M.POINT_NUM < P.ZAN THEN 'P' END) MP
				pointGetService.cutDownPointLogicForMaigration( herasPoint, fmemberId,insertUser);
			
			}
		}
		
		//会員テーブルのポイントが大きいリスト出力
		List<BeanMap> memberLargeList = pointGetService.memberPointLarge();
		
		StringBuilder sb = new StringBuilder();
		
		sb.append(SinaburoConstant.newLine.RN);
		sb.append(SinaburoConstant.newLine.RN);
		sb.append(SinaburoConstant.newLine.RN);
		sb.append("・会員テーブルのポイントが大きいリスト。");
		sb.append(SinaburoConstant.newLine.RN);
		sb.append("会員ID\tポイントPOINT_GET\tポイントMember\tPOINT_GET/Member");
		sb.append(SinaburoConstant.newLine.RN);
		
		if(!CollectionUtils.isEmpty(memberLargeList)){
		
			for (BeanMap memberLargeMap : memberLargeList) {
				sb.append(memberLargeMap.get("memberId")+"\t"+memberLargeMap.get("ppoint").toString()+"\t"+memberLargeMap.get("pointNum").toString()+"\t"+memberLargeMap.get("mp").toString());
				sb.append(SinaburoConstant.newLine.RN);
			}
		}
		/**結果ファイル作成*/
		String dateStr = new DateTool().format("yyyyMMdd_HHmmss",DateTool.getSystemDate());
		
		//20170508 SJIS文字化け対応
		String outStr = SinaburoUtil.addDoubleStr(csvService.getContents(pointCsvList, PointMigrationInfoCsv.class).replace("\"", "")+sb.toString());
		outStr = SinaburoUtil.toSJIS(outStr);
		FileUtil.write(memberMigrationInfoRegistForm.fileFullPath+"_"+dateStr, outStr.getBytes("Windows-31J"));
		//FileUtil.write(memberMigrationInfoRegistForm.fileFullPath+"_"+dateStr, SinaburoUtil.addDoubleStr(csvService.getContents(pointCsvList, PointMigrationInfoCsv.class).replace("\"", "")+sb.toString()).getBytes("Shift_JIS"));
		
		/**一括登録*/
		//result = memberService.memberAllRegist(memberCsvList, getAdmin());
		logger.info("一括更新件数:"+pointCsvList.size());

		return null;

	}
	
	public String setvalidatePointDate(String args) {
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		if(StringUtil.isBlank(args))
			return "";
		format.setLenient(false);
		
		try {
			date=format.parse(args);
		} catch (ParseException e) {
			return "";
		}
		format = new SimpleDateFormat("yyyyMMdd");
		return format.format(date);
	}

	
	
}

///**
// *
// */
//package jp.agentec.sinaburocast.action.batch;
//
//import jp.agentec.sinaburocast.action.AbstractAction;
//import jp.agentec.sinaburocast.common.util.PropertyUtil;
//import jp.agentec.sinaburocast.service.EnqueteService;
//
//import org.apache.log4j.Logger;
//import org.seasar.struts.annotation.Execute;
//
///**
// * @author arakawa
// *
// */
//public class MemberMigrationInfoRegist extends AbstractAction {
//	private final Logger logger = Logger.getLogger(getClass());
//
//	public EnqueteService enqueteService;
//
//	@Execute(validator=false)
//	public String index() throws Exception {
//
//		//アンケートステータスを未実施から実施中に変更する
//		enqueteService.updateStatusToEnforcement(PropertyUtil.getString("BATCH_UPDATE"));
//
//		//アンケートステータスを実施中から完了に変更する
//		enqueteService.updateStatusToComplete(PropertyUtil.getString("BATCH_UPDATE"));
//
//		return null;
//	}
//
//}