/**
@description 收货清单明细生成PDF
*/
public without sharing class ReceivingNotePDFController {

    private final static Integer LINE_COUNT_PER_PAGE = 13; //每页13条明细
    private String receivingNoteId;
    public List<PDFPage> pages { get; set; } // 所有PDF页的内容
    public Integer pageCnt { get; set; }
    public ReceivingNote__c receivingNoteHeadShow { get; set; }
    public String errorMessage{get;set;}

    public ReceivingNotePDFController() {
        receivingNoteId = ApexPages.currentPage().getParameters().get('rnid');
        receivingNoteHeadShow = new ReceivingNote__c();
        pages = new List<PDFPage>();
    }

    /**
    @description 收货单PDF画面初始化
    */
    public void init() {
        List<ReceivingNote__c> receivingNoteList = new List<ReceivingNote__c>();
        if(receivingNoteId!=null){
            receivingNoteList = [
                    SELECT Sales_order_number__c      //合同编码字段
                         , OrderCode__c              //订单编码
                         , ApprovalNumber__c          //采购申请决裁号
                         , DateOfDelivery__c          //发货日
                         , Internal_asset_location__c //备品存放地
                         //20210312 you WLIG-BV8CHF start
                        //, ReceivingNoteNo__c //清单编号
                        , ReceivingNoteNo_New__c
                        //20210312 you WLIG-BV8CHF end
                         , QRId__c                     //二维码
                      FROM ReceivingNote__c
                     WHERE Id = :receivingNoteId
                ];
        }

        if(receivingNoteList.size()>0){
            receivingNoteHeadShow = receivingNoteList[0];
        }
        else{
            errorMessage = '收货清单Id不存在';
            return;
        }

        List<ReceivingNoteDetail__c> rndList = new List<ReceivingNoteDetail__c>();

        rndList = [
                SELECT ReceivingNoteDetailNo__c
                     , OT_CODE__c
                     , Fixture_Model_No_F__c
                     , Fixture_Model_Name_F__c
                     , Internal_Asset_number_key__c
                     , SerialNumber_F__c
                     , PeriodOfUse__c
                     , Packing_list_Fixture_F__c
                     , EquipmentSet_Managment_Code__c
                     , Salesdepartment__c
                     , SalesProvince__c
                     , Product_category__c
                     , Equipment_Type__c
                     , Manage_type__c
                     , unknow_serial_NO_product__c
                     , Asset_loaner_category_F__c
                     , Loaner_accsessary__c
                     , RND_Status__c

                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__r.ReceivingNote__c = :receivingNoteId
                   AND Delete_Flag__c = false
                   AND Change_Point__c != '删除'
                 ORDER BY ReceivingNoteSummary__c, Name ASC NULLS LAST
                ];

        // 用行数算出页数
        Integer pageCount = rndList.size() / LINE_COUNT_PER_PAGE;
        if (Math.mod(rndList.size(), LINE_COUNT_PER_PAGE) > 0) {
            pageCount += 1;
        }

        //新建一页
        PDFPage pdfPage = new PDFPage();

        for (Integer i = 0; i < pageCount * LINE_COUNT_PER_PAGE; i++) {
            // 有明细则用明细，没明细则用空行填满页面
            if ( i < rndList.size()) {
                pdfPage.lineList.add(rndList[i]);
            } else {
                pdfPage.lineList.add(new ReceivingNoteDetail__c());
            }

            // 装满一页 ，则新造一页
            if (pdfPage.lineList.size() >= LINE_COUNT_PER_PAGE) {
                pages.add(pdfPage);
                pdfPage = new PDFPage();
            }
        }

        pageCnt = pages.size();

    }

    /**
    @description 一页pdf上的内容
    */
    public class PDFPage {
        public List<ReceivingNoteDetail__c> lineList { get; private set; }
        /**
        @description
        */
        public PDFPage() {
            lineList = new List<ReceivingNoteDetail__c>();
        }
    }

}