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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.domain.bo.BusAccountsBo;
import org.dromara.payment.common.domain.vo.BusAccountsVo;
import org.dromara.payment.common.domain.BusAccounts;
import org.dromara.payment.common.mapper.BusAccountsMapper;
import org.dromara.payment.common.service.IBusAccountsService;

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

/**
 * 资金账户汇总Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class BusAccountsServiceImpl implements IBusAccountsService {

    private final BusAccountsMapper baseMapper;

    /**
     * 查询资金账户汇总
     */
    @Override
    public BusAccountsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询资金账户汇总列表
     */
    @Override
    public TableDataInfo<BusAccountsVo> queryPageList(BusAccountsBo bo, PageQuery pageQuery) {
        QueryWrapper<BusAccounts> lqw = buildQueryWrapper(bo);
        Page<BusAccountsVo> result = baseMapper.queryPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询资金账户汇总列表
     */
    @Override
    public List<BusAccountsVo> queryList(BusAccountsBo bo) {
        QueryWrapper<BusAccounts> lqw = buildQueryWrapper(bo);
        return baseMapper.queryList(lqw);
    }

    private QueryWrapper<BusAccounts> buildQueryWrapper(BusAccountsBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<BusAccounts> lqw = new QueryWrapper<>();
        lqw.eq(bo.getSydId() != null, "t.syd_id", bo.getSydId());
        lqw.eq(bo.getUnitId() != null, "t.unit_id", bo.getUnitId());
        lqw.eq(bo.getAccId() != null, "t.acc_id", bo.getAccId());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), "t.bank_type", bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), "t.bank_card", bo.getBankCard());
        lqw.eq(bo.getBalance() != null, "t.balance", bo.getBalance());
        lqw.eq(bo.getFrozen() != null, "t.frozen", bo.getFrozen());
        lqw.eq(bo.getIsDefault() != null, "t.is_default", bo.getIsDefault());
        lqw.eq(bo.getAddTime() != null, "t.add_time", bo.getAddTime());
        lqw.eq(bo.getStatus() != null, "t.status", bo.getStatus());
        lqw.eq(bo.getAddYpe() != null, "t.add_ype", bo.getAddYpe());
        lqw.eq(StringUtils.isNotBlank(bo.getOutBankCard()), "t.out_bank_card", bo.getOutBankCard());
        if(StringUtils.isNotBlank(bo.getParams().get("begin_time")) && StringUtils.isNotBlank(bo.getParams().get("end_time"))){
            lqw.between("t.add_time",bo.getParams().get("begin_time")+" 00:00:00",bo.getParams().get("end_time")+" 23:59:59");
        }
        if(StringUtils.isNotBlank(bo.getParams().get("syd_name"))){
            lqw.like("t2.gsmc",bo.getParams().get("syd_name"));
        }
        if(StringUtils.isNotBlank(bo.getParams().get("mer_name"))){
            lqw.like("t1.name",bo.getParams().get("mer_name"));
        }
        if(StringUtils.isNotBlank(params.get("channel_no"))){
            lqw.likeRight("t.channel_no",params.get("channel_no"));
        }
        if(StringUtils.isNotBlank(bo.getParams().get("cust_no"))){
            lqw.eq("t1.cust_no",bo.getParams().get("cust_no"));
        }
        lqw.orderByDesc("t.add_time");
        return lqw;
    }

    /**
     * 新增资金账户汇总
     */
    @Override
    public Boolean insertByBo(BusAccountsBo bo) {
        BusAccounts add = MapstructUtils.convert(bo, BusAccounts.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改资金账户汇总
     */
    @Override
    public Boolean updateByBo(BusAccountsBo bo) {
        BusAccounts update = MapstructUtils.convert(bo, BusAccounts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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