package com.weather.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.weather.entity.WeatherProductInfo;
import com.weather.service.IWeatherProductInfoService;
import com.weather.vo.ApiResult;
import com.weather.vo.req.PageProductReq;
import com.weather.vo.res.WeatherProductInfoRes;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务产品信息 前端控制器
 * </p>
 *
 * @author : fengyuhao
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/product/info")
public class WeatherProductInfoController {

    @Autowired
    IWeatherProductInfoService productInfoService;

    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "农气服务列表", response = WeatherProductInfo.class)
    public ApiResult list(@RequestBody PageProductReq req) {

        IPage<WeatherProductInfo> page = new Page<>();
        page.setSize(req.getLimit());
        page.setCurrent(req.getPage());

        QueryWrapper<WeatherProductInfo> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(req.getCityName())) {
            wrapper.lambda().like(WeatherProductInfo::getCityname, req.getCityName());
        }else{
            wrapper.lambda().like(WeatherProductInfo::getCityname, "贵州省");
        }

        if (StringUtils.isNotEmpty(req.getTitle())) {
            wrapper.lambda().like(WeatherProductInfo::getProductTitle, req.getTitle());
        }
        wrapper.lambda().orderByDesc(WeatherProductInfo::getProductTime);
        page = productInfoService.page(page, wrapper);
        List<WeatherProductInfo> records = page.getRecords();
        if (records==null||records.size()<=0){
            return ApiResult.noData();
        }

        Map<String,String> type=new HashMap<>();
        type.put("gjnsjjqxfw","关键农事季节气象服务");
        type.put("nyqxqb","农业气象情报");
        type.put("tszwqxfwzt","特色作物气象服务专题");
        type.put("zhjcyypg","灾害监测预警评估");
        type.put("nqyxpg","农气影响评估");

        List<WeatherProductInfoRes>resList=new ArrayList<>();
        WeatherProductInfoRes model;
        for (WeatherProductInfo record : records) {
            model=new WeatherProductInfoRes();
            BeanUtils.copyProperties(record,model);
            model.setId(record.getId().toString());
            String[] strings = model.getProductTime().split(" ");
            model.setDate(strings[0]);
            model.setTime(strings[1]);
            model.setProductTypeName(type.get(model.getProductTypeId()));
            resList.add(model);
        }

        return ApiResult.success(resList);
    }

}
