package org.dromara.payment.channel.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.payment.channel.domain.bo.BusChannelAccountSynBo;
import org.dromara.payment.channel.domain.vo.BusChannelAccountSynVo;
import org.dromara.payment.channel.domain.BusChannelAccountSyn;
import org.dromara.payment.channel.mapper.BusChannelAccountSynMapper;
import org.dromara.payment.channel.service.IBusChannelAccountSynService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 代理商账户同步记录Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class BusChannelAccountSynServiceImpl implements IBusChannelAccountSynService {

    private final BusChannelAccountSynMapper baseMapper;

    /**
     * 查询代理商账户同步记录
     */
    @Override
    public BusChannelAccountSynVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询代理商账户同步记录列表
     */
    @Override
    public TableDataInfo<BusChannelAccountSynVo> queryPageList(BusChannelAccountSynBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusChannelAccountSyn> lqw = buildQueryWrapper(bo);
        Page<BusChannelAccountSynVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询代理商账户同步记录列表
     */
    @Override
    public List<BusChannelAccountSynVo> queryList(BusChannelAccountSynBo bo) {
        LambdaQueryWrapper<BusChannelAccountSyn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusChannelAccountSyn> buildQueryWrapper(BusChannelAccountSynBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusChannelAccountSyn> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSort() != null, BusChannelAccountSyn::getSort, bo.getSort());
        lqw.eq(bo.getSynLock() != null, BusChannelAccountSyn::getSynLock, bo.getSynLock());
        lqw.eq(StringUtils.isNotBlank(bo.getSynBatch()), BusChannelAccountSyn::getSynBatch, bo.getSynBatch());
        lqw.eq(bo.getSynPosition() != null, BusChannelAccountSyn::getSynPosition, bo.getSynPosition());
        return lqw;
    }

    /**
     * 新增代理商账户同步记录
     */
    @Override
    public Boolean insertByBo(BusChannelAccountSynBo bo) {
        BusChannelAccountSyn add = MapstructUtils.convert(bo, BusChannelAccountSyn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改代理商账户同步记录
     */
    @Override
    public Boolean updateByBo(BusChannelAccountSynBo bo) {
        BusChannelAccountSyn update = MapstructUtils.convert(bo, BusChannelAccountSyn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusChannelAccountSyn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除代理商账户同步记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
