package com.ylx.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.project.domain.Project;
import com.ylx.project.domain.dto.ProjectAddDTO;
import com.ylx.project.domain.dto.ProjectSearchDTO;
import com.ylx.project.domain.dto.ProjectUpdateDTO;
import com.ylx.project.domain.vo.ProjectDetailVo;
import com.ylx.project.domain.vo.ProjectPageVo;
import com.ylx.project.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/project")
@Api(tags = {"项目相关"})
@Slf4j
public class ProjectController {

    @Resource
    private ProjectService projectService;

    /**
     * 根据条件分页获取项目列表。
     *
     * @param page 分页参数
     * @param dto  项目查询条件
     * @return R<Page<ProjectPageVo>> 项目分页数据
     */
    @PreAuthorize("@ss.hasPermi('project:list')")
    @ApiOperation("根据条件分页查询项目")
    @GetMapping("/page")
    public R<Page<ProjectPageVo>> page(Page<Project> page, ProjectSearchDTO dto) {
        try {
            Page<ProjectPageVo> pageData = this.projectService.list(page, dto);
            return R.ok(pageData);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("分页查询项目异常", e);
        }
    }

    /**
     * 编辑项目信息。
     *
     * @param dto 项目编辑参数
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('project:edit')")
    @ApiOperation("编辑项目")
    @Log(title = "编辑项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@Validated @RequestBody ProjectUpdateDTO dto) {
        this.projectService.edit(dto);
        return R.ok();
    }

    /**
     * 新增项目信息。
     *
     * @param dto 项目新增参数
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('project:add')")
    @ApiOperation("新增项目")
    @Log(title = "新增项目", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@Validated @RequestBody ProjectAddDTO dto) {
        try {
            this.projectService.add(dto);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("新增项目失败", e);
        }
    }

    /**
     * 根据主键ID查询项目详情信息。
     *
     * @param id 项目ID
     * @return 项目详情信息
     */
    @PreAuthorize("@ss.hasPermi('project:list')")
    @ApiOperation("根据主键ID查询项目详情")
    @GetMapping("/{id}")
    public R<ProjectDetailVo> getDetailsInfo(@PathVariable("id") Long id) {
        try {
            ProjectDetailVo vo = this.projectService.getDetailsInfo(id);
            return R.ok(vo);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据主键ID查询项目详情异常", e);
            throw new RuntimeException("根据主键ID查询项目详情异常", e);
        }
    }

    /**
     * 根据主键ID删除项目。
     *
     * @param id 项目ID
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('project:remove')")
    @ApiOperation("根据主键ID删除项目")
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable("id") Long id) {
        try {
            this.projectService.deleteProjectById(id);
            return R.ok();
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("根据主键ID删除项目异常", e);
            throw new RuntimeException("根据主键ID删除项目异常", e);
        }
    }

}
