package com.oqpo.api.service.sttlmng;


import com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity;
import com.oqpo.api.entity.loanmng.LoanMgntDtlHstEntity;
import com.oqpo.api.entity.oper.UserMngEntity;
import com.oqpo.api.entity.rtnmng.RtnProcInfoPtclEntity;
import com.oqpo.api.entity.settmng.StlMgntBaseInfoEntity;
import com.oqpo.api.entity.settmng.StlMgntDtlPtclEntity;
import com.oqpo.api.entity.stinfo.StoreBaseInfoEntity;
import com.oqpo.api.entity.stockmng.WhsProcInfoPtclEntity;
import com.oqpo.api.enums.*;
import com.oqpo.api.mapper.loanmng.DsptMngMapper;
import com.oqpo.api.mapper.oper.UserMngMapper;
import com.oqpo.api.mapper.stinfo.StoreBaseInfoMapper;
import com.oqpo.api.mapper.sttlmng.SttlReqMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.util.DateUtil;
import com.oqpo.api.util.StringUtil;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.sttlmng.*;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.loanmng.StlDtlListResponse;
import com.oqpo.api.web.dto.response.sttlmng.SttlMngInfoResponse;
import com.oqpo.api.web.dto.response.sttlmng.SttlReqResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SttlInspectStateService extends CommonService {

    @Autowired
    private SttlReqMapper sttlReqMapper;

    /* 정산확정  */
    @Transactional
    public void confirmSttl(String userId, SttlConfirmRequest sttlConfirmRequest) throws Exception {
        // 정산기본정보 조회
        StlMgntBaseInfoEntity entity = sttlReqMapper.selectStlMgntBaseInfo(sttlConfirmRequest.getSttlMgntUnqNo());

        // 정산관리기본정보 - 정산상태코드 변경
        StlMgntBaseInfoEntity infoEntity = new StlMgntBaseInfoEntity();
        infoEntity.setSttlMgntUnqNo(sttlConfirmRequest.getSttlMgntUnqNo());
        infoEntity.setSttlStCd(SttlStCd.STTL_CONFIRM.getCd());
        infoEntity.setSttlDt(DateUtil.getCurrentDate());
        infoEntity.setSttlAmt(entity.getSttlReqAmt()); // 정산요청금액을 정산금액으로 지정한다.
        sttlReqMapper.updateStlMgntBaseInfo4SttlConfirm(userId, infoEntity);
    }

    /* 정산반려  */
    @Transactional
    public void rejectSttl(String userId, SttlRejectRequest sttlRejectRequest) throws Exception {
        // 정산관리기본정보 - 정산상태코드, 정산반려사유 변경
        StlMgntBaseInfoEntity infoEntity = new StlMgntBaseInfoEntity();
        infoEntity.setSttlMgntUnqNo(sttlRejectRequest.getSttlMgntUnqNo());
        infoEntity.setSttlStCd(SttlStCd.STTL_REJECT.getCd());
        infoEntity.setSttlRjctRsn(sttlRejectRequest.getSttlRjctRsn());
        sttlReqMapper.updateStlMgntBaseInfo4SttlReject(userId, infoEntity);
    }

}
