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

import cn.iocoder.yudao.server.controller.admin.vo.crm.CrmNoticePageReqVO;
import cn.iocoder.yudao.server.controller.admin.vo.crm.CrmNoticeRespVO;
import cn.iocoder.yudao.server.controller.admin.vo.crm.CrmNoticeSaveReqVO;
import cn.iocoder.yudao.server.dal.dataobject.CrmNoticeDO;
import cn.iocoder.yudao.server.service.crm.CrmNoticeService;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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


@Tag(name = "管理后台 - CRM消息通知")
@RestController
@RequestMapping("/rq/iot-crm-notice")
@Validated
public class CrmNoticeController {

    @Resource
    private CrmNoticeService iotCrmNoticeService;

    @PostMapping("/create")
    @Operation(summary = "创建CRM消息通知")
    @PreAuthorize("@ss.hasPermission('rq:iot-crm-notice:create')")
    public CommonResult<Long> createIotCrmNotice(@Valid @RequestBody CrmNoticeSaveReqVO createReqVO) {
        return success(iotCrmNoticeService.createIotCrmNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新CRM消息通知")
    @PreAuthorize("@ss.hasPermission('rq:iot-crm-notice:update')")
    public CommonResult<Boolean> updateIotCrmNotice(@Valid @RequestBody CrmNoticeSaveReqVO updateReqVO) {
        iotCrmNoticeService.updateIotCrmNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除CRM消息通知")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-crm-notice:delete')")
    public CommonResult<Boolean> deleteIotCrmNotice(@RequestParam("id") Long id) {
        iotCrmNoticeService.deleteIotCrmNotice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得CRM消息通知")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-crm-notice:query')")
    public CommonResult<CrmNoticeRespVO> getIotCrmNotice(@RequestParam("id") Long id) {
        CrmNoticeDO iotCrmNotice = iotCrmNoticeService.getIotCrmNotice(id);
        return success(BeanUtils.toBean(iotCrmNotice, CrmNoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得CRM消息通知分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-crm-notice:query')")
    public CommonResult<PageResult<CrmNoticeRespVO>> getIotCrmNoticePage(@Valid CrmNoticePageReqVO pageReqVO) {
        PageResult<CrmNoticeDO> pageResult = iotCrmNoticeService.getIotCrmNoticePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrmNoticeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出CRM消息通知 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-crm-notice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotCrmNoticeExcel(@Valid CrmNoticePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CrmNoticeDO> list = iotCrmNoticeService.getIotCrmNoticePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "CRM消息通知.xls", "数据", CrmNoticeRespVO.class,
                        BeanUtils.toBean(list, CrmNoticeRespVO.class));
    }

}