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.BusinessTripApproval;
import com.ruoyi.database.domain.dto.ApprovaltDto;
import com.ruoyi.database.service.BusinessTripApprovalService;
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 BusinessTripApprovalController
 * @Author lijingtong
 * @Date 2025-05-29
 */

@RestController
@RequestMapping("/BusinessTripApproval")
@RequiredArgsConstructor
@Api(tags = "出差审批单")
public class BusinessTripApprovalController extends BaseController {
    private final BusinessTripApprovalService businessTripApprovalService;

    @GetMapping("/list")
    @ApiOperation("查询出差审批单")
    public TableDataInfo list(BusinessTripApproval businessTripApproval) {
        startPage();
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<BusinessTripApproval> qw = new QueryWrapper<>(businessTripApproval);
        qw.orderByDesc("create_time");
        String fTime = businessTripApproval.getFTime();
        String eTime = businessTripApproval.getETime();
//        String 的字符串转成LoaclDateTime格式
        LocalDateTime.parse(fTime);
        List<BusinessTripApproval> list = businessTripApprovalService.list(qw);
        long size = list.size();
        return getDataTableEnhance(list,size);
    }

    @ApiOperation("新增或修改出差审批单 传id为修改 不传id为新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BusinessTripApproval businessTripApproval) {
        SysUser user = getLoginUser().getUser();
        if (businessTripApproval.getId() == null) {
            businessTripApproval.setCreateBy(user.getNickName());
            businessTripApproval.setState(0);
            boolean result = businessTripApprovalService.save(businessTripApproval);
            if (!result) {
                return AjaxResult.error("新增出差审批单失败");
            }
            return AjaxResult.success(result);
        }else {
            businessTripApproval.setUpdateBy(user.getNickName());
            boolean result = businessTripApprovalService.updateById(businessTripApproval);
            if (!result) {
                return AjaxResult.error("修改出差审批单失败");
            }
            return AjaxResult.success(result);
        }
    }

    @ApiOperation("删除出差审批单")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        boolean result = businessTripApprovalService.removeById(id);
        if (!result) {
            return AjaxResult.error("删除出差审批单失败");
        }
        return AjaxResult.success(result);
    }


    @ApiOperation("查询审批流程")
    @GetMapping("/state/{id}")
    public AjaxResult state(@PathVariable Long id) {
        BusinessTripApproval businessTripApproval = businessTripApprovalService.getById(id);
        if (businessTripApproval == null) {
            return AjaxResult.error("查询审批流程失败");
        }
        return AjaxResult.success(businessTripApproval.getState());
    }


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


}
