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

package nc.impl.pp.supplierprice.rule;

import java.util.ArrayList;
import java.util.List;
import nc.vo.pp.report.entity.PurpReportTransMap;
import nc.vo.pp.report.util.PurpRptUtils;
import nc.vo.pp.supplierprice.entity.SupPriceRptConst;
import nc.vo.pp.util.StringUtils;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.query.ConditionVO;
import nc.vo.pubapp.AppContext;
import nc.vo.scmpub.util.ArrayUtil;
import nc.vo.util.CloneUtil;

/**
 * 最新价格查询
 * 2005适配2312
 *
 * @author mzr
 * @date 2025/04/16
 */
public class SupPriceQryRule {
    public SupPriceQryRule() {
    }

    public String getWhereCtForHis(PurpReportTransMap tranMap, PurpRptUtils rptutils, String htablealias, String btablealias) {
        ConditionVO[] convos = this.getInitQryCondVOs(tranMap, rptutils);
        if (ArrayUtil.isEmpty(convos)) {
            return "";
        } else {
            ConditionVO[] copyconvos = (ConditionVO[])CloneUtil.deepClone(convos);
            List<ConditionVO> list = new ArrayList();

            for(ConditionVO vo : copyconvos) {
                if (vo.getFieldCode().equals("pk_supplier")) {
                    vo.setFieldCode(htablealias + ".cvendorid");
                    list.add(vo);
                } else if (vo.getFieldCode().equals("tcreatetime")) {
                    if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                        UFDate date = new UFDate(vo.getValue());
                        vo.setValue(date.toLocalString());
                    }

                    vo.setFieldCode(htablealias + ".creationtime");
                    list.add(vo);
                } else if (!vo.getFieldCode().equals("fpricesrctype")) {
                    if (vo.getFieldCode().equals("dvaliddate")) {
                        if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                            UFDate date = new UFDate(vo.getValue());
                            vo.setValue(date.toLocalString());
                        }

                        vo.setFieldCode(htablealias + ".dbilldate");
                        list.add(vo);
                    } else if (vo.getFieldCode().equals("dinvaliddate")) {
                        if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                            UFDate date = new UFDate(vo.getValue());
                            vo.setValue(date.toLocalString());
                        }
                    } else {
                        vo.setFieldCode(htablealias + "." + vo.getFieldCode());
                        list.add(vo);
                    }
                }
            }

            StringBuilder sql = new StringBuilder("");
            sql.append((new ConditionVO()).getSQLStr((ConditionVO[])list.toArray(new ConditionVO[list.size()])));
            this.addMarWhere(sql, tranMap, rptutils, btablealias);
            return sql.toString();
        }
    }

    public String getWhereCtForNew(PurpReportTransMap tranMap, PurpRptUtils rptutils, String htablealias, String btablealias) {
        ConditionVO[] convos = this.getInitQryCondVOs(tranMap, rptutils);
        if (ArrayUtil.isEmpty(convos)) {
            return "";
        } else {
            ConditionVO[] copyconvos = (ConditionVO[])CloneUtil.deepClone(convos);
            List<ConditionVO> list = new ArrayList();
            for(ConditionVO vo : copyconvos) {
                if (vo.getFieldCode().equals(SupPriceRptConst.DBUSINESSDATE)) {
                    int month = Integer.valueOf(vo.getValue());
                    UFDate curdate = AppContext.getInstance().getBusiDate();
                    UFDate befdate = curdate.getDateBefore(month * 30);
                    vo.setFieldCode(htablealias + ".dbilldate");
                    vo.setOperaCode("between");
                    vo.setDataType(3);
                    String befdateStr = befdate.toString();
                    vo.setValue(befdateStr + " ," + curdate.toString() + " ");
                } else if (vo.getFieldCode().equals("pk_supplier")) {
                    vo.setFieldCode(htablealias + ".cvendorid");
                } else if (vo.getFieldCode().equalsIgnoreCase("memo")) {
                    tranMap.put("memo", vo);
                } else {
                    vo.setFieldCode(htablealias + "." + vo.getFieldCode());
                }
                if (!vo.getFieldCode().equalsIgnoreCase("memo")) {
                    list.add(vo);
                }
            }

            StringBuilder sql = new StringBuilder("");
            ConditionVO[] array = list.toArray(new ConditionVO[0]);
            sql.append((new ConditionVO()).getSQLStr(array));
            this.addMarWhere(sql, tranMap, rptutils, btablealias);
            return sql.toString();
        }
    }

    public String getWhereOrderForHis(PurpReportTransMap tranMap, PurpRptUtils rptutils, String htablealias, String btablealias) {
        List<ConditionVO> list = new ArrayList();
        ConditionVO[] convos = this.getInitQryCondVOs(tranMap, rptutils);
        if (ArrayUtil.isEmpty(convos)) {
            return "";
        } else {
            ConditionVO[] convosCt = (ConditionVO[])CloneUtil.deepClone(convos);

            for(ConditionVO vo : convosCt) {
                if (!vo.getFieldCode().equals("bsc") && !vo.getFieldCode().equals("fpricesrctype")) {
                    if (vo.getFieldCode().equals("dinvaliddate")) {
                        if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                            UFDate date = new UFDate(vo.getValue());
                            vo.setValue(date.toLocalString());
                        }
                    } else if (vo.getFieldCode().equals("dvaliddate")) {
                        if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                            UFDate date = new UFDate(vo.getValue());
                            vo.setValue(date.toLocalString());
                        }
                    } else if (vo.getFieldCode().equals("tcreatetime")) {
                        vo.setFieldCode(htablealias + ".creationtime");
                        if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                            UFDate date = new UFDate(vo.getValue());
                            vo.setValue(date.toLocalString());
                        }

                        list.add(vo);
                    } else {
                        vo.setFieldCode(htablealias + "." + vo.getFieldCode());
                        list.add(vo);
                    }
                }
            }

            StringBuilder sql = new StringBuilder("");
            sql.append((new ConditionVO()).getSQLStr((ConditionVO[])list.toArray(new ConditionVO[list.size()])));
            this.addMarWhere(sql, tranMap, rptutils, btablealias);
            return sql.toString();
        }
    }

    public String getWhereOrderForNew(PurpReportTransMap tranMap, PurpRptUtils rptutils, String htablealias, String btablealias) {
        List<ConditionVO> list = new ArrayList();
        ConditionVO[] convos = this.getInitQryCondVOs(tranMap, rptutils);
        ConditionVO[] convosCt = (ConditionVO[])CloneUtil.deepClone(convos);
        if (convosCt != null && convosCt.length > 0) {
            for(ConditionVO vo : convosCt) {
                if (vo.getFieldCode().equals(SupPriceRptConst.DBUSINESSDATE)) {
                    int month = Integer.valueOf(vo.getValue());
                    UFDate curdate = AppContext.getInstance().getBusiDate();
                    UFDate befdate = curdate.getDateBefore(month * 30);
                    vo.setFieldCode(htablealias + ".dbilldate");
                    vo.setOperaCode("between");
                    vo.setDataType(3);
                    String befdateStr = befdate.toString();
                    vo.setValue(befdateStr + " ," + curdate.toString() + " ");
                    list.add(vo);
            } else if (vo.getFieldCode().equalsIgnoreCase("memo")) {
                tranMap.put("memo", vo);
                } else if (!vo.getFieldCode().equals("bsc")) {
                    vo.setFieldCode(htablealias + "." + vo.getFieldCode());
                    list.add(vo);
                }
            }
        }

        StringBuilder sql = new StringBuilder("");
        sql.append((new ConditionVO()).getSQLStr((ConditionVO[])list.toArray(new ConditionVO[list.size()])));
        this.addMarWhere(sql, tranMap, rptutils, btablealias);
        return sql.toString();
    }

    public String getWhereSupPrice(PurpReportTransMap tranMap, PurpRptUtils rptutils, String tablealias) {
        ConditionVO[] convos = this.getInitQryCondVOs(tranMap, rptutils);
        if (ArrayUtil.isEmpty(convos)) {
            return "";
        } else {
            ConditionVO[] copyconvos = (ConditionVO[])CloneUtil.deepClone(convos);
            List<ConditionVO> list = new ArrayList();
            for(ConditionVO vo : copyconvos) {
                if (vo.getFieldCode().equals(SupPriceRptConst.DBUSINESSDATE)) {
                    int month = Integer.valueOf(vo.getValue());
                    UFDate curdate = AppContext.getInstance().getBusiDate();
                    UFDate befdate = curdate.getDateBefore(month * 30);
                    vo.setFieldCode(tablealias + ".dvaliddate");
                    vo.setOperaCode("between");
                    vo.setDataType(3);
                    String befdateStr = befdate.toString();
                    vo.setValue(befdateStr + " ," + curdate.toString() + " ");
                } else if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                    UFDate date = new UFDate(vo.getValue());
                    vo.setValue(date.toLocalString());
                    vo.setFieldCode(tablealias + "." + vo.getFieldCode());
                } else if (vo.getFieldCode().equalsIgnoreCase("memo")) {
                    tranMap.put("memo", vo);
                } else {
                    vo.setFieldCode(tablealias + "." + vo.getFieldCode());
                }
                if (!vo.getFieldCode().equalsIgnoreCase("memo")) {
                    list.add(vo);
                }
            }

            StringBuilder sql = new StringBuilder("");
            ConditionVO[] array = list.toArray(new ConditionVO[0]);
            sql.append((new ConditionVO()).getSQLStr(array));
            this.addMarWhere(sql, tranMap, rptutils, tablealias);
            return sql.toString();
        }
    }

    public String getWhereSupPriceNew(PurpReportTransMap tranMap, PurpRptUtils rptutils, String tablealias) {
        ConditionVO[] convos = this.getInitQryCondVOs(tranMap, rptutils);
        if (ArrayUtil.isEmpty(convos)) {
            return "";
        } else {
            ConditionVO[] copyconvos = (ConditionVO[])CloneUtil.deepClone(convos);
            List<ConditionVO> list = new ArrayList();

            for(ConditionVO vo : copyconvos) {
                if (vo.getFieldCode().equals(SupPriceRptConst.DBUSINESSDATE)) {
                    int month = Integer.valueOf(vo.getValue());
                    UFDate curdate = AppContext.getInstance().getBusiDate();
                    UFDate befdate = curdate.getDateBefore(month * 30);
                    vo.setFieldCode(tablealias + ".dinvaliddate");
                    vo.setOperaCode("between");
                    vo.setDataType(3);
                    String var10001 = befdate.toString();
                    vo.setValue(var10001 + " ," + curdate.toString() + " ");
                    ConditionVO newvo = new ConditionVO();
                    newvo.setLogic(false);
                    newvo.setFieldCode(tablealias + ".dvaliddate");
                    newvo.setOperaCode("between");
                    newvo.setDataType(3);
                    var10001 = befdate.toString();
                    newvo.setValue(var10001 + " ," + curdate.toString() + " ");
                    list.add(vo);
                    list.add(newvo);
                } else if (vo.getDataType() == 3 && vo.getOperaCode().equalsIgnoreCase("=")) {
                    UFDate date = new UFDate(vo.getValue());
                    vo.setValue(date.toLocalString());
                    vo.setFieldCode(tablealias + "." + vo.getFieldCode());
                    list.add(vo);
                } else {
                    vo.setFieldCode(tablealias + "." + vo.getFieldCode());
                    list.add(vo);
                }
            }

            StringBuilder sql = new StringBuilder("");
            sql.append((new ConditionVO()).getSQLStr((ConditionVO[])list.toArray(new ConditionVO[0])));
            this.addMarWhere(sql, tranMap, rptutils, tablealias);
            return sql.toString();
        }
    }

    private void addMarWhere(StringBuilder sql, PurpReportTransMap tranMap, PurpRptUtils rptutils, String btablealias) {
        String[] marQryKeys = new String[]{SupPriceRptConst.MARBASCLASS, SupPriceRptConst.MATERIALCODE, SupPriceRptConst.MARTERIALNAME};
        String marwhere = rptutils.getWhereForMar(tranMap, btablealias, marQryKeys);
        if (!StringUtils.isEmpty(marwhere)) {
            sql.append(" and " + marwhere);
        }

    }

    private ConditionVO[] getInitQryCondVOs(PurpReportTransMap tranMap, PurpRptUtils rptutils) {
        ConditionVO[] generalvos = tranMap.getConditionVOs(SupPriceRptConst.SUPPRICE_GENERALCOND);
        ConditionVO[] logicalvos = tranMap.getConditionVOs(SupPriceRptConst.SUPPRICE_LOGICALCOND);
        return rptutils.combineCondVOs(generalvos, logicalvos);
    }
}
