GAT專案新需求:ETC管理增加查詢統計

ZHOU_VIP發表於2017-04-21

需求:按車牌號HOST_NO分組合計CAR_PAY金額,最後合計所有金額,還要過濾出本月的時間


因為要顯示明細,不是真的group by分組,而是按車牌號排序

消費合計和合計則是這種格式,前臺需要這樣的格式方便展示:




{
    "retCode":1,
    "retMsg":"操作成功",
    "list":[
        {
            "recordId":"20161201200445671015895",
            "carEtcNo":"測試0002",
            "carHostNO":"蘇AM10001",
            "carStartEtcTime":"2017-04-01 17:43:50",
            "carEndEtcTime":"2017-04-18 20:05:44",
            "carLine":"三牌樓--古林公園111",
            "carPay":10,
            "carProvince":"320000",
            "carProvinceDesc":"江蘇省"
        },
        {
            "carPay":10,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161129180514209018381",
            "carEtcNo":"1333333333",
            "carHostNO":"蘇A2002",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":66,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20161129180325044015371",
            "carEtcNo":"1333333333",
            "carHostNO":"蘇A2002",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":66,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20161129175757881014238",
            "carEtcNo":"1333333333",
            "carHostNO":"蘇A2002",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":14,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":146,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161129180513905010007",
            "carEtcNo":"1222222222",
            "carHostNO":"蘇A2001",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":88,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20161129180309437011679",
            "carEtcNo":"1222222222",
            "carHostNO":"蘇A2001",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":55,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":143,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20170323192547109019398",
            "carEtcNo":"ee94",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-01 12:12:12",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":16,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20170323192547121011329",
            "carEtcNo":"ee96",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-03 12:12:12",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":150,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20170323192547128013962",
            "carEtcNo":"ee97",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-02 12:12:12",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":100,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20170323192547134010071",
            "carEtcNo":"ee98",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-05 12:12:12",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":100,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20170323192547140018430",
            "carEtcNo":"ee99",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-20 12:12:12",
            "carEndEtcTime":"2017-04-25 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":100,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20170323192547147016616",
            "carEtcNo":"ee100",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-01 12:12:12",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":100,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20170323192547115010841",
            "carEtcNo":"ee95",
            "carHostNO":"人a1002",
            "carStartEtcTime":"2017-04-01 12:12:12",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":100,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":666,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161202101559495018075",
            "carEtcNo":"333",
            "carHostNO":"2900",
            "carStartEtcTime":"2017-04-01 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":44,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":44,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161130111132840016135",
            "carEtcNo":"2656",
            "carHostNO":"15000",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":15,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":15,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161202110607795014435",
            "carEtcNo":"003",
            "carHostNO":"14000",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":42,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":42,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161130183025739012483",
            "carEtcNo":"2",
            "carHostNO":"1301",
            "carStartEtcTime":"2017-04-01 17:43:50",
            "carEndEtcTime":"2017-04-23 18:47:04",
            "carLine":"中山門到漢中門",
            "carPay":87,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":87,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20170221091233259012244",
            "carEtcNo":"112233",
            "carHostNO":"1010",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-23 18:47:04",
            "carLine":"中山門到漢中門",
            "carPay":25,
            "carProvince":"230000",
            "carProvinceDesc":"黑龍江省"
        },
        {
            "carPay":25,
            "carProvinceDesc":"消費合計"
        },
        {
            "recordId":"20161202110501256014722",
            "carEtcNo":"3",
            "carHostNO":"0213",
            "carStartEtcTime":"2017-04-01 17:43:50",
            "carEndEtcTime":"2017-04-23 18:47:04",
            "carLine":"中山門到漢中門",
            "carPay":10,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":10,
            "carProvinceDesc":"消費合計"
        },
        {
            "carEtcNo":"合計",
            "carPay":1188
        }
    ]
}


資料庫查出來的依然是明細:


只不過是在程式碼中拼出消費合計和合計的列

-------------------------------------------------------------------------------------------------------------------------------

下面看程式碼:



關鍵是要動態增加索引,這樣才可以在每個車牌號下增加消費合計的一列



@SuppressWarnings("unchecked")
@Override
public ListResp queryCarUseEtcListTotal(CarUseEtcListReq req) throws Exception{
    ListResp resp = new ListResp();
    List<CarUseEtcListResp> list = new ArrayList<CarUseEtcListResp>();
    
    long begin = System.currentTimeMillis();
    
    list = carUseEtcDAO.queryCarUseEtcListTotal(req);
    
    //儲存有序的車牌號
    List<String> listTemp = new ArrayList<String>();
    
    //儲存車牌號,隨著list中的車牌號索引的增加而增加
    List<String> noList = new ArrayList<String>();
    if(Util.isNotEmpty(list)){
        for(int i=0;i<list.size();i++){  
            if(!listTemp.contains(list.get(i).getCarHostNO())){  
                listTemp.add(list.get(i).getCarHostNO());  
            }  
            noList.add(list.get(i).getCarHostNO());
        }  
    }
    //總的合計
    Double total = 0.0;
    if(Util.isNotEmpty(listTemp)){
        for (String cardNo : listTemp) {
            //相同車牌號的消費合計
            Double num = 0.0;
            for(int i=0;i<list.size();i++){
                CarUseEtcListResp one = list.get(i);
                if(Util.isNotEmpty(one) && Util.isNotEmpty(one.getCarHostNO())){
                    if(one.getCarHostNO().equals(cardNo)){
                        num += one.getCarPay();
                    }
                }
            }
            CarUseEtcListResp beanC = new CarUseEtcListResp();
            //得到noList集合中車牌號的索引
            Integer a = noList.lastIndexOf(cardNo);
            //消費合計的金額
            beanC.setCarPay(num);
            //給前臺的文字"消費合計",頁面放在省份列表下了
            //beanC.setCarProvince("消費合計");
            beanC.setCarProvinceDesc("消費合計");
            //在需要的索引下增加一列
            list.add(a+1, beanC);
            //noList集合也增加索引
            noList.add(a+1,beanC.getCarHostNO());
            
            //總的合計
            total += num;
            
        }
    }
    
    
    CarUseEtcListResp bean = new CarUseEtcListResp();
    //給前臺的文字"合計",頁面放在ETC卡號列表下了
    bean.setCarEtcNo("合計");
    //合計的金額
    bean.setCarPay(total);
    list.add(list.size(),bean);
    
    //System.out.println(sumCarPay + "," + total);
    boolean isDebug = log.isDebugEnabled();
    if(isDebug) {
        log.debug("資料庫查詢耗時 " + (System.currentTimeMillis() - begin) + " ms");
    }
    
    if(!Util.isEmpty(list)){
        begin = System.currentTimeMillis();
        DictionaryUtil.translate(list);
        resp.getList().addAll(list);
        if(isDebug) {
            log.debug("結果集轉換耗時 " + (System.currentTimeMillis() - begin) + " ms");
        }
    }
    return resp;
    
}



時間引數沒傳值,預設查當月的:


時間引數傳值:



引數都傳值:




{
    "retCode":1,
    "retMsg":"操作成功",
    "list":[
        {
            "recordId":"20161129180514209018381",
            "carEtcNo":"1333333333",
            "carHostNO":"蘇A2002",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":66,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20161129180325044015371",
            "carEtcNo":"1333333333",
            "carHostNO":"蘇A2002",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":66,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "recordId":"20161129175757881014238",
            "carEtcNo":"1333333333",
            "carHostNO":"蘇A2002",
            "carStartEtcTime":"2017-04-02 17:43:50",
            "carEndEtcTime":"2017-04-15 12:12:12",
            "carLine":"中山門到漢中門",
            "carPay":14,
            "carProvince":"120000",
            "carProvinceDesc":"天津市"
        },
        {
            "carPay":146,
            "carProvinceDesc":"消費合計"
        },
        {
            "carEtcNo":"合計",
            "carPay":146
        }
    ]
}

開發完成後的頁面效果圖:





相關文章