package cokr.xit.ens.modules.nvsigntalk.domain.repository;

import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.modules.nvsigntalk.domain.SendDetailNvSigntalk;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.Optional;

import static cokr.xit.ens.modules.common.domain.QSendMast.sendMast;
import static cokr.xit.ens.modules.nvsigntalk.domain.QSendDetailNvSigntalk.sendDetailNvSigntalk;

@RequiredArgsConstructor
public class SendDetailNvSigntalkRepositoryImpl implements SendDetailNvSigntalkRepositoryCustom {
    private final JPAQueryFactory query;

//
//    @Override
//    public List<SendDetailNvSigntalk> findAllFetchBySendMastIdAndMkBillUseYAndUrlIsNull(Long sendMastId, boolean urlIsNull) {
//        return query.selectFrom(sendDetailNvSigntalk)
//                .innerJoin(sendDetailNvSigntalk.sendMast, sendMast)
//                .fetchJoin()
//                .leftJoin(sendDetailNvSigntalk.kkoBillMast, kkoBillMast)
//                .fetchJoin()
//                .where(sendDetailNvSigntalk.sendMast.sendMastId.eq(sendMastId)
//                        .and(sendDetailNvSigntalk.mkBillUseYn.eq("Y"))
//                        .and(urlIsNull ? sendDetailNvSigntalk.kkoBillMast.url.isNull() : sendDetailNvSigntalk.kkoBillMast.url.isNotNull())
//                )
//                .fetch();
//    }

    @Override
    public Optional<SendDetailNvSigntalk> findFetchByClientDocIdAndDocId(String clientDocId, String docId) {
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(sendDetailNvSigntalk.docId.eq(docId));
        if (!CmmnUtil.isEmpty(clientDocId))
            builder.and(sendDetailNvSigntalk.clientDocId.eq(clientDocId));

        return Optional.ofNullable(query.selectFrom(sendDetailNvSigntalk)
                .innerJoin(sendDetailNvSigntalk.sendMast, sendMast).fetchJoin()
                .where(builder)
                .fetchOne());
    }
}
