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.page.TableDataInfo;
import com.ruoyi.database.domain.PublicBusinessTrip;
import com.ruoyi.database.service.PublicBusinessTripService;
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 PublicBusinessTripController
 * @Author lijingtong
 * @Date 2025-05-28
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/business/publicBusinessTrip")
@Api(tags = "公务出差申请")
public class PublicBusinessTripController extends BaseController {

    private final PublicBusinessTripService publicBusinessTripService;


    @GetMapping("/getList")
    @ApiOperation("查询公务出差申请")
    public TableDataInfo getList(PublicBusinessTrip publicBusinessTrip) {
        startPage();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<PublicBusinessTrip> queryWrapper = new QueryWrapper<>(publicBusinessTrip);
        queryWrapper.orderByDesc("create_time");
        String fTime = publicBusinessTrip.getFTime();
        String eTime = publicBusinessTrip.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<PublicBusinessTrip> list = publicBusinessTripService.list();
        long size = list.size();
        return getDataTableEnhance(list, size);
    }

    @ApiOperation("新增或修改公务出差申请 传id为修改 不传id为新增")
    @PostMapping("/addOrUpdate")
    public AjaxResult add(@RequestBody PublicBusinessTrip publicBusinessTrip) {
        if (publicBusinessTrip.getId() == null) {
            publicBusinessTrip.setCreateBy(getUsername());
            publicBusinessTrip.setCreateTime(LocalDateTime.now());
            publicBusinessTrip.setUpdateBy(getUsername());
            publicBusinessTrip.setUpdateTime(LocalDateTime.now());
            boolean result = publicBusinessTripService.save(publicBusinessTrip);
            if (result) {
                return AjaxResult.success("新增成功");
            }
            return AjaxResult.error("新增失败");
        } else {
            publicBusinessTrip.setUpdateBy(getUsername());
            boolean result = publicBusinessTripService.updateById(publicBusinessTrip);
            if (!result) {
                return AjaxResult.error("修改公务出差申请失败");
            }
            return AjaxResult.success(result);
        }
    }

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

    @ApiOperation("查询公务出差申请详情")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        PublicBusinessTrip publicBusinessTrip = publicBusinessTripService.getById(id);
        if (publicBusinessTrip == null) {
            return AjaxResult.error("查询失败");
        }
        return AjaxResult.success(publicBusinessTrip);
    }

    @ApiOperation("审批公务出差申请")
    @PostMapping("/approval")
    public AjaxResult approval(@RequestBody PublicBusinessTrip publicBusinessTrip) {
        Integer state = publicBusinessTrip.getState();
        publicBusinessTrip.setState(state);
        boolean result = publicBusinessTripService.updateById(publicBusinessTrip);
        if (!result) {
            return AjaxResult.error("审批公务出差申请失败");
        }
        return AjaxResult.success("审批公务出差申请成功");
    }

    @ApiOperation("批量删除公务出差申请")
    @DeleteMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        boolean result = publicBusinessTripService.removeByIds(ids);
        if (!result) {
            return AjaxResult.error("批量删除公务出差申请失败");
        }
        return AjaxResult.success("批量删除公务出差申请成功");
    }

}
