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

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.quantityUnitRelation.vo.*;
import com.zt.plat.module.base.dal.dataobject.quantityUnitRelation.QuantityUnitRelationDO;
import com.zt.plat.module.base.service.quantityUnitRelation.QuantityUnitRelationService;

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


    @Resource
    private QuantityUnitRelationService quantityUnitRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建计量单位量与单位关联")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:create')")
    public CommonResult<QuantityUnitRelationRespVO> createQuantityUnitRelation(@Valid @RequestBody QuantityUnitRelationSaveReqVO createReqVO) {
        return success(quantityUnitRelationService.createQuantityUnitRelation(createReqVO));
    }

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得计量单位量与单位关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:query')")
    public CommonResult<QuantityUnitRelationRespVO> getQuantityUnitRelation(@RequestParam("id") Long id) {
        QuantityUnitRelationDO quantityUnitRelation = quantityUnitRelationService.getQuantityUnitRelation(id);
        return success(BeanUtils.toBean(quantityUnitRelation, QuantityUnitRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计量单位量与单位关联分页")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:query')")
    public CommonResult<PageResult<QuantityUnitRelationRespVO>> getQuantityUnitRelationPage(@Valid QuantityUnitRelationPageReqVO pageReqVO) {
        PageResult<QuantityUnitRelationDO> pageResult = quantityUnitRelationService.getQuantityUnitRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuantityUnitRelationRespVO.class));
    }

    @GetMapping("/simple-page")
    @Operation(summary = "获得计量单位量与单位关联精简分页")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:query')")
    public CommonResult<PageResult<QuantityUnitRelationSimpleRespVO>> getQuantityUnitRelationSimplePage(@Valid QuantityUnitRelationSimplePageReqVO pageReqVO) {
        return success(quantityUnitRelationService.getQuantityUnitRelationSimplePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计量单位量与单位关联 Excel")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuantityUnitRelationExcel(@Valid QuantityUnitRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuantityUnitRelationDO> list = quantityUnitRelationService.getQuantityUnitRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计量单位量与单位关联.xls", "数据", QuantityUnitRelationRespVO.class,
                        BeanUtils.toBean(list, QuantityUnitRelationRespVO.class));
    }

    @PostMapping("/batch-save")
    @Operation(summary = "批量保存计量单位量与单位关联")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:create')")
    public CommonResult<Boolean> batchSaveQuantityUnitRelations(@Valid @RequestBody QuantityUnitRelationBatchSaveReqVO batchSaveReqVO) {
        quantityUnitRelationService.batchSaveQuantityUnitRelations(batchSaveReqVO);
        return success(true);
    }

    @PostMapping("/create-unit-with-relation")
    @Operation(summary = "创建单位并关联到量纲（组合接口）")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:create')")
    public CommonResult<QuantityUnitRelationRespVO> createUnitWithRelation(@Valid @RequestBody CreateUnitWithRelationReqVO createReqVO) {
        return success(quantityUnitRelationService.createUnitWithRelation(createReqVO));
    }

    @PostMapping("/delete-unit-with-relation")
    @Operation(summary = "删除单位及关联关系（组合接口）")
    @PreAuthorize("@ss.hasPermission('unit-management:quantity-unit-relation:delete')")
    public CommonResult<Boolean> deleteUnitWithRelation(@Valid @RequestBody DeleteUnitWithRelationReqVO deleteReqVO) {
        quantityUnitRelationService.deleteUnitWithRelation(deleteReqVO);
        return success(true);
    }

}