package com.yc.ship.module.resource.controller.admin.project;

import com.yc.ship.framework.common.enums.CommonStatusEnum;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.resource.controller.admin.project.vo.ProjectPageReqVO;
import com.yc.ship.module.resource.controller.admin.project.vo.ProjectRespVO;
import com.yc.ship.module.resource.controller.admin.project.vo.ProjectSaveReqVO;
import com.yc.ship.module.resource.controller.admin.project.vo.ProjectSimpleRespVO;
import com.yc.ship.module.resource.controller.admin.vo.SimpleListReqVO;
import com.yc.ship.module.resource.dal.dataobject.project.ProjectDO;
import com.yc.ship.module.resource.service.project.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 产品项目")
@RestController
@RequestMapping("/resource/project")
@Validated
public class ProjectController {

    @Resource
    private ProjectService projectService;


    @PostMapping("/create")
    @Operation(summary = "创建产品项目")
    @PreAuthorize("@ss.hasPermission('product:project:create')")
    public CommonResult<Long> createProject(@Valid @RequestBody ProjectSaveReqVO createReqVO) {
        return success(projectService.createProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品项目")
    @PreAuthorize("@ss.hasPermission('product:project:update')")
    public CommonResult<Boolean> updateProject(@Valid @RequestBody ProjectSaveReqVO updateReqVO) {
        projectService.updateProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:project:delete')")
    public CommonResult<Boolean> deleteProject(@RequestParam("id") Long id) {
        projectService.deleteProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:project:query')")
    public CommonResult<ProjectRespVO> getProject(@RequestParam("id") Long id) {
        ProjectDO project = projectService.getProject(id);
        return success(BeanUtils.toBean(project, ProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品项目分页")
    @PreAuthorize("@ss.hasPermission('product:project:query')")
    public CommonResult<PageResult<ProjectRespVO>> getProjectPage(@Valid ProjectPageReqVO pageReqVO) {
        PageResult<ProjectDO> pageResult = projectService.getProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品项目 Excel")
    @PreAuthorize("@ss.hasPermission('product:project:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportProjectExcel(@Valid ProjectPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectDO> list = projectService.getProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品项目.xls", "数据", ProjectRespVO.class,
                BeanUtils.toBean(list, ProjectRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品项目")
    @PreAuthorize("@ss.hasPermission('product:project:query')")
    public CommonResult<List<ProjectRespVO>> getProjectList(@Valid ProjectPageReqVO pageReqVO) {
        List<ProjectDO> list = projectService.getProjectList(pageReqVO);
        return success(BeanUtils.toBean(list, ProjectRespVO.class));
    }

    @GetMapping({"/list-all-simple", "simple-list"})
    @Operation(summary = "获取项目精简信息列表", description = "只包含开启的项目,在多租户的场景下,会只返回租户所建项目")
    public CommonResult<List<ProjectSimpleRespVO>> getSimpleProjectList() {
        SimpleListReqVO reqVO = SimpleListReqVO.builder().status(CommonStatusEnum.DISABLE.getStatus()).build();
        List<ProjectDO> list = projectService.getProjectListByTenant(reqVO);
        list.sort(Comparator.comparing(ProjectDO::getSort));
        return success(BeanUtils.toBean(list, ProjectSimpleRespVO.class));
    }

    @GetMapping("/project-list")
    @Operation(summary = "获取项目精简信息列表")
    public CommonResult<List<ProjectSimpleRespVO>> getSimpleProjectList2() {
        List<ProjectDO> list = projectService.getProjectList();
        return success(BeanUtils.toBean(list, ProjectSimpleRespVO.class));
    }
}