package com.ruoyi.web.controller.warehouse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.warehouse.domain.WmsBatchActionUpdateRequest;
import com.ruoyi.warehouse.domain.WmsBatchItem;
import com.ruoyi.warehouse.domain.WmsBatchJob;
import com.ruoyi.warehouse.service.IWmsBatchImportService;

@RestController
@RequestMapping("/warehouse/batch")
public class WmsBatchImportController extends BaseController
{
    @Autowired
    private IWmsBatchImportService batchImportService;

    @GetMapping("/list")
    public TableDataInfo list(WmsBatchJob job)
    {
        startPage();
        return getDataTable(batchImportService.selectJobList(job));
    }

    @PostMapping("/upload")
    @Log(title = "WMS批量导入", businessType = BusinessType.IMPORT)
    public AjaxResult upload(MultipartFile file) throws Exception
    {
        return success(batchImportService.uploadExcel(file, getUsername()));
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(batchImportService.selectJobById(id));
    }

    @GetMapping("/{jobId}/items")
    public AjaxResult items(@PathVariable Long jobId)
    {
        return success(batchImportService.selectItemList(jobId));
    }

    @PutMapping("/item")
    public AjaxResult updateItem(@RequestBody WmsBatchItem item)
    {
        return success(batchImportService.updateItem(item));
    }

    @PutMapping("/{jobId}/items/action")
    public AjaxResult updateItemAction(@PathVariable Long jobId, @RequestBody WmsBatchActionUpdateRequest request)
    {
        return toAjax(batchImportService.updateItemAction(jobId, request.getActionType(), request.getItemIds()));
    }

    @DeleteMapping("/item/{id}")
    public AjaxResult deleteItem(@PathVariable Long id)
    {
        return toAjax(batchImportService.deleteItem(id));
    }

    @PostMapping("/{jobId}/confirm")
    @Log(title = "WMS批量确认执行", businessType = BusinessType.INSERT)
    public AjaxResult confirm(@PathVariable Long jobId)
    {
        return success(batchImportService.confirmJob(jobId, getUsername()));
    }
}
