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 400-电子发票（航空运输客票电子行程单）
 * @Author xiangerlin
 * @Date 2025/4/21 14:30
 **/
public class ElecFlightInvoiceAssembler implements ParamAssembler{

    private JSONObject jsonObject;

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

    /**
     * 组装参数
     *
     * @return
     */
    @Override
    public JSONObject assembleParam() {
        JSONObject param = 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);
            // 1. 单据号码/电子客票号码，必填
            param.put("djhm", planeticketH.getPT_ETICKETNO());
            // 2. 验证码，非必填
            param.put("yzm", planeticketH.getPT_VALIDCODE());
            // 3. 开票日期，格式为 yyyy-MM-dd，必填
            param.put("kprq", planeticketH.getPT_DATE());
            // 4. 销售单位代号，非必填
            param.put("xsdwdh", planeticketH.getPT_SALERCODE());
            // 5. 填开单位，非必填
            param.put("tkdw", planeticketH.getPT_SALERNAME());
            // 6. 乘车人姓名，非必填
            param.put("cjrxm", planeticketH.getPT_CUSTOMER());
            // 7. 身份证号，非必填
            param.put("sfzh", planeticketH.getPT_IDNO());
            // 8. 票价，必填
            param.put("pj", planeticketH.getPT_PRICE());
            // 9. 民航发展基金，非必填
            param.put("mhfzjj", planeticketH.getPT_FUNDSAMOUNT());
            // 10. 保险费，非必填
            param.put("bxf", planeticketH.getPT_INSURANCE());
            // 11. 总额，必填
            param.put("ze", planeticketH.getPT_AMOUNT());
            // 12. 发票消费类型，非必填
            param.put("fpxflx", "");
            // 13. 国内国际标签，非必填
            param.put("gngjbq", planeticketH.getPT_INTERNATIONALFLAG());
            // 14. 印刷序号，非必填
            param.put("ysxh", planeticketH.getPT_SERIALNO());
            // 15. 燃油附加费，非必填
            param.put("ryfjf", planeticketH.getPT_FUELAMOUNT());
            if ("1".equals(planeticketH.getPT_ELECTRONICMARK())){
                //电子票需要的字段
                // 16. 购方税号，非必填
                param.put("gfsh", planeticketH.getPT_BUYERTAXNO());
                // 17. 购方名称，非必填
                param.put("gfmc", planeticketH.getPT_BUYERNAME());
                // 18. 签注，非必填
                param.put("xcdqz", planeticketH.getPT_MARKDESC());
                // 19. 开票状态，非必填
                param.put("kpzt", planeticketH.getPT_ISSUINGSTATE());
                // 20. 行程单提示信息，非必填
                param.put("xcdtsxx", planeticketH.getPT_PROMPTINFO());
                // 21. 增值税税率，非必填
                param.put("zzssl", planeticketH.getPT_TAXRATE());
                // 22. 增值税税额，非必填
                param.put("zzsse", planeticketH.getPT_TAXRATEAMOUNT());
                // 23. 合计，非必填
                param.put("hj", planeticketH.getPT_NOTAXAMOUNT());
            }
            List<FlightItemInfo> filightItemList = new LinkedList<>();
            param.put("flightInfos",filightItemList);
            if (CollectionUtils.isNotEmpty(planeticketBList)){
                for (LexmiscivPlaneticketsEntity b : planeticketBList) {
                    //for循环
                    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());//舱位等级
                    if ("1".equals(planeticketH.getPT_ELECTRONICMARK())){
                        //电子票需要的字段
                        flightItem.setSsflbm("");//税收分类编码
                        flightItem.setXmmc("");//项目名称
                        flightItem.setGgxh("");//规格型号
                        flightItem.setDw("");//单位
                        flightItem.setSl("");//数量
                        flightItem.setDj("");//单价
                        flightItem.setJe(String.valueOf(planeticketH.getPT_PRICE()));//金额
                        flightItem.setSlv(planeticketH.getPT_TAXRATE());//税率
                        flightItem.setSe(planeticketH.getPT_TAXAMOUNT());//税额
                    }
                    filightItemList.add(flightItem);
                }
            }
        }
        return param;
    }
}
