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

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.system.controller.admin.houselocation.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseLocationDO;
import cn.iocoder.yudao.module.system.service.houselocation.HouseLocationService;
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("/system/ware/house-location")
@Validated
public class WareHouseLocationController {

    @Resource
    private HouseLocationService houseLocationService;

    @PostMapping("/create")
    @Operation(summary = "创建库位")
    @PreAuthorize("@ss.hasPermission('ware:house-location:create')")
    public CommonResult<Long> createHouseLocation(@Valid @RequestBody WareHouseLocationSaveReqVO createReqVO) {
        return success(houseLocationService.createHouseLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库位")
    @PreAuthorize("@ss.hasPermission('ware:house-location:update')")
    public CommonResult<Boolean> updateHouseLocation(@Valid @RequestBody WareHouseLocationSaveReqVO updateReqVO) {
        houseLocationService.updateHouseLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ware:house-location:delete')")
    public CommonResult<Boolean> deleteHouseLocation(@RequestParam("id") Long id) {
        houseLocationService.deleteHouseLocation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ware:house-location:query')")
    public CommonResult<WareHouseLocationRespVO> getHouseLocation(@RequestParam("id") Long id) {
        WareHouseLocationDO houseLocation = houseLocationService.getHouseLocation(id);
        return success(BeanUtils.toBean(houseLocation, WareHouseLocationRespVO.class));
    }

    @GetMapping("/getByMapItemId")
    @Operation(summary = "根据点位获取库位信息列表")
    @Parameter(name = "mapId", description = "地图id", required = true, example = "1024")
    @Parameter(name = "mapItemId", description = "地图点位id", required = true, example = "1024")
    public CommonResult<List<WareHouseLocationRespVO>> getByMapItemId(@RequestParam("mapId") Long mapId, @RequestParam("mapItemId") Long mapItemId) {
        List<WareHouseLocationDO> list = houseLocationService.getByMapItemId(mapId, mapItemId);
        return success(BeanUtils.toBean(list, WareHouseLocationRespVO.class));
    }

    @GetMapping("/getByMapId")
    @Operation(summary = "根据地图id查询，此地图对应的所有库位")
    @Parameter(name = "mapId", description = "地图id", required = true, example = "1024")
    public CommonResult<List<WareHouseLocationRespVO>> getByMapId(@RequestParam("mapId") Long mapId) {
        List<WareHouseLocationDO> list = houseLocationService.getByMapId(mapId);
        return success(BeanUtils.toBean(list, WareHouseLocationRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得库位分页")
    @PreAuthorize("@ss.hasPermission('ware:house-location:query')")
    public CommonResult<PageResult<WareHouseLocationRespVO>> getHouseLocationPage(@Valid @RequestBody WareHouseLocationPageReqVO pageReqVO) {
        PageResult<WareHouseLocationDO> pageResult = houseLocationService.getHouseLocationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WareHouseLocationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库位 Excel")
    @PreAuthorize("@ss.hasPermission('ware:house-location:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHouseLocationExcel(@Valid WareHouseLocationPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WareHouseLocationDO> list = houseLocationService.getHouseLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库位.xls", "数据", WareHouseLocationRespVO.class,
                BeanUtils.toBean(list, WareHouseLocationRespVO.class));
    }

    @PostMapping("/getLocationByName")
    @Operation(summary = "查询线库/库位/区域")
    @PreAuthorize("@ss.hasPermission('ware:house-location:getLocationByName')")
    public CommonResult<List<WareHouseLocationRespVO>> getLocationByName(@Valid @RequestBody WareHouseLocationVO requestVO) {
        return success(houseLocationService.getLocationByName(requestVO));
    }

    @PutMapping("/updateBatch")
    @Operation(summary = "批量更新库位")
    @PreAuthorize("@ss.hasPermission('ware:house-location:updateBatch')")
    public CommonResult<Boolean> updateHouseLocationList(@Valid @RequestBody List<WareHouseLocationSaveReqVO> list) {
        houseLocationService.updateHouseLocationList(list);
        return success(true);
    }

    @GetMapping("/getLocationsXYByMapId")
    @Operation(summary = "根据地图id查询，此地图对应的所有库位,包括库位坐标")
    @Parameter(name = "mapId", description = "地图id", required = true, example = "1024")
    public CommonResult<List<WareHouseLocationItemRespVO>> getLocationsXYByMapId(@RequestParam("mapId") Long mapId) {
        List<WareHouseLocationItemRespVO> list = houseLocationService.getLocationsXYByMapId(mapId);
        return success(list);
    }
}
