package com.ruoyi.business.service.impl;

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 com.ruoyi.business.domain.FileExchangePackageLog;
import com.ruoyi.business.mapper.FileExchangePackageLogMapper;
import com.ruoyi.business.service.IFileExchangePackageLogService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

import static com.ruoyi.common.core.constant.Constants.DELETE_YES;

/**
 * 对外文件打包日志Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-29
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class FileExchangePackageLogServiceImpl extends ServiceImpl<FileExchangePackageLogMapper, FileExchangePackageLog> implements IFileExchangePackageLogService {

    private final FileExchangePackageLogMapper fileExchangePackageLogMapper;

    /**
     * 查询对外文件打包日志列表
     * 
     * @param fileExchangePackageLog 对外文件打包日志
     * @return 对外文件打包日志
     */
    @Override
    public List<FileExchangePackageLog> selectFileExchangePackageLogList(FileExchangePackageLog fileExchangePackageLog)
    {
        LambdaQueryWrapper<FileExchangePackageLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileExchangePackageLog::getParentId, fileExchangePackageLog.getParentId());
        if (fileExchangePackageLog.getPackageTimePeriod() != null) {
            queryWrapper.between(FileExchangePackageLog::getPackageTime, fileExchangePackageLog.getPackageTimePeriod()[0],
                    fileExchangePackageLog.getPackageTimePeriod()[1] + " 23:59:59");
        }
        queryWrapper.orderByDesc(FileExchangePackageLog::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询对外文件打包日志详情
     *
     * @param id 对外文件打包日志主键
     * @return 对外文件打包日志
     */
    @Override
    public FileExchangePackageLog selectFileExchangePackageLogById(Long id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增对外文件打包日志
     * 
     * @param fileExchangePackageLog 对外文件打包日志
     * @return 结果
     */
    @Override
    public int insertFileExchangePackageLog(FileExchangePackageLog fileExchangePackageLog)
    {
        fileExchangePackageLog.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(fileExchangePackageLog);
    }

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

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

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