package com.ruoyi.web.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.WarehouseBase;
import com.ruoyi.web.base.service.IWarehouseBaseService;
import com.ruoyi.web.base.service.IWarehouseBaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 仓库 前端控制器
 * </p>
 *
 * @author author
 * @since 2026-02-06
 */
@RestController
@RequestMapping("/warehouse-base")
public class WarehouseBaseController {
    @Autowired
    private IWarehouseBaseService warehouseBaseService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("仓库管理添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WarehouseBase warehouseBase, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (warehouseBaseService.count(new QueryWrapper<WarehouseBase>().eq("org_id",loginOrgId).eq("warehouse_no", warehouseBase.getWarehouseNo())) >0 ){
            throw new Exception("该编号已存在");
        }
        warehouseBase.setOrgId(loginOrgId);
        warehouseBase.setCreateBy(getUsername());

        return success(warehouseBaseService.save(warehouseBase));
    }

    @ApiOperation("仓库管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody WarehouseBase warehouseBase, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (warehouseBaseService.count(new QueryWrapper<WarehouseBase>().ne("id",warehouseBase.getId()).eq("org_id",loginOrgId).eq("warehouse_no", warehouseBase.getWarehouseNo())) >0 ){
            throw new Exception("该编号已存在");
        }
        warehouseBase.setUpdateBy(getUsername());
        return success(warehouseBaseService.updateById(warehouseBase));
    }

    @ApiOperation("仓库管理删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            warehouseBaseService.removeById(s);
        }
        return success();
    }

    @ApiOperation("仓库管理列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){

        return success(warehouseBaseService.list(new QueryWrapper<WarehouseBase>().eq("org_id", tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("仓库管理分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , HttpServletRequest request) {
        return success(warehouseBaseService.page(new Page<WarehouseBase>(pageNum,pageSize),new QueryWrapper<WarehouseBase>().eq("org_id", tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("仓库管理详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(warehouseBaseService.getById(id));
    }

}
