package com.weather.controller;


import cn.hutool.core.date.DateUtil;
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.weather.entity.AgriculturalKnowledgeBase;

import com.weather.service.IAgriculturalKnowledgeBaseService;
import com.weather.vo.ApiResult;
import com.weather.vo.req.PageProductReq;


import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 农技措施 前端控制器
 * </p>
 *
 * @author : fengyuhao
 * @since 2021-02-02
 */
@Slf4j
@RestController

@RequestMapping("/agricultural/knowledge")


public class AgriculturalKnowledgeBaseController {

    @Autowired
    IAgriculturalKnowledgeBaseService knowledgeBaseService;

    @GetMapping("/{typeid}")

    @ApiOperation(value = "获取一条农技措施")

    public ApiResult one(@PathVariable("typeid") Integer typeid) throws ParseException {

        QueryWrapper<AgriculturalKnowledgeBase>wrapper=new QueryWrapper<>();
        String month = DateUtil.format(new Date(), "M月");
        Integer day = Integer.valueOf(DateUtil.format(new Date(), "d"));
        log.info("月份：{}",month);
        log.info("天：{}",day);

        wrapper.lambda()
                .eq(AgriculturalKnowledgeBase::getCropTypeId,typeid)
        ;

        List<AgriculturalKnowledgeBase> list = knowledgeBaseService.list(wrapper);


        if (list==null||list.size()<=0){
            return ApiResult.error("暂无措施");
        }
        SimpleDateFormat df = new SimpleDateFormat("MM-dd");
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("MM-dd");
        String todayStr = df.format(new Date());
        AgriculturalKnowledgeBase resEntity=null;

        for (AgriculturalKnowledgeBase item : list) {

            String[] date=  item.getDateSegment().split(";");
            Date date1=simpleDateFormat.parse(date[0]);
            Date date2=simpleDateFormat.parse(date[1]);
            Date nowDay=simpleDateFormat.parse(todayStr);
            if(date1.getTime()<=nowDay.getTime() && date2.getTime()>=nowDay.getTime()) {
                resEntity = item;
                System.out.println(item.getAgriMeasure());
            }
        }
        // 只有一条数据
        return ApiResult.success(resEntity);
    }

    @ApiOperation(value = "根据生育期获取一条农技措施")

    @GetMapping("/{typeid}/{shengyuqi}")
    public ApiResult one(@PathVariable("typeid") Integer typeid,@PathVariable("shengyuqi") String shengyuqi) throws ParseException {
        QueryWrapper<AgriculturalKnowledgeBase>wrapper=new QueryWrapper<>();

        wrapper.lambda()
                .eq(AgriculturalKnowledgeBase::getCropTypeId,typeid)
                .eq(AgriculturalKnowledgeBase::getBearingPeriod,shengyuqi)
        ;

        List<AgriculturalKnowledgeBase> list = knowledgeBaseService.list(wrapper);


        if (list==null||list.size()<=0){
            return ApiResult.error("暂无措施");
        }
        // 只有一条数据
        return ApiResult.success(list.get(0));
    }


    @ApiOperation(value = "根据条件查询农技措施")

    @PostMapping("page")

    public ApiResult list(@RequestBody PageProductReq req) throws ParseException {

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

        QueryWrapper<AgriculturalKnowledgeBase> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AgriculturalKnowledgeBase::getCropTypeId, req.getTypeId());

        page = knowledgeBaseService.page(page, wrapper);
        List<AgriculturalKnowledgeBase> records = page.getRecords();
        if (records==null||records.size()<=0){
            return ApiResult.noData();
        }
        SimpleDateFormat df = new SimpleDateFormat("MM-dd");
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("MM-dd");
        String todayStr = df.format(new Date());

        List<AgriculturalKnowledgeBase>resList=new ArrayList<>();
        AgriculturalKnowledgeBase model;

        for (AgriculturalKnowledgeBase record : records) {
            model=new AgriculturalKnowledgeBase();
            BeanUtils.copyProperties(record,model);
            String[] date=  record.getDateSegment().split(";");
            Date date1=simpleDateFormat.parse(date[0]);
            Date date2=simpleDateFormat.parse(date[1]);
            Date nowDay=simpleDateFormat.parse(todayStr);
            if(date1.getTime()<=nowDay.getTime() && date2.getTime()>=nowDay.getTime()) {
                // 标记 是否当前
                model.setDateSegment(model.getDateSegment().replace(",","至"));
                model.setIsCurrentDate(true);
            }
            resList.add(model);
        }
        return ApiResult.success(resList);
    }

}
