Ticket #130: 报价库排行榜拼sql的方法.txt

File 报价库排行榜拼sql的方法.txt, 12.1 KB (added by chenchongqi, 13 years ago)
Line 
1 /**
2     * ÕâÊÇеĻñÈ¡TOP PRODUCTSµÄÁбí 2008-09-28 MODIFY BY CANNYMO
3     * @param countForm
4     * @param properties -ÊôÐÔÏîµÄKEY £¬ÊôÐÔ·ÖÀàÅÅÐÐ,ŒÛžñ¡¢Æ·ÅƵØÇøÒ²¿Ž×÷ÊÇÒ»žöÊôÐÔ
5     * @param values -ÊôÐÔÏîµÄÖµ
6     * @return
7     * @throws AppException
8     */
9    public List searchTopProducts(VisitCountForm countForm,List properties,Map values) throws AppException {
10        if (countForm == null) {
11            throw new AppException("Null parameter is found in searchProductVisitCount function !");
12        }
13        List list = null;
14        int brandId = countForm.getBrandList();
15        int smalltypeId = countForm.getSmallList();
16        String smallTypes = countForm.getSmallTypes();
17        int recPerPage = countForm.getPageSize();
18        if (recPerPage <= 0) {
19            recPerPage = PageUtil.getPageLines();
20        }
21        initCountData(null);
22        int ipicType = countForm.getPicType();
23        int iartpicCount = countForm.getArtpicCount();//͌ƬÊý;
24        boolean isAreaprice = false; //ÊÇ·ñ°ŽµØÇø±šŒÛ
25        boolean isProperty = false; //ÊÇ·ñ°ŽÊôÐÔÏî
26        boolean hasProperty = false;
27        StringBuffer propertyFilter = new StringBuffer();
28        for(int i=0;properties!=null && values!=null && i<properties.size();i++){
29                String property = (String)properties.get(i);
30                if(i == 0)isAreaprice = property.equals("ŒÛžñ");
31                List pValues = (List)values.get(property);
32                String tmp = buildProperty(smalltypeId,property,pValues);
33            if (tmp.length()>0 && hasProperty) {
34                propertyFilter.append(" INTERSECT ");
35                propertyFilter.append(tmp);
36            } else if (tmp.length()>0) {
37                propertyFilter.append(tmp);
38                hasProperty = true;
39            }
40        }
41        isProperty=propertyFilter.length()>0;
42        boolean isRise=countForm.isRise(); //true:ÉÏÉý×î¿ìµÄ
43       
44        //added by huoqing begin
45        boolean isQg = false;
46       
47        List priceValues = values!=null?(List)values.get("ŒÛžñ"):null;
48        if(priceValues!=null){
49                String areaId = (String)priceValues.get(0);
50                if(areaId.equals( "100" )){
51                        isQg=true;
52                }
53        }
54        //added by huoqing end
55       
56        String[] skipBrandIdArr = countForm.getSkipBrandIdArr();
57        String noBrandSql = null;
58        Map skipBrandMap = null;
59        if(skipBrandIdArr != null) {
60                noBrandSql = " and a.type_id not in(";
61                skipBrandMap = new HashMap();
62                for(int i = 0, l = skipBrandIdArr.length; i < l; i++) {
63                        if(i != 0) {
64                                noBrandSql += ",";
65                        }
66                        noBrandSql += ":brandid"+i;
67                        skipBrandMap.put("brandid"+i, skipBrandIdArr[i]);
68                }
69                noBrandSql += ") ";
70        }
71        try {
72            String stmt = "select * from ( "
73                        + "select f.name as variance_name, a.*,(a.last_count_order-a.count_order) as rise_diff "
74                        + ",b.name as t_name,b.parent_id as t_parent_id,b.forum_dir,b.forum_id as t_forum_id,b.pub_url as t_pub_url,b.pub_dir "
75                        + (ipicType >= 0 ? " ,c.pic_path " : " ")
76                        + (isAreaprice&&!isQg ? " ,d.price as area_price " : " ")
77                        + (isAreaprice&&isQg ? " ,a.price as area_price " : " ")
78                        + (", nvl(e.min_price,0) as min_price, nvl(e.max_price,0) as  max_price")
79                        + " from PDL_PRODUCT a, PDL_PRODUCT_TYPE b "
80                        + (ipicType >= 0 ? " ,(select * from PDL_PRODUCT_PIC where type= :ipicType) c " : " ")
81                        + (isProperty&&!isQg?",("+propertyFilter+") d":" ")
82                        + (",eml_price e ")
83                        + (",pdl_product_variance f")
84                        //ÌíŒÓÌõŒþ and a.concept is null ·ÇžÅÄî²úÆ·
85                        + " where a.status!=0 and a.review_status<>4 and a.order_type<2 and a.concept is null and a.type_id=b.id and b.status=1 and b.type=3 "
86                        + (isAreaprice&&isQg ? " and a.price  BETWEEN :beginPrice AND :endPrice and a.price<>0 " : " ")
87                        + (isAreaprice&&!isQg ? " and d.price<>0 " : " ")
88                        + (isAreaprice ? " and a.hot_new not in(3,4) " : "")
89                        + " and b.parent_id" + (smallTypes!=null && smallTypes.trim().length()>0 ? " in ("+smallTypes+") " :"=:smalltypeId")
90                        + (brandId>0?" and a.type_id= :brandId":" ")
91                    + (iartpicCount>0 ? (" and a.artpic_count>= :iartpicCount") : " ")
92                    + (ipicType >= 0 ? " and c.product_id(+)=a.id " : " ")
93                    + (isProperty&&!isQg ? " and a.id=d.product_id " : " ")
94                    + (skipBrandIdArr != null ? noBrandSql : "")
95                    + (" and e.product_id(+)= a.id")
96                    + (" and a.variance_id=f.id(+) ")
97                    + " order by a.last_count_order asc"
98                    + ") where rownum <= :rownum"
99                    + (isRise ? " order by rise_diff asc " : " ")
100                    ;
101            if (isRise){
102                stmt="select * from ("+stmt+") where rownum<= :recPerPage";
103               
104            }
105            //System.out.println("stmt ===== " + stmt);
106            Map params = new HashMap();
107            params.put("ipicType", new Integer(ipicType));
108            params.put("smalltypeId", new Long(smalltypeId));
109            params.put("brandId", new Long(brandId));
110            params.put("iartpicCount", new Long(iartpicCount));
111            params.put("rownum", new Integer((isRise ? 100 : recPerPage)));
112            params.put("recPerPage", new Integer(recPerPage));
113           
114            if(isAreaprice&&!isQg){
115                params.put("areaId", (String)priceValues.get( 0 ));
116                params.put("beginPrice", (String)priceValues.get( 1 ));
117                params.put("endPrice", (String)priceValues.get( 2 ));
118            }
119            if(isAreaprice&&isQg){
120                params.put("beginPrice", (String)priceValues.get( 1 ));
121                params.put("endPrice", (String)priceValues.get( 2 ));
122            }
123           
124
125            if(skipBrandMap != null) {
126                params.putAll(skipBrandMap);
127            }
128            //System.out.println("stmt================"+stmt);
129            list = DBQueryUtils.psQuery(stmt, params);
130            //System.out.println("list=====111==========="+list.size());
131            //list = DBQueryUtils.psQuery(stmt, params);//
132            //System.out.println("list======2222=========="+list.size());
133           
134
135           
136           
137            //list = BasePeer.executeQuery(stmt);
138           
139            //¹Ì¶š²úÆ·id by wind.ldr
140            //¹Ì¶šÐÎÊœ 1_21345,3_232 Öž¶šÎ»Öù̶š
141            //¹Ì¶šÐÎÊœ 123,234 ˳Ðò¹Ì¶š
142            if(countForm.getFixedIds() != null && countForm.getFixedIds().length > 0){
143                int oldListSize = list.size();
144                boolean fixOrder = false;
145                for(int i=0;i<countForm.getFixedIds().length;i++){
146                        fixOrder = countForm.getFixedIds()[i].indexOf("_") != -1;
147                        if(fixOrder){
148                                break;
149                        }
150                }
151                int maxOrder = 0;
152                Map orderMap = new HashMap();
153                if(fixOrder){
154                        for(int i=0;i<countForm.getFixedIds().length;i++){
155                                String items[] = countForm.getFixedIds()[i].split("_");
156                                if(items.length != 2){
157                                        continue;
158                                }
159                                int forder = NumberUtils.stringToInt(items[0]);
160                                if(forder > maxOrder){
161                                        maxOrder = forder;
162                                }
163                                orderMap.put(items[0], String.valueOf(items[1]));
164                        }
165                }
166               
167               
168                int order = 1;
169                maxOrder = maxOrder < countForm.getFixedIds().length ? countForm.getFixedIds().length : maxOrder;
170                maxOrder = maxOrder < list.size() ? order : list.size();
171                int notFixedOrderCnt = 0;
172                for(int i=1;i<=maxOrder;i++){
173                        if(orderMap.get(String.valueOf(i)) == null){
174                                //ÕÒ³öÒ»žö·Ç¹Ì¶šÎ»ÖÃµÄ Ìî²¹œøÈ¥
175                                while(notFixedOrderCnt < countForm.getFixedIds().length){
176                                        if(countForm.getFixedIds()[notFixedOrderCnt].indexOf("_") == -1){
177                                                orderMap.put(String.valueOf(i), countForm.getFixedIds()[notFixedOrderCnt]);
178                                                break;
179                                        }
180                                       
181                                        notFixedOrderCnt ++;
182                                }
183                        }
184                }
185               
186                //È¥µôlistÖÐ µÄÔªËØorderMap
187                Collection orderIds = orderMap.values();
188                Map orderRecord = new HashMap();//pId -> Record
189                for(Iterator iter = list.iterator();iter.hasNext();){
190                        Record record = (Record)iter.next();
191                        if(orderIds.contains(record.getValue("ID").asString())){
192                                orderRecord.put(record.getValue("ID").asString(), record);
193                                iter.remove();
194                        }
195                }
196               
197                //²¹³äorderRecordÊýŸÝ
198                StringBuffer needQueryIds = new StringBuffer();
199                for(Iterator iter = orderIds.iterator();iter.hasNext();){
200                        String oId = (String)iter.next();
201                        if(!orderRecord.keySet().contains(oId)){
202                                if(needQueryIds.length() > 0){
203                                        needQueryIds.append(",");
204                                }
205                                needQueryIds.append(oId);
206                        }
207                }
208                if(needQueryIds.length() > 0){
209                        //ÓÐidÌõŒþ û±ØÒª×öÓÅ»¯ÁË
210                        stmt = "select * from ( "
211                                + "select f.name as variance_name, a.*,(a.last_count_order-a.count_order) as rise_diff "
212                                + ",b.name as t_name,b.parent_id as t_parent_id,b.forum_dir,b.forum_id as t_forum_id,b.pub_url as t_pub_url,b.pub_dir "
213                                + (ipicType >= 0 ? " ,c.pic_path " : " ")
214                                //+ (isAreaprice ? " ,d.price as area_price " : " ")
215                                + (", nvl(e.min_price,0) as min_price, nvl(e.max_price,0) as  max_price")
216                                + " from PDL_PRODUCT a, PDL_PRODUCT_TYPE b "
217                                + (ipicType >= 0 ? " ,(select * from PDL_PRODUCT_PIC where type= " + ipicType + ") c " : " ")
218                                //+ (isProperty?",("+propertyFilter+") d":" ")
219                                + (",eml_price e ")
220                                + (",pdl_product_variance f")
221                                + " where a.id in("+needQueryIds.toString()+")"//ŒÓÈë¶ÁÈ¡idÌõŒþ
222                                + " and a.status!=0 and a.review_status<>4 and a.order_type<2 and a.concept is null and a.type_id=b.id and b.status=1 and b.type=3 "
223                                + " and b.parent_id" + (smallTypes!=null && smallTypes.trim().length()>0?" in ("+smallTypes+") " :"="+smalltypeId)
224                                + (brandId>0?" and a.type_id="+brandId:" ")
225                                + (iartpicCount>0 ? (" and a.artpic_count>="+iartpicCount) : " ")
226                                + (ipicType >= 0 ? " and c.product_id(+)=a.id " : " ")
227                               // + (isProperty ? " and a.id=d.product_id " : " ")
228                                + (" and e.product_id(+)= a.id")
229                                + (" and a.variance_id=f.id(+) ")
230                                + ")";
231                        List needQueryList = BasePeer.executeQuery(stmt);
232                        for(int i=0;i<needQueryList.size();i++){
233                                Record record = (Record)needQueryList.get(i);
234                                orderRecord.put(record.getValue("ID").asString(), record);
235                        }
236                }
237               
238                //¶ÔÊýŸÝœøÐÐÕûºÏ
239                List newList = new ArrayList();
240                int listCnt = 0;
241                for(int i=1;i<=oldListSize;i++){
242                        String oId = (String)orderMap.get(String.valueOf(i));
243                        Object record = null;
244                        if(oId != null){
245                                record = orderRecord.get(oId);
246                                if(record != null){
247                                        newList.add(record);
248                                        continue;
249                                }
250                        }
251                        //Ìî³äĬÈϵÄ
252                        record = list.get(listCnt);
253                        newList.add(record);
254                        listCnt ++;
255                }
256                list = newList;
257            }
258           
259        } catch (Exception e) {
260            e.printStackTrace();
261            throw Db2AppError.TransException(e);
262        }
263        return list;
264    }