package cn.iocoder.yudao.module.erp.controller.admin.autocode;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.autocode.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.autocode.AutocodePartDO;
import cn.iocoder.yudao.module.erp.service.autocode.AutocodePartService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 编码规则组成")
@RestController
@RequestMapping("/erp/autocode-part")
@Validated
public class AutocodePartController {

    @Resource
    private AutocodePartService autocodePartService;

    @PostMapping("/create")
    @Operation(summary = "创建编码规则组成")
    @PreAuthorize("@ss.hasPermission('erp:autocode-part:create')")
    public CommonResult<Long> createAutocodePart(@Valid @RequestBody AutocodePartSaveReqVO createReqVO) {
        return success(autocodePartService.createAutocodePart(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码规则组成")
    @PreAuthorize("@ss.hasPermission('erp:autocode-part:update')")
    public CommonResult<Boolean> updateAutocodePart(@Valid @RequestBody AutocodePartSaveReqVO updateReqVO) {
        autocodePartService.updateAutocodePart(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码规则组成")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:autocode-part:delete')")
    public CommonResult<Boolean> deleteAutocodePart(@RequestParam("id") Long id) {
        autocodePartService.deleteAutocodePart(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码规则组成")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:autocode-part:query')")
    public CommonResult<AutocodePartRespVO> getAutocodePart(@RequestParam("id") Long id) {
        AutocodePartDO autocodePart = autocodePartService.getAutocodePart(id);
        return success(BeanUtils.toBean(autocodePart, AutocodePartRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码规则组成分页")
    @PreAuthorize("@ss.hasPermission('erp:autocode-part:query')")
    public CommonResult<PageResult<AutocodePartRespVO>> getAutocodePartPage(@Valid AutocodePartPageReqVO pageReqVO) {
        PageResult<AutocodePartDO> pageResult = autocodePartService.getAutocodePartPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AutocodePartRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码规则组成 Excel")
    @PreAuthorize("@ss.hasPermission('erp:autocode-part:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAutocodePartExcel(@Valid AutocodePartPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AutocodePartDO> list = autocodePartService.getAutocodePartPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码规则组成.xls", "数据", AutocodePartRespVO.class,
                        BeanUtils.toBean(list, AutocodePartRespVO.class));
    }

}