package io.gisbi.application.baseinfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.gisbi.application.baseinfo.domain.Application;
import io.gisbi.application.baseinfo.service.IApplicationService;
import io.gisbi.config.ResponseResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 应用系统 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2025-03-24
 */
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Resource
    private IApplicationService applicationService;

    /**
     * 分页查询应用列表
     */
    @GetMapping("/page")
    public ResponseResult page(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "10") Integer size) {
        Page<Application> page = new Page<>(current, size);
        LambdaQueryWrapper<Application> wrapper = new LambdaQueryWrapper<>();
        // 根据创建时间倒序
        wrapper.orderByDesc(Application::getCode);
        return ResponseResult.successData(applicationService.page(page, wrapper));
    }

    /**
     * 新增应用
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody Application application) {
      return ResponseResult.successData(applicationService.createApplication(application));
    }

    /**
     * 修改应用
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Application application) {
        return ResponseResult.successData(applicationService.updateById(application));
    }

    /**
     * 删除应用
     */
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable String id) {
        return ResponseResult.successData(applicationService.removeById(id));
    }

    /**
     * 获取应用详情
     */
    @GetMapping("/{id}")
    public ResponseResult getById(@PathVariable String id) {
        return ResponseResult.successData(applicationService.getById(id));
    }
}
