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

package nc.bs.ssctp.sscbd.tasktype.query.convertor;

import nc.bs.ssctp.sscbd.tasktype.config.TaskTypeConfigFactory;
import nc.bs.ssctp.sscbd.tasktype.query.SSCTaskQueryCondition;

import java.util.ArrayList;
import java.util.List;

public class BlurConvertor implements ITaskQueryConditionConvertor {
    public BlurConvertor() {
    }

    public String getWherePart(SSCTaskQueryCondition condition) {
        StringBuilder whereSql = new StringBuilder();
        whereSql.append(" ");
        if (TaskTypeConfigFactory.QUERYCONDITION_BLUR.equals(condition.getQueryField())) {
            String blur = condition.getQueryFieldValue();
            if (blur == null || "".equals(blur)) {
                return "";
            }

            String[] blurs = blur.split(",");
            List<String> vas = new ArrayList();

            for(String str : blurs) {
                vas.add(str);
            }

            if (vas != null && vas.size() > 0) {
                for(String s : vas) {
                    whereSql.append(" and ( NODETASKS.busiid in ( ");
                    if (s.contains("=")) {
                        String[] strs = s.split("=");
                        whereSql.append(" SELECT a.busiid FROM sscrp_bill a,sscrp_detail b WHERE a.pk_bill = b.pk_bill AND CODE = '" + strs[0] + "' AND VALUE like '%" + strs[1].replaceAll("'", "") + "%'  ");
                        whereSql.append(" UNION ALL ");
                        whereSql.append(" SELECT a.busiid FROM sscrp_bill_done a,sscrp_detail_done b WHERE a.pk_bill = b.pk_bill AND CODE = '" + strs[0] + "' AND VALUE like '%" + strs[1].replaceAll("'", "") + "%'  ");
                    } else {
                        whereSql.append(" SELECT a.busiid FROM sscrp_bill a,sscrp_detail b WHERE a.pk_bill = b.pk_bill AND VALUE like '%" + s.replaceAll("'", "") + "%'  ");
                        whereSql.append(" UNION ALL ");
                        whereSql.append(" SELECT a.busiid FROM sscrp_bill_done a,sscrp_detail_done b WHERE a.pk_bill = b.pk_bill AND VALUE like '%" + s.replaceAll("'", "") + "%'  ");
                    }

                    whereSql.append(" ) ");


                    // 新增四个EXISTS条件 //联查 付款单、收款单、主报销单、主借款单 四张单子
                    whereSql.append(" OR EXISTS (SELECT 1 FROM AR_GATHERITEM bill JOIN BD_BALATYPE bala ON bala.pk_balatype = bill.pk_balatype WHERE NODETASKS.busiid = bill.pk_gatherbill AND bala.name LIKE '%" + s.replaceAll("'", "") + "%')");
                    whereSql.append(" OR EXISTS (SELECT 1 FROM ap_paybill bill JOIN AP_PAYITEM item ON item.pk_paybill = bill.pk_paybill JOIN BD_BALATYPE bala ON bala.pk_balatype = item.pk_balatype WHERE NODETASKS.busiid = bill.pk_paybill AND bala.name LIKE '%" + s.replaceAll("'", "") + "%')");
                    whereSql.append(" OR EXISTS (SELECT 1 FROM er_bxzb bill JOIN BD_BALATYPE bala ON bala.pk_balatype = bill.jsfs WHERE NODETASKS.busiid = bill.pk_jkbx AND bala.name LIKE '%" + s.replaceAll("'", "") + "%')");
                    whereSql.append(" OR EXISTS (SELECT 1 FROM er_jkzb bill JOIN BD_BALATYPE bala ON bala.pk_balatype = bill.jsfs WHERE NODETASKS.busiid = bill.pk_jkbx AND bala.name LIKE '%" + s.replaceAll("'", "") + "%')");
                    whereSql.append(" ) ");
                }
            }
        }

        return whereSql.toString();
    }

