package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.business.mapper.FileExchangeResolveLogMapper;
import com.ruoyi.business.domain.FileExchangeResolveLog;
import com.ruoyi.business.service.IFileExchangeResolveLogService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import static com.ruoyi.common.core.constant.Constants.DELETE_YES;

/**
 * 文件打包解析日志Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-31
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class FileExchangeResolveLogServiceImpl extends ServiceImpl<FileExchangeResolveLogMapper, FileExchangeResolveLog> implements IFileExchangeResolveLogService {

    private final FileExchangeResolveLogMapper fileExchangeResolveLogMapper;

    /**
     * 查询文件打包解析日志列表
     * 
     * @param fileExchangeResolveLog 文件打包解析日志
     * @return 文件打包解析日志
     */
    @Override
    public List<FileExchangeResolveLog> selectFileExchangeResolveLogList(FileExchangeResolveLog fileExchangeResolveLog)
    {
        LambdaQueryWrapper<FileExchangeResolveLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileExchangeResolveLog::getParentId, fileExchangeResolveLog.getParentId());
        queryWrapper.orderByDesc(FileExchangeResolveLog::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询文件打包解析日志详情
     *
     * @param id 文件打包解析日志主键
     * @return 文件打包解析日志
     */
    @Override
    public FileExchangeResolveLog selectFileExchangeResolveLogById(Long id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增文件打包解析日志
     * 
     * @param fileExchangeResolveLog 文件打包解析日志
     * @return 结果
     */
    @Override
    public int insertFileExchangeResolveLog(FileExchangeResolveLog fileExchangeResolveLog)
    {
        fileExchangeResolveLog.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(fileExchangeResolveLog);
    }

    /**
     * 修改文件打包解析日志
     * 
     * @param fileExchangeResolveLog 文件打包解析日志
     * @return 结果
     */
    @Override
    public Boolean updateFileExchangeResolveLog(FileExchangeResolveLog fileExchangeResolveLog)
    {
        fileExchangeResolveLog.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(fileExchangeResolveLog);
    }

    /**
     * 批量删除文件打包解析日志
     * 
     * @param ids 需要删除的文件打包解析日志主键
     * @return 结果
     */
    @Override
    public int deleteFileExchangeResolveLogByIds(Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return updateByIds(list);
    }

    /**
     * 删除文件打包解析日志信息
     * 
     * @param id 文件打包解析日志主键
     * @return 结果
     */
    @Override
    public int deleteFileExchangeResolveLogById(Long id)
    {
        List<Long> list = new ArrayList<>();
        list.add(id);
        return updateByIds(list);
    }

    /**
    * 批量更新
    * @param list ids
    */
    private int updateByIds(List<Long> list) {
        LambdaUpdateWrapper<FileExchangeResolveLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(FileExchangeResolveLog::getDeleteBy, SecurityUtils.getUsername())
                .set(FileExchangeResolveLog::getDeleteTime, DateUtils.getNowDate())
                .in(FileExchangeResolveLog::getId, list)
                .set(FileExchangeResolveLog::getDeleteFlag, DELETE_YES);
        return this.baseMapper.updateByIds(updateWrapper);
    }
}
