package com.xindazhou.business.controller.admin.version;

import com.xindazhou.framework.apilog.core.annotation.ApiAccessLog;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageParam;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.excel.core.util.ExcelUtils;
import com.xindazhou.business.controller.admin.version.vo.VersionPageReqVO;
import com.xindazhou.business.controller.admin.version.vo.VersionRespVO;
import com.xindazhou.business.controller.admin.version.vo.VersionSaveReqVO;
import com.xindazhou.business.convert.version.VersionConvert;
import com.xindazhou.business.dal.dataobject.version.VersionDO;
import com.xindazhou.business.service.version.VersionService;
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.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.xindazhou.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.xindazhou.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 版本管理")
@RestController
@RequestMapping("/system/version")
@Validated
public class VersionController {

    @Resource
    private VersionService versionService;

    @PostMapping("/create")
    @Operation(summary = "创建版本")
    @PreAuthorize("@ss.hasPermission('system:version:create')")
    public CommonResult<Long> createVersion(@Valid @RequestBody VersionSaveReqVO createReqVO) {
        return success(versionService.createVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改版本")
    @PreAuthorize("@ss.hasPermission('system:version:update')")
    public CommonResult<Boolean> updateVersion(@Valid @RequestBody VersionSaveReqVO updateReqVO) {
        versionService.updateVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:version:delete')")
    public CommonResult<Boolean> deleteVersion(@RequestParam("id") Long id) {
        versionService.deleteVersion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除版本")
    @Parameter(name = "ids", description = "编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('system:version:delete')")
    public CommonResult<Boolean> deleteVersionList(@RequestParam("ids") List<Long> ids) {
        versionService.deleteVersionList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:version:query')")
    public CommonResult<VersionRespVO> getVersion(@RequestParam("id") Long id) {
        VersionDO version = versionService.getVersion(id);
        return success(VersionConvert.INSTANCE.convert(version));
    }

    @GetMapping("/page")
    @Operation(summary = "获得版本分页")
    @PreAuthorize("@ss.hasPermission('system:version:query')")
    public CommonResult<PageResult<VersionRespVO>> getVersionPage(@Valid VersionPageReqVO pageReqVO) {
        PageResult<VersionDO> page = versionService.getVersionPage(pageReqVO);
        return success(VersionConvert.INSTANCE.convertPage(page));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出版本")
    @PreAuthorize("@ss.hasPermission('system:version:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVersion(VersionPageReqVO exportReqVO,
                              HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VersionDO> list = versionService.getVersionPage(exportReqVO).getList();
        // 输出
        ExcelUtils.write(response, "版本管理.xls", "数据", VersionRespVO.class,
                VersionConvert.INSTANCE.convertList(list));
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改版本状态")
    @PreAuthorize("@ss.hasPermission('system:version:update')")
    public CommonResult<Boolean> updateVersionStatus(@RequestParam("id") Long id,
                                                      @RequestParam("status") Integer status) {
        versionService.updateVersionStatus(id, status);
        return success(true);
    }

}

