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

import org.dromara.common.core.utils.MapstructUtils;
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.SysSydConfig;
import org.dromara.payment.sys.domain.vo.SysSydConfigVo;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.SysSydConfigBo;
import org.dromara.payment.sys.mapper.SysSydConfigMapper;
import org.dromara.payment.sys.service.ISysSydConfigService;

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

/**
 * 服务商配置Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-26
 */
@RequiredArgsConstructor
@Service
public class SysSydConfigServiceImpl implements ISysSydConfigService {

    private final SysSydConfigMapper baseMapper;

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

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

    /**
     * 查询服务商配置列表
     */
    @Override
    public List<SysSydConfigVo> queryList(SysSydConfigBo bo) {
        LambdaQueryWrapper<SysSydConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSydConfig> buildQueryWrapper(SysSydConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSydConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSydId() != null, SysSydConfig::getSydId, bo.getSydId());
        lqw.eq(bo.getBegin() != null, SysSydConfig::getBegin, bo.getBegin());
        lqw.eq(bo.getEnd() != null, SysSydConfig::getEnd, bo.getEnd());
        lqw.eq(bo.getRate() != null, SysSydConfig::getRate, bo.getRate());
        lqw.eq(bo.getIsTaxCertificate() != null, SysSydConfig::getIsTaxCertificate, bo.getIsTaxCertificate());
        lqw.eq(bo.getCalStyle() != null, SysSydConfig::getCalStyle, bo.getCalStyle());
        return lqw;
    }

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

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

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

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