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.BusMerchantConfigBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantConfigVo;
import org.dromara.payment.merchant.domain.BusMerchantConfig;
import org.dromara.payment.merchant.mapper.BusMerchantConfigMapper;
import org.dromara.payment.merchant.service.IBusMerchantConfigService;

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

/**
 * 商户配置Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class BusMerchantConfigServiceImpl implements IBusMerchantConfigService {

    private final BusMerchantConfigMapper baseMapper;

    /**
     * 查询商户配置
     */
    @Override
    public BusMerchantConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商户配置列表
     */
    @Override
    public TableDataInfo<BusMerchantConfigVo> queryPageList(BusMerchantConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusMerchantConfig> lqw = buildQueryWrapper(bo);
        Page<BusMerchantConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商户配置列表
     */
    @Override
    public List<BusMerchantConfigVo> queryList(BusMerchantConfigBo bo) {
        LambdaQueryWrapper<BusMerchantConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusMerchantConfig> buildQueryWrapper(BusMerchantConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusMerchantConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMerId() != null, BusMerchantConfig::getMerId, bo.getMerId());
        lqw.eq(bo.getAutoApprove() != null, BusMerchantConfig::getAutoApprove, bo.getAutoApprove());
        lqw.eq(bo.getValiWorker() != null, BusMerchantConfig::getValiWorker, bo.getValiWorker());
        return lqw;
    }

    /**
     * 新增商户配置
     */
    @Override
    public Boolean insertByBo(BusMerchantConfigBo bo) {
        BusMerchantConfig add = MapstructUtils.convert(bo, BusMerchantConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商户配置
     */
    @Override
    public Boolean updateByBo(BusMerchantConfigBo bo) {
        BusMerchantConfig update = MapstructUtils.convert(bo, BusMerchantConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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