package com.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.dto.AppSecurityLogDTO;
import com.om.entity.dto.GuidDTO;
import com.om.entity.dto.SecurityLogQueryPageDTO;
import com.om.entity.po.BrushDescription;
import com.om.entity.po.BrushFile;
import com.om.entity.po.SecurityLog;
import com.om.entity.vo.BlushQueryPageVO;
import com.om.entity.vo.BrushDescriptionVO;
import com.om.entity.vo.BrushVO;
import com.om.entity.vo.SecurityLogQueryPageVO;
import com.om.mapper.SecurityLogMapper;
import com.om.service.ISecurityLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.utils.AesUtil;
import com.om.utils.Result;
import com.om.utils.SecurityLogUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bmmx
 * @since 2024-04-27
 */
@Service
@Slf4j
public class SecurityLogServiceImpl extends ServiceImpl<SecurityLogMapper, SecurityLog> implements ISecurityLogService {


    @Override
    @Transactional
    public Result updateLog(AppSecurityLogDTO dto) {
        //从dto中获取数据
        MultipartFile file = dto.getFile();
        String guid = dto.getGuid();
        GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);

        List<SecurityLog> securityLogs = SecurityLogUtil.parseLogFile(file);

        return Result.ok(this.saveBatch(securityLogs));
    }

    @Override
    public Result<SecurityLogQueryPageVO> getPageList(SecurityLogQueryPageDTO dto) {
        Integer pageIndex = dto.getPageIndex();
        Integer pageSize = dto.getPageSize();
        String username = dto.getUsername();

        Page<SecurityLog> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(username), SecurityLog::getUserName, username)
                .orderByDesc(SecurityLog::getCreateTime)
                .page(new Page<>(pageIndex, pageSize));


        //封装vo
        SecurityLogQueryPageVO vo = new SecurityLogQueryPageVO();
        vo.setCurrent((int) page.getCurrent());
        vo.setSize((int) page.getSize());
        vo.setPages((int) page.getPages());
        vo.setTotal((int) page.getTotal());
        if (StringUtils.isNotBlank(username)) {
            vo.setSearchCount(true);
        }

        List<SecurityLog> records = page.getRecords();
        if (records.isEmpty()){
            return Result.ok(vo);
        }
        vo.setRecords(records);
        return Result.ok(vo);
    }
}
