lizijie
2022-04-20 fcbb7cbe05b6cdcdf91b8599f064b85738810996
穿衣指数接口
1 files added
3 files modified
220 ■■■■■ changed files
screen-api/src/main/java/com/moral/api/controller/WeatherController.java 23 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/CityWeatherService.java 9 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/CityWeatherServiceImpl.java 25 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/util/WeatherUtils.java 163 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/WeatherController.java
@@ -1,16 +1,19 @@
package com.moral.api.controller;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.entity.CityWeather;
import com.moral.api.service.CityWeatherService;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
@@ -42,4 +45,16 @@
        Map<String, Object> value = cityWeatherService.queryWeatherByRegionCode(regionCode);
        return ResultMessage.ok(value);
    }
    @RequestMapping(value = "dressingIndex", method = RequestMethod.GET)
    public ResultMessage dressingIndex(HttpServletRequest request){
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        Object regionCode = parameters.get("regionCode");
        Object time = parameters.get("time");
        if (ObjectUtils.isEmpty(regionCode) || ObjectUtils.isEmpty(time)){
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> resultMap = cityWeatherService.dressingIndex(parameters);
        return ResultMessage.ok(resultMap);
    }
}
screen-api/src/main/java/com/moral/api/service/CityWeatherService.java
@@ -22,4 +22,13 @@
            * @Date: 2021/10/29
            */
    Map<String,Object> queryWeatherByRegionCode(Integer regionCode);
    /**
      *@Description: 根据地区码和时间查询穿衣指数
      *@Param: [map]
      *@return: java.util.Map<java.lang.String,java.lang.Object>
      *@Author: lizijie
      *@Date: 2022/4/15 17:00
     **/
    Map<String,Object> dressingIndex(Map map);
}
screen-api/src/main/java/com/moral/api/service/impl/CityWeatherServiceImpl.java
@@ -1,16 +1,20 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.entity.CityWeather;
import com.moral.api.mapper.CityWeatherMapper;
import com.moral.api.service.CityWeatherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.RedisConstants;
import com.moral.util.WeatherUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
@@ -36,6 +40,27 @@
        return value;
    }
    @Override
    public Map<String, Object> dressingIndex(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        int city_code = Integer.parseInt(map.get("regionCode").toString());
        String time = map.get("time").toString();
        QueryWrapper<CityWeather> cityWeatherQueryWrapper = new QueryWrapper<>();
        cityWeatherQueryWrapper.eq("city_code",city_code);
        cityWeatherQueryWrapper.eq("time",time);
        CityWeather cityWeather = cityWeatherMapper.selectOne(cityWeatherQueryWrapper);
        if (ObjectUtils.isEmpty(cityWeather)){
            return resultMap;
        }
        JSONObject jsonObject = JSONObject.parseObject(cityWeather.getValue());
        Map<String,Object> weatherMap = new HashMap<>();
        weatherMap.put("temp",jsonObject.get("temp"));
        weatherMap.put("humidity",jsonObject.get("humidity"));
        weatherMap.put("windScale",jsonObject.get("windScale"));
        resultMap = WeatherUtils.dressingIndex(weatherMap);
        return resultMap;
    }
    /**
    * @Description: 从数据库查询天气数据
            * @Param: [regionCode]
screen-common/src/main/java/com/moral/util/WeatherUtils.java
New file
@@ -0,0 +1,163 @@
package com.moral.util;
import java.util.HashMap;
import java.util.Map;
/**
 * @program: screen
 * @description: 天气相关数据计算工具类
 * @author: lizijie
 * @create: 2022-04-08 14:15
 **/
