package com.xueyi.system.emcs.manager.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.Page;
import com.xueyi.common.core.constant.basic.OperateConstants;
import com.xueyi.common.core.utils.core.CollUtil;
import com.xueyi.common.core.utils.page.PageUtil;
import com.xueyi.common.web.entity.domain.SlaveRelation;
import com.xueyi.system.api.digitalmans.domain.dto.DmManDeviceDto;
import com.xueyi.system.digitalmans.manager.impl.DmManDeviceManager;
import com.xueyi.system.emcs.domain.po.DmDeviceLogFilePo;
import com.xueyi.system.emcs.domain.dto.DmDeviceLogFileDto;
import com.xueyi.system.emcs.domain.query.DmDeviceLogFileQuery;
import com.xueyi.system.emcs.domain.model.DmDeviceLogFileConverter;
import com.xueyi.system.emcs.mapper.DmDeviceLogFileMapper;
import com.xueyi.common.web.entity.manager.impl.BaseManagerImpl;
import com.xueyi.system.emcs.manager.IDmDeviceLogFileManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

import static com.xueyi.system.api.exception.domain.merge.MergeGroup.LOG_DmDevice_GROUP;

/**
 * 日志文件信息记录管理 数据封装层处理
 *
 * @author xueyi
 */
@Component
public class DmDeviceLogFileManager extends BaseManagerImpl<DmDeviceLogFileQuery, DmDeviceLogFileDto, DmDeviceLogFilePo, DmDeviceLogFileMapper, DmDeviceLogFileConverter> implements IDmDeviceLogFileManager {

    @Autowired
    DmManDeviceManager manDeviceManager;

    @Override
    protected List<SlaveRelation> subRelationInit() {
        List result = new ArrayList<SlaveRelation>();
        result.add(new SlaveRelation(LOG_DmDevice_GROUP, DmManDeviceManager.class, OperateConstants.SubOperateLimit.ONLY_SEL));
        return result;
    }

    @Override
    public List<DmDeviceLogFileDto> selectList(DmDeviceLogFileQuery query) {
        // 因为Page在执行第一次查询时会强制注入Limit，所以导致业务逻辑错误，需要先清除Page信息后查询数字人列表，再重新设置Page信息后查询日志
        Page page = PageUtil.getLocalPage();
        System.out.println(page.getPageNum() + " " + page.getPageSize());
        PageUtil.clearPage();
        List<DmManDeviceDto> manDeviceDtos = manDeviceManager.selectList(query.getManDeviceQuery());
        List<Long> manDeviceIds = manDeviceDtos.stream().map(DmManDeviceDto::getId).collect(Collectors.toList());
        if (CollUtil.isEmpty(manDeviceIds)) {
            return new Page<>();
        }
        PageUtil.startPage(page.getPageNum(), page.getPageSize());
        return subMerge(mapperDto(super.baseMapper.selectList(selectListQuery(query)
                        .and(StringUtils.isNotEmpty(query.getStartTime()) && StringUtils.isNotEmpty(query.getEndTime()), t->{
                            t.ge(DmDeviceLogFilePo::getCreateTime,query.getStartTime());
                            t.le(DmDeviceLogFilePo::getCreateTime,query.getEndTime());
                        })
                .and(CollUtil.isNotEmpty(manDeviceIds),i->i.in(DmDeviceLogFilePo::getManDeviceId, manDeviceIds)))));
    }

}