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

import cn.iocoder.yudao.module.erp.controller.admin.autocode.util.AutoCodeUtil;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
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.AutocodeRecordDO;
import cn.iocoder.yudao.module.erp.service.autocode.AutocodeRecordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 编码生成记录")
@RestController
@RequestMapping("/erp/autocode-record")
@Validated
public class AutocodeRecordController {

    @Resource
    private AutocodeRecordService autocodeRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建编码生成记录")
    @PreAuthorize("@ss.hasPermission('erp:autocode-record:create')")
    public CommonResult<Long> createAutocodeRecord(@Valid @RequestBody AutocodeRecordSaveReqVO createReqVO) {
        return success(autocodeRecordService.createAutocodeRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码生成记录")
    @PreAuthorize("@ss.hasPermission('erp:autocode-record:update')")
    public CommonResult<Boolean> updateAutocodeRecord(@Valid @RequestBody AutocodeRecordSaveReqVO updateReqVO) {
        autocodeRecordService.updateAutocodeRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码生成记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:autocode-record:delete')")
    public CommonResult<Boolean> deleteAutocodeRecord(@RequestParam("id") Long id) {
        autocodeRecordService.deleteAutocodeRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码生成记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:autocode-record:query')")
    public CommonResult<AutocodeRecordRespVO> getAutocodeRecord(@RequestParam("id") Long id) {
        AutocodeRecordDO autocodeRecord = autocodeRecordService.getAutocodeRecord(id);
        return success(BeanUtils.toBean(autocodeRecord, AutocodeRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码生成记录分页")
    @PreAuthorize("@ss.hasPermission('erp:autocode-record:query')")
    public CommonResult<PageResult<AutocodeRecordRespVO>> getAutocodeRecordPage(@Valid AutocodeRecordPageReqVO pageReqVO) {
        PageResult<AutocodeRecordDO> pageResult = autocodeRecordService.getAutocodeRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AutocodeRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码生成记录 Excel")
    @PreAuthorize("@ss.hasPermission('erp:autocode-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAutocodeRecordExcel(@Valid AutocodeRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AutocodeRecordDO> list = autocodeRecordService.getAutocodeRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码生成记录.xls", "数据", AutocodeRecordRespVO.class,
                        BeanUtils.toBean(list, AutocodeRecordRespVO.class));
    }

}