package com.ruoyi.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.database.domain.VehicleRequest;
import com.ruoyi.database.domain.dto.ApprovaltDto;
import com.ruoyi.database.service.VehicleRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Description VehicleRequestController
 * @Author lijingtong
 * @Date 2025-05-28
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/business/vehicleRequest")
@Api(tags = "公务用车出县申请")
public class VehicleRequestController extends BaseController {
    private final VehicleRequestService vehicleRequestService;

    @ApiOperation("查询公务用车出县申请")
    @GetMapping("/list")
    public TableDataInfo list(VehicleRequest vehicleRequest) {
        startPage();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<VehicleRequest> queryWrapper = new QueryWrapper<>(vehicleRequest);
        String fTime = vehicleRequest.getFTime();
        String eTime = vehicleRequest.getETime();
//        把时间转成LocalDateTime格式
        LocalDateTime fromTime = LocalDateTime.parse(fTime, formatter);
        LocalDateTime endTime = LocalDateTime.parse(eTime, formatter);
        queryWrapper.orderByDesc("create_time");
        if (fTime != null && fTime != null) {
            queryWrapper.between("create_time", fromTime, endTime);
        }
        List<VehicleRequest> list = vehicleRequestService.list();
        long size = list.size();
        return getDataTableEnhance(list, size);
    }

    @ApiOperation("新增或修改公务用车出县申请 传id为修改 不传id为新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VehicleRequest vehicleRequest) {
        SysUser user = getLoginUser().getUser();
        if (vehicleRequest.getId() == null) {
            vehicleRequest.setCreateBy(user.getNickName());
            vehicleRequest.setState(0);
            boolean result = vehicleRequestService.save(vehicleRequest);
            if (!result) {
                return AjaxResult.error("新增公务用车出县申请失败");
            }
            return AjaxResult.success(result);
        } else {
            vehicleRequest.setUpdateBy(user.getNickName());
            boolean result = vehicleRequestService.updateById(vehicleRequest);
            if (!result) {
                return AjaxResult.error("修改公务用车出县申请失败");
            }
            return AjaxResult.success(result);
        }

    }

    @ApiOperation("删除公务用车出县申请")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        boolean result = vehicleRequestService.removeById(id);
        if (!result) {
            return AjaxResult.error("删除公务用车出县申请失败");
        }
        return AjaxResult.success("删除公务用车出县申请成功");
    }

    @ApiOperation("查询公务用车出县申请详情")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        VehicleRequest vehicleRequest = vehicleRequestService.getById(id);
        return AjaxResult.success(vehicleRequest);
    }

    @ApiOperation("审批公务用车出县申请")
    @PostMapping("/approval")
    public AjaxResult approval(@RequestBody ApprovaltDto dto) {
        Long id = dto.getId();
        Integer state = dto.getState();
        VehicleRequest vehicleRequest = vehicleRequestService.getById(id);
        vehicleRequest.setState(state);
        boolean result = vehicleRequestService.updateById(vehicleRequest);
        if (!result) {
            return AjaxResult.error("审批公务用车出县申请失败");
        }
        return AjaxResult.success("审批公务用车出县申请成功");
    }
}
