package org.agt.module.license.controller.admin.project;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.framework.excel.core.util.ExcelUtils;
import org.agt.framework.translate.core.TranslateUtils;
import org.agt.module.license.controller.admin.project.vo.ProjectListReqVO;
import org.agt.module.license.controller.admin.project.vo.ProjectPageReqVO;
import org.agt.module.license.controller.admin.project.vo.ProjectRespVO;
import org.agt.module.license.controller.admin.project.vo.ProjectSaveReqVO;
import org.agt.module.license.dal.dataobject.project.ProjectDO;
import org.agt.module.license.dal.mysql.license.LicenseMapper;
import org.agt.module.license.service.project.ProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;

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

    @Resource
    private ProjectService projectService;

    @Resource
    private LicenseMapper licenseMapper;

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

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermission('license: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('license: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('license: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('license:project:query')")
    public CommonResult<PageResult<ProjectRespVO>> getProjectPage(@Valid ProjectPageReqVO pageReqVO) {
        PageResult<ProjectRespVO> pageResult = projectService.getProjectPage(pageReqVO);
        return success(pageResult);
    }

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

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目 Excel")
    @PreAuthorize("@ss.hasPermission('license:project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectExcel(@Valid ProjectPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectRespVO> list = projectService.getProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目.xls", "数据", ProjectRespVO.class,
                TranslateUtils.translate(list));
    }

    @GetMapping("/name-unique")
    @Operation(summary = "校验项目名称是否唯一")
    public CommonResult<Boolean> validateProjectNameUnique(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "id", required = false) Long id) {
        Boolean isExists = projectService.validateProjectNameUnique(name, id);
        return success(isExists);
    }

    @GetMapping("/code-unique")
    @Operation(summary = "校验项目编号是否唯一")
    public CommonResult<Boolean> validateProjectCodeUnique(@RequestParam(value = "code", required = false) String code, @RequestParam(value = "id", required = false) Long id) {
        Boolean isExists = projectService.validateProjectCodeUnique(code, id);
        return success(isExists);
    }

    @GetMapping("/max-sn")
    @Operation(summary = "返回当前最大项目sn")
    public CommonResult<Integer> selectMaxCode() {
        return success(projectService.selectMaxCode());
    }
}