package cn.iocoder.yudao.module.system.controller.admin.houselocation;

import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointBindingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointBindingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointBindingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseTakePointBindingDO;
import cn.iocoder.yudao.module.system.service.houselocation.WareHouseTakePointBindingService;
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("/system/ware/house-take-point-binding")
@Validated
public class WareHouseTakePointBindingController {

    @Resource
    private WareHouseTakePointBindingService houseTakePointBindingService;

    @PostMapping("/create")
    @Operation(summary = "创建取货点位绑定")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point-binding:create')")
    public CommonResult<Long> createHouseTakePointBinding(@Valid @RequestBody WareHouseTakePointBindingSaveReqVO createReqVO) {
        return success(houseTakePointBindingService.createHouseTakePointBinding(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新取货点位绑定")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point-binding:update')")
    public CommonResult<Boolean> updateHouseTakePointBinding(@Valid @RequestBody WareHouseTakePointBindingSaveReqVO updateReqVO) {
        houseTakePointBindingService.updateHouseTakePointBinding(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除取货点位绑定")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ware:house-take-point-binding:delete')")
    public CommonResult<Boolean> deleteHouseTakePointBinding(@RequestParam("id") Long id) {
        houseTakePointBindingService.deleteHouseTakePointBinding(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得取货点位绑定")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point-binding:query')")
    public CommonResult<WareHouseTakePointBindingRespVO> getHouseTakePointBinding(@RequestParam("id") Long id) {
        WareHouseTakePointBindingDO houseTakePointBinding = houseTakePointBindingService.getHouseTakePointBinding(id);
        return success(BeanUtils.toBean(houseTakePointBinding, WareHouseTakePointBindingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得取货点位绑定分页")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point-binding:query')")
    public CommonResult<PageResult<WareHouseTakePointBindingRespVO>> getHouseTakePointBindingPage(@Valid WareHouseTakePointBindingPageReqVO pageReqVO) {
        PageResult<WareHouseTakePointBindingDO> pageResult = houseTakePointBindingService.getHouseTakePointBindingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WareHouseTakePointBindingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出取货点位绑定 Excel")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point-binding:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHouseTakePointBindingExcel(@Valid WareHouseTakePointBindingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WareHouseTakePointBindingDO> list = houseTakePointBindingService.getHouseTakePointBindingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "取货点位绑定.xls", "数据", WareHouseTakePointBindingRespVO.class,
                        BeanUtils.toBean(list, WareHouseTakePointBindingRespVO.class));
    }

}