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.bo.AccAccountsBo;
import org.dromara.payment.sys.service.IAccAccountsService;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.vo.AccAccountsVo;
import org.dromara.payment.sys.domain.AccAccounts;
import org.dromara.payment.sys.mapper.AccAccountsMapper;

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

/**
 * 线下银行账户Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-27
 */
@RequiredArgsConstructor
@Service
public class AccAccountsServiceImpl implements IAccAccountsService {

    private final AccAccountsMapper baseMapper;

    /**
     * 查询线下银行账户
     */
    @Override
    public AccAccountsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询线下银行账户列表
     */
    @Override
    public TableDataInfo<AccAccountsVo> queryPageList(AccAccountsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AccAccounts> lqw = buildQueryWrapper(bo);
        Page<AccAccountsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询线下银行账户列表
     */
    @Override
    public List<AccAccountsVo> queryList(AccAccountsBo bo) {
        LambdaQueryWrapper<AccAccounts> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AccAccounts> buildQueryWrapper(AccAccountsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AccAccounts> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSydId() != null, AccAccounts::getSydId, bo.getSydId());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), AccAccounts::getBankType, bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), AccAccounts::getBankCard, bo.getBankCard());
        lqw.like(StringUtils.isNotBlank(bo.getBankAccName()), AccAccounts::getBankAccName, bo.getBankAccName());
        lqw.eq(StringUtils.isNotBlank(bo.getBankArgNo()), AccAccounts::getBankArgNo, bo.getBankArgNo());
        lqw.eq(bo.getIsUse() != null, AccAccounts::getIsUse, bo.getIsUse());
        lqw.eq(bo.getUseObj() != null, AccAccounts::getUseObj, bo.getUseObj());
        lqw.eq(bo.getUseObjId() != null, AccAccounts::getUseObjId, bo.getUseObjId());
        lqw.like(StringUtils.isNotBlank(bo.getUseObjName()), AccAccounts::getUseObjName, bo.getUseObjName());
        lqw.eq(bo.getAddTime() != null, AccAccounts::getAddTime, bo.getAddTime());
        return lqw;
    }

    /**
     * 新增线下银行账户
     */
    @Override
    public Boolean insertByBo(AccAccountsBo bo) {
        AccAccounts add = MapstructUtils.convert(bo, AccAccounts.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改线下银行账户
     */
    @Override
    public Boolean updateByBo(AccAccountsBo bo) {
        AccAccounts update = MapstructUtils.convert(bo, AccAccounts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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