package cn.axzo.msg.center.inside.notices.service.impl;

import cn.axzo.msg.center.dal.PendingMessageRecordDao;
import cn.axzo.msg.center.domain.entity.PendingMessageRecord;
import cn.axzo.msg.center.domain.enums.YesNoEnum;
import cn.axzo.msg.center.inside.notices.service.PendingMessageRecordService;
import cn.azxo.framework.common.model.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class PendingMessageRecordServiceImpl implements PendingMessageRecordService {

    @Autowired
    private PendingMessageRecordDao pendingMessageRecordDao;

    @Override
    public Page<PendingMessageRecord> page(PendingMessageRecordPageReq request) {

        IPage<PendingMessageRecord> page = request.toPage();
        page = pendingMessageRecordDao.lambdaQuery()
                .eq(Objects.nonNull(request.getIsOuIdMigrated()), PendingMessageRecord::getIsOuIdMigrated, request.getIsOuIdMigrated())
                .in(CollectionUtils.isNotEmpty(request.getIds()), PendingMessageRecord::getId, request.getIds())
                .eq(Objects.nonNull(request.getExecutorType()), PendingMessageRecord::getExecutorType, request.getExecutorType())
                .eq(Objects.nonNull(request.getOrgType()), PendingMessageRecord::getOrgType, request.getOrgType())
                .in(CollectionUtils.isNotEmpty(request.getTemplateCodes()), PendingMessageRecord::getTemplateCode, request.getTemplateCodes())
                .gt(Objects.nonNull(request.getIdGT()), PendingMessageRecord::getId, request.getIdGT())
                .eq(PendingMessageRecord::getIsDelete, YesNoEnum.NO.getCode())
                .orderByAsc(PendingMessageRecord::getId)
                .page(page);

        return Page.toPage(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }
}
