package com.sunyard.ssp.modules.monitor.log.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunyard.ssp.modules.monitor.log.entity.AuditLog;
import com.sunyard.ssp.modules.monitor.log.entity.LogSign;
import com.sunyard.ssp.modules.monitor.log.mapper.LogSignMapper;
import com.sunyard.ssp.modules.monitor.log.service.IAuditLogService;
import com.sunyard.ssp.modules.monitor.log.service.ILogSignService;
import com.sunyard.ssp.vo.SearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Auther: wp
 * @Date: 2022/2/17 11:25
 * @Description:
 */
@Slf4j
@Service
@Transactional
public class LogSignServiceImpl extends ServiceImpl<LogSignMapper, LogSign> implements ILogSignService {

    @Autowired
    private IAuditLogService auditLogService;

    @Override
    public Page<LogSign> findByCondition(LogSign logSign, SearchVo searchVo, Pageable pageable) {
        LambdaQueryWrapper<LogSign> wrapper = new QueryWrapper<LogSign>().lambda();

        if(null!=logSign.getOperatorId()){
            wrapper.eq(LogSign::getOperatorId,logSign.getOperatorId());
        }

        if(StrUtil.isNotBlank(logSign.getOperatorName())){
            wrapper.like(LogSign::getOperatorName,logSign.getOperatorName());
        }

        //创建时间
        if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
            Date start = DateUtil.parse(searchVo.getStartDate());
            Date end = DateUtil.parse(searchVo.getEndDate());
            wrapper.between(LogSign::getCreateTime,start,DateUtil.endOfDay(end));
        }
        wrapper.orderByDesc(LogSign::getCreateTime);
        IPage page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>( pageable.getPageNumber()+1,pageable.getPageSize());
        IPage logSignList = baseMapper.selectPage(page,wrapper);

        //查询关联日志详情
        List<LogSign> records = logSignList.getRecords();
        if(CollectionUtils.isNotEmpty(records)){
            for (LogSign sign:records){
                List<AuditLog> auditLogs = auditLogService.getByLogSignId(sign.getId());
                if(CollectionUtil.isNotEmpty(auditLogs)){
                    sign.setAuditLogs(auditLogs);
                    sign.setAuditLogsCount(auditLogs.size());
                }
            }
        }

        Page<LogSign> res = new PageImpl<LogSign>(records,pageable,logSignList.getTotal());
        return res;
    }
}
