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.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.AdapterMapper;
import com.ruoyi.business.domain.Adapter;
import com.ruoyi.business.service.IAdapterService;
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 gx
 * @date 2024-02-22
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AdapterServiceImpl extends ServiceImpl<AdapterMapper, Adapter> implements IAdapterService {

    private final AdapterMapper adapterMapper;

    /**
     * 查询适配器列表
     * 
     * @param adapter 适配器
     * @return 适配器
     */
    @Override
    public List<Adapter> selectAdapterList(Adapter adapter)
    {
        LambdaQueryWrapper<Adapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(adapter.getServiceBusinessId() != null, Adapter::getServiceBusinessId, adapter.getServiceBusinessId());
        queryWrapper.like(StringUtils.isNotEmpty(adapter.getName()), Adapter::getName, adapter.getName());
        queryWrapper.eq(StringUtils.isNotEmpty(adapter.getType()), Adapter::getType, adapter.getType());
        queryWrapper.eq(StringUtils.isNotEmpty(adapter.getStatus()), Adapter::getStatus, adapter.getStatus());
        queryWrapper.like(StringUtils.isNotEmpty(adapter.getServiceBusinessName()), Adapter::getServiceBusinessName, adapter.getServiceBusinessName());
        queryWrapper.like(StringUtils.isNotEmpty(adapter.getServiceBusinessField()), Adapter::getServiceBusinessField, adapter.getServiceBusinessField());
        queryWrapper.orderByDesc(Adapter::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询适配器详情
     *
     * @param id 适配器主键
     * @return 适配器
     */
    @Override
    public Adapter selectAdapterById(Long id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增适配器
     * 
     * @param adapter 适配器
     * @return 结果
     */
    @Override
    public int insertAdapter(Adapter adapter)
    {
        adapter.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(adapter);
    }

    /**
     * 修改适配器
     * 
     * @param adapter 适配器
     * @return 结果
     */
    @Override
    public Boolean updateAdapter(Adapter adapter)
    {
        adapter.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(adapter);
    }

    /**
     * 批量删除适配器
     * 
     * @param ids 需要删除的适配器主键
     * @return 结果
     */
    @Override
    public int deleteAdapterByIds(Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return updateByIds(list);
    }

    /**
     * 删除适配器信息
     * 
     * @param id 适配器主键
     * @return 结果
     */
    @Override
    public int deleteAdapterById(Long id)
    {
        List<Long> list = new ArrayList<>();
        list.add(id);
        return updateByIds(list);
    }

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