package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.Project;
import com.ruoyi.web.work.domain.dto.ProjectChangeDto;
import com.ruoyi.web.work.domain.dto.ProjectDto;
import com.ruoyi.web.work.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.util.Arrays;
import java.util.List;

/**
 * 项目管理
 *
 * @author lsw
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/work/project")
public class ProjectController extends BaseController {
    @Autowired
    private IProjectService projectService;

    @PreAuthorize("@ss.hasPermi('work:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        project.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Project> list = projectService.selectList(project);
        return getDataTable(list);
    }

    @GetMapping("/selectAll")
    public AjaxResult selectAll(Project project) {
        project.setCompanyId(getLoginUser().getUser().getCompanyId());
        project.setState(0);
        List<Project> list = projectService.selectList(project);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('work:project:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(projectService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:project:add')")
    @Log(title = "新增项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Project project) throws ServerException {
        return projectService.add(project);
    }

    @PreAuthorize("@ss.hasPermi('work:project:edit')")
    @Log(title = "编辑项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProjectDto project) throws ServerException {
        return projectService.edit(project);
    }

    @PreAuthorize("@ss.hasPermi('work:project:edit')")
    @Log(title = "停用或启用项目", businessType = BusinessType.UPDATE)
    @PostMapping("/change")
    public AjaxResult change(@Validated @RequestBody ProjectChangeDto dto) throws ServerException {
        return projectService.change(dto);
    }

    @PreAuthorize("@ss.hasPermi('work:project:remove')")
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectService.removeByIds(Arrays.asList(ids)));
    }
}
