package net.lab1024.sa.admin.module.business.wms.base.stock.controller;

import jakarta.servlet.http.HttpServletResponse;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.form.ItemQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.form.ItemSelect;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.vo.ItemsExcelVO;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.entity.StockEntity;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.form.StockAddForm;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.form.StockQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.form.StockSelect;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.form.StockUpdateForm;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.vo.StockVO;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.vo.StocksExcelVO;
import net.lab1024.sa.admin.module.business.wms.base.stock.service.StockQueryService;
import net.lab1024.sa.admin.module.business.wms.base.stock.service.StockService;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.form.StockQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.form.StockUpdateForm;
import net.lab1024.sa.admin.module.business.wms.excel.ExportTaskService;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.common.util.SmartExcelUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import org.springframework.web.bind.annotation.*;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 容器信息 Controller
 *
 * @author 霍锦
 * @since 2024-11-26 11:37:48
 * copyright 友仓
 */

@RestController
@Tag(name = "容器信息")
public class StockController {

    @Resource
    private StockService stockService;

    @Resource
    private StockQueryService stockQueryService;

    @Resource
    private ExportTaskService exportTaskService;

    @Operation(summary = "分页查询 @author 霍锦")
    @PostMapping("/stock/queryPage")
    @SaCheckPermission("stock:query")
    public ResponseDTO<PageResult<StockVO>> queryPage(@RequestBody @Valid StockQueryForm queryForm) {
        return ResponseDTO.ok(stockQueryService.queryPage(queryForm));
    }

    @Operation(summary = "添加 @author 霍锦")
    @PostMapping("/stock/add")
    @SaCheckPermission("stock:add")
    @OperateLog
    public ResponseDTO<String> add(@RequestBody @Valid StockAddForm addForm) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        addForm.setCreateUserId(requestUser.getUserId());
        addForm.setCreateUserName(requestUser.getUserName());
        return stockService.add(addForm);
    }

    @Operation(summary = "更新 @author 霍锦")
    @PostMapping("/stock/update")
    @SaCheckPermission("stock:update")
    @OperateLog
    public ResponseDTO<String> update(@RequestBody @Valid StockUpdateForm updateForm) {
        return stockService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author 霍锦")
    @PostMapping("/stock/batchDelete")
    @SaCheckPermission("stock:batchDelete")
    @OperateLog
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<Long> idList) {
        return stockService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author 霍锦")
    @GetMapping("/stock/delete")
    @SaCheckPermission("stock:delete")
    @OperateLog
    public ResponseDTO<String> delete(@RequestParam Long stockId) {
        return stockService.delete(stockId);
    }

    @Operation(summary = "容器下拉查询")
    @PostMapping("/stock/queryStock")
    public ResponseDTO<List<StockEntity>> queryStock(@RequestBody StockSelect stockSelect) {
        return ResponseDTO.ok(stockQueryService.queryStock(stockSelect));
    }

    @Operation(summary = "导入 霍锦")
    @PostMapping("/stock/importStocks")
    @SaCheckPermission("stock:importStocks")
    @OperateLog
    public ResponseDTO<String> importStocks(@RequestParam MultipartFile file) {
        return stockService.importStocks(file);
    }

    @Operation(summary = "导出 霍锦")
    @PostMapping("/stock/exportStocks/{taskId}")
    @SaCheckPermission("stock:exportStocks")
    @OperateLog
    public void stock(@PathVariable String taskId, @RequestBody @Valid StockQueryForm queryForm, HttpServletResponse response){
        stockQueryService.exportStock(taskId, queryForm, response);
        exportTaskService.cleanupTask(taskId);
    }
}
