package com.lutao.cloud.oil.typhoon;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.security.annotation.Inner;
import com.lutao.cloud.oil.pojo.entity.TyphoonInfo;
import com.lutao.cloud.oil.pojo.entity.TyphoonPointInfo;
import com.lutao.cloud.oil.pojo.vo.TyphoonInfoVO;
import com.lutao.cloud.oil.service.TyphoonInfoService;
import com.lutao.cloud.oil.service.TyphoonPointInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("typhoon")
@Api(value = "台风信息", tags = "台风信息")
public class TyphoonController {

    @Resource
    private TyphoonInfoService typhoonInfoService;

    @Resource
    private TyphoonPointInfoService typhoonPointInfoService;

    @ApiOperation(value = "台风信息列表", notes = "台风信息列表")
    @GetMapping("list/{year}")
    @Inner(value = false)
    public MessageInfo<List<TyphoonInfoVO>> listTyphoonInfo(@PathVariable("year") String year) {
        List<TyphoonInfoVO> resultList = Lists.newArrayList();
        List<TyphoonInfo> typhoonInfoList = typhoonInfoService.list(Wrappers.lambdaQuery(TyphoonInfo.class).eq(TyphoonInfo::getYear, year).orderByDesc(TyphoonInfo::getCode));
        List<TyphoonPointInfo> typhoonPointInfos = typhoonPointInfoService.list();
        if (!typhoonInfoList.isEmpty()) {
            for (TyphoonInfo typhoonInfo : typhoonInfoList) {
                resultList.add(BeanUtil.copyProperties(typhoonInfo, TyphoonInfoVO.class));
            }
        }
        for (TyphoonInfoVO typhoonInfoVO : resultList) {
            List<TyphoonPointInfo> pointInfos = typhoonPointInfos.stream().filter(typhoonPointInfo -> typhoonPointInfo.getCode().equals(typhoonInfoVO.getCode())).collect(Collectors.toList());
            typhoonInfoVO.setTyphoonPointInfoList(pointInfos);
        }
        return MessageInfo.ok(resultList);
    }

    @ApiOperation(value = "台风信息详情", notes = "台风信息详情")
    @GetMapping("detail/{code}")
    @Inner(value = false)
    public MessageInfo<List<TyphoonInfoVO>> detailTyphoonInfo(@PathVariable("code") String code) {
        List<TyphoonInfoVO> resultList = Lists.newArrayList();
        List<TyphoonInfo> typhoonInfoList = typhoonInfoService.list(Wrappers.lambdaQuery(TyphoonInfo.class).eq(TyphoonInfo::getCode, code));
        List<TyphoonPointInfo> typhoonPointInfos = typhoonPointInfoService.list(Wrappers.lambdaQuery(TyphoonPointInfo.class).eq(TyphoonPointInfo::getCode, code).orderByAsc(TyphoonPointInfo::getTime));
        if (!typhoonInfoList.isEmpty()) {
            for (TyphoonInfo typhoonInfo : typhoonInfoList) {
                resultList.add(BeanUtil.copyProperties(typhoonInfo, TyphoonInfoVO.class));
            }
        }
        for (TyphoonInfoVO typhoonInfoVO : resultList) {
            List<TyphoonPointInfo> pointInfos = typhoonPointInfos.stream().filter(typhoonPointInfo -> typhoonPointInfo.getCode().equals(typhoonInfoVO.getCode())).collect(Collectors.toList());
            typhoonInfoVO.setTyphoonPointInfoList(pointInfos);
        }
        return MessageInfo.ok(resultList);
    }

}
