package cokr.xit.fims.cmmn.hwp.format;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import cokr.xit.applib.Print;
import cokr.xit.applib.hwp.format.HWPFormat;
import cokr.xit.fims.cmmn.FimsPrintOption;
import cokr.xit.fims.mngt.OgdpUtil;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.DataFormat;
import cokr.xit.foundation.data.DataObject;

/**
 * 단속내역서
 */
public class CrdnList extends HWPFormat {

	public CrdnList(Print print, FimsPrintOption printOption, List<DataObject> dataObjectList) {
		super(print, printOption, dataObjectList);
		this.maxRunCount = 1;
		this.formatFilePath = "template/hwp/crdnList.hwp";
		this.print.setFormatKorName("단속내역서");
		this.print.setFormatName("crdnList");
	}

	@Override
	public List<String> getDownloadDataNames() {
		List<String> list = new ArrayList<>();
		list.add("과태료구분");
		list.add("시군구명");
		list.add("단속일시");
		list.add("단속장소");
		list.add("차량번호");
		list.add("대체차량번호");
		list.add("고지번호");
		list.add("부과금액");
		list.add("가상계좌번호");
		list.add("전자납부번호");
		list.add("처리상태");
		list.add("처리일자");
		list.add("납부기한");
		list.add("수납일자");
		return list;
	}



	@Override
	public void runAsWriter() {

		String globalRtpyrNm = data.get(0).string("RTPYR_NM");
		String globalRtpyrNo = data.get(0).string("RTPYR_NO");
		String globalRtpyrFullAddr = data.get(0).string("RTPYR_FULL_ADDR");
		int globalTotalAmount = data.stream().mapToInt(i -> i.number("LEVY_AMT").intValue()).sum();

		writer.setValue("출력일시", DataFormat.yyyy_mm_dd_hh_mm_ss(print.getPrintRequestDt()));
		writer.setValue("성명", globalRtpyrNm);
		if(print.getPrivateInfoYn(printOption)) {
			writer.setValue("주민번호", globalRtpyrNo);
		} else {
			writer.setValue("주민번호", "*************");
		}
		writer.setValue("주소", globalRtpyrFullAddr);
		writer.setValue("총건수", DataFormat.n_nn0(this.data.size()));
		writer.setValue("총금액", DataFormat.n_nn0(globalTotalAmount));

		String instNm = (String) print.getPrintRequestUserInfo().getInfo().get("instNm");
		instNm = OgdpUtil.getLastWord(instNm);
		writer.setValue("발신", instNm);

		List<DataObject> list = IntStream.range(0, this.data.size()).boxed().map(i -> {
			DataObject one = data.get(i);
			String sttsChgDt = one.string("CRDN_STTS_CHG_DT");
			if (!Assert.isEmpty(sttsChgDt))
				sttsChgDt = sttsChgDt.substring(0, 8);
			return (DataObject) new DataObject()
			.set("과태료", one.string("TASK_SE_NM"))
			.set("시군구명", one.string("SGG_NM"))
			.set("단속일시", DataFormat.yyyy_mm_dd_hh_mm_ss(one.string("CRDN_YMD_TM")))
			.set("단속장소", one.string("CRDN_PLC"))
			.set("차량번호", one.string("VHRNO"))
			.set("대체차량번호", one.string("RPM_SZR_VHRNO"))
			.set("고지번호", one.string("GOJI_NO"))
			.set("금액", DataFormat.n_nn0(one.string("LEVY_AMT")))
			.set("가상계좌번호", one.string("VR_ACTNO"))
			.set("전자납부번호", one.string("EPAYNO"))
			.set("처리상태", one.string("CRDN_STTS_NM"))
			.set("처리일자", DataFormat.yyyy_mm_dd(sttsChgDt))
			.set("납부기한", DataFormat.yyyy_mm_dd(one.string("DUDT_YMD")))
			.set("수납일자", DataFormat.yyyy_mm_dd(one.string("RCVMT_YMD")));
		}).toList();
		writer.table(2, 0, 3).setValues(list);


	}


}
