package com.hw.wms.controller;

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

import com.hw.wms.domain.WmsBaseCategory;
import com.hw.wms.domain.WmsWarehouseMaterial;
import com.hw.wms.service.IWmsBaseCategoryService;
import com.hw.wms.service.IWmsWarehouseMaterialService;
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.WmsBaseWarehouse;
import com.hw.wms.service.IWmsBaseWarehouseService;
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("/wmswarehouse")
public class WmsBaseWarehouseController extends BaseController
{
    @Autowired
    private IWmsBaseWarehouseService wmsBaseWarehouseService;

    @Autowired
    private IWmsBaseCategoryService wmsBaseCategoryService;

    @Autowired
    private IWmsWarehouseMaterialService wmsWarehouseMaterialService;

    /**
     * 查询仓库列表
     */
    @RequiresPermissions("wms:wmswarehouse:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsBaseWarehouse wmsBaseWarehouse)
    {
        startPage();
        List<WmsBaseWarehouse> list = wmsBaseWarehouseService.selectWmsBaseWarehouseJoinList(wmsBaseWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库列表
     */
    @RequiresPermissions("wms:wmswarehouse:export")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsBaseWarehouse wmsBaseWarehouse)
    {
        List<WmsBaseWarehouse> list = wmsBaseWarehouseService.selectWmsBaseWarehouseJoinList(wmsBaseWarehouse);
        ExcelUtil<WmsBaseWarehouse> util = new ExcelUtil<WmsBaseWarehouse>(WmsBaseWarehouse.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @RequiresPermissions("wms:wmswarehouse:query")
    @GetMapping(value = "/{warehouseId}")
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(wmsBaseWarehouseService.selectWmsBaseWarehouseByWarehouseId(warehouseId));
    }

    /**
     * 新增仓库
     */
    @RequiresPermissions("wms:wmswarehouse:add")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsBaseWarehouse wmsBaseWarehouse)
    {
        return toAjax(wmsBaseWarehouseService.insertWmsBaseWarehouse(wmsBaseWarehouse));
    }

    /**
     * 修改仓库
     */
    @RequiresPermissions("wms:wmswarehouse:edit")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsBaseWarehouse wmsBaseWarehouse)
    {
        return toAjax(wmsBaseWarehouseService.updateWmsBaseWarehouse(wmsBaseWarehouse));
    }

    /**
     * 删除仓库
     */
    @RequiresPermissions("wms:wmswarehouse:remove")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds)
    {
        return toAjax(wmsBaseWarehouseService.deleteWmsBaseWarehouseByWarehouseIds(warehouseIds));
    }


    /**
     * 查询仓库类别列表，提供给添加、修改、查询仓库等使用
     */
    @RequiresPermissions("wms:wmswarehouse:list")
    @GetMapping("/getWarehouseCategories")
    public AjaxResult getWarehouseCategories(WmsBaseCategory wmsBaseCategory)
    {
        List<WmsBaseCategory> list = wmsBaseCategoryService.selectWmsBaseCategoryList(wmsBaseCategory);
        return success(list);
    }

    /**
     * 查询已分配物料
     */
    @RequiresPermissions("wms:wmswarehouse:materialallocate")
    @GetMapping("/selectWmsWarehouseMaterialList")
    public TableDataInfo selectWmsWarehouseMaterialList(WmsWarehouseMaterial wmsWarehouseMaterial)
    {
        startPage();
        List<WmsWarehouseMaterial> warehouseMaterials = wmsWarehouseMaterialService.selectWmsWarehouseMaterialJoinList(wmsWarehouseMaterial);
        return getDataTable(warehouseMaterials);
    }

    /**
     * 分配仓库可入物料
     */
    @RequiresPermissions("wms:wmswarehouse:materialallocate")
    @Log(title = "仓库管理", businessType = BusinessType.ALLOCATE)
    @PutMapping("/allocateMaterials")
    public AjaxResult allocateMaterials(Long warehouseId, Long[] materialIds)
    {
        wmsWarehouseMaterialService.batchInsertWarehouseMaterial(warehouseId, materialIds);
        return success();
    }


    /**
     * 删除仓库配置的物料信息
     */
    @RequiresPermissions("wms:wmswarehouse:materialallocate")
    @Log(title = "仓库管理", businessType = BusinessType.UNALLOCATE)
    @PostMapping("/unallocateMaterials")
    public AjaxResult unallocateMaterials(Long[] warehouseMaterialIds)
    {
        return toAjax(wmsWarehouseMaterialService.deleteWmsWarehouseMaterialByWarehouseMaterialIds(warehouseMaterialIds));
    }


//    /**
//     * 根据仓库分配可入库物料信息
//     */
//    @RequiresPermissions("wms:wmswarehouse:materialallocate")
//    @GetMapping("/allocateMaterial/{warehouseId}")
//    public AjaxResult allocateMaterial(@PathVariable("warehouseId") Long warehouseId)
//    {
//        AjaxResult ajax = AjaxResult.success();
//        WmsBaseWarehouse warehouse = wmsBaseWarehouseService.selectWmsBaseWarehouseByWarehouseId(warehouseId);
//
//        WmsWarehouseMaterial queryWarehouseMaterial = new WmsWarehouseMaterial();
//        queryWarehouseMaterial.setWarehouseId(warehouseId);
//        queryWarehouseMaterial.setStorageType(WmsConstants.WMS_WAREHOUSE_MATERIAL_STORAGE_TYPE_MATERIAL);
//
//        List<WmsWarehouseMaterial> warehouseMaterials = wmsWarehouseMaterialService.selectWmsWarehouseMaterialList(queryWarehouseMaterial);
//        ajax.put("warehouse", warehouse);
//        ajax.put("warehouseMaterials", warehouseMaterials);
//        return ajax;
//    }


}
