package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.tenant.entity.dto.AppLogDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.AppLogPageDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.AppLogVo;
import cn.hanghui.outapi.platform.tenant.entity.po.AppLog;
import cn.hanghui.outapi.platform.tenant.entity.po.AppLogExtend;
import cn.hanghui.outapi.platform.tenant.mapper.AppLogExtendMapper;
import cn.hanghui.outapi.platform.tenant.mapper.AppLogMapper;
import cn.hanghui.outapi.platform.tenant.service.AppLogService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.service.impl
 * @class AppLogServiceImpl
 * @date 2023/12/19 上午10:49
 * @description
 */
@Slf4j
@Service
@Deprecated
public class AppLogServiceImpl implements AppLogService {

    @Resource
    private AppLogMapper appLogMapper;

    @Resource
    private AppLogExtendMapper appLogExtendMapper;

    @Override
    @DS("mysqlMiddlePlatform")
    public RestResult add(AppLogDto dto) {
        //插入日志基本信息
        AppLog log = BeanUtil.copyProperties(dto, AppLog.class);
        log.setCreateTime(new Date());
        log.setIsDeleted(Boolean.FALSE);
        log.setUpdateTime(new Date());
        log.setVersion(1);
        appLogMapper.insert(log);

        //插入日志内容
        AppLogExtend extend = new AppLogExtend();
        extend.setAppLogId(log.getId());
        extend.setLogContent(dto.getLogContent());
        extend.setCreateTime(new Date());
        extend.setIsDeleted(Boolean.FALSE);
        extend.setUpdateTime(new Date());
        extend.setVersion(1);
        appLogExtendMapper.insert(extend);
        return RestResult.ok();
    }

    @Override
    @DS("mysqlMiddlePlatform")
    public RestResult<IPage<AppLogVo>> page(AppLogPageDto dto) {
        Page<AppLog> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<AppLog> wrapper = new LambdaQueryWrapper<>();
        if (dto.getSn() != null) {
            wrapper.eq(AppLog::getSn, dto.getSn());
        }
        if (dto.getLogType() != null) {
            wrapper.eq(AppLog::getLogType, dto.getLogType());
        }
        if (dto.getLogTag() != null) {
            wrapper.like(AppLog::getLogTag, dto.getLogTag());
        }
        if (dto.getStartTime() != null) {
            wrapper.ge(AppLog::getCreateTime, dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            wrapper.le(AppLog::getCreateTime, dto.getEndTime());
        }
        wrapper.orderByDesc(AppLog::getId);
        Page<AppLog> iPage = appLogMapper.selectPage(page, wrapper);
        IPage<AppLogVo> convert = iPage.convert(t -> {
            AppLogVo logVo = BeanUtil.copyProperties(t, AppLogVo.class);
            return logVo;
        });
        complete(convert);
        return RestResult.ok(convert);
    }

    @Override
    @DS("mysqlMiddlePlatform")
    public void delExpireLog(Date endTime) {
//        LambdaQueryWrapper<AppLog> wrapper = new LambdaQueryWrapper<>();
//        wrapper.lt(AppLog::getCreateTime, endTime);
//        List<AppLog> logs = appLogMapper.selectList(wrapper);
//        if (logs == null || logs.size() == 0) {
//            log.info("删除过期日志完成，共删除0条数据");
//            return;
//        }
//        List<Long> logIds = logs.stream().map(AppLog::getId).collect(Collectors.toList());
//        appLogMapper.deleteBatchIds(logIds);
//        appLogExtendMapper.delete(Wrappers.lambdaQuery(AppLogExtend.class).in(AppLogExtend::getAppLogId, logIds));
//        log.info("删除过期日志完成，共删除{}条数据", logIds.size());
    }

    private void complete(IPage<AppLogVo> convert) {
        List<AppLogVo> records = convert.getRecords();
        if (records == null || records.size() == 0) {
            return;
        }
        List<Long> logIds = records.stream().map(AppLogVo::getId).collect(Collectors.toList());
        List<AppLogExtend> logExtends = appLogExtendMapper.selectList(Wrappers.lambdaQuery(AppLogExtend.class).in(AppLogExtend::getAppLogId, logIds));
        if (logExtends == null || logExtends.size() == 0) {
            return;
        }
        Map<Long, AppLogExtend> extendsMap = logExtends.stream().collect(Collectors.toMap(AppLogExtend::getAppLogId, a -> a, (k1, k2) -> k1));
        records.stream().forEach(item -> {
            AppLogExtend logExtend = extendsMap.get(item.getId());
            if (logExtend != null) {
                item.setLogContent(logExtend.getLogContent());
            }
        });
        convert.setRecords(records);
    }


}
