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.FlightItemInfo;
import com.hzya.frame.plugin.cinvoice.lexmis.entity.LexmiscivPlaneticketEntity;
import com.hzya.frame.plugin.cinvoice.lexmis.entity.LexmiscivPlaneticketsEntity;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * @Description 航空运输电子客票行程单（a4） 参数组装
 * @Author xiangerlin
 * @Date 2025/4/21 10:57
 **/
public class FlightInvoiceAssembler implements ParamAssembler{

    private JSONObject jsonObject;

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

    /**
     * 组装参数
     *
     * @return
     */
    @Override
    public JSONObject assembleParam() {
        JSONObject params = new JSONObject();
        String planeticketHStr = jsonObject.getString("planeticketH");
        String planeticketBListStr = jsonObject.getString("planeticketBList");
        if (StrUtil.isNotEmpty(planeticketHStr)){
            LexmiscivPlaneticketEntity planeticketH = JSONObject.parseObject(planeticketHStr, LexmiscivPlaneticketEntity.class);
            List<LexmiscivPlaneticketsEntity> planeticketBList = JSONObject.parseArray(planeticketBListStr, LexmiscivPlaneticketsEntity.class);
            params.put("djhm", planeticketH.getPT_ETICKETNO()); // 单据号码/电子客票号码
            params.put("yzm", planeticketH.getPT_VALIDCODE()); // 验证码
            params.put("kprq", planeticketH.getPT_DATE()); // 开票日期,yyyy-MM-dd
            params.put("xsdwdh", planeticketH.getPT_SALERCODE()); // 销售单位代号
            params.put("tkdw", planeticketH.getPT_SALERNAME()); // 填开单位
            params.put("cjrxm", planeticketH.getPT_CUSTOMER()); // 乘车人姓名
            params.put("sfzh", planeticketH.getPT_IDNO()); // 身份证号
            params.put("pj", planeticketH.getPT_PRICE()); // 票价
            params.put("mhfzjj", planeticketH.getPT_FUNDSAMOUNT()); // 民航发展基金
            params.put("bxf", planeticketH.getPT_INSURANCE()); // 保险费
            params.put("ze", planeticketH.getPT_AMOUNT()); // 总额
            params.put("fpxflx", ""); // 发票消费类型
            params.put("gngjbq", planeticketH.getPT_INTERNATIONALFLAG()); // 国内国际标签
            params.put("ysxh", planeticketH.getPT_SERIALNO()); // 印刷序号
            params.put("ryfjf", planeticketH.getPT_FUELAMOUNT()); // 燃油附加费
            List<FlightItemInfo> filightItemList = new LinkedList<>();
            params.put("flightInfos",filightItemList);
            //for循环
            if (CollectionUtils.isNotEmpty(planeticketBList)){
                for (LexmiscivPlaneticketsEntity b : planeticketBList) {
                    FlightItemInfo flightItem = new FlightItemInfo();
                    flightItem.setCfz(b.getPTS_FSTATION());//出发站
                    flightItem.setDdz(b.getPTS_TSTATION());//到达站
                    flightItem.setHbh(b.getPTS_NO());//航班号
                    flightItem.setCjsj(b.getPTS_TIME());//乘机时间
                    flightItem.setZwdj(b.getPTS_CLASS());//座位等级
                    flightItem.setCyr(b.getPTS_CARRIER());//承运人
                    flightItem.setCwdj(b.getPTS_CLASSNAME());//舱位等级
                    filightItemList.add(flightItem);
                }
            }
        }
        return params;
    }
}
