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.BusMerchantAgreementBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantAgreementVo;
import org.dromara.payment.merchant.domain.BusMerchantAgreement;
import org.dromara.payment.merchant.mapper.BusMerchantAgreementMapper;
import org.dromara.payment.merchant.service.IBusMerchantAgreementService;

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

/**
 * 商户协议Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class BusMerchantAgreementServiceImpl implements IBusMerchantAgreementService {

    private final BusMerchantAgreementMapper baseMapper;

    /**
     * 查询商户协议
     */
    @Override
    public BusMerchantAgreementVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商户协议列表
     */
    @Override
    public TableDataInfo<BusMerchantAgreementVo> queryPageList(BusMerchantAgreementBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusMerchantAgreement> lqw = buildQueryWrapper(bo);
        Page<BusMerchantAgreementVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商户协议列表
     */
    @Override
    public List<BusMerchantAgreementVo> queryList(BusMerchantAgreementBo bo) {
        LambdaQueryWrapper<BusMerchantAgreement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusMerchantAgreement> buildQueryWrapper(BusMerchantAgreementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusMerchantAgreement> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSydId() != null, BusMerchantAgreement::getSydId, bo.getSydId());
        lqw.eq(StringUtils.isNotBlank(bo.getSydNo()), BusMerchantAgreement::getSydNo, bo.getSydNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOpNo()), BusMerchantAgreement::getOpNo, bo.getOpNo());
        lqw.eq(bo.getOpId() != null, BusMerchantAgreement::getOpId, bo.getOpId());
        lqw.eq(bo.getSignStyle() != null, BusMerchantAgreement::getSignStyle, bo.getSignStyle());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BusMerchantAgreement::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), BusMerchantAgreement::getNo, bo.getNo());
        lqw.eq(bo.getStatus() != null, BusMerchantAgreement::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), BusMerchantAgreement::getFileUrl, bo.getFileUrl());
        lqw.eq(bo.getChannelId() != null, BusMerchantAgreement::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNo()), BusMerchantAgreement::getChannelNo, bo.getChannelNo());
        lqw.eq(bo.getMerId() != null, BusMerchantAgreement::getMerId, bo.getMerId());
        lqw.eq(StringUtils.isNotBlank(bo.getMerNo()), BusMerchantAgreement::getMerNo, bo.getMerNo());
        return lqw;
    }

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

    /**
     * 修改商户协议
     */
    @Override
    public Boolean updateByBo(BusMerchantAgreementBo bo) {
        BusMerchantAgreement update = MapstructUtils.convert(bo, BusMerchantAgreement.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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