package com.tmerclub.cloud.order.controller.supplier;

import com.tmerclub.cloud.common.constant.SysTypeEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.order.dto.AllotOrderDTO;
import com.tmerclub.cloud.order.service.AllotOrderService;
import com.tmerclub.cloud.order.vo.AllotOrderVO;
import com.tmerclub.cloud.order.vo.AllotSpuImportVO;
import com.tmerclub.cloud.order.vo.AllotSpuInboundImportVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author gaozijie
 * @date 2023-11-20
 */
@Tag(name = "调拨订单(供应商端)")
@RestController("supplierAllotOrderController")
@RequestMapping("/s/allot_order")
public class AllotOrderController {

    @Autowired
    private AllotOrderService allotOrderService;

    @Operation(summary = "获取调拨订单详情")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @GetMapping("/info/{allotOrderId}")
    public ServerResponseEntity<AllotOrderVO> getAllotOrder(@PathVariable("allotOrderId") Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        AllotOrderVO allotOrderVO = allotOrderService.getAllotOrder(allotOrderDTO);
        return ServerResponseEntity.success(allotOrderVO);
    }

    @Operation(summary = "新增调拨订单")
    @PostMapping
    public ServerResponseEntity<Void> addAllotOrder(@RequestBody AllotOrderDTO allotOrderDTO) {
        allotOrderDTO.setShopId(AuthUserContext.get().getTenantId());
        allotOrderDTO.setUserId(AuthUserContext.get().getUserId());
        allotOrderDTO.setSysType(SysTypeEnum.SUPPLIER.value());
        allotOrderService.addAllotOrder(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "入库调拨订单")
    @PutMapping("/inbound")
    public ServerResponseEntity<Void> inbound(@RequestBody AllotOrderDTO allotOrderDTO) {
        allotOrderDTO.setShopId(AuthUserContext.get().getTenantId());
        allotOrderDTO.setSysType(SysTypeEnum.SUPPLIER.value());
        allotOrderService.inbound(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "完成调拨订单")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @PutMapping("/complete/{allotOrderId}")
    public ServerResponseEntity<Void> finish(@PathVariable("allotOrderId") Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        allotOrderService.completeOrder(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "作废调拨订单")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @DeleteMapping("/nullify/{allotOrderId}")
    public ServerResponseEntity<Void> nullify(@PathVariable("allotOrderId") Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        allotOrderService.nullifyOrder(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "下载商品excel模板")
    @GetMapping("/download_model")
    public void downloadModel(HttpServletResponse response) {
        allotOrderService.downloadModel(response);
    }

    @Operation(summary = "导入商品excel")
    @Parameters({
            @Parameter(name = "outWarehouseId", description = "调出仓库id"),
            @Parameter(name = "inWarehouseId", description = "调入仓库id")
    })
    @PostMapping("/importExcel")
    public ServerResponseEntity<AllotSpuImportVO> importExcel(Long outWarehouseId,
                                                              Long inWarehouseId,
                                                              @RequestPart("excelFile") MultipartFile excelFile) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setOutWarehouseId(outWarehouseId);
        allotOrderDTO.setInWarehouseId(inWarehouseId);
        return ServerResponseEntity.success(allotOrderService.parseFile(excelFile, allotOrderDTO));
    }

    @Operation(summary = "下载商品入库excel模板")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @GetMapping("/download_inbound_model")
    public void downloadInboundModel(HttpServletResponse response, Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        allotOrderService.downloadInboundModel(response, allotOrderDTO);
    }

    @Operation(summary = "导入商品入库excel")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @PostMapping("/import_inbound_excel")
    public ServerResponseEntity<AllotSpuInboundImportVO> importInboundExcel(Long allotOrderId,
                                                                            @RequestPart("excelFile") MultipartFile excelFile) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        return ServerResponseEntity.success(allotOrderService.parseInboundFile(excelFile, allotOrderDTO));
    }

    private AllotOrderDTO getAllotOrderDTO() {
        AllotOrderDTO allotOrderDTO = new AllotOrderDTO();
        allotOrderDTO.setShopId(AuthUserContext.get().getTenantId());
        allotOrderDTO.setSysType(SysTypeEnum.SUPPLIER.value());
        return allotOrderDTO;
    }
}
