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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.HouseLaneVO;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.WareHouseLanePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.WareHouseLaneRespVO;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.WareHouseLaneSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselane.WareHouseLaneDO;
import cn.iocoder.yudao.module.system.service.houselane.WareHouseLaneService;
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.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 线库")
@RestController
@RequestMapping("/system/ware/house-lane")
@Validated
public class WareHouseLaneController {

    @Resource
    private WareHouseLaneService houseLaneService;

    @PostMapping("/createOrEditOrDel")
    @Operation(summary = "创建修改删除线库")
    @PreAuthorize("@ss.hasPermission('ware:house-lane:create')")
    public CommonResult<HouseLaneVO> createOrEditOrDel(@Valid @RequestBody WareHouseLaneSaveReqVO createReqVO) {
        HouseLaneVO vo = houseLaneService.createOrEditOrDel(createReqVO);
        return success(vo);
    }

    @GetMapping("/get")
    @Operation(summary = "获得线库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ware:house-lane:query')")
    public CommonResult<WareHouseLaneRespVO> getHouseLane(@RequestParam("id") Long id) {
        WareHouseLaneDO houseLane = houseLaneService.getHouseLane(id);
        return success(BeanUtils.toBean(houseLane, WareHouseLaneRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得线库列表")
    public CommonResult<List<WareHouseLaneRespVO>> list(@RequestParam Long positionMapId) {
        List<WareHouseLaneDO> list = houseLaneService.list(new LambdaQueryWrapperX<WareHouseLaneDO>()
                .eq(WareHouseLaneDO::getPositionMapId, positionMapId));
        return success(BeanUtils.toBean(list, WareHouseLaneRespVO.class));
    }

    @GetMapping("/getHouseLaneList")
    @Operation(summary = "获得所有线库列表")
    public CommonResult<List<WareHouseLaneRespVO>> getHouseLaneList() {
        List<WareHouseLaneDO> list = houseLaneService.list(new LambdaQueryWrapperX<WareHouseLaneDO>()
                .orderByAsc(WareHouseLaneDO::getId));
        return success(BeanUtils.toBean(list, WareHouseLaneRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得线库分页")
    @PreAuthorize("@ss.hasPermission('ware:house-area:query')")
    public CommonResult<PageResult<WareHouseLaneRespVO>> getHouseAreaPage(@Valid WareHouseLanePageReqVO pageReqVO) {
        PageResult<WareHouseLaneDO> pageResult = houseLaneService.getHouseLanePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WareHouseLaneRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除线库")
    @Parameter(name = "id", description = "菜单编号", required = true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        houseLaneService.delete(id);
        return success(true);
    }
}
