package cn.iocoder.yudao.module.identity.controller.admin.generatebatch;

import cn.iocoder.yudao.module.identity.dal.dataobject.generaterecord.GenerateRecordDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.identity.controller.admin.generatebatch.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.generatebatch.GenerateBatchDO;
import cn.iocoder.yudao.module.identity.convert.generatebatch.GenerateBatchConvert;
import cn.iocoder.yudao.module.identity.service.generatebatch.GenerateBatchService;

@Api(tags = "管理后台 - 码生成记录与批次关联记录")
@RestController
@RequestMapping("/identity/generate-batch")
@Validated
public class GenerateBatchController {

    @Resource
    private GenerateBatchService generateBatchService;

    @PostMapping("/create")
    @ApiOperation("创建码生成记录与批次关联记录")
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:create')")
    public CommonResult<Long> createGenerateBatch(@Valid @RequestBody GenerateBatchCreateReqVO createReqVO) {
        return success(generateBatchService.createGenerateBatch(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新码生成记录与批次关联记录")
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:update')")
    public CommonResult<Boolean> updateGenerateBatch(@Valid @RequestBody GenerateBatchUpdateReqVO updateReqVO) {
        generateBatchService.updateGenerateBatch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除码生成记录与批次关联记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:delete')")
    public CommonResult<Boolean> deleteGenerateBatch(@RequestParam("id") Long id) {
        generateBatchService.deleteGenerateBatch(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得码生成记录与批次关联记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:query')")
    public CommonResult<GenerateBatchRespVO> getGenerateBatch(@RequestParam("id") Long id) {
        GenerateBatchDO generateBatch = generateBatchService.getGenerateBatch(id);
        return success(GenerateBatchConvert.INSTANCE.convert(generateBatch));
    }

    @GetMapping("/list")
    @ApiOperation("获得码生成记录与批次关联记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:query')")
    public CommonResult<List<GenerateBatchRespVO>> getGenerateBatchList(@RequestParam("ids") Collection<Long> ids) {
        List<GenerateBatchDO> list = generateBatchService.getGenerateBatchList(ids);
        return success(GenerateBatchConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得码生成记录与批次关联记录分页")
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:query')")
    public CommonResult<PageResult<GenerateBatchRespVO>> getGenerateBatchPage(@Valid GenerateBatchPageReqVO pageVO) {
        PageResult<GenerateBatchDO> pageResult = generateBatchService.getGenerateBatchPage(pageVO);
        return success(GenerateBatchConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出码生成记录与批次关联记录 Excel")
    @PreAuthorize("@ss.hasPermission('identity:generate-batch:export')")
    @OperateLog(type = EXPORT)
    public void exportGenerateBatchExcel(@Valid GenerateBatchExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GenerateBatchDO> list = generateBatchService.getGenerateBatchList(exportReqVO);
        // 导出 Excel
        List<GenerateBatchExcelVO> datas = GenerateBatchConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "码生成记录与批次关联记录.xls", "数据", GenerateBatchExcelVO.class, datas);
    }


    @GetMapping("/getGenerateRecordByRecordId")
    @ApiOperation("根据码生成记录ID获取码生成记录与批次关联记录数据信息")
    @ApiImplicitParam(name = "generateRecordId", value = "码生成记录id", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<GenerateBatchDO> getGenerateRecordByRecordId(@RequestParam("generateRecordId") String generateRecordId) {
        GenerateBatchDO generateBatchByRecordId = generateBatchService.getGenerateBatchByRecordId(generateRecordId);
        return success(generateBatchByRecordId);
    }

}
