package com.oqpo.api.web.controller.stockmng;

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.stockmng.StockMngService;
import com.oqpo.api.web.dto.request.stockmng.ProcStockRequest;
import com.oqpo.api.web.dto.request.stockmng.StockHistGridRequest;
import com.oqpo.api.web.dto.request.stockmng.StockMngGridRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/stock/mng")
public class StockMngController {

    @Autowired
    private StockMngService stockMngService;

    /**
     * 설명 : 재고관리 그리드 리스트
     *
     * @param stockMngGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "재고관리 그리드 리스트")
    @PostMapping("/detail-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@RequestBody @Valid StockMngGridRequest stockMngGridRequest) throws Exception {
        return ResponseEntity.ok(stockMngService.selectStockMngGridList(stockMngGridRequest.getSBrandId(), stockMngGridRequest.getSStoreId(),
                stockMngGridRequest.getSWhsId(), stockMngGridRequest.getSLocation(),
                stockMngGridRequest.getSItemClass1(), stockMngGridRequest.getSItemClass2(), stockMngGridRequest.getSItemClass3(), stockMngGridRequest.getSItemClass4(),
                stockMngGridRequest.getSItemId(), stockMngGridRequest.getSItemNm(), stockMngGridRequest.toDTO(stockMngGridRequest)));
    }

    /**
     * 설명 : 재고이력 그리드 리스트
     *
     * @param stockHistGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "재고이력 그리드 리스트")
    @PostMapping("/hist-grid-list")
    public ResponseEntity<GridResponse> histGridList(@RequestBody @Valid StockHistGridRequest stockHistGridRequest) throws Exception {
        return ResponseEntity.ok(stockMngService.selectStockHistGridList(stockHistGridRequest.getSBrandId(), stockHistGridRequest.getSWhsId(),
                stockHistGridRequest.getSLocation(), stockHistGridRequest.getSItemId(), stockHistGridRequest.getSInoutCd(),
                stockHistGridRequest.getFromDt(), stockHistGridRequest.getToDt(),
                stockHistGridRequest.toDTO(stockHistGridRequest)));
    }

    /**
     * 설명 : 재고 처리
     *
     * @param userId
     * @param procStockRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "재고 처리")
    @PostMapping("/proc-stock")
    public ResponseEntity<SaveResponse> procStock(@ApiIgnore String userId, @RequestBody @Valid ProcStockRequest procStockRequest) throws Exception {
        stockMngService.procStock(userId, procStockRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

}
