package com.hzya.frame.plugin.cinvoice.assembler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.plugin.cinvoice.dto.ZzsItemInfo;
import com.hzya.frame.plugin.cinvoice.lexmis.entity.LexmiscivInvoicedetailEntity;
import com.hzya.frame.plugin.cinvoice.lexmis.entity.LexmiscivInvoicemainEntity;
import org.apache.commons.collections.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description 增值税类发票参数
 * 增值税类发票(01增值税专票，04增值税普票，08增值税电子专票，10增值税电子普票，11增值税普票(票卷)，31电子发票(增值税专票)，32电子发票(普票))
 * @Author xiangerlin
 * @Date 2025/4/21 09:27
 **/
public class ZzsInvoiceAssembler implements ParamAssembler{

    private JSONObject jsonObject;

    public ZzsInvoiceAssembler(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    /**
     * 增值税类发票参数组装
     *
     * @return
     */
    @Override
    public JSONObject assembleParam() {
        JSONObject param = new JSONObject();
        String zzsInvoiceHStr = jsonObject.getString("zzsInvoiceH");
        String zzsInvoiceBListStr = jsonObject.getString("zzsInvoiceBList");
        if (StrUtil.isNotEmpty(zzsInvoiceHStr) && StrUtil.isNotEmpty(zzsInvoiceBListStr)){
            LexmiscivInvoicemainEntity zzsInvoiceH = JSONObject.parseObject(zzsInvoiceHStr, LexmiscivInvoicemainEntity.class);
            List<LexmiscivInvoicedetailEntity> zzsInvoiceBList =  JSONObject.parseArray(zzsInvoiceBListStr, LexmiscivInvoicedetailEntity.class);
            if (null != zzsInvoiceH){
                param.put("fpdm", zzsInvoiceH.getIM_FPDM());//发票代码
                param.put("fphm", zzsInvoiceH.getIM_FPHM());//发票号码
                param.put("kprq", zzsInvoiceH.getIM_DATE());//开票日期 yyyy-MM-dd
                param.put("xfsh", zzsInvoiceH.getIM_SALERTAXNO());//销方税号
                param.put("xfmc", zzsInvoiceH.getIM_SALERNAME());//销方名称
                param.put("gfsh", zzsInvoiceH.getIM_BUYERTAXNO());//购方税号
                param.put("gfmc", zzsInvoiceH.getIM_BUYERNAME());//购方名称
                param.put("hjje", zzsInvoiceH.getIM_NOTAXAMOUNT());//合计金额（不含税）
                param.put("hjse", zzsInvoiceH.getIM_TAXAMOUNT());//合计税额
                param.put("jshj", zzsInvoiceH.getIM_TOTALAMOUNT());//价税合计，发票类型为31电子发票(增值税专票)，32电子发票(普票) 时必填
                param.put("jym", zzsInvoiceH.getIM_CHECKCODE());//校验码，发票类型为04增值税普票、10增值税电子普票，11增值税普票(票卷) 时必填
                param.put("xfdzdh", zzsInvoiceH.getIM_SALERADDRESS());//销售方地址，电话
                param.put("xfyhzh", zzsInvoiceH.getIM_SALERBANKNO());//销售方开户行及账号
                param.put("gfdzdh", zzsInvoiceH.getIM_BUYERADDRESS());//购买方地址、电话
                param.put("gfyhzh", zzsInvoiceH.getIM_BUYERBANKNO());//购买方开户行及账号
                param.put("mmq", zzsInvoiceH.getIM_ENCRYPTIONCODE());//密码区
                param.put("bz", zzsInvoiceH.getIM_MEMO());//备注
                param.put("jqbh", zzsInvoiceH.getIM_MACHINENO());//机器编号
                param.put("jdhm", zzsInvoiceH.getIM_PRINTEDFPHM());//机打号码，发票类型为11增值税普票(票卷) 时必填
                param.put("kpr", zzsInvoiceH.getIM_MAKERNM());//开票人
                param.put("skr", zzsInvoiceH.getIM_PAYEE());//收款人
                param.put("fhr", zzsInvoiceH.getIM_AUDITOR());//复核人
                // 发票明细信息
                List<ZzsItemInfo> zzsItemInfoList = new LinkedList<>();
                param.put("zzsItemInfos", zzsItemInfoList);
                if (CollectionUtils.isNotEmpty(zzsInvoiceBList)){
                    for (LexmiscivInvoicedetailEntity b : zzsInvoiceBList) {
                        ZzsItemInfo zzsItemInfo = new ZzsItemInfo();
                        // 发票明细项
                        zzsItemInfo.setXh(b.getID_ROWID());//序号
                        zzsItemInfo.setSsflbm("");//税收分类编码
                        zzsItemInfo.setXmmc(b.getID_SERVICENM());//项目名称
                        zzsItemInfo.setGgxh(b.getID_SERVICETYPE());//规格型号
                        zzsItemInfo.setDw(b.getID_UNIT());//单位
                        zzsItemInfo.setSl(b.getID_NUM());//数量
                        zzsItemInfo.setDj(b.getID_PRICE());//单价
                        zzsItemInfo.setJe(b.getID_NOTAXAMOUNT());//金额
                        zzsItemInfo.setSlv(b.getID_TAX());//税率
                        zzsItemInfo.setSe(b.getID_TAXAMOUNT());//税额
                        zzsItemInfoList.add(zzsItemInfo);
                    }
                }
            }
        }

        return param;
    }
}
