package com.ruoyi.business.service.impl;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;

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.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.business.mapper.FileExchangePackageMapper;
import com.ruoyi.business.domain.FileExchangePackage;
import com.ruoyi.business.service.IFileExchangePackageService;
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-29
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class FileExchangePackageServiceImpl extends ServiceImpl<FileExchangePackageMapper, FileExchangePackage> implements IFileExchangePackageService {

    private final FileExchangePackageMapper fileExchangePackageMapper;

    /**
     * 查询对外文件打包列表
     * 
     * @param fileExchangePackage 对外文件打包
     * @return 对外文件打包
     */
    @Override
    public List<FileExchangePackage> selectFileExchangePackageList(FileExchangePackage fileExchangePackage)
    {
        LambdaQueryWrapper<FileExchangePackage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(fileExchangePackage.getInterfaceName()), FileExchangePackage::getInterfaceName, fileExchangePackage.getInterfaceName());
        queryWrapper.like(StringUtils.isNotEmpty(fileExchangePackage.getPackageSourceDbName()), FileExchangePackage::getPackageSourceDbName,
                fileExchangePackage.getPackageSourceDbName());
        queryWrapper.like(StringUtils.isNotEmpty(fileExchangePackage.getPackageSourceTableName()), FileExchangePackage::getPackageSourceTableName,
                fileExchangePackage.getPackageSourceTableName());
        queryWrapper.orderByDesc(FileExchangePackage::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

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

    /**
     * 新增对外文件打包
     * 
     * @param fileExchangePackage 对外文件打包
     * @return 结果
     */
    @Override
    public int insertFileExchangePackage(FileExchangePackage fileExchangePackage)
    {
        fileExchangePackage.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(fileExchangePackage);
    }

    /**
     * 修改对外文件打包
     * 
     * @param fileExchangePackage 对外文件打包
     * @return 结果
     */
    @Override
    public Boolean updateFileExchangePackage(FileExchangePackage fileExchangePackage)
    {
        fileExchangePackage.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(fileExchangePackage);
    }

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

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

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