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

import com.oqpo.api.enums.DataMessageCode;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.pomng.PchOrdService;
import com.oqpo.api.service.rtnmng.RtnOdrService;
import com.oqpo.api.web.dto.request.pomng.pchOrd.*;
import com.oqpo.api.web.dto.request.rtnmng.CancelRtnOrdDtlRequest;
import com.oqpo.api.web.dto.request.rtnmng.RtnOrdSearchGridRequest;
import com.oqpo.api.web.dto.request.rtnmng.RtnReqOrdInitRequest;
import com.oqpo.api.web.dto.request.rtnmng.SaveRtnOrdRequest;
import com.oqpo.api.web.dto.response.DataResponse;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.pomng.pchOrd.*;
import com.oqpo.api.web.dto.response.rtnmng.rtnOrd.RtnOdrInfoResponse;
import com.oqpo.api.web.dto.response.rtnmng.rtnOrd.RtnOrdInitInfoResponse;
import com.oqpo.api.web.dto.response.rtnmng.rtnOrd.RtnOrdNInitInfoResponse;
import io.swagger.annotations.Api;
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/rtnmng/rtn")
@Api(tags = {"반품발주 -"})
public class RtnOrdController {
    @Autowired
    private RtnOdrService rtnOdrService;

    //
    /**
     * 설명 : 구매요청 발주 생성 가능 여부 체크
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "반품요청 반품 발주가능 여부 체크")
    @PostMapping("/chk-rtnOrd")
    public ResponseEntity<DataResponse> chkRtnReqUseOrd(@ApiIgnore String userId, @RequestBody @Valid RtnReqOrdInitRequest rtnReqOrdInitRequest) throws Exception {
        String chkUsePo = rtnOdrService.chkRtnReqUseOrd(userId, rtnReqOrdInitRequest);
        DataMessageCode message = null;
        if ("Y".equals(chkUsePo)) {
            message = DataMessageCode.OK_PCHORD;
        } else {
            message = DataMessageCode.NOT_NONRTN;
        }
        return ResponseEntity.ok(DataResponse.toDTO(message));
    }

    /**
     * 설명 : 구매요청 발주생성  초기 정보
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "반품요청 반품발주생성  초기 정보")
    @PostMapping("/init-rtnOrd")
    public ResponseEntity<RtnOrdInitInfoResponse> selectPchOrdInitInfo(@ApiIgnore String userId, @RequestBody @Valid RtnReqOrdInitRequest rtnReqOrdInitRequest) throws Exception {
        return ResponseEntity.ok(rtnOdrService.selectRecRtnInitInfo(userId, rtnReqOrdInitRequest));
    }

    /**
     * 설명 : 신규 발주생성  초기 정보
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "brandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "신규반품 발주생성  초기 정보")
    @GetMapping("/init-new-rtnOrd")
    public ResponseEntity<RtnOrdNInitInfoResponse> selectPoOrdInitInfo(@ApiIgnore String userId, @RequestParam(value = "brandId") String brandId) throws Exception {
        return ResponseEntity.ok(rtnOdrService.selectRtnInitInfo(userId, brandId));
    }

    /**
     * 설명 : 구매발주 정보 저장
     *
     * @param userId
     * @param savePchReqlRequest
     * @return SaveResponse
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "반품발주 정보 저장")
    @PostMapping("/save-rtnPo")
    public ResponseEntity<SaveResponse> saveRtnOdr(@ApiIgnore String userId, @RequestBody @Valid SaveRtnOrdRequest saveRtnOrdRequest) throws Exception {
        rtnOdrService.saveRtnOdrInfo(userId,  saveRtnOrdRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 구매발주 그리드 리스트
     *
     * @param rtnOrdSearchGridRequest
     * @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> detailRtnSearchGridList(@RequestBody @Valid RtnOrdSearchGridRequest rtnOrdSearchGridRequest) throws Exception {
        return ResponseEntity.ok(rtnOdrService.selectRtnOdrGridList(rtnOrdSearchGridRequest.getSBrandId(), rtnOrdSearchGridRequest.getFromDt(), rtnOrdSearchGridRequest.getToDt(), rtnOrdSearchGridRequest.getSRtnOdrStCd(), rtnOrdSearchGridRequest.getSRtnWhsId(), rtnOrdSearchGridRequest.getSColDvsn(), rtnOrdSearchGridRequest.getSWhsId(), rtnOrdSearchGridRequest.getSSpplyId(), rtnOrdSearchGridRequest.getSItemNm(),  rtnOrdSearchGridRequest.toDTO(rtnOrdSearchGridRequest)));
    }

    /**
     * 설명 : 구매발주 정보
     *
     * @param
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "rtnPodrUnqNo", value = "반품발주번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "반품발주 정보")
    @GetMapping("/info-rtnOdr")
    public ResponseEntity<RtnOdrInfoResponse> infoPchReq(@RequestParam(value = "rtnPodrUnqNo") String rtnPodrUnqNo) throws Exception {
        return ResponseEntity.ok(rtnOdrService.selectrtnOdrInfo(rtnPodrUnqNo));
    }


    /**
     * 설명 : 구매발주 취소처리
     *
     * @param userId
     * @param savePchReqlRequest
     * @return SaveResponse
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "반품발주 취소 처리")
    @PostMapping("/cancel-rtnPo")
    public ResponseEntity<SaveResponse> cancelRtnOdr(@ApiIgnore String userId, @RequestBody @Valid CancelRtnOrdDtlRequest cancelRtnOrdDtlRequest) throws Exception {
        rtnOdrService.cancelRtnOrdDtl(userId,  cancelRtnOrdDtlRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }



    /**
     * 설명 : 공급사 수주내역
     *
     * @param spplyOdrPoGridRequest
     * @return
     * @throws Exception


    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 공급사 수주내역 그리드 리스트")
    @PostMapping("/spply-po-grid-list")
    public ResponseEntity<GridResponse> selectSpplyPoGridList(@RequestBody @Valid SpplyOdrPoGridRequest spplyOdrPoGridRequest) throws Exception {
        return ResponseEntity.ok(pchOrdService.selectSpplyPoGridList(spplyOdrPoGridRequest.getSBrandId(), spplyOdrPoGridRequest.getFromDt(), spplyOdrPoGridRequest.getToDt(), spplyOdrPoGridRequest.getSSpplyId(), spplyOdrPoGridRequest.getSWhsId(), spplyOdrPoGridRequest.getSPchOdrStCd(),  spplyOdrPoGridRequest.getSItemNm(),  spplyOdrPoGridRequest.toDTO(spplyOdrPoGridRequest)));
    }
     */
    /**
     * 설명 : 공급사 수주상세 정보
     *
     * @param
     * @return
     * @throws Exception

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "brandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "spplyId", value = "공급사아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pchPodrUnqNo", value = "구매발주번호", required = true, dataType = "String", paramType = "query")

    })
    @ApiOperation(value = "공급사 수주상세 정보")
    @GetMapping("/info-spply-pchOdr")
    public ResponseEntity<SpplyPchOdrInfoResponse> infoSpplyPchReq(@RequestParam(value = "brandId") String brandId, @RequestParam(value = "pchPodrUnqNo") String pchPodrUnqNo, @RequestParam(value = "spplyId") String spplyId) throws Exception {

        //System.out.println("============================>" +authTpCd );
        //if ("60".equals(authTpCd)  && !spplyId1.equals(spplyId)) {
        //    throw new GlobalException(SystemMessageCode.ERR_NONSTOREID);
        //}

        return ResponseEntity.ok(pchOrdService.selectSpplyPchOdrInfo(brandId, pchPodrUnqNo, spplyId));
    }
     */

    /**
     * 설명 : 발주 가능 수량 리스트 출력
     *
     * @param userId
     * @return
     * @throws Exception

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "발주 가능 수량 리스트 출력")
    @PostMapping("/whs-passqty-list")
    public ResponseEntity<PchOrdWhsQtyResponse> selectWhsPassQty(@RequestBody @Valid PchReqOrdPassQtyRequest pchReqOrdPassQtyRequest) throws Exception {
        return ResponseEntity.ok(pchOrdService.selectWhsPassQty(pchReqOrdPassQtyRequest));
    }
     */
}
