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.SysSydSignContent;
import org.dromara.payment.sys.domain.vo.SysSydSignContentVo;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.SysSydSignContentBo;
import org.dromara.payment.sys.mapper.SysSydSignContentMapper;
import org.dromara.payment.sys.service.ISysSydSignContentService;

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

/**
 * 服务商协议内容Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-26
 */
@RequiredArgsConstructor
@Service
public class SysSydSignContentServiceImpl implements ISysSydSignContentService {

    private final SysSydSignContentMapper baseMapper;

    /**
     * 查询服务商协议内容
     */
    @Override
    public SysSydSignContentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务商协议内容列表
     */
    @Override
    public TableDataInfo<SysSydSignContentVo> queryPageList(SysSydSignContentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSydSignContent> lqw = buildQueryWrapper(bo);
        Page<SysSydSignContentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务商协议内容列表
     */
    @Override
    public List<SysSydSignContentVo> queryList(SysSydSignContentBo bo) {
        LambdaQueryWrapper<SysSydSignContent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSydSignContent> buildQueryWrapper(SysSydSignContentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSydSignContent> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SysSydSignContent::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getEditer()), SysSydSignContent::getEditer, bo.getEditer());
        lqw.eq(bo.getEditTime() != null, SysSydSignContent::getEditTime, bo.getEditTime());
        return lqw;
    }

    /**
     * 新增服务商协议内容
     */
    @Override
    public Boolean insertByBo(SysSydSignContentBo bo) {
        SysSydSignContent add = MapstructUtils.convert(bo, SysSydSignContent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId().longValue());
        }
        return flag;
    }

    /**
     * 修改服务商协议内容
     */
    @Override
    public Boolean updateByBo(SysSydSignContentBo bo) {
        SysSydSignContent update = MapstructUtils.convert(bo, SysSydSignContent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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