package com.zt.plat.module.base.controller.admin.unitConversion;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.base.controller.admin.unitConversion.vo.*;
import com.zt.plat.module.base.dal.dataobject.unitConversion.UnitConversionDO;
import com.zt.plat.module.base.service.unitConversion.UnitConversionService;

@Tag(name = "管理后台 - 单位转换")
@RestController
@RequestMapping("/base/unit-management/unit-conversion")
@Validated
public class UnitConversionController implements BusinessControllerMarker {

    @Resource
    private UnitConversionService unitConversionService;

    @PostMapping("/create")
    @Operation(summary = "创建单位转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:create')")
    public CommonResult<UnitConversionRespVO> createUnitConversion(@Valid @RequestBody UnitConversionSaveReqVO createReqVO) {
        return success(unitConversionService.createUnitConversion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单位转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:update')")
    public CommonResult<Boolean> updateUnitConversion(@Valid @RequestBody UnitConversionSaveReqVO updateReqVO) {
        unitConversionService.updateUnitConversion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单位转换")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:delete')")
    public CommonResult<Boolean> deleteUnitConversion(@RequestParam("id") Long id) {
        unitConversionService.deleteUnitConversion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除单位转换")
                @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:delete')")
    public CommonResult<Boolean> deleteUnitConversionList(@RequestBody BatchDeleteReqVO req) {
        unitConversionService.deleteUnitConversionListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单位转换")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<UnitConversionRespVO> getUnitConversion(@RequestParam("id") Long id) {
        UnitConversionDO unitConversion = unitConversionService.getUnitConversion(id);
        return success(BeanUtils.toBean(unitConversion, UnitConversionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单位转换分页")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<PageResult<UnitConversionRespVO>> getUnitConversionPage(@Valid UnitConversionPageReqVO pageReqVO) {
        PageResult<UnitConversionDO> pageResult = unitConversionService.getUnitConversionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UnitConversionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单位转换 Excel")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUnitConversionExcel(@Valid UnitConversionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UnitConversionDO> list = unitConversionService.getUnitConversionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单位转换.xls", "数据", UnitConversionRespVO.class,
                        BeanUtils.toBean(list, UnitConversionRespVO.class));
    }

    @PostMapping("/convert")
    @Operation(summary = "单位转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<UnitConvertRespVO> convert(@Valid @RequestBody UnitConvertReqVO convertReqVO) {
        return success(unitConversionService.convert(convertReqVO));
    }

    @PostMapping("/batch-convert")
    @Operation(summary = "批量单位转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<BatchUnitConvertRespVO> batchConvert(@Valid @RequestBody BatchUnitConvertReqVO batchReqVO) {
        return success(unitConversionService.batchConvert(batchReqVO));
    }

    @PostMapping("/convert-by-symbol")
    @Operation(summary = "按单位符号转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<UnitConvertRespVO> convertBySymbol(@Valid @RequestBody UnitConvertBySymbolReqVO reqVO) {
        return success(unitConversionService.convertBySymbol(reqVO));
    }

    @PostMapping("/convert-by-name")
    @Operation(summary = "按单位名称转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<UnitConvertRespVO> convertByName(@Valid @RequestBody UnitConvertByNameReqVO reqVO) {
        return success(unitConversionService.convertByName(reqVO));
    }

    @PostMapping("/batch-convert-by-symbol")
    @Operation(summary = "批量按单位符号转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<BatchUnitConvertRespVO> batchConvertBySymbol(@Valid @RequestBody BatchUnitConvertBySymbolReqVO reqVO) {
        return success(unitConversionService.batchConvertBySymbol(reqVO));
    }

    @PostMapping("/batch-convert-by-name")
    @Operation(summary = "批量按单位名称转换")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<BatchUnitConvertRespVO> batchConvertByName(@Valid @RequestBody BatchUnitConvertByNameReqVO reqVO) {
        return success(unitConversionService.batchConvertByName(reqVO));
    }

    @GetMapping("/validate-paths")
    @Operation(summary = "校验量纲的转换路径")
    @Parameter(name = "quantityId", description = "量纲ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-conversion:query')")
    public CommonResult<UnitConversionValidationRespVO> validateConversionPaths(@RequestParam("quantityId") Long quantityId) {
        return success(unitConversionService.validateConversionPaths(quantityId));
    }

}