package yem.wmc.ca.utils;

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 org.openxmlformats.schemas.drawingml.x2006.chart.STRadarStyle;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DyDataUtils {

    /**
     * 表头动态默认特殊处理方法
     *
     * @param bill
     * @param type
     * @param jsonfield
     * @return
     */
    public static Object getDyData(DynamicObject bill, String type, String jsonfield) {
        Object obj = null;
        if ("org".equals(type)) {//处理组织基础资料
            String yem_terprisecode = bill.getString("yem_cnsntradecode");//境内收发货人海关代码
            if (StringUtils.isNotEmpty(yem_terprisecode)) {
                QFilter filter = new QFilter("yem_terprisecode", QCP.equals, yem_terprisecode);
                DynamicObject companyinfo = BusinessDataServiceHelper.loadSingle("yem_ca_companyinfo", "id,name,number,yem_erporg",
                        new QFilter[]{filter});
                if (companyinfo != null) {
                    obj = companyinfo.getDynamicObject("yem_erporg");
                }
            }
        } else if ("currency".equals(type)) {//处理币别currency
            String yem_curr = bill.getString(jsonfield);
            if (yem_curr != null && yem_curr != "") {
                QFilter filter = new QFilter("number", QCP.equals, yem_curr).or("name", QCP.equals, yem_curr);
                DynamicObject erpcurrency = BusinessDataServiceHelper.loadSingle("yem_ca_currencycode", "id,name,number,yem_erpcurrency",
                        new QFilter[]{filter});
                if (erpcurrency != null) {
                    obj = erpcurrency.getDynamicObject("yem_erpcurrency");
                }
            }
        } else if ("sumcurrency".equals(type)) {//处理币别currency
            //获取报关单明细信息
            DynamicObjectCollection yem_ca_cddetailedentry = bill.getDynamicObjectCollection("yem_ca_cddetailedentry");
            if (yem_ca_cddetailedentry.size() > 0) {
                DynamicObject yem_curr = yem_ca_cddetailedentry.get(0);
                String curr = yem_curr.getString("yem_tradecurrnum");
                if (curr != null && curr != "") {
                    QFilter filter = new QFilter("number", QCP.equals, curr);
                    DynamicObject erpcurrency = BusinessDataServiceHelper.loadSingle("yem_ca_currencycode", "id,name,number,yem_erpcurrency",
                            new QFilter[]{filter});
                    if (erpcurrency != null) {
                        obj = erpcurrency.getDynamicObject("yem_erpcurrency");
                    }
                }
            }
        } else if (type.equals("sumdecltotal")) {//处理单一窗口报关单表头汇总金额 报关币别/报关汇率 人民币
            BigDecimal sum = BigDecimal.ZERO;
            DynamicObjectCollection yem_ca_cddetailedentry = bill.getDynamicObjectCollection("yem_ca_cddetailedentry");
            String yem_curr = "";
            for (int k = 0; k < yem_ca_cddetailedentry.size(); k++) {
                DynamicObject yem_cur = yem_ca_cddetailedentry.get(k);
                sum = sum.add(BigDecimalUtils.toBigDecimal(yem_cur.get("yem_decltotal")));
                yem_curr = yem_cur.getString("yem_tradecurrnum");//分录币别编码
                if (yem_curr != null && yem_curr != "") {
                    QFilter filter = new QFilter("number", QCP.equals, yem_curr);
                    DynamicObject erpcurrency = BusinessDataServiceHelper.loadSingle("yem_ca_currencycode", "id,name,number,yem_erpcurrency",
                            new QFilter[]{filter});
                    if (erpcurrency != null) {
                        bill.set("yem_currencybase", erpcurrency.getDynamicObject("yem_erpcurrency"));//表头报关币别赋值
                    }
                }
            }
            //获取进出口日期
            if (yem_curr.length() > 0) {
                Date yem_fiedatets = bill.getDate("yem_fiedatets");//进出口日期
                if (yem_fiedatets != null) {
                    Date Min = DateUtils.getMinMonthDate(yem_fiedatets);
                    Date max = DateUtils.getMaxMonthDate(yem_fiedatets);
                    QFilter filter = new QFilter("yem_date", QCP.large_equals, Min)
                            .and(new QFilter("yem_date", QCP.less_equals, max));
                    //查查询字段标识
                    String Fileid = "yem_" + yem_curr.toLowerCase();//将字段标识转换为小写
                    DynamicObject[] erpcurrency = BusinessDataServiceHelper.load("yem_ca_centralrate",
                            "id,name,number,yem_date," + Fileid + "",
                            new QFilter[]{filter}, "yem_date");
                    if (erpcurrency != null && erpcurrency.length > 0) {
                        DynamicObject yem_ca_billlogon = erpcurrency[0];
                        if (yem_ca_billlogon != null) {
                            BigDecimal exchangerate = BigDecimalUtils.toBigDecimal(yem_ca_billlogon.getString(Fileid)).divide(BigDecimalUtils.one_handred);
                            bill.set("yem_exchangerate", exchangerate);//汇率赋值
                            bill.set("yem_sumlocdecltotal", exchangerate.multiply(sum));//汇率赋值
                        }
                    }
                }
                obj = sum;
            }
        } else if ("taxorg".equals(type)) {//处理组织基础资料
            String yem_terprisecode = bill.getString("yem_tradecode");//收发货单位编码
            if (StringUtils.isNotEmpty(yem_terprisecode)) {
                QFilter filter = new QFilter("yem_terprisecode", QCP.equals, yem_terprisecode);
                DynamicObject companyinfo = BusinessDataServiceHelper.loadSingle("yem_ca_companyinfo", "id,name,number,yem_erporg",
                        new QFilter[]{filter});
                if (companyinfo != null) {
                    obj = companyinfo.getDynamicObject("yem_erporg");
                }
            }
        } else if ("cusorg".equals(type)) {//处理组织基础资料
            String yem_terprisecode = bill.getString("yem_tradeco");//收发货单位编码
            if (StringUtils.isNotEmpty(yem_terprisecode)) {
                QFilter filter = new QFilter("yem_terprisecode", QCP.equals, yem_terprisecode);
                DynamicObject companyinfo = BusinessDataServiceHelper.loadSingle("yem_ca_companyinfo", "id,name,number,yem_erporg",
                        new QFilter[]{filter});
                if (companyinfo != null) {
                    obj = companyinfo.getDynamicObject("yem_erporg");
                }
            }
        } else if ("cussumamt".equals(type)) {//退税报关单【汇总金额】
            BigDecimal sum = BigDecimal.ZERO;
            DynamicObjectCollection yem_ca_cddetailedentry = bill.getDynamicObjectCollection("yem_ca_cuslcard");
            for (int k = 0; k < yem_ca_cddetailedentry.size(); k++) {
                DynamicObject yem_cur = yem_ca_cddetailedentry.get(k);
                sum = sum.add(BigDecimalUtils.toBigDecimal(yem_cur.get("yem_decltotal")));
            }
            obj = sum;
        } else if ("custommastername".equals(type)) {
            String yem_custommaster = bill.getString("yem_custommaster");//申报地海关编码
            if (yem_custommaster.length() > 0) {
                QFilter filter = new QFilter("number", QCP.equals, yem_custommaster);
                DynamicObject companyinfo = BusinessDataServiceHelper.loadSingle("yem_ca_cusareacode", "id,name,number",
                        new QFilter[]{filter});
                if (companyinfo != null) {
                    obj = companyinfo.getString("name");
                }
            }
        } else if ("ieportname".equals(type)) {
            String yem_custommaster = bill.getString("yem_ieport");//申报地海关编码
            if (yem_custommaster.length() > 0) {
                QFilter filter = new QFilter("number", QCP.equals, yem_custommaster);
                DynamicObject companyinfo = BusinessDataServiceHelper.loadSingle("yem_ca_cusareacode", "id,name,number",
                        new QFilter[]{filter});
                if (companyinfo != null) {
                    obj = companyinfo.getString("name");
                }
            }
        } else if ("feemarkname".equals(type)) {
            String yem_custommaster = bill.getString("yem_feemark");//申报地海关编码
            if (yem_custommaster.length() > 0) {
                if (yem_custommaster.equals("1")) {
                    obj = "率";
                } else if (yem_custommaster.equals("2")) {
                    obj = "单价";
                } else if (yem_custommaster.equals("3")) {
                    obj = "总价";
                }
            }
        } else if ("othermarkname".equals(type)) {
            String yem_custommaster = bill.getString("yem_othermark");//申报地海关编码
            if (yem_custommaster.length() > 0) {
                if (yem_custommaster.equals("1")) {
                    obj = "率";
                } else if (yem_custommaster.equals("2")) {
                    obj = "单价";
                } else if (yem_custommaster.equals("3")) {
                    obj = "总价";
                }
            }
        } else if ("insurmarkname".equals(type)) {
            String yem_custommaster = bill.getString("yem_insurmark");//申报地海关编码
            if (yem_custommaster.length() > 0) {
                if (yem_custommaster.equals("1")) {
                    obj = "率";
                } else if (yem_custommaster.equals("2")) {
                    obj = "单价";
                } else if (yem_custommaster.equals("3")) {
                    obj = "总价";
                }
            }
        }
        return obj;
    }

    /**
     * 处理分录动态默认方法
     *
     * @param EntryData 当前行对象
     * @param type      动态默认方法名称
     * @param jsonfield Json字段标识：此处应填写单据上的标识
     * @return obj
     */
    public static Object getDyDataEntry(DynamicObject EntryData, String type, String jsonfield) {
        Object obj = null;
        if ("currency".equals(type)) {//处理币别currency
            String yem_curr = EntryData.getString(jsonfield);
            if (yem_curr != null && yem_curr != "") {
                QFilter filter = new QFilter("number", QCP.equals, yem_curr).or("name", QCP.equals, yem_curr);
                DynamicObject erpcurrency = BusinessDataServiceHelper.loadSingle("yem_ca_currencycode", "id,name,number,yem_erpcurrency",
                        new QFilter[]{filter});
                if (erpcurrency != null) {
                    obj = erpcurrency.getDynamicObject("yem_erpcurrency");
                }
            }
        } else if ("dutymodename".equals(type)) {
            String yem_curr = EntryData.getString(jsonfield);
            Map map = new HashMap<>();
            map.put("1", "照章征税");
            map.put("2", "折半征税");
            map.put("3", "全免");
            map.put("4", "特案");
            map.put("5", "随征免性质");
            map.put("6", "保证金");
            map.put("7", "保函");
            map.put("8", "折半补税");
            map.put("9", "全额退税");
            if (yem_curr.length() > 0) {
                obj = map.get(yem_curr);
            }
        }
        return obj;
    }
}
