package nccloud.web.ic.pub.action;

import com.alibaba.fastjson.JSON;
import nc.vo.ic.general.define.ICBillVO;
import nc.vo.pubapp.res.NCModule;
import nccloud.base.reflect.NCClass;
import nccloud.dto.ic.pub.entity.GeneralSumPrintInfo;
import nccloud.framework.core.io.WebFileType;
import nccloud.framework.core.json.IJson;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.processor.template.BillCardConvertProcessor;
import nccloud.framework.web.processor.template.ExtBillCardConvertProcessor;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.pubitf.platform.print.IPrintInfo;
import nccloud.pubitf.platform.print.vo.BatchPrintInfo;
import nccloud.web.platform.print.AbstractPrintAction;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 出入库汇总打印
 * @author zhegnxinm
 * @date 2018-12-16 上午10:48:39
 * @version ncc1.0
 */
public class GeneralSumPrintAction extends AbstractPrintAction {

    @Override
    public IPrintInfo convertPrintInfo(BatchPrintInfo info) {
        IJson json = JsonFactory.create();
        GeneralSumPrintInfo newinfo = json.fromJson(info.getUserjson(), GeneralSumPrintInfo.class);
        newinfo.setFilename((info.getFilename() == null) ? "download-"
                + String.valueOf(System.currentTimeMillis()) : info
                .getFilename());

        String appcode = info.getAppcode();
        String billcardStr = newinfo.getBillcard();

        if (appcode == null) {
            appcode = SessionContext.getInstance().getAppcode();
        }
        ICBillVO bill = null;
        if(appcode.equals("400800800")){
            ExtBillCard billcard = json.fromJson(billcardStr, ExtBillCard.class);
            ExtBillCardConvertProcessor processor = new ExtBillCardConvertProcessor();
            bill = processor.fromExtBillCard(billcard);
        }else if("400801214".equals(appcode)){
            BillCardConvertProcessor processor = new BillCardConvertProcessor();
            List<ICBillVO> billVOList=new ArrayList<>();
            newinfo.setAppcode(appcode);
            newinfo.setIds(info.getOids());
            newinfo.setNodeKey(info.getNodekey());
            NCClass printServiceClazz = new NCClass();
            printServiceClazz.setModule(getPrintServiceModule());
            printServiceClazz.setName(getPrintServiceName());
            newinfo.setPrintServiceClazz(printServiceClazz);
            newinfo.setPrintTemplateID(info.getPrintTemplateID());
            //判断billcard是否为数组
            if(billcardStr.startsWith("[")){
                BillCard[] billcardList = json.fromJson(billcardStr,BillCard[].class);
                for(BillCard billcard:billcardList ){
                    bill = processor.fromBillCard(billcard);
                    billVOList.add(bill);
                }
                newinfo.setData(billVOList.toArray(new Object[0]));
            }else{
                BillCard billcard = json.fromJson(billcardStr,BillCard.class);
                bill = processor.fromBillCard(billcard);
                newinfo.setData(new Object[] { bill });
            }
            return newinfo;
        }else{
            BillCard billcard = json.fromJson(billcardStr,BillCard.class);
            BillCardConvertProcessor processor = new BillCardConvertProcessor();
            bill = processor.fromBillCard(billcard);
        }
        newinfo.setAppcode(appcode);
        newinfo.setIds(info.getOids());
        newinfo.setNodeKey(info.getNodekey());
        NCClass printServiceClazz = new NCClass();
        printServiceClazz.setModule(getPrintServiceModule());
        printServiceClazz.setName(getPrintServiceName());
        newinfo.setPrintServiceClazz(printServiceClazz);
        newinfo.setPrintTemplateID(info.getPrintTemplateID());
        newinfo.setData(new Object[] { bill });
        return newinfo;
    }




    public String getPrintServiceModule() {
        return NCModule.IC.getName();
    }

    public String getPrintServiceName() {
        return "nccloud.pubimpl.ic.inbound.generalin.operator.GeneralSumPrintOperator";
    }

    public WebFileType getWebFileType() {
        return WebFileType.Pdf;
    }

}
