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

import com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity;
import com.oqpo.api.enums.DataMessageCode;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.service.pomng.PchOrdService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.pomng.pchOrd.*;
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.pomng.pchReq.PchReqInfoResponse;
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;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/pomng/po")
@Api(tags = {"구매발주 - 공급수 수주 관리 (PchOrdController) -- 진행중"})
public class PchOrdController {
    @Autowired
    private PchOrdService pchOrdService;

    //
    /**
     * 설명 : 구매요청 발주 생성 가능 여부 체크
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "구매요청 발주가능 여부 체크")
    @PostMapping("/chk-pchOrd")
    public ResponseEntity<DataResponse> chkPchReqUseOrd(@ApiIgnore String userId, @RequestBody @Valid PchReqOrdInitRequest pchReqOrdInitRequest) throws Exception {
        String chkUsePo = pchOrdService.chkPchReqUseOrd(userId, pchReqOrdInitRequest);
        DataMessageCode message = null;
        if ("Y".equals(chkUsePo)) {
            message = DataMessageCode.OK_PCHORD;
        } else {
            message = DataMessageCode.NOT_NONPO;
        }
        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-pchOrd")
    public ResponseEntity<PchOrdInitInfoResponse> selectPchOrdInitInfo(@ApiIgnore String userId, @RequestBody @Valid PchReqOrdInitRequest pchReqOrdInitRequest) throws Exception {
        return ResponseEntity.ok(pchOrdService.selectRecPchInitInfo(userId, pchReqOrdInitRequest));
    }

    /**
     * 설명 : 신규 발주생성  초기 정보
     *
     * @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-poOrd")
    public ResponseEntity<PoOrdInitInfoResponse> selectPoOrdInitInfo(@ApiIgnore String userId,@RequestParam(value = "brandId") String brandId) throws Exception {
        return ResponseEntity.ok(pchOrdService.selectPoInitInfo(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-pchPo")
    public ResponseEntity<SaveResponse> savePchOdr(@ApiIgnore String userId, @RequestBody @Valid SavePchOrdRequest savePchOrdRequest) throws Exception {
        pchOrdService.savePchOrdInfo(userId,  savePchOrdRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 구매발주 그리드 리스트
     *
     * @param pchOrdSearchGridRequest
     * @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> detailPoSearchGridList(@RequestBody @Valid PchOrdSearchGridRequest pchOrdSearchGridRequest) throws Exception {
        return ResponseEntity.ok(pchOrdService.selectPchOdrGridList(pchOrdSearchGridRequest.getSBrandId(), pchOrdSearchGridRequest.getFromDt(), pchOrdSearchGridRequest.getToDt(), pchOrdSearchGridRequest.getSPchOdrStCd(), pchOrdSearchGridRequest.getSSpplyId(), pchOrdSearchGridRequest.getSWhsId(), pchOrdSearchGridRequest.getSItemNm(),  pchOrdSearchGridRequest.toDTO(pchOrdSearchGridRequest)));
    }

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


    /**
     * 설명 : 구매발주 취소처리
     *
     * @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-pchPo")
    public ResponseEntity<SaveResponse> cancelPchOdr(@ApiIgnore String userId, @RequestBody @Valid CancelPchOrdDtlRequest cancelPchOrdDtlRequest) throws Exception {
        pchOrdService.cancelPchOrdDtl(userId,  cancelPchOrdDtlRequest);
        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
     * @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-pchSteamlinePo")
    public ResponseEntity<SaveResponse> savePchSteamlineOdr(@ApiIgnore String userId, @RequestBody @Valid SavePchSteamlineOrdRequest savePchSteamlineOrdRequest) throws Exception {
        pchOrdService.savePchSteamlineOrdInfo(userId,  savePchSteamlineOrdRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

}
