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

import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointRespVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseTakePointDO;
import cn.iocoder.yudao.module.system.service.houselocation.WareHouseTakePointService;
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")
@Validated
public class WareHouseTakePointController {

    @Resource
    private WareHouseTakePointService houseTakePointService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得取货点位分页")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point:query')")
    public CommonResult<PageResult<WareHouseTakePointRespVO>> getHouseTakePointPage(@Valid WareHouseTakePointPageReqVO pageReqVO) {
        PageResult<WareHouseTakePointDO> pageResult = houseTakePointService.getHouseTakePointPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WareHouseTakePointRespVO.class));
    }

    @PostMapping("/list")
    @Operation(summary = "获得取货点位集合")
    @PreAuthorize("@ss.hasPermission('ware:house-take-point:list')")
    public CommonResult<List<WareHouseTakePointRespVO>> getHouseTakePointList(@RequestBody WareHouseTakePointSaveReqVO updateReqVO) {
        List<WareHouseTakePointDO> list = houseTakePointService.getHouseTakePointList(updateReqVO);
        return success(BeanUtils.toBean(list, WareHouseTakePointRespVO.class));
    }

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

}