package cn.iocoder.yudao.module.supplier.controller.admin.product;

import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.ConnectRecordPageReqVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.ConnectRecordRespVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.ConnectRecordSaveReqVO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.ConnectRecordDO;
import cn.iocoder.yudao.module.supplier.service.product.ConnectRecordService;
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.*;


@Tag(name = "管理后台 - 供应商联系记录")
@RestController
@RequestMapping("/supplier/connect-record")
@Validated
public class ConnectRecordController {

    @Resource
    private ConnectRecordService connectRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商联系记录")
    @PreAuthorize("@ss.hasPermission('supplier:connect-record:create')")
    public CommonResult<Long> createConnectRecord(@Valid @RequestBody ConnectRecordSaveReqVO createReqVO) {
        return success(connectRecordService.createConnectRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商联系记录")
    @PreAuthorize("@ss.hasPermission('supplier:connect-record:update')")
    public CommonResult<Boolean> updateConnectRecord(@Valid @RequestBody ConnectRecordSaveReqVO updateReqVO) {
        connectRecordService.updateConnectRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商联系记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('supplier:connect-record:delete')")
    public CommonResult<Boolean> deleteConnectRecord(@RequestParam("id") Long id) {
        connectRecordService.deleteConnectRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商联系记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:connect-record:query')")
    public CommonResult<ConnectRecordRespVO> getConnectRecord(@RequestParam("id") Long id) {
        ConnectRecordDO connectRecord = connectRecordService.getConnectRecord(id);
        return success(BeanUtils.toBean(connectRecord, ConnectRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商联系记录分页")
    @PreAuthorize("@ss.hasPermission('supplier:connect-record:query')")
    public CommonResult<PageResult<ConnectRecordRespVO>> getConnectRecordPage(@Valid ConnectRecordPageReqVO pageReqVO) {
        PageResult<ConnectRecordDO> pageResult = connectRecordService.getConnectRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConnectRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商联系记录 Excel")
    @PreAuthorize("@ss.hasPermission('supplier:connect-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConnectRecordExcel(@Valid ConnectRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConnectRecordDO> list = connectRecordService.getConnectRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商联系记录.xls", "数据", ConnectRecordRespVO.class,
                        BeanUtils.toBean(list, ConnectRecordRespVO.class));
    }

}
