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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.identity.controller.admin.generaterecord.vo.*;
import cn.iocoder.yudao.module.identity.convert.generaterecord.GenerateRecordConvert;
import cn.iocoder.yudao.module.identity.dal.dataobject.generaterecord.GenerateRecordDO;
import cn.iocoder.yudao.module.identity.service.generaterecord.GenerateRecordService;
import com.google.zxing.WriterException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 标识码生成记录")
@RestController
@RequestMapping("/identity/generate-record")
@Validated
public class GenerateRecordController {

    @Resource
    private GenerateRecordService generateRecordService;

    @PostMapping("/create")
    @ApiOperation("创建标识码生成记录")
    @PreAuthorize("@ss.hasPermission('identity:generate-record:create')")
    public CommonResult<Long> createGenerateRecord(@Valid @RequestBody GenerateRecordCreateReqVO createReqVOe) {
        return success(generateRecordService.createGenerateRecord(createReqVOe));
    }

    @PutMapping("/update")
    @ApiOperation("更新标识码生成记录")
    @PreAuthorize("@ss.hasPermission('identity:generate-record:update')")
    public CommonResult<Boolean> updateGenerateRecord(@Valid @RequestBody GenerateRecordUpdateReqVO updateReqVO) {
        generateRecordService.updateGenerateRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除标识码生成记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-record:delete')")
    public CommonResult<Boolean> deleteGenerateRecord(@RequestParam("id") Long id) {
        generateRecordService.deleteGenerateRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得标识码生成记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-record:query')")
    public CommonResult<GenerateRecordRespVO> getGenerateRecord(@RequestParam("id") Long id) {
        GenerateRecordDO generateRecord = generateRecordService.getGenerateRecord(id);
        return success(GenerateRecordConvert.INSTANCE.convert(generateRecord));
    }

    @GetMapping("/list")
    @ApiOperation("获得标识码生成记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-record:query')")
    public CommonResult<List<GenerateRecordRespVO>> getGenerateRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<GenerateRecordDO> list = generateRecordService.getGenerateRecordList(ids);
        return success(GenerateRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得标识码生成记录分页")
    @PreAuthorize("@ss.hasPermission('identity:generate-record:query')")
    public CommonResult<PageResult<GenerateRecordRespVO>> getGenerateRecordPage(@Valid GenerateRecordPageReqVO pageVO) {
        return success(generateRecordService.getGenerateRecordPage(pageVO));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出标识码生成记录 Excel")
    @PreAuthorize("@ss.hasPermission('identity:generate-record:export')")
    @OperateLog(type = EXPORT)
    public void exportGenerateRecordExcel(@Valid GenerateRecordExportReqVO exportReqVO,
                                          HttpServletResponse response) throws IOException {
        List<GenerateRecordDO> list = generateRecordService.getGenerateRecordList(exportReqVO);
        // 导出 Excel
        List<GenerateRecordExcelVO> datas = GenerateRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "标识码生成记录.xls", "数据", GenerateRecordExcelVO.class, datas);
    }


    @GetMapping("/getGenerateRecordByRecordId")
    @ApiOperation("根据记录编码查询出该条数据信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:generate-record:query')")
    public CommonResult<GenerateRecordDO> getGenerateRecordByRecordId(@RequestParam("generateRecordId") String generateRecordId) {
        GenerateRecordDO generateRecordByRecordId = generateRecordService.getGenerateRecordByRecordId(generateRecordId);
        return success(generateRecordByRecordId);
    }

}
