/**
 *
 */
package org.springblade.finance.service;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springblade.finance.entity.DateRangeAccount;
import org.springblade.finance.entity.IntCwDetailView;
import org.springblade.finance.entity.YksytAccountView;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author wookvn
 *
 *	导出PDF
 */
@Component
public class PdfExportService {

	/**
	 * 导出第一次结算金额 - PDF文件
	 * @param dateRangeAccount
	 * @return
	 * @throws Exception
	 */
	public void createOnePdf(DateRangeAccount dateRangeAccount, ByteArrayOutputStream baos) throws Exception {
		Document document = new Document();
		PdfWriter.getInstance(document, baos);
		document.open();

		String theMonthStr = dateRangeAccount.getTheMonth().toString();
		String year = theMonthStr.substring(0, 4), month = theMonthStr.substring(4);

		// 使用语言包字体
		BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 外部字体
		ClassPathResource cpr = new ClassPathResource("/fonts/simfang.ttf");
		BaseFont bf = BaseFont.createFont(cpr.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font titleFont = new Font(bf, 12, Font.NORMAL);

		PdfPTable table = new PdfPTable(2);

		PdfPCell cell = new PdfPCell();
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(2);
		cell.setPaddingBottom(10f);
		Paragraph paragraph = new Paragraph(String.format("《联通在线渠道推广%s%s月预结金额确认单》", year, month), new Font(abf, 13, Font.BOLD));
		paragraph.setAlignment(Paragraph.ALIGN_CENTER);
		cell.addElement(paragraph);
		table.addCell(cell);

		cell = new PdfPCell();
		cell.setColspan(2);
		cell.setPadding(10f);
		Paragraph para = new Paragraph(
				"1.甲方为推广其产品或服务，同意按照本协议的规定在乙方渠道推广其产品或服务，按合作结算渠道推广费用。\n" +
				"2.本统计表自双方签署盖章之日起生效。\n" +
				"3.本统计表一式贰份，双方各执壹份。\n" +
				"4.请乙方收到此单据后  3  日予以确认。", titleFont);
		para.setSpacingBefore(10f);
		cell.addElement(para);
		table.addCell(cell);


		cell = new PdfPCell(new Phrase("甲方：联通在线信息科技有限公司", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("乙方：" + dateRangeAccount.getCompany(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("统计周期", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(String.format("%s 年 %s 月", year, month), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("产品名称", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getSpname(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("平台有效用户数", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getOnPlatform() + "  户", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("T+1 预结用户数", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getOnDateRange() + "  户", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		/*cell = new PdfPCell(new Phrase("400扣罚", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getMoneyOneDiscount() + "  元", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);*/

		cell = new PdfPCell(new Phrase("T+1 预结金额", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getMoneyOneReal() + "  元", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("乙方确认意见", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("  ", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);


		// 配置行间距
		cell.addElement(new Paragraph(24,
				"乙方： \n" +
				"\n" +
				"盖章：\n" +
				"\n" +
				"日期：",
				titleFont));
		cell.setColspan(2);
		cell.setPadding(10f);
		table.addCell(cell);
		document.add(table);
		document.close();
	}

	/**
	 * 导出第二次结算金额 - PDF文件
	 * @param dateRangeAccount
	 * @return
	 */
	public void createTwoPdf(DateRangeAccount dateRangeAccount, ByteArrayOutputStream baos) throws Exception {
		Document document = new Document();
		PdfWriter.getInstance(document, baos);
		document.open();

		String theMonthStr = dateRangeAccount.getTheMonth().toString();
		String year = theMonthStr.substring(0, 4), month = theMonthStr.substring(4);

		// 使用语言包字体
		BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 外部字体
		ClassPathResource cpr = new ClassPathResource("/fonts/simfang.ttf");
		BaseFont bf = BaseFont.createFont(cpr.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font titleFont = new Font(bf, 12, Font.NORMAL);

		PdfPTable table = new PdfPTable(2);

		PdfPCell cell = new PdfPCell();
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(2);
		cell.setPaddingBottom(10f);
		Paragraph paragraph = new Paragraph(String.format("《联通在线渠道推广%s%s月清算金额确认单》", year, month), new Font(abf, 13, Font.BOLD));
		paragraph.setAlignment(Paragraph.ALIGN_CENTER);
		cell.addElement(paragraph);
		table.addCell(cell);

		cell = new PdfPCell();
		cell.setColspan(2);
		cell.setPadding(10f);
		Paragraph para = new Paragraph(
				"1.甲方为推广其产品或服务，同意按照本协议的规定在乙方渠道推广其产品或服务，按合作结算渠道推广费用。\n" +
				"2.本统计表自双方签署盖章之日起生效。\n" +
				"3.本统计表一式贰份，双方各执壹份。\n" +
				"4.请乙方收到此单据后  3  日予以确认。", titleFont);
		para.setSpacingBefore(10f);
		cell.addElement(para);
		table.addCell(cell);


		cell = new PdfPCell(new Phrase("甲方：联通在线信息科技有限公司", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("乙方：" + dateRangeAccount.getCompany(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("统计周期", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(String.format("%s 年 %s 月", year, month), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("产品名称", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getSpname(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("PRM有效用户数", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getAccountUsers() + "  户", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);


		BigDecimal totalPrice = dateRangeAccount.getMoneyTwoDiscount1()
				.add(dateRangeAccount.getMoneyTwoDiscount2()).subtract(dateRangeAccount.getMoneyTwoDiscount3())
				.add(dateRangeAccount.getMoneyTwoDiscount4());
		cell = new PdfPCell(new Phrase("扣款合计", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(totalPrice + "  元", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("PRM有效用户清算金额", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getMoneyTwoReal() + "  元", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("乙方确认意见", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("  ", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);


		// 配置行间距
		cell.addElement(new Paragraph(24,
				"乙方： \n" +
				"\n" +
				"盖章：\n" +
				"\n" +
				"日期：",
				titleFont));
		cell.setColspan(2);
		cell.setPadding(10f);
		table.addCell(cell);
		document.add(table);
		document.close();
	}



	/**
	 * 导出互联网1第一次结算金额 - PDF文件
	 * @param dateRangeAccount
	 * @return
	 * @throws Exception
	 */
	public void createH1OnePdf(DateRangeAccount dateRangeAccount, ByteArrayOutputStream baos) throws Exception {
		Document document = new Document();
		PdfWriter.getInstance(document, baos);
		document.open();

		String theMonthStr = dateRangeAccount.getTheMonth().toString();
		String year = theMonthStr.substring(0, 4), month = theMonthStr.substring(4);

		// 使用语言包字体
		BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 外部字体
		ClassPathResource cpr = new ClassPathResource("/fonts/simfang.ttf");
		BaseFont bf = BaseFont.createFont(cpr.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font titleFont = new Font(bf, 12, Font.NORMAL);

		PdfPTable table = new PdfPTable(2);

		PdfPCell cell = new PdfPCell();
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(2);
		cell.setPaddingBottom(10f);
		Paragraph paragraph = new Paragraph(String.format("《联通在线渠道推广%s%s月预结金额确认单》", year, month), new Font(abf, 13, Font.BOLD));
		paragraph.setAlignment(Paragraph.ALIGN_CENTER);
		cell.addElement(paragraph);
		table.addCell(cell);

		cell = new PdfPCell();
		cell.setColspan(2);
		cell.setPadding(10f);
		Paragraph para = new Paragraph(
				"1.甲方为推广其产品或服务，同意按照本协议的规定在乙方渠道推广其产品或服务，按合作结算渠道推广费用。\n" +
						"2.本统计表自双方签署盖章之日起生效。\n" +
						"3.本统计表一式贰份，双方各执壹份。\n" +
						"4.请乙方收到此单据后  3  日予以确认。", titleFont);
		para.setSpacingBefore(10f);
		cell.addElement(para);
		table.addCell(cell);


		cell = new PdfPCell(new Phrase("甲方：联通在线信息科技有限公司", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("乙方：" + dateRangeAccount.getCompany(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("统计周期", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(String.format("%s 年 %s 月", year, month), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("产品名称", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getSpname(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("平台有效用户数", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getOnPlatform() + "  户", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("T+1 预结用户数", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getOnDateRange() + "  户", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("T+1 预结金额", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getMoneyOneReal() + "  元", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("乙方确认意见", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("  ", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);


		// 配置行间距
		cell.addElement(new Paragraph(24,
				"乙方： \n" +
						"\n" +
						"盖章：\n" +
						"\n" +
						"日期：",
				titleFont));
		cell.setColspan(2);
		cell.setPadding(10f);
		table.addCell(cell);
		document.add(table);
		document.close();
	}

	/**
	 * 导出互联网1第二次结算金额 - PDF文件
	 * @param dateRangeAccount
	 * @return
	 */
	public void createH1TwoPdf(DateRangeAccount dateRangeAccount, ByteArrayOutputStream baos) throws Exception {
		Document document = new Document();
		PdfWriter.getInstance(document, baos);
		document.open();

		String theMonthStr = dateRangeAccount.getTheMonth().toString();
		String year = theMonthStr.substring(0, 4), month = theMonthStr.substring(4);

		// 使用语言包字体
		BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 外部字体
		ClassPathResource cpr = new ClassPathResource("/fonts/simfang.ttf");
		BaseFont bf = BaseFont.createFont(cpr.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font titleFont = new Font(bf, 12, Font.NORMAL);

		PdfPTable table = new PdfPTable(2);

		PdfPCell cell = new PdfPCell();
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(2);
		cell.setPaddingBottom(10f);
		Paragraph paragraph = new Paragraph(String.format("《联通在线渠道推广%s%s月清算金额确认单》", year, month), new Font(abf, 13, Font.BOLD));
		paragraph.setAlignment(Paragraph.ALIGN_CENTER);
		cell.addElement(paragraph);
		table.addCell(cell);

		cell = new PdfPCell();
		cell.setColspan(2);
		cell.setPadding(10f);
		Paragraph para = new Paragraph(
				"1.甲方为推广其产品或服务，同意按照本协议的规定在乙方渠道推广其产品或服务，按合作结算渠道推广费用。\n" +
						"2.本统计表自双方签署盖章之日起生效。\n" +
						"3.本统计表一式贰份，双方各执壹份。\n" +
						"4.请乙方收到此单据后  3  日予以确认。", titleFont);
		para.setSpacingBefore(10f);
		cell.addElement(para);
		table.addCell(cell);


		cell = new PdfPCell(new Phrase("甲方：联通在线信息科技有限公司", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("乙方：" + dateRangeAccount.getCompany(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("统计周期", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(String.format("%s 年 %s 月", year, month), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("产品名称", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(dateRangeAccount.getSpname(), titleFont));
		cell.setPadding(10f);
		table.addCell(cell);

		if(dateRangeAccount.getChannelName().equals("腾讯收银台")){
			cell = new PdfPCell(new Phrase("平台有效用户数", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(dateRangeAccount.getOnDateRange() + "  户", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);

			cell = new PdfPCell(new Phrase("平台有效用户清算金额", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(dateRangeAccount.getMoneyTwoReal() + "  元", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);
		}else {
			cell = new PdfPCell(new Phrase("PRM有效用户数", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(dateRangeAccount.getAccountUsers() + "  户", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);

			cell = new PdfPCell(new Phrase("PRM有效用户清算金额", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);
			cell = new PdfPCell(new Phrase(dateRangeAccount.getMoneyTwoReal() + "  元", titleFont));
			cell.setPadding(10f);
			table.addCell(cell);
		}

		cell = new PdfPCell(new Phrase("乙方确认意见", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("  ", titleFont));
		cell.setPadding(10f);
		table.addCell(cell);


		// 配置行间距
		cell.addElement(new Paragraph(24,
				"乙方： \n" +
						"\n" +
						"盖章：\n" +
						"\n" +
						"日期：",
				titleFont));
		cell.setColspan(2);
		cell.setPadding(10f);
		table.addCell(cell);
		document.add(table);
		document.close();
	}




	/**
	 * 社会渠道 结算明细导出PDF
	 * @param list
	 * @param baos
	 * @throws Exception
	 * ByteArrayOutputStream
	 */
	public void createReadyPdf(List<IntCwDetailView> list, ByteArrayOutputStream baos) throws Exception {
		Document document = new Document();
		Rectangle pageSize = new Rectangle(PageSize.A4.getHeight(), PageSize.A4.getWidth());
        pageSize.rotate();
        document.setPageSize(pageSize);
		PdfWriter.getInstance(document, baos);
		document.open();

		// 使用语言包字体
		BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 外部字体
		ClassPathResource cpr = new ClassPathResource("/fonts/simfang.ttf");
		BaseFont bf = BaseFont.createFont(cpr.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font titleFont = new Font(bf, 12, Font.NORMAL);

		//设置没列的宽度
		float[] widths = new float[] {20f, 60f, 70f, 35f, 40f, 50f, 50f, 40f, 45f, 45f, 39f, 50f, 39f};
		PdfPTable table = new PdfPTable(13);
		double totalWidth = IntStream.range(0, widths.length).mapToDouble(i -> Double.valueOf(widths[i])).sum();
		table.setTotalWidth(Double.valueOf(totalWidth).floatValue() + 200f); // + 250f
		table.setLockedWidth(true);
		table.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.setWidths(widths);

		PdfPCell cell = new PdfPCell();
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(13);
		cell.setPaddingBottom(10f);
		cell.setBorder(0);
		Paragraph paragraph = new Paragraph("畅视事业部   渠道结算明细表", new Font(abf, 13, Font.BOLD));
		paragraph.setAlignment(Paragraph.ALIGN_CENTER);
		cell.addElement(paragraph);
		table.addCell(cell);

		String[] tableFieldList = new String[] {"序号", "合作伙伴名称", "结算类型（T+1预结/T+5清算）", "账期", "产品名称", "预结有效用户数",
				"PRM有效用户数", "400扣款金额", "客服退赔付费", "省份违约扣款", "调账金额", "不规范定制处罚费", "结算金额"};

		for(String tableField : tableFieldList) {
			cell = new PdfPCell(new Phrase(tableField, titleFont));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setVerticalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
		}

		for(int i = 0; i < list.size(); i++) {
			IntCwDetailView data = list.get(i);
			cell = new PdfPCell(new Phrase(String.valueOf(i+1), titleFont));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setVerticalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getCompany(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getDealTypeneme(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getThemonth().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getSpname(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getVuserOndaterange().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getAccountUsers().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoney1Discount().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoney2Discount1().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoney2Discount2().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoney2Discount3().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoney2Discount4().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoneyReal().toString(), titleFont));
			table.addCell(cell);
		}

		//统计总数
		cell = new PdfPCell(new Phrase("总计：", titleFont));
		cell.setColspan(5);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getVuserOndaterange).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getAccountUsers).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getMoney1Discount).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getMoney2Discount1).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getMoney2Discount2).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getMoney2Discount3).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getMoney2Discount4).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);
	  /*BigDecimal totalRealMoney = list.stream().map(f -> {
			if("T+1预结".equals(f.getReadyTypeStr())) {
				return f.getMoneyOneReal();
			}else {
				return f.getMoneyTwoReal();
			}
		}).reduce(BigDecimal.ZERO, BigDecimal::add);*/
		cell = new PdfPCell(new Phrase(list.stream().map(IntCwDetailView::getMoneyReal).reduce(BigDecimal.ZERO, BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("经办人确认：", titleFont));
		cell.setColspan(13);
		cell.setBorder(0);
		cell.setPaddingTop(50);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("业务组确认：", titleFont));
		cell.setColspan(13);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("数字化运营中心确认：", titleFont));
		cell.setColspan(13);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("质量管控中心确认：", titleFont));
		cell.setColspan(13);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("财务支撑中心确认：", titleFont));
		cell.setColspan(13);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("部门负责人确认：", titleFont));
		cell.setColspan(13);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		document.add(table);
		document.close();
	}


	/**
	 * 互联网2 结算明细导出PDF
	 * @param list
	 * @param baos
	 * @throws Exception
	 */
	public void createYksytReadyPdf(List<YksytAccountView> list, ByteArrayOutputStream baos) throws Exception {
		Document document = new Document();
		Rectangle pageSize = new Rectangle(PageSize.A4.getHeight(), PageSize.A4.getWidth());
		pageSize.rotate();
		document.setPageSize(pageSize);
		PdfWriter.getInstance(document, baos);
		document.open();

		// 使用语言包字体
		BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 外部字体
		ClassPathResource cpr = new ClassPathResource("/fonts/simfang.ttf");
		BaseFont bf = BaseFont.createFont(cpr.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font titleFont = new Font(bf, 12, Font.NORMAL);

		//设置每列的宽度
		float[] widths = new float[] {20f, 60f, 70f, 35f, 40f, 50f, 50f, 40f, 45f, 45f, 39f};
		PdfPTable table = new PdfPTable(11);
		double totalWidth = IntStream.range(0, widths.length).mapToDouble(i -> Double.valueOf(widths[i])).sum();
		table.setTotalWidth(Double.valueOf(totalWidth).floatValue() + 200f); // + 250f
		table.setLockedWidth(true);
		table.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.setWidths(widths);

		PdfPCell cell = new PdfPCell();
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(11);
		cell.setPaddingBottom(10f);
		cell.setBorder(0);
		Paragraph paragraph = new Paragraph("畅视事业部   渠道结算明细表", new Font(abf, 13, Font.BOLD));
		paragraph.setAlignment(Paragraph.ALIGN_CENTER);
		cell.addElement(paragraph);
		table.addCell(cell);

		String[] tableFieldList = new String[] {"序号", "公司名称","账期", "产品名称", "新增用户",
				"续订", "结算用户总数", "结算单价", "总扣罚金额", "预估金额", "结算金额"};

		for(String tableField : tableFieldList) {
			cell = new PdfPCell(new Phrase(tableField, titleFont));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setVerticalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
		}

		for(int i = 0; i < list.size(); i++) {
			YksytAccountView data = list.get(i);
			cell = new PdfPCell(new Phrase(String.valueOf(i+1), titleFont));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setVerticalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getCompany(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getThemonth().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getSpName(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getNewuser().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getOlduser().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getTotaluser().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getPrice().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoneyDiscount().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoneyReckon().toString(), titleFont));
			table.addCell(cell);

			cell = new PdfPCell(new Phrase(data.getMoney().toString(), titleFont));
			table.addCell(cell);

		}

		//统计总数
		cell = new PdfPCell(new Phrase("总计：", titleFont));
		cell.setColspan(4);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getNewuser).reduce(Long::sum).get().toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getOlduser).reduce(Long::sum).get().toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getTotaluser).reduce(Long::sum).get().toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getPrice).reduce(BigDecimal.ZERO,BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getMoneyDiscount).reduce(BigDecimal.ZERO,BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getMoneyReckon).reduce(BigDecimal.ZERO,BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(list.stream().map(YksytAccountView::getMoney).reduce(BigDecimal.ZERO,BigDecimal::add).toString(), titleFont));
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("经办人确认：", titleFont));
		cell.setColspan(11);
		cell.setBorder(0);
		cell.setPaddingTop(50);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("业务组确认：", titleFont));
		cell.setColspan(11);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("数字化运营中心确认：", titleFont));
		cell.setColspan(11);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("质量管控中心确认：", titleFont));
		cell.setColspan(11);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("财务支撑中心确认：", titleFont));
		cell.setColspan(11);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		cell = new PdfPCell(new Paragraph("部门负责人确认：", titleFont));
		cell.setColspan(11);
		cell.setBorder(0);
		cell.setPaddingTop(15);
		table.addCell(cell);

		document.add(table);
		document.close();
	}

}
