package cn.iocoder.yudao.module.server.controller.admin.smsrecord;

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.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

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.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.server.controller.admin.smsrecord.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.smsrecord.SmsRecordDO;
import cn.iocoder.yudao.module.server.service.smsrecord.SmsRecordService;

@Tag(name = "管理后台 - 短信发送记录")
@RestController
@RequestMapping("/server/sms-record")
@Validated
public class SmsRecordController {

    @Resource
    private SmsRecordService smsRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建短信发送记录")
    @PreAuthorize("@ss.hasPermission('server:sms-record:create')")
    public CommonResult<Long> createSmsRecord(@Valid @RequestBody SmsRecordSaveReqVO createReqVO) {
        return success(smsRecordService.createSmsRecord(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新短信发送记录")
    @PreAuthorize("@ss.hasPermission('server:sms-record:update')")
    public CommonResult<Boolean> updateSmsRecord(@Valid @RequestBody SmsRecordSaveReqVO updateReqVO) {
        smsRecordService.updateSmsRecord(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除短信发送记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:sms-record:delete')")
    public CommonResult<Boolean> deleteSmsRecord(@RequestParam("id") Long id) {
        smsRecordService.deleteSmsRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信发送记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:sms-record:query')")
    public CommonResult<SmsRecordRespVO> getSmsRecord(@RequestParam("id") Long id) {
        SmsRecordDO smsRecord = smsRecordService.getSmsRecord(id);
        return success(BeanUtils.toBean(smsRecord, SmsRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信发送记录分页")
    @PreAuthorize("@ss.hasPermission('server:sms-record:query')")
    public CommonResult<PageResult<SmsRecordRespVO>> getSmsRecordPage(@Valid SmsRecordPageReqVO pageReqVO) {
        PageResult<SmsRecordDO> pageResult = smsRecordService.getSmsRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmsRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信发送记录 Excel")
    @PreAuthorize("@ss.hasPermission('server:sms-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSmsRecordExcel(@Valid SmsRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmsRecordDO> list = smsRecordService.getSmsRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信发送记录.xls", "数据", SmsRecordRespVO.class,
                        BeanUtils.toBean(list, SmsRecordRespVO.class));
    }

}