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

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.unitQuantity.vo.*;
import com.zt.plat.module.base.dal.dataobject.unitQuantity.UnitQuantityDO;
import com.zt.plat.module.base.service.unitQuantity.UnitQuantityService;

@Tag(name = "管理后台 - 计量单位量")
@RestController
@RequestMapping("/base/unit-management/unit-quantity")
@Validated
public class UnitQuantityController implements BusinessControllerMarker {


    @Resource
    private UnitQuantityService unitQuantityService;

    @PostMapping("/create")
    @Operation(summary = "创建计量单位量")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-quantity:create')")
    public CommonResult<UnitQuantityRespVO> createUnitQuantity(@Valid @RequestBody UnitQuantitySaveReqVO createReqVO) {
        return success(unitQuantityService.createUnitQuantity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计量单位量")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-quantity:update')")
    public CommonResult<Boolean> updateUnitQuantity(@Valid @RequestBody UnitQuantitySaveReqVO updateReqVO) {
        unitQuantityService.updateUnitQuantity(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得计量单位量")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-quantity:query')")
    public CommonResult<UnitQuantityRespVO> getUnitQuantity(@RequestParam("id") Long id) {
        UnitQuantityDO unitQuantity = unitQuantityService.getUnitQuantity(id);
        return success(BeanUtils.toBean(unitQuantity, UnitQuantityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计量单位量分页")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-quantity:query')")
    public CommonResult<PageResult<UnitQuantityRespVO>> getUnitQuantityPage(@Valid UnitQuantityPageReqVO pageReqVO) {
        PageResult<UnitQuantityDO> pageResult = unitQuantityService.getUnitQuantityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UnitQuantityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计量单位量 Excel")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-quantity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUnitQuantityExcel(@Valid UnitQuantityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UnitQuantityDO> list = unitQuantityService.getUnitQuantityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计量单位量.xls", "数据", UnitQuantityRespVO.class,
                        BeanUtils.toBean(list, UnitQuantityRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取量纲及单位树")
    @PreAuthorize("@ss.hasPermission('unit-management:unit-quantity:query')")
    public CommonResult<List<UnitQuantityTreeRespVO>> getUnitQuantityTree() {
        return success(unitQuantityService.getUnitQuantityTree());
    }

}