package com.fastbee.data.controller.project;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fastbee.common.annotation.Log;
import com.fastbee.common.enums.BusinessType;
import com.fastbee.common.utils.poi.ExcelUtil;
import com.fastbee.project.domain.Project;
import com.fastbee.project.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.page.TableDataInfo;

/**
 * 项目Controller
 *
 * @author kerwincui
 * &#064;date  2024-09-26
 */
@RestController
@RequestMapping("/project")
@Api(tags = "项目")
public class ProjectController extends BaseController
{

    private final IProjectService projectService;

    public ProjectController(IProjectService projectService) {
        this.projectService = projectService;
    }

    /**
     * 查询/搜索项目列表
     */
    @PreAuthorize("@ss.hasPermi('iot:project:list')")
    @GetMapping("/list")
    @ApiOperation("查询项目列表")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 获取管理员所管理的项目id以及子项目id列表
     */
    @GetMapping("/info/{userId}")
    @ApiOperation("获取项目信息")
    public AjaxResult getProjectInfo(@PathVariable("userId") Long userId){
        return success(projectService.selectProjectByUserId(userId));
    }

    /**
     * 导出项目列表
     */
    @ApiOperation("导出项目列表")
    @PreAuthorize("@ss.hasPermi('iot:project:export')")
    @Log(title = "巡检路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:project:query')")
    @GetMapping(value = "/{projectId}")
    @ApiOperation("获取项目详细信息")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(projectService.selectProjectByProjectId(projectId));
    }
    /**
     * 查询管理员项目切换下拉框数据
     */
    @GetMapping("/adminProjectList")
//    @PreAuthorize("@ss.hasPermi('iot:project:adminProjectList')")
    @ApiOperation("查询管理员项目切换下拉框数据")
    public AjaxResult getAdminProjectList(){
        return success(projectService.selectAdminProjectList());
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('iot:project:add')")
    @PostMapping
    @ApiOperation("新增项目")
    public AjaxResult add(@RequestBody Project project)
    {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('iot:project:edit')")
    @PutMapping
    @ApiOperation("修改项目")
    public AjaxResult edit(@RequestBody Project project)
    {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('iot:project:remove')")
    @DeleteMapping("/{projectIds}")
    @ApiOperation("删除项目")
    public AjaxResult remove(@PathVariable Long projectIds)
    {
        return toAjax(projectService.deleteProjectByProjectIds(projectIds));
    }



    /**
     * 查询项目管理机构的当前管理的行政区划树状列表
     */
    @GetMapping("/areaTree")
    @ApiOperation("查询项目管理机构的当前管理的行政区划树状列表")
    public AjaxResult getProjectAreaTree(@Param("projectId") Long deptId,@Param("startLevel") Integer startLevel, @Param("isDeptHold") Integer isDeptHold){
        return success(projectService.getProjectAreaTree(deptId, startLevel, isDeptHold));
    }
    /**
     * 查询项目管理机构管理的最高行政区划级别下的全部行政区划的树状列表
     */
    @GetMapping("/areaTreeTop")
    @ApiOperation("查询项目管理机构管理的最高行政区划级别下的全部行政区划的树状列表")
    public AjaxResult getProjectAreaTreeTop(@Param("projectId") Long deptId,@Param("startLevel") Integer startLevel){
        return success(projectService.getProjectAreaTreeAll(deptId, startLevel));
    }
}
