CityService.java 1.9 KB
package com.viontech.service.impl;

import com.viontech.model.City;
import com.viontech.model.Mall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhaibolin
 */
@Service
public class CityService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String SQL_SELECT_MALL = "select name,status,province_id,city_id from b_mall";
    private String SQL_SELECT_CITY = "select area_name as cityName,province_id,weather_code,city_id from b_city where city_id in (:cityIds)";
    private String SQL_SELECT_WEATHER = "select city_id from d_weather where data_date=?";

    public List<Mall> selectMallList() {
        List<Mall> mallList = jdbcTemplate.query(SQL_SELECT_MALL, new BeanPropertyRowMapper<>(Mall.class));
        return mallList;
    }

    public List<City> selectCityList(List<Mall> mallList) {
        if (mallList.isEmpty()) {
            System.out.println("获取不到商场列表");
            return Collections.emptyList();
        }
        List<Integer> cityIdList = mallList.stream().filter(x -> x.getCity_id() != null).map(Mall::getCity_id).collect(Collectors.toList());
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("cityIds", cityIdList);
        return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class));
    }

    public List<Integer> selectTodayWeather(Date date) {
        List<Integer> cityIdList = jdbcTemplate.queryForList(SQL_SELECT_WEATHER, new Object[]{date}, Integer.class);
        return cityIdList;
    }

}