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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.ContactPageReqVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.ContactRespVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.ContactSaveReqVO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.ContactDO;
import cn.iocoder.yudao.module.supplier.service.product.ContactService;
import cn.iocoder.yudao.module.supplier.service.product.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.List;

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


@Tag(name = "管理后台 - 供应商联系人")
@RestController
@RequestMapping("/supplier/contact")
@Validated
public class ContactController {

    @Resource
    private ContactService contactService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商联系人")
    @PreAuthorize("@ss.hasPermission('supplier:contact:create')")
    public CommonResult<Long> createContact(@Valid @RequestBody ContactSaveReqVO createReqVO) {
        return success(contactService.createContact(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商联系人")
    @PreAuthorize("@ss.hasPermission('supplier:contact:update')")
    public CommonResult<Boolean> updateContact(@Valid @RequestBody ContactSaveReqVO updateReqVO) {
        contactService.updateContact(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得供应商联系人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:contact:query')")
    public CommonResult<ContactRespVO> getContact(@RequestParam("id") Long id) {
        ContactDO contact = contactService.getContact(id);
        return success(BeanUtils.toBean(contact, ContactRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商联系人分页")
    @PreAuthorize("@ss.hasPermission('supplier:contact:query')")
    public CommonResult<PageResult<ContactRespVO>> getContactPage(@Valid ContactPageReqVO pageReqVO) {
        PageResult<ContactDO> pageResult = contactService.getContactPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContactRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商联系人 Excel")
    @PreAuthorize("@ss.hasPermission('supplier:contact:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContactExcel(@Valid ContactPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContactDO> list = contactService.getContactPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商联系人.xls", "数据", ContactRespVO.class,
                        BeanUtils.toBean(list, ContactRespVO.class));
    }

}