public class WeatherUtils {
    public static Map<String,Object> dressingIndex(Map map){
        //温度
        Double temp = Double.parseDouble(map.get("temp").toString());
        //湿度
        Double humidity = Double.parseDouble(map.get("humidity").toString());
        //风力等级
        Double windScale = Double.parseDouble(map.get("windScale").toString());
        //Map<String,Object> resultMap = new HashMap<>();
        String index = "";
        String feel = "";
        String clothingThickness = null;
        String advise = "";
        if(temp >= 40.0){
            index = "0";
            feel = "酷热,很不舒适";
            clothingThickness = "0mm";
            advise = "停止户外露天作业,对老弱病幼人群采取保护措施。";
        }else if (temp >= 37.0){
            index = "1-4";
            feel = "暑热,不舒适";
            clothingThickness = "0mm";
            advise = "避免在高温时段进行户外活动,老弱病幼落实防暑降温防护措施。";
        }else if (temp >= 35.0){
            index = "1-3";
            feel = "闷热,不舒适";
            clothingThickness = "0mm";
            advise = "天气闷热,适宜着丝麻、轻棉织物制作的短衣、短裙、薄短裙、短裤等夏季服装。午后尽量减少户外活动;高温条件下作业和露天作业人员采取必要防护措施。";
        }else if (temp >= 33.0){
            index = "1-2";
            feel = "炎热,不舒适";
            clothingThickness = "0mm";
            advise = "天气炎热,适宜着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等夏季服装。";
        }else if (temp >= 28.0){
            index = "1-1";
            feel = "热,较不舒适";
            clothingThickness = "0mm";
            advise = "天气较热,适宜着棉麻面料的衬衫、薄长裙、薄T恤等夏季服装;年老体弱者:长袖衬衫和单裤。";
        }else if (temp >= 25.0){
            if(humidity > 80){
                index = "1-1";
                feel = "热,较不舒适";
                clothingThickness = "0mm";
                advise = "天气较热,适宜着棉麻面料的衬衫、薄长裙、薄T恤等夏季服装;年老体弱者:长袖衬衫和单裤。";
            }else {
                index = "2";
                feel = "热舒适";
                clothingThickness = "0~1.5mm";
                advise = "天气偏热,适宜着短衫、短裙、短套装、T恤等夏季服装;年老体弱者:单层薄短裤、薄型棉衫。";
            }
        }else if (temp >= 23.0){
            index = "3";
            feel = "较舒适";
            clothingThickness = "1.5~2.4mm";
            advise = "天气暖和,适宜着单层棉麻面料的短套装、T恤衫、薄牛仔衫裤、休闲服、职业套装等春秋过渡装。年老体弱者请适当增减衣物。";
        }else if (temp >= 21.0){
            index = "4-1";
            feel = "舒适";
            clothingThickness = "2.41~4.0mm";
            advise = "天气温暖,适宜着长袖衬衫加单裤、单层薄短裤,薄型棉衫等春秋过渡装;年老体弱者“针织长袖衬衫+背心、长裤、薄型套装。";
        }else if (temp >= 18.0){
            index = "4-2";
            feel = "凉舒适";
            clothingThickness = "4.01~6.0mm";
            advise = "天气温和,适宜着单层薄衫裤,薄型棉衫、长袖T恤、薄型套装、牛仔衫裤、西服套装、薄型夹克等春秋过渡装;年老体弱者宜着针织长袖衬衫+马甲、长裤、夹克衫、西服套装等。";
        }else if (temp >= 15.0){
            index = "5";
            feel = "温凉,较舒适";
            clothingThickness = "6.0~7.0mm";
            advise = "天气温凉,适宜着夹衣、马甲衬衫、长裤、夹克衫、西服套装、加薄羊毛衫等春秋服装。年老体弱者:夹衣或风衣加羊毛衫。";
        }else if (temp >= 13.0){
            index = "6-1";
            feel = "微凉";
            clothingThickness = "7.01~8.0mm";
            advise = "天气微凉,适宜着意见羊毛衫、夹克衫、西服套装、马甲衬衫+夹克衫配长裤等春秋着装;年老体弱者:厚外套加毛衣、呢外套加羊毛衫。";;
        }else if (temp >= 11.0){
            index = "6-2";
            feel = "较凉";
            clothingThickness = "8.01~9.0";
            advise = "天球较凉,适宜着厚外套加毛衣、大衣、毛套装、西服套装等春秋服装。体弱者宜着大衣、毛衣加呢外套等厚型春秋服装。";
        }else if (temp >= 8.0){
            index = "6-3";
            feel = "凉";
            clothingThickness = "9.01~10.0mm";
            advise = "天气凉,适宜着一到两件羊毛衫、大衣、毛套装、皮夹克等春秋着装;年老体弱者宜着大衣、夹衣或风衣加羊毛衫等厚型春秋着装。";
        }else if (temp >= 5.0){
            index = "7-1";
            feel = "微冷";
            clothingThickness = "10.01~11.0mm";
            advise = "天气微冷,适宜着毛衣、风衣、大衣、皮夹克、外套、毛套装、西装、防寒服等厚型春秋着装;年老体弱者:一到两件羊毛衫+大衣或毛套装、薄棉外套等。";
        }else if (temp >= 0.0){
            if (windScale >= 4 || humidity >= 80){
                index = "7-3";
                feel = "较冷";
                clothingThickness = "12.01~13.0mm";
                advise = "天气冷,冬季着装:棉衣、羽绒衣、冬大衣、皮夹克、毛衣再外罩大衣等;年老体弱者尤其要注意保暖防冻。";
            }else {
                index = "7-2";
                feel = "较冷";
                clothingThickness = "11.01~12.0mm";
                advise = "天气较冷,适宜着薄棉衣、薄羽绒衣、皮夹克加羊毛衫等冬季服装;年老体弱者宜着厚棉衣或冬大衣。";
            }
        }else if (temp >= -4.9){
            if (windScale >= 4 || humidity >= 80){
                index = "8-1";
                feel = "寒冷";
                clothingThickness = ">=13.0.0mm";
                advise = "天气寒冷,冬季着装:棉衣、羽绒服、冬大衣、皮夹克加羊毛衫、厚呢外套、呢帽、手套等;年老体弱者尽量少外出。";
            }else {
                index = "7-3";
                feel = "冷";
                clothingThickness = "12.01~13.0";
                advise = "天气冷,冬季着装:棉衣、羽绒衣、冬大衣、皮夹克、毛衣再外罩大衣等;年老体弱者尤其要注意保暖防冻。";
            }
        }else if (temp >= -9.9){
            if (windScale <= 4 || humidity < 80){
                index = "8-1";
                feel = "寒冷";
                clothingThickness = ">=13.0.0mm";
                advise = "天气寒冷,冬季着装:棉衣、羽绒服、冬大衣、皮夹克加羊毛衫、厚呢外套、呢帽、手套等;年老体弱者尽量少外出。";
            }else if (windScale <= 4 || humidity >= 80){
                index = "8-2";
                feel = "很冷";
                clothingThickness = ">=15.0";
                advise = "温度极低,尽量少外出;建议着厚棉衣、厚羽绒服、冬大衣、皮夹克、裘皮大衣、棉(皮)帽、棉(皮)手套、棉(皮)靴等隆冬着装。";
            }else if (windScale >= 4 || humidity < 80){
                index = "8-2";
                feel = "很冷";
                clothingThickness = ">=15.0";
                advise = "温度极低,尽量少外出;建议着厚棉衣、厚羽绒服、冬大衣、皮夹克、裘皮大衣、棉(皮)帽、棉(皮)手套、棉(皮)靴等隆冬着装。";
            }else if (windScale >= 4 || humidity >= 80){
                index = "8-3";
                feel = "极冷";
                clothingThickness = ">=15.0";
                advise = "温度极低,尽量少外出;建议着厚棉衣、厚羽绒服、冬大衣、皮夹克、裘皮大衣、棉(皮)帽、棉(皮)手套、棉(皮)靴等隆冬着装。";
            }
        }else if (temp <= -10.0){
            index = "8-3";
            feel = "极冷";
            clothingThickness = ">=15.0";
            advise = "温度极低,尽量少外出;建议着厚棉衣、厚羽绒服、冬大衣、皮夹克、裘皮大衣、棉(皮)帽、棉(皮)手套、棉(皮)靴等隆冬着装。";
        }
        map.put("index",index);
        map.put("feel",feel);
        map.put("clothingThickness",clothingThickness);
        map.put("advise",advise);
        System.out.println(map);
        return map;
    }
}