/*
 * 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.EventMangeService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.maintain.EventMange;
import com.tofly.pojo.maintain.dto.EventMangeDto;
import com.tofly.pojo.maintain.vo.EventMangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
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 javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 
 *
 * @author wangzhun
 * @date Wed May 11 00:00:00 CST 2022
 */
//@RestController
//@RequestMapping("/eventmange")
//@Api(tags="事件上报接口")
public class EventMangeController {

        @Autowired
     private  EventMangeService eventMangeService;
    /**
    * 分页查询
    * @param page 分页对象
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<EventMangeVo>>  getEventMangePage(Page page, @ModelAttribute EventMangeDto eventMangeDto) {
        //List<String> status = Arrays.asList(eventMangeDto.getStatusMulti().split(","));
        boolean notBlank = StringUtils.isNotBlank(eventMangeDto.getStatusMulti());
        List<String> statusList=null;
        if(notBlank){
           statusList = Arrays.asList(eventMangeDto.getStatusMulti().split(","));
        }
        Date startDate=null,endDate=null;
        if(StringUtils.isNotBlank(eventMangeDto.getTime())){
            // 格式化传入时间
            LocalDateTime localDateTime = DateTimeUtil.fromString2LocalDateTime(eventMangeDto.getTime(), DateTimeUtil.FULL_FORMAT);
            // 获取指定开始/结束时间
            assert localDateTime != null;
            startDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayBeginTime(localDateTime));
            endDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayEndTime(localDateTime));
        }

        Page page1 = eventMangeService.page(page,
                Wrappers.query(JSON.parseObject(JSON.toJSONString(eventMangeDto), EventMange.class))
                        .<EventMange>lambda().in(notBlank&&statusList.size()>0,
                        EventMange::getStatus,statusList)
                        .like(StringUtils.isNotBlank(eventMangeDto.getQueryLike()), EventMange::getType, eventMangeDto.getQueryLike())
                        .or()
                        .like(StringUtils.isNotBlank(eventMangeDto.getQueryLike()), EventMange::getName, eventMangeDto.getQueryLike())
                        .or()
                        .like(StringUtils.isNotBlank(eventMangeDto.getQueryLike()), EventMange::getAddress, eventMangeDto.getQueryLike())
                        .lt(endDate!=null, EventMange::getCreateTime,endDate)
                        .gt(startDate!=null,EventMange::getCreateTime,startDate)
                        .orderByDesc(EventMange::getCreateTime)
        );
        List<EventMange> records = page1.getRecords();

        List<EventMangeVo> eventMangeVos = eventMangeService.viewConversion(records);
        page1.setRecords(eventMangeVos);
        return  ResultRespone.success(page1);
    }


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

    /**
     * 新增
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增" )
    @ApiOperation(value = "新增")
    @PostMapping
    public ResultRespone save(@Valid EventMangeDto eventMangeDto,
                              BindingResult result, @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {


      return ResultRespone.success(eventMangeService.saveEventMange(fileList, eventMangeDto));
    }

    /**
     * 修改
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改" )
    @ApiOperation(value = "修改")
    @PutMapping
    public ResultRespone updateById(@Valid EventMangeDto eventMangeDto,
                                    BindingResult result,
                                    @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {

      return ResultRespone.success(eventMangeService.updateEventMangeById(fileList, eventMangeDto));
    }


    /**
     * 通过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(eventMangeService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
