
package org.example.Entity.System;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.Map;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.bytedeco.opencv.presets.opencv_core;
import org.example.Entity.Control.Fields.Field;
import org.example.Enums.SystemTypeEnums;
import org.example.Utils.NativeExtensionUtils;
import org.example.Utils.PublicUtil;
import org.example.Utils.SqlAnalyzer;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import static org.example.Utils.DataTableUtil.*;

public class BillModule extends ModuleBaseEntity {
    @JsonIgnore
    public Map<String, Object> _row;


    @JsonProperty("ModuleType")
    public int ModuleType = 1;


    @JsonIgnore
    public int AuditFlag;

    @JsonIgnore
    public String DetailGuid;

    @JsonIgnore
    public String ComfirmRemark;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("Main")
    public List<Field> Main;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("Detail")
    public Object Detail;

    public BillModule() {
        super();
    }

    public BillModule(Map<String, Object> row) {
        super(row);
        this._row = row;
    }

    private String _detailKey;

    @JsonIgnore
    @JsonProperty("DetailKey")
    public String getDetailKey() {
        return (String) get(this._row, "DetailKey", _detailKey);
    }

    public void setDetailKey(String detailKey) {
        this._detailKey = detailKey;
    }


    private String _redMenuName;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("RedMenuName")
    public String getRedMenuName() {
        if (_redMenuName == null) {
            _redMenuName = (String) get(this._row, "RedMenuName", _redMenuName);
        }
        return _redMenuName;
    }

