//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.vo.pp.report.util;

import nc.bs.ic.icreport.pub.RPMetaDataUtil;
import nc.bs.pubapp.report.ReportPermissionUtils;
import nc.pub.smart.context.SmartContext;
import nc.pub.smart.data.DataSet;
import nc.pub.smart.metadata.MetaData;
import nc.vo.ml.MultiLangUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pp.report.entity.PurpReportTransMap;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.query.ConditionVO;
import nc.vo.pubapp.pattern.model.entity.view.AbstractDataView;
import nc.vo.pubapp.pattern.model.meta.entity.view.IDataViewMeta;
import nc.vo.scmbd.pub.SCMESAPI;
import nc.vo.scmpub.util.ArrayUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 最新价格查询
 * 2005适配2312
 *
 * @author mzr
 * @date 2025/04/16
 */
public class PurpRptUtils {
    public static final int UFDATEDATATIMETYPE = 8;
    public static final int UFDATEDATATYPE = 3;

    public PurpRptUtils() {
    }

    public ConditionVO[] combineCondVOs(ConditionVO[] generalvos, ConditionVO[] logicalvos) {
        ConditionVO[] endvos = null;
        if (!ArrayUtil.isEmpty(generalvos)) {
            endvos = generalvos;
        }

        if (!ArrayUtil.isEmpty(logicalvos) && ArrayUtil.isEmpty(endvos)) {
            endvos = logicalvos;
        } else if (!ArrayUtil.isEmpty(logicalvos) && endvos != null && endvos.length > 0) {
            System.arraycopy(logicalvos, 0, endvos, endvos.length, logicalvos.length);
        }

        return endvos;
    }

    public DataSet convertDataSet(IDataViewMeta md, AbstractDataView[] vos) {
        String[] fields = md.getAttributeNames();
        MetaData mtd = new MetaData();
        RPMetaDataUtil.addVOMetaFieldByKeys(mtd, md, fields);
        if (vos != null && vos.length > 0) {
            Object[][] datas = new Object[vos.length][fields.length];

            for (int i = 0; i < vos.length; ++i) {
                if (vos[i] != null) {
                    for (int k = 0; k < fields.length; ++k) {
                        datas[i][k] = vos[i].getAttributeValue(fields[k]);
                    }
                }
            }

            return new DataSet(mtd, datas);
        } else {
            DataSet ds = new DataSet();
            ds.setMetaData(mtd);
            return ds;
        }
    }

    public String getChnNo() {
        return NCLangRes4VoTransl.getNCLangRes().getStrByID("40050004", "1400500040031");
    }

    public String getChnYes() {
        return NCLangRes4VoTransl.getNCLangRes().getStrByID("40050004", "1400500040030");
    }

    public String getPermisionInnerJoin(SmartContext context, Class<? extends ISuperVO> marclass, String btablealias) {
        ReportPermissionUtils utils = new ReportPermissionUtils(context);
        Map<Class<? extends ISuperVO>, String> beanMap = new HashMap();
        beanMap.put(marclass, btablealias);
        return utils.getPermissionSQL(beanMap);
    }

    public String getWhereForMar(PurpReportTransMap tranMap, String btablealias, String[] marQryKeys) {
        String marclassKey = marQryKeys[0];
        String marcodeKey = marQryKeys[1];
        String marnameKey = marQryKeys[2];
        ConditionVO codeVO = this.getMarCondVO(tranMap, marcodeKey);
        ConditionVO nameVO = this.getMarCondVO(tranMap, marnameKey);
        ConditionVO classVO = this.getMarCondVO(tranMap, marclassKey);
        boolean flag = tranMap.get("memo") == null;
        if (null == codeVO && null == nameVO && null == classVO && flag) {
            return "";
        } else {
            StringBuilder wheresql = new StringBuilder();
            wheresql.append(btablealias + ".pk_material in ( select bd_material.pk_material from bd_material bd_material where bd_material.dr=0 ");
            if (codeVO != null) {
                wheresql.append(" and ");
                this.setMarWhere(codeVO, "bd_material.code", wheresql);
            }

            if (nameVO != null) {
                String langseq = MultiLangUtil.getCurrentLangSeqSuffix();
                wheresql.append(" and ");
                this.setMarWhere(nameVO, "bd_material.name" + langseq, wheresql);
            }

            if (classVO != null) {
                wheresql.append(" and ");
                this.setMarWhere(classVO, "bd_material.pk_marbasclass", wheresql);
            }
            if (!flag) {
                ConditionVO memoVo = (ConditionVO) tranMap.get("memo");
                wheresql.append(" and ");
                this.setMarWhere(memoVo, "bd_material.memo", wheresql);
            }

            wheresql.append(" ) ");
            return wheresql.toString();
        }
    }

    public ConditionVO updUFDateCondVO(ConditionVO condvo, String qryDlgWhereSql, String dateField) {
        if (!qryDlgWhereSql.contains(dateField)) {
            return null;
        } else {
            String[] conds = qryDlgWhereSql.split(dateField);
            StringBuilder valueBuilder = new StringBuilder();
            String first = conds[1].split("'")[1];
            if (condvo.getOperaCode().equalsIgnoreCase("between")) {
                if (condvo.getValue().toLowerCase().contains("isnull")) {
                    if (condvo.getValue().toLowerCase().startsWith("isnull")) {
                        valueBuilder.append(first);
                        condvo.setOperaCode("<=");
                    } else {
                        valueBuilder.append(first);
                        condvo.setOperaCode(">=");
                    }

                    condvo.setValue(valueBuilder.toString());
                    return condvo;
                }

                String end = conds[2].split("'")[1];
                valueBuilder.append(first);
                valueBuilder.append(",");
                valueBuilder.append(end);
            } else {
                valueBuilder.append(first);
            }

            condvo.setValue(valueBuilder.toString());
            return condvo;
        }
    }

    private ConditionVO getMarCondVO(PurpReportTransMap tranMap, String key) {
        ConditionVO[] tempvos = tranMap.getConditionVOs(key);
        return ArrayUtil.isEmpty(tempvos) ? null : tempvos[0];
    }

    private void setMarWhere(ConditionVO condvo, String field, StringBuilder wheresql) {
        if (condvo.getOperaCode().equals("in")) {
            wheresql.append(field + " in " + condvo.getValue());
        } else if (condvo.getOperaCode().equals("=")) {
            wheresql.append(field + " = '" + SCMESAPI.sqlEncodeGeneral(condvo.getValue()) + "' ");
        } else if (condvo.getOperaCode().equals("left like")) {
            String value = condvo.getValue().replaceAll("\\(", "").replaceAll("\\)", "");
            wheresql.append(field + " like '" + SCMESAPI.sqlEncodeGeneral(value) + "%' ");
        } else if (condvo.getOperaCode().equals("like")) {
            String value = condvo.getValue().replaceAll("\\(", "").replaceAll("\\)", "");
            wheresql.append(field + " like '%" + SCMESAPI.sqlEncodeGeneral(value) + "%' ");
        }

    }
}
