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

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
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.ItemAddForm;
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.form.ItemUpdateForm;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.vo.ItemVO;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.vo.ItemsExcelVO;
import net.lab1024.sa.admin.module.business.wms.base.item.service.ItemQueryService;
import net.lab1024.sa.admin.module.business.wms.base.item.service.ItemService;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.form.ItemAddForm;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.form.ItemQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.item.service.ItemQueryService;
import net.lab1024.sa.admin.module.business.wms.base.item.service.ItemService;
import net.lab1024.sa.admin.module.business.wms.excel.ExportTaskService;
import net.lab1024.sa.base.common.domain.PageResult;
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 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-25 17:08:18
 * copyright 友仓
 */

@RestController
@Tag(name = "物料信息")
@Slf4j
public class ItemController {

    @Resource
    private ItemService itemService;

    @Resource
    private ItemQueryService itemQueryService;

    @Resource
    private ExportTaskService exportTaskService;

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

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

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

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

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

    @Operation(summary = "物料下拉查询")
    @PostMapping("/item/queryItem")
    public ResponseDTO<List<ItemEntity>> queryItem(@RequestBody ItemSelect itemSelect) {
        return ResponseDTO.ok(itemQueryService.queryItem(itemSelect));
    }

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

    @Operation(summary = "导出 霍锦")
    @PostMapping("/item/exportItems/{taskId}")
    @SaCheckPermission("item:exportItems")
    @OperateLog
    public void exportItems(@PathVariable String taskId,@RequestBody @Valid ItemQueryForm queryForm, HttpServletResponse response){
        itemQueryService.exportItem(taskId, queryForm,response);
        exportTaskService.cleanupTask(taskId);
    }
}
