/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.maintain;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.service.maintain.FloodSeasonWService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.maintain.FloodSeasonW;
import com.tofly.pojo.maintain.dto.FloodSeasonDto;
import com.tofly.pojo.maintain.vo.FloodSeasonWVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author wangzhun
 * @date Wed May 11 00:00:00 CST 2022
 */
//@RestController
//@AllArgsConstructor
//@RequestMapping("/floodseasonw")
//@Api(tags="汛情上报接口")
public class FloodSeasonWController {

    private  FloodSeasonWService floodSeasonWService;

    /**
    * 分页查询
    * @param page 分页对象
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<FloodSeasonWVo>> getFloodSeasonWPage(Page page, @ModelAttribute FloodSeasonDto floodSeasonDto) {

        Date startDate=null,endDate=null;
        if(StringUtils.isNotBlank(floodSeasonDto.getTime())){
            // 格式化传入时间
            LocalDateTime localDateTime = DateTimeUtil.fromString2LocalDateTime(floodSeasonDto.getTime(), DateTimeUtil.NUMBER_FORMAT);
            // 获取指定开始/结束时间
            assert localDateTime != null;
            startDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayBeginTime(localDateTime));
            endDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayEndTime(localDateTime));
        }
        List<String> statusCollect=null;
       if( StringUtils.isNotBlank(floodSeasonDto.getStatusMulti())){
            statusCollect = Arrays.stream(floodSeasonDto.getStatusMulti().split(",")).collect(Collectors.toList());
        }
        Page<FloodSeasonW> page1 = floodSeasonWService.page(page,
                Wrappers.query(JSON.parseObject(JSON.toJSONString(floodSeasonDto), FloodSeasonW.class))
                        .<FloodSeasonW>lambda().in(statusCollect!=null&&statusCollect.size()>0 ,FloodSeasonW::getStatus,statusCollect)
                        .like(StringUtils.isNotBlank(floodSeasonDto.getQueryLike()),FloodSeasonW::getPosition, floodSeasonDto.getQueryLike())
                        .or()
                        .like(StringUtils.isNotBlank(floodSeasonDto.getQueryLike()),FloodSeasonW::getAddress, floodSeasonDto.getQueryLike())
                        .lt(endDate!=null,FloodSeasonW::getCreateTime,endDate)
                        .gt(startDate!=null,FloodSeasonW::getCreateTime,startDate)
                        .orderByDesc(FloodSeasonW::getCreateTime)
        );
        List<FloodSeasonW> records = page1.getRecords();
        List<FloodSeasonWVo> floodSeasonWVos = floodSeasonWService.viewConversion(records);
        page.setRecords(floodSeasonWVos);
        return ResultRespone.success(page);
    }


    /**
     * 通过id查询
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    public ResultRespone getById(@PathVariable("id" ) BigDecimal id) {
      return ResultRespone.success(floodSeasonWService.getById(id));
    }

    /**
     * 新增
     * @param floodSeasonW 
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增" )
    @ApiOperation(value = "新增")
    @PostMapping
    public ResultRespone save( FloodSeasonW floodSeasonW ,@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        floodSeasonWService.saveWithFile(floodSeasonW,fileList);
      return ResultRespone.success(floodSeasonW);
    }

    /**
     * 修改
     * @param floodSeasonW 
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改" )
    @ApiOperation(value = "修改")
    @PutMapping
    public ResultRespone updateById(FloodSeasonW floodSeasonW
            ,@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {


      return ResultRespone.success(floodSeasonWService.updateFloodByID(floodSeasonW,fileList));
    }

    /**
     * 通过id删除
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除" )
    @ApiOperation(value = "通过ID删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable BigDecimal id) {
      return ResultRespone.success(floodSeasonWService.removeById(id));
    }

    /**
     * 通过id批量删除
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除" )
    @ApiOperation(value = "通过ID批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(floodSeasonWService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
