package org.dromara.payment.merchant.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.merchant.domain.bo.BusMerchantAccountSynBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantAccountSynVo;
import org.dromara.payment.merchant.domain.BusMerchantAccountSyn;
import org.dromara.payment.merchant.mapper.BusMerchantAccountSynMapper;
import org.dromara.payment.merchant.service.IBusMerchantAccountSynService;

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

/**
 * 商户账户同步Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class BusMerchantAccountSynServiceImpl implements IBusMerchantAccountSynService {

    private final BusMerchantAccountSynMapper baseMapper;

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

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

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

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

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

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

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

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