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.common.mapper.BusAccountsMapper;
import org.dromara.payment.sys.domain.vo.SysSydAccountVo;
import org.dromara.payment.sys.domain.vo.SysSydVo;
import org.dromara.payment.sys.mapper.SysSydMapper;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.SysSydAccountBo;
import org.dromara.payment.sys.domain.SysSydAccount;
import org.dromara.payment.sys.mapper.SysSydAccountMapper;
import org.dromara.payment.sys.service.ISysSydAccountService;
import org.springframework.transaction.annotation.Transactional;

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

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

    private final SysSydAccountMapper baseMapper;

    private final BusAccountsMapper accountsMapper;

    private final SysSydMapper sydMapper;

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

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

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

    private LambdaQueryWrapper<SysSydAccount> buildQueryWrapper(SysSydAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSydAccount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSydId() != null, SysSydAccount::getSydId, bo.getSydId());
        lqw.eq(bo.getAccId() != null, SysSydAccount::getAccId, bo.getAccId());
        lqw.eq(bo.getAccType() != null, SysSydAccount::getAccType, bo.getAccType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), SysSydAccount::getBankType, bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), SysSydAccount::getBankCard, bo.getBankCard());
        lqw.eq(bo.getBalance() != null, SysSydAccount::getBalance, bo.getBalance());
        lqw.eq(bo.getFrozen() != null, SysSydAccount::getFrozen, bo.getFrozen());
        lqw.eq(bo.getIsDefault() != null, SysSydAccount::getIsDefault, bo.getIsDefault());
        lqw.eq(bo.getAddTime() != null, SysSydAccount::getAddTime, bo.getAddTime());
        lqw.eq(bo.getSettlement() != null, SysSydAccount::getSettlement, bo.getSettlement());
        lqw.eq(bo.getInvoice() != null, SysSydAccount::getInvoice, bo.getInvoice());
        lqw.eq(bo.getStatus() != null, SysSydAccount::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOutBankCard()), SysSydAccount::getOutBankCard, bo.getOutBankCard());
        return lqw;
    }

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

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

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateBalance(Long sydId, long unitId, long amount, String bankType) {
        Integer baseNum = accountsMapper.updateBalance(sydId,unitId,amount,bankType);
        Integer curNum = baseMapper.updateBalance(sydId,Short.parseShort(unitId+""),amount,bankType);
        return baseNum > 0 && curNum > 0 ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateFrozen(Long sydId, long unitId, long amount, String bankType) {
        Integer baseNum = accountsMapper.updateFrozen(sydId,unitId,amount,bankType);
        Integer curNum = baseMapper.updateFrozen(sydId,Short.parseShort(unitId+""),amount,bankType);
        return baseNum > 0 && curNum > 0 ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateBalanceByAccId(int accId, long amount) {
        Integer baseNum = accountsMapper.updateBalanceByAccId(accId,amount);
        Integer curNum = baseMapper.updateBalanceByAccId(accId,amount);
        return baseNum > 0 && curNum > 0 ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateFrozenByAccId(int accId, long amount) {
        Integer baseNum = accountsMapper.updateFrozenByAccId(accId,amount);
        Integer curNum = baseMapper.updateFrozenByAccId(accId,amount);
        return baseNum > 0 && curNum > 0 ? 1 : 0;
    }

    @Override
    public SysSydAccountVo getCashoutAccount(Long id) {
        SysSydAccountVo rtn = baseMapper.selectVoById(id);
        if(rtn != null){
            SysSydVo vo  = sydMapper.selectVoById(rtn.getSydId());
            rtn.setAccName(vo.getGsmc());
            rtn.setBankCard(vo.getBankCard());
            rtn.setKfd(vo.getKfd());
            rtn.setKfh(vo.getKfh());
            rtn.setKhhNo(vo.getKhhNo());
        }
        return rtn;
    }
}
