package com.yem.tws.common1;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

/**
 * @author zhouc
 * @date 2023/7/22 10:30
 * @className InvoicingRemarksUtils
 * @description
 */
public class InvoicingRemarksUtils {
    /**
     * 根据开票备注ID、报关单ID，构建开票备注
     * @param InvoicingRemarksId 开票备注ID
     * @param customsBillId 报关单ID
     * @param customsBillType 报关单标识
     * @return
     */
    public static String getRemarks(long InvoicingRemarksId,long customsBillId,String customsBillType){
        String remarks = "";
        if(0L != InvoicingRemarksId && 0L != customsBillId){
            DynamicObject customsBill = null;
            QFilter qFilter = new QFilter("id", QCP.equals, customsBillId);
            DynamicObject[] customsBill_s = BusinessDataServiceHelper.load(customsBillType,
                    "id",
                    new QFilter[]{qFilter});
            if(null != customsBill_s && customsBill_s.length>0){
                customsBill = customsBill_s[0];
                customsBill = BusinessDataServiceHelper.loadSingle(customsBillId,customsBillType);
            }
            if(null == customsBill){
                return remarks;
            }
            qFilter = new QFilter("id", QCP.equals, InvoicingRemarksId);
            DynamicObject[] yem_invoicingremarks_s = BusinessDataServiceHelper.load("yem_invoicingremarks",
                    "id,yem_ir_entry," +
                            "yem_ir_entry.yem_display,yem_ir_entry.yem_delimiter,yem_ir_entry.yem_customsfield,yem_ir_entry.yem_precision," +
                            "yem_ir_entry.yem_display1,yem_ir_entry.yem_delimiter1,yem_ir_entry.yem_customsfield1,yem_ir_entry.yem_precision1," +
                            "yem_ir_entry.yem_display2,yem_ir_entry.yem_delimiter2,yem_ir_entry.yem_customsfield2,yem_ir_entry.yem_precision2",
                    new QFilter[]{qFilter});
            if(null != yem_invoicingremarks_s && yem_invoicingremarks_s.length>0){
                DynamicObject yem_invoicingremarks = yem_invoicingremarks_s[0];
                DynamicObjectCollection yem_ir_entry_s = yem_invoicingremarks.getDynamicObjectCollection("yem_ir_entry");
                StringBuffer remarks_s = new StringBuffer();
                for(int i=0;i<yem_ir_entry_s.size();i++){
                    DynamicObject yem_ir_entry = yem_ir_entry_s.get(i);
                    //第一段
                    String yem_display = yem_ir_entry.getString("yem_display");
                    if(StringUtils.isNotEmpty(yem_display)){
                        remarks_s.append(yem_display);
                    }
                    String yem_delimiter = yem_ir_entry.getString("yem_delimiter");
                    if(StringUtils.isNotEmpty(yem_delimiter)){
                        remarks_s.append(yem_delimiter);
                    }
                    String yem_customsfield = yem_ir_entry.getString("yem_customsfield");
                    if(StringUtils.isNotEmpty(yem_customsfield)){
                        String yem_precision = yem_ir_entry.getString("yem_precision");
                        int scale = 0;
                        if(!(null == yem_precision || "".equals(yem_precision) || "无".equals(yem_precision))){
                            scale = BigDecimalUtils.toBigDecimal(yem_precision).intValue();
                        }
                        String val = "";
                        if(customsBill.get(yem_customsfield) instanceof BigDecimal){
                            val = BigDecimalUtils.toBigDecimal(customsBill.get(yem_customsfield),scale).toPlainString();
                        }else{
                            if(customsBill.get(yem_customsfield)==null){
                                remarks = "Err"+customsBill.getString("yem_entryid");

                            }else{
                                val = customsBill.get(yem_customsfield).toString();
                            }

                        }
                        remarks_s.append(val);
                    }
                    //第二段
                    String yem_display1 = yem_ir_entry.getString("yem_display1");
                    if(StringUtils.isNotEmpty(yem_display1)){
                        remarks_s.append(yem_display1);
                    }
                    String yem_delimiter1 = yem_ir_entry.getString("yem_delimiter1");
                    if(StringUtils.isNotEmpty(yem_delimiter1)){
                        remarks_s.append(yem_delimiter1);
                    }
                    String yem_customsfield1 = yem_ir_entry.getString("yem_customsfield1");
                    if(StringUtils.isNotEmpty(yem_customsfield1)){
                        String yem_precision1 = yem_ir_entry.getString("yem_precision1");
                        int scale1 = 0;
                        if(!(null == yem_precision1 || "".equals(yem_precision1) || "无".equals(yem_precision1))){
                            scale1 = BigDecimalUtils.toBigDecimal(yem_precision1).intValue();
                        }
                        String val1 = "";
                        if(customsBill.get(yem_customsfield1) instanceof BigDecimal){
                            val1 = BigDecimalUtils.toBigDecimal(customsBill.get(yem_customsfield1),scale1).toPlainString();
                        }else{
                            val1 = customsBill.get(yem_customsfield1).toString();
                        }
                        remarks_s.append(val1);
                    }
                    //第三段
                    String yem_display2 = yem_ir_entry.getString("yem_display2");
                    if(StringUtils.isNotEmpty(yem_display2)){
                        remarks_s.append(yem_display2);
                    }
                    String yem_delimiter2 = yem_ir_entry.getString("yem_delimiter2");
                    if(StringUtils.isNotEmpty(yem_delimiter2)){
                        remarks_s.append(yem_delimiter2);
                    }
                    String yem_customsfield2 = yem_ir_entry.getString("yem_customsfield2");
                    if(StringUtils.isNotEmpty(yem_customsfield2)){
                        String yem_precision2 = yem_ir_entry.getString("yem_precision2");
                        int scale2 = 0;
                        if(!(null == yem_precision2 || "".equals(yem_precision2) || "无".equals(yem_precision2))){
                            scale2 = BigDecimalUtils.toBigDecimal(yem_precision2).intValue();
                        }
                        String val2 = "";
                        if(customsBill.get(yem_customsfield2) instanceof BigDecimal){
                            val2 = BigDecimalUtils.toBigDecimal(customsBill.get(yem_customsfield2),scale2).toPlainString();
                        }else{
                            val2 = customsBill.get(yem_customsfield2).toString();
                        }
                        remarks_s.append(val2);
                    }

                    remarks_s.append("\n");
                }
                remarks = remarks_s.toString();
            }
        }
        return remarks;
    }
}
