package org.dromara.payment.sys.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.dromara.payment.sys.domain.SysSydAccountSyn;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.SysSydAccountSynBo;
import org.dromara.payment.sys.domain.vo.SysSydAccountSynVo;
import org.dromara.payment.sys.mapper.SysSydAccountSynMapper;
import org.dromara.payment.sys.service.ISysSydAccountSynService;

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

/**
 * 服务商账户同步Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-26
 */
@RequiredArgsConstructor
@Service
public class SysSydAccountSynServiceImpl implements ISysSydAccountSynService {

    private final SysSydAccountSynMapper baseMapper;

    /**
     * 查询服务商账户同步
     */
    @Override
    public SysSydAccountSynVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务商账户同步列表
     */
    @Override
    public TableDataInfo<SysSydAccountSynVo> queryPageList(SysSydAccountSynBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSydAccountSyn> lqw = buildQueryWrapper(bo);
        Page<SysSydAccountSynVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务商账户同步列表
     */
    @Override
    public List<SysSydAccountSynVo> queryList(SysSydAccountSynBo bo) {
        LambdaQueryWrapper<SysSydAccountSyn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增服务商账户同步
     */
    @Override
    public Boolean insertByBo(SysSydAccountSynBo bo) {
        SysSydAccountSyn add = MapstructUtils.convert(bo, SysSydAccountSyn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务商账户同步
     */
    @Override
    public Boolean updateByBo(SysSydAccountSynBo bo) {
        SysSydAccountSyn update = MapstructUtils.convert(bo, SysSydAccountSyn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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