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.FileExchangeResolve;
import com.ruoyi.business.mapper.FileExchangeResolveMapper;
import com.ruoyi.business.service.IFileExchangeResolveService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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 gx
 * @date 2024-01-31
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class FileExchangeResolveServiceImpl extends ServiceImpl<FileExchangeResolveMapper, FileExchangeResolve> implements IFileExchangeResolveService {

    private final FileExchangeResolveMapper fileExchangeResolveMapper;

    /**
     * 查询文件打包解析列表
     * 
     * @param fileExchangeResolve 文件打包解析
     * @return 文件打包解析
     */
    @Override
    public List<FileExchangeResolve> selectFileExchangeResolveList(FileExchangeResolve fileExchangeResolve)
    {
        LambdaQueryWrapper<FileExchangeResolve> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(fileExchangeResolve.getInterfaceName()), FileExchangeResolve::getInterfaceName, fileExchangeResolve.getInterfaceName());
        queryWrapper.like(StringUtils.isNotEmpty(fileExchangeResolve.getDatabaseName()), FileExchangeResolve::getDatabaseName,
                fileExchangeResolve.getDatabaseName());
        queryWrapper.like(StringUtils.isNotEmpty(fileExchangeResolve.getTableRemark()), FileExchangeResolve::getTableRemark,
                fileExchangeResolve.getTableRemark());
        queryWrapper.orderByDesc(FileExchangeResolve::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

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

    /**
     * 新增文件打包解析
     * 
     * @param fileExchangeResolve 文件打包解析
     * @return 结果
     */
    @Override
    public int insertFileExchangeResolve(FileExchangeResolve fileExchangeResolve)
    {
        fileExchangeResolve.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(fileExchangeResolve);
    }

    /**
     * 修改文件打包解析
     * 
     * @param fileExchangeResolve 文件打包解析
     * @return 结果
     */
    @Override
    public Boolean updateFileExchangeResolve(FileExchangeResolve fileExchangeResolve)
    {
        fileExchangeResolve.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(fileExchangeResolve);
    }

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

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

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