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


import jakarta.servlet.http.HttpServletResponse;
import net.lab1024.sa.admin.module.business.wms.base.location.domain.form.*;
import net.lab1024.sa.admin.module.business.wms.base.location.domain.vo.LocationVO;
import net.lab1024.sa.admin.module.business.wms.base.location.service.LocationQueryService;
import net.lab1024.sa.admin.module.business.wms.base.location.service.LocationService;
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.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 霍锦
 * @Date 2024-11-18 14:17:31
 * @Copyright 友仓
 */

@RestController
@Tag(name = "库位信息")
public class LocationController {

    @Resource
    private LocationService locationService;

    @Resource
    private LocationQueryService locationQueryService;

    @Resource
    private ExportTaskService exportTaskService;

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

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

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

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

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

    @Operation(summary = "库位下拉查询")
    @PostMapping("/location/queryLocation")
    public ResponseDTO<List<LocationVO>> queryLocation(@RequestBody LocationSelect locationSelect) {
        return ResponseDTO.ok(locationQueryService.queryLocation(locationSelect));
    }

    @Operation(summary = "批量调整 霍锦")
    @PostMapping("/location/multipleAdjust")
    @SaCheckPermission("location:multipleAdjust")
    @OperateLog
    public ResponseDTO<String> multipleAdjust(@RequestBody @Valid MultipleAdjust multipleAdjust) {
        return locationService.multipleAdjust(multipleAdjust);
    }

    @Operation(summary = "批量新建 霍锦")
    @PostMapping("/location/multipleInsert")
    @SaCheckPermission("location:multipleInsert")
    @OperateLog
    public ResponseDTO<String> multipleInsert(@RequestBody @Valid MultipleInsert multipleInsert) {
        return locationService.multipleInsert(multipleInsert);
    }

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

    @Operation(summary = "导出 霍锦")
    @PostMapping("/location/exportLocations/{taskId}")
    @SaCheckPermission("location:exportLocations")
    @OperateLog
    public void exportLocations(@PathVariable String taskId, @RequestBody @Valid LocationQueryForm queryForm, HttpServletResponse response) {
        locationQueryService.exportLocation(taskId, queryForm, response);
        exportTaskService.cleanupTask(taskId);
    }

}
