package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.fms.domain.WmsStock;
import com.yunfeiyun.agmp.fms.domain.reqvo.WmsStockAddReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.WmsStockBatchChangeReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.WmsStockListReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.WmsStockAppMapResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.WmsStockListResVo;
import com.yunfeiyun.agmp.fms.service.IWmsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 库存Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/wms/stock")
public class WmsStockController extends BaseController
{
    @Autowired
    private IWmsStockService wmsStockService;

    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsStockListReqVo wmsStockListReqVo)
    {
        startPage();
        List<WmsStockListResVo> list = wmsStockService.selectWmsStockList(wmsStockListReqVo);
        return getDataTable(list);
    }

    /**
     * app查询库存农资信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:app:map')")
    @GetMapping("/app/map")
    public AjaxResult appMap()
    {

        List<WmsStockAppMapResVo> list = wmsStockService.selectWmsStockAppMapResVoList();
        return success(list);
    }


    /**
     * 导出库存列表
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:export')")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockListReqVo wmsStockListReqVo)
    {
        List<WmsStockListResVo> list = wmsStockService.selectWmsStockList(wmsStockListReqVo);
        ExcelUtil<WmsStockListResVo> util = new ExcelUtil<>(WmsStockListResVo.class);
        util.exportExcel(response, list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:query')")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") String stockId)
    {
        return success(wmsStockService.selectWmsStockByStockId(stockId));
    }
    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:info')")
    @GetMapping(value = "/info")
    public AjaxResult infoByGoodsIdAndWarehouseId( @Valid WmsStock wmsStock)
    {
        return success(wmsStockService.selectWmsStockByGoodsIdAndWarehouseId(wmsStock));
    }

    /**
     * 新增库存
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:add')")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WmsStockAddReqVo wmsStockAddReqVo)
    {
        return toAjax(wmsStockService.add(wmsStockAddReqVo));
    }

    /**
     * 新增库存
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:batch:change')")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping("/batch/change")
    public AjaxResult batchChange(@Valid @RequestBody WmsStockBatchChangeReqVo wmsStockBatchChangeReqVo)
    {
        return toAjax(wmsStockService.batchChange(wmsStockBatchChangeReqVo));
    }

    /**
     * 修改库存
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:edit')")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsStock wmsStock)
    {
        return toAjax(wmsStockService.updateWmsStock(wmsStock));
    }

    /**
     * 删除库存
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:remove')")
    @Log(title = "库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable String[] stockIds)
    {
        return toAjax(wmsStockService.deleteWmsStockByStockIds(stockIds));
    }


}