    public void setRedMenuName(String redMenuName) {
        this._redMenuName = redMenuName;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("HasSource")
    public Boolean HasSource;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("SourceTab")
    public List<Map<String, Object>> SourceTab;


    private String _billSourceIds;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("BillSourceIds")
    public String getBillSourceIds() {
//        if (_billSourceIds == null) {
//            _billSourceIds = (String) get(this._row, "BillSourceIds", null,null);
//        }
        _billSourceIds = (_billSourceIds != null) ?
                _billSourceIds : (get(_row, "BillSourceIds", null, null) instanceof String) ?
                (String) get(_row, "BillSourceIds", null, null) : null;
        return _billSourceIds;
    }

    public void setBillSourceIds(String billSourceIds) {
        this._billSourceIds = billSourceIds;
    }

    private String IdField;

    @JsonProperty("IdField")
    public String getIdField() {
//        return IdField != null ? IdField : getMenuPrefix() + "billdocument_id";
        return Optional.ofNullable(super.getIdField())
                .filter(id -> !id.trim().isEmpty()) // 处理空字符串（如""、"   "）
                .orElse(getMenuPrefix() + "billdocument_id");
    }

    public void setIdField(String idField) {
        IdField = idField;
    }


    private String _masterTab;

    @JsonIgnore
    @JsonProperty("MasterTable")
    public String getMasterTable() {
        return (String) get(_row, "MasterTable", _masterTab);
    }

    public void setMasterTable(String masterTable) {
        this._masterTab = masterTable;
    }


    private String _masterSql;

    @JsonIgnore
    @JsonProperty("MasterSql")
    public String getMasterSql() {
        _masterSql = (String) get(_row, "MasterSql", _masterSql);
        if (_masterSql != null && !_masterSql.isEmpty() && getOtherCond() != null && !getOtherCond().isEmpty()) {
            Map<String, String> con = new HashMap<>();
            con.put("$other", getOtherCond());
            return new SqlAnalyzer(_masterSql).InsertWhere(con, false, false, false);
        }
        return _masterSql;
    }

    public void setMasterSql(String masterSql) {
        this._masterSql = masterSql;
    }

    private String _detailTable;

    @JsonIgnore
    @JsonProperty("DetailTable")
    public String getDetailTable() {
        return (String) get(_row, "DetailTable", _detailTable);
    }

    public void setDetailTable(String detailTable) {
        this._detailTable = detailTable;
    }


    private String _detailSql;

    @JsonIgnore
    @JsonProperty("DetailSql")
    public String getDetailSql() {
        String value = getStringValue(_row, "DetailSQL", _detailSql, null);
        return (value instanceof String) ? (String) value : null;
    }

    public void setDetailSql(String detailSql) {
        this._detailSql = detailSql;
    }

    private String _scanSql;

    @JsonIgnore
    @JsonProperty("ScanSql")
    public String getScanSql() {
        return (String) get(_row, "ScanSql", _scanSql);
    }

    public void setScanSql(String scanSql) {
        this._scanSql = scanSql;
    }


    private Integer _leftWidth;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftWidth")
    public Integer getLeftWidth() {
        return (Integer) get(_row, "LeftWidth", _leftWidth);
    }

    public void setLeftWidth(Integer leftWidth) {
        this._leftWidth = leftWidth;
    }


    private Integer _topHeight;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("TopHeight")
    public Integer getTopHeight() {
        return (Integer) get(_row, "TopHeight", _topHeight);
    }

    public void setTopHeight(Integer topHeight) {
        this._topHeight = topHeight;
    }


    private String _updateCond;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("UpdateCond")
    public String getUpdateCond() {
        if (!OperAble) {
            return "1<>1";
        }
        if (_updateCond != null && !_updateCond.isEmpty()) {
            return _updateCond;
        }
        _updateCond = (String) get(_row, "UpdateCond", _updateCond);
        _updateCond = PublicUtil.ReqSqlPmsByRow(null, null, PublicUtil.SqlToCode(_updateCond), SystemTypeEnums.PmType.ignorenull);
        if (_updateCond != null && _updateCond.indexOf("{") < 0) {
            if (_updateCond.isEmpty()) {
                _updateCond = "1==1";
            }
        }
        return _updateCond;
    }

    public void setUpdateCond(String updateCond) {
        this._updateCond = updateCond;
    }


    private String _otherCond;

    @JsonIgnore
    @JsonProperty("OtherCond")
    public String getOtherCond() {
        return (String) get(_row, "OtherCond", _otherCond);
    }

    public void setOtherCond(String otherCond) {
        this._otherCond = otherCond;
    }


    private String _printFile;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("PrintFile")
    public String getPrintFile() {
        return (String) get(_row, "PrintFile", _printFile);
    }

    public void setPrintFile(String printFile) {
        this._printFile = printFile;
    }


    private Integer _printType;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("PrintType")
    public Integer getPrintType() {
        return (Integer) get(_row, "PrintType", _printType);
    }

    public void setPrintType(Integer printType) {
        this._printType = printType;
    }

    private String _printSql;

    @JsonIgnore
    @JsonProperty("PrintSql")
    public String getPrintSql() {
        return (String) get(_row, "PrintSQL", _printSql);
    }

    public void setPrintSql(String printSql) {
        this._printSql = printSql;
    }


    private String _printSql2;

    @JsonIgnore
    public String getPrintSql2() {
        return (String) get(_row, "PrintSQL2", _printSql2);
    }

    public void setPrintSql2(String printSql2) {
        this._printSql2 = printSql2;
    }


    private String _printSql3;

    @JsonIgnore
    public String getPrintSql3() {
        return (String) get(_row, "PrintSQL3", _printSql3);
    }

    public void setPrintSql3(String printSql3) {
        this._printSql3 = printSql3;
    }


    private Integer _billFlag;

    @JsonProperty("BillFlag")
    public Integer getBillFlag() {
        if (_billFlag == null) {
            _billFlag = (Integer) get(_row, "BillFlag", 0);
        }
        return _billFlag;
    }

    public void setBillFlag(Integer billFlag) {
        this._billFlag = billFlag;
    }


    @JsonProperty("RedBill")
    public Integer getRedBill() {
        return NativeExtensionUtils.parseInt(get(_row, "redbill", 0) + "");
    }


    private Boolean _emptyDetailFlag;

    @JsonIgnore
    public Boolean getEmptyDetailSaveAble() {
        return toBoolean(get(_row, "EmptyDetailFlag", _emptyDetailFlag, false));
    }

    public void setEmptyDetailSaveAble(Boolean emptyDetailSaveAble) {
        this._emptyDetailFlag = emptyDetailSaveAble;
    }

    private String _detailPrefix;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("DetailPrefix")
    public String getDetailPrefix() {
        String prefix = (String) get(_row, "detailprefix", _detailPrefix);
        if (prefix != null && !prefix.isEmpty()) {
            _detailPrefix = prefix.toLowerCase();
        }
        return _detailPrefix;
    }

    public void setDetailPrefix(String detailPrefix) {
        this._detailPrefix = detailPrefix;
    }

    private String _billSeq;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("BillSeq")
    public String getBillSeq() {
        return (String) get(_row, "billseq", _billSeq);
    }

    public void setBillSeq(String billSeq) {
        this._billSeq = billSeq;
    }

    private String _typeCode;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("TypeCode")
    public String getTypeCode() {
        return (String) get(_row, "MenuCode", _typeCode);
    }

    public void setTypeCode(String typeCode) {
        this._typeCode = typeCode;
    }


    private Boolean _emptyRow;

    @JsonProperty("EmptyRow")
    public Boolean getEmptyRow() {
        if (_emptyRow == null) {
            _emptyRow = toBoolean(get(_row, "addEmptyRow", _emptyRow, false));
        }
        return _emptyRow;
    }

    public void setEmptyRow(Boolean emptyRow) {
        this._emptyRow = emptyRow;
    }


    @JsonProperty("CopyAble")
    public Boolean getCopyAble() {
        return getAddCopyAble();
    }

    private String _printCond;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("PrintCond")
    public String getPrintCond() {
        return (String) get(_row, "PrintCond", _printCond);
    }

    public void setPrintCond(String printCond) {
        this._printCond = printCond;
    }

    @JsonIgnore
    public int Rtagid;

    public Integer _comfirmFlag;

    @JsonIgnore
    public Integer getComfirmFlag() {
        if (_comfirmFlag == null) {
            _comfirmFlag = (Integer) get(_row, "ComfirmFlag", null);
        }
        return _comfirmFlag;
    }

    public void setComfirmFlag(Integer comfirmFlag) {
        this._comfirmFlag = comfirmFlag;
    }

    private String _popupUnionCode;

    @JsonProperty("PopupUnionCode")
    public String getPopupUnionCode() {
        return (String) get(_row, "PopupUnionCode", _popupUnionCode, null);
    }

    public void setPopupUnionCode(String popupUnionCode) {
        this._popupUnionCode = popupUnionCode;
    }

    @JsonProperty("PopupFields")
    public List<Map<String, Object>> PopupFields;

}