    public String getWhere(String condition, Object blur) {
        StringBuilder whereSql = new StringBuilder();
        whereSql.append(condition);
        List<String> vas = new ArrayList();
        if (blur != null && !blur.toString().equals("")) {
            for(Object c : (List)blur) {
                vas.add((String)c);
            }
        }

        for(String str : vas) {
            vas.add(str);
        }

        if (vas != null && vas.size() > 0) {
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT TASK.busiid                 ");
            sql.append("   FROM SSCTP_TASK TASK                                                          ");
            sql.append("   LEFT JOIN SSCTP_CURRENTTASK CURTASK                                            ");
            sql.append("     ON CURTASK.PK_CURRENTTASK = TASK.PK_CURRENTTASK                             ");
            sql.append("   LEFT JOIN SSCTP_TASKEXTENDINFO EXTENDINFO                                     ");
            sql.append("     ON EXTENDINFO.PK_CURRENTTASK = TASK.PK_CURRENTTASK                          ");
            sql.append("  WHERE " + condition + " and TASK.dr=0 and (CURTASK.saga_frozen is null or CURTASK.saga_frozen <> 1) ");

            for(String s : vas) {
                whereSql.append(" and nodetasks.busiid in ( ");
                if (s.contains("=")) {
                    String[] strs = s.split("=");
                    whereSql.append(" SELECT a.busiid FROM sscrp_bill a,sscrp_detail b WHERE a.pk_bill = b.pk_bill AND CODE = '" + strs[0] + "' AND VALUE like '%" + strs[1].replaceAll("'", "") + "%' AND a.busiid IN (" + sql.toString() + ") ");
                    whereSql.append(" UNION ALL ");
                    whereSql.append(" SELECT a.busiid FROM sscrp_bill_done a,sscrp_detail_done b WHERE a.pk_bill = b.pk_bill AND CODE = '" + strs[0] + "' AND VALUE like '%" + strs[1].replaceAll("'", "") + "%' AND a.busiid IN (" + sql.toString() + ") ");
                } else {
                    String var10001 = s.replaceAll("'", "");
                    whereSql.append(" SELECT a.busiid FROM sscrp_bill a,sscrp_detail b WHERE a.pk_bill = b.pk_bill AND VALUE like '%" + var10001 + "%' AND a.busiid IN (" + sql.toString() + ") ");
                    whereSql.append(" UNION ALL ");
                    var10001 = s.replaceAll("'", "");
                    whereSql.append(" SELECT a.busiid FROM sscrp_bill_done a,sscrp_detail_done b WHERE a.pk_bill = b.pk_bill AND VALUE like '%" + var10001 + "%' AND a.busiid IN (" + sql.toString() + ") ");
                }

                whereSql.append(" ) ");
            }
        }

        return whereSql.toString();
    }

    public String getWhere4QM(String condition, Object blur) {
        StringBuilder whereSql = new StringBuilder();
        whereSql.append(condition);
        List<String> vas = new ArrayList();
        if (blur != null && !blur.toString().equals("")) {
            for(Object c : (List)blur) {
                vas.add((String)c);
            }
        }

        for(String str : vas) {
            vas.add(str);
        }

        if (vas != null && vas.size() > 0) {
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT TASK.busiid                 ");
            sql.append("   FROM SSCTP_TASK TASK                                                          ");
            sql.append("   LEFT JOIN SSCTP_CURRENTTASK CURTASK                                            ");
            sql.append("     ON CURTASK.PK_CURRENTTASK = TASK.PK_CURRENTTASK                             ");
            sql.append("   LEFT JOIN SSCTP_TASKEXTENDINFO EXTENDINFO                                     ");
            sql.append("     ON EXTENDINFO.PK_CURRENTTASK = TASK.PK_CURRENTTASK                          ");
            sql.append("  WHERE " + condition + " and TASK.dr=0 and (CURTASK.saga_frozen is null or CURTASK.saga_frozen <> 1) ");

            for(String s : vas) {
                whereSql.append(" and nodetasks.busiid in ( ");
                whereSql.append(" select id from sscqm_check_bill where pk_bill in ( ");
                String var10001 = s.replaceAll("'", "");
                whereSql.append(" SELECT a.busiid FROM sscrp_bill a,sscrp_detail b WHERE a.pk_bill = b.pk_bill AND VALUE like '%" + var10001 + "%' AND a.busiid IN (" + sql.toString() + ") ");
                whereSql.append(" UNION ALL ");
                var10001 = s.replaceAll("'", "");
                whereSql.append(" SELECT a.busiid FROM sscrp_bill_done a,sscrp_detail_done b WHERE a.pk_bill = b.pk_bill AND VALUE like '%" + var10001 + "%' AND a.busiid IN (" + sql.toString() + ") ");
                whereSql.append(" ) )");
            }
        }

        return whereSql.toString();
    }
}
