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

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.untInfo.vo.*;
import com.zt.plat.module.base.dal.dataobject.untInfo.UntInfoDO;
import com.zt.plat.module.base.service.untInfo.UntInfoService;

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


    @Resource
    private UntInfoService untInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建计量单位")
    @PreAuthorize("@ss.hasPermission('unit-management:unt-info:create')")
    public CommonResult<UntInfoRespVO> createUntInfo(@Valid @RequestBody UntInfoSaveReqVO createReqVO) {
        return success(untInfoService.createUntInfo(createReqVO));
    }

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得计量单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('unit-management:unt-info:query')")
    public CommonResult<UntInfoRespVO> getUntInfo(@RequestParam("id") Long id) {
        UntInfoDO untInfo = untInfoService.getUntInfo(id);
        return success(BeanUtils.toBean(untInfo, UntInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计量单位分页")
    @PreAuthorize("@ss.hasPermission('unit-management:unt-info:query')")
    public CommonResult<PageResult<UntInfoRespVO>> getUntInfoPage(@Valid UntInfoPageReqVO pageReqVO) {
        PageResult<UntInfoDO> pageResult = untInfoService.getUntInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UntInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计量单位 Excel")
    @PreAuthorize("@ss.hasPermission('unit-management:unt-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUntInfoExcel(@Valid UntInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UntInfoDO> list = untInfoService.getUntInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计量单位.xls", "数据", UntInfoRespVO.class,
                        BeanUtils.toBean(list, UntInfoRespVO.class));
    }

}