public void search(Page<CityProdEntity> page) {
// TODO Auto-generated method stub
String hql = "from PmsMobileCityProd order by pid";
List<PmsMobileCityProd> result = dao.find(hql);
// 处理
List<CityProdEntity> list = new ArrayList();
List<PmsMobileCity> city = null;
CityProdEntity cpe = null;
int tag = 0;
for (int i = 0; i < result.size(); i++) {
PmsMobileCityProd pmsMobileCityProd = result.get(i);
int pid = pmsMobileCityProd.getPmsMobileProduct().getId();
int next = 0;
PmsMobileCity pmc = pmsMobileCityProd.getPmsMobileCity();
// 无城市数据是单独算一条
if (pmc != null) {
// 比较下一个数据是否相同 最后一个跟上一个比较
PmsMobileCityProd flag = null;
// 标记是否是同一个产品不同的城市 产品不同时归0
if (tag == 0) {
city = new ArrayList<PmsMobileCity>();
}
tag++;
// 不是最后一个 跟下一个比较
if (i != result.size() - 1) {
flag = result.get(i + 1);
next = flag.getPmsMobileProduct().getId();
if (pid == next) {
city.add(pmc);
continue;
} else {
// 当下一个不同时 tag归0 保存数据
city.add(pmc);
}
} else {
// 处理最后一个 跟上一个比较
flag = result.get(i - 1);
next = flag.getPmsMobileProduct().getId();
// 相同 加入同一条数据
if (pid == next) {
city.add(pmc);
} else {
// 不同 新建一条数据
city = new ArrayList<PmsMobileCity>();
city.add(pmc);
}
}
}
cpe = new CityProdEntity();
cpe.setCreateDate(pmsMobileCityProd.getCreateDate());
cpe.setIsValid(pmsMobileCityProd.getIsValid());
cpe.setPmsMobileProduct(pmsMobileCityProd.getPmsMobileProduct());
cpe.setPmsMobileCity(city);
list.add(cpe);
tag = 0;
}
page.setAutoCount(true);
page.setTotalCount(list.size());
page.setResult(list);
}

解决方案 »

  1.   

    for (int i = 0; i < result.size(); i++) 
    {
            PmsMobileCityProd pmsMobileCityProd = result.get(i);
            int pid = pmsMobileCityProd.getPmsMobileProduct().getId();
            int next = 0;
            PmsMobileCity pmc = pmsMobileCityProd.getPmsMobileCity();
            // 无城市数据是单独算一条
            if (pmc != null) 
            {
                // 比较下一个数据是否相同 最后一个跟上一个比较
                PmsMobileCityProd flag = null;
                // 标记是否是同一个产品不同的城市 产品不同时归0
                if (tag == 0)
               {
                    city = new ArrayList<PmsMobileCity>();
                }
                tag++;
                // 不是最后一个 跟下一个比较
                if (i != result.size() - 1) 
                {
                    flag = result.get(i + 1);
                    next = flag.getPmsMobileProduct().getId();                if (pid == next) 
                   {
                        city.add(pmc);
                        continue;
                    } 
                   else
                   {
                        // 当下一个不同时 tag归0 保存数据
                        city.add(pmc);
                    }
                }            else {
                    // 处理最后一个 跟上一个比较
                    flag = result.get(i - 1);
                    next = flag.getPmsMobileProduct().getId();
                    // 相同 加入同一条数据                if (pid == next) 
                    {
                        city.add(pmc);
                    } 
                   else 
                   {
                        // 不同 新建一条数据
                        city = new ArrayList<PmsMobileCity>();
                        city.add(pmc);
                    }
                }
            }
            cpe = new CityProdEntity();
            cpe.setCreateDate(pmsMobileCityProd.getCreateDate());
            cpe.setIsValid(pmsMobileCityProd.getIsValid());
            cpe.setPmsMobileProduct(pmsMobileCityProd.getPmsMobileProduct());
            cpe.setPmsMobileCity(city);
            list.add(cpe);
            tag = 0;
        }
    代码结构如果这样写会不会好看点,你那样写我都晕了