package com.hw.wms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.hw.common.core.constant.WmsConstants;
import com.hw.wms.domain.WmsBaseWarehouse;
import com.hw.wms.service.IWmsBaseWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.wms.domain.WmsBaseLocation;
import com.hw.wms.service.IWmsBaseLocationService;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;

/**
 * 库位Controller
 *
 * @author xs
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/wmslocation")
public class WmsBaseLocationController extends BaseController
{
    @Autowired
    private IWmsBaseLocationService wmsBaseLocationService;

    @Autowired
    private IWmsBaseWarehouseService wmsBaseWarehouseService;

    /**
     * 查询库位列表
     */
    @RequiresPermissions("wms:wmslocation:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsBaseLocation wmsBaseLocation)
    {
        startPage();
        List<WmsBaseLocation> list = wmsBaseLocationService.selectWmsBaseLocationJoinList(wmsBaseLocation);
        return getDataTable(list);
    }

    /**
     * 导出库位列表
     */
    @RequiresPermissions("wms:wmslocation:export")
    @Log(title = "库位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsBaseLocation wmsBaseLocation)
    {
        List<WmsBaseLocation> list = wmsBaseLocationService.selectWmsBaseLocationJoinList(wmsBaseLocation);
        ExcelUtil<WmsBaseLocation> util = new ExcelUtil<WmsBaseLocation>(WmsBaseLocation.class);
        util.exportExcel(response, list, "库位数据");
    }

    /**
     * 获取库位详细信息
     */
    @RequiresPermissions("wms:wmslocation:query")
    @GetMapping(value = "/{locationId}")
    public AjaxResult getInfo(@PathVariable("locationId") Long locationId)
    {
        return success(wmsBaseLocationService.selectWmsBaseLocationByLocationId(locationId));
    }

    /**
     * 新增库位
     */
    @RequiresPermissions("wms:wmslocation:add")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsBaseLocation wmsBaseLocation)
    {
        return toAjax(wmsBaseLocationService.insertWmsBaseLocation(wmsBaseLocation));
    }

    /**
     * 修改库位
     */
    @RequiresPermissions("wms:wmslocation:edit")
    @Log(title = "库位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsBaseLocation wmsBaseLocation)
    {
        return toAjax(wmsBaseLocationService.updateWmsBaseLocation(wmsBaseLocation));
    }

    /**
     * 删除库位
     */
    @RequiresPermissions("wms:wmslocation:remove")
    @Log(title = "库位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{locationIds}")
    public AjaxResult remove(@PathVariable Long[] locationIds)
    {
        return toAjax(wmsBaseLocationService.deleteWmsBaseLocationByLocationIds(locationIds));
    }


    @RequiresPermissions("wms:wmslocation:list")
    @GetMapping("/getWarehouses")
    public AjaxResult list(WmsBaseWarehouse wmsBaseWarehouse)
    {
        wmsBaseWarehouse.setActiveFlag(WmsConstants.WMS_WAREHOUSE_ACTIVE_FLAG_YES);
        List<WmsBaseWarehouse> list = wmsBaseWarehouseService.selectWmsBaseWarehouseList(wmsBaseWarehouse);
        return success(list);
    }


    /**
     * 锁定库位
     */
    @RequiresPermissions("wms:wmslocation:lock")
    @Log(title = "库位", businessType = BusinessType.LOCK)
    @GetMapping("/lockWmsLocation/{locationId}")
    public AjaxResult lockWmsLocation(@PathVariable Long locationId)
    {
        return toAjax(wmsBaseLocationService.lockWmsBaseLocation(locationId));
    }

    /**
     * 解锁库位
     */
    @RequiresPermissions("wms:wmslocation:lock")
    @Log(title = "库位", businessType = BusinessType.UNLOCK)
    @GetMapping("/unlockWmsLocation/{locationId}")
    public AjaxResult unlockWmsLocation(@PathVariable Long locationId)
    {
        return toAjax(wmsBaseLocationService.unlockWmsBaseLocation(locationId));
    }

}
