package org.dromara.payment.operator.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.operator.domain.bo.BusOperatorAgreementBo;
import org.dromara.payment.operator.domain.vo.BusOperatorAgreementVo;
import org.dromara.payment.operator.domain.BusOperatorAgreement;
import org.dromara.payment.operator.mapper.BusOperatorAgreementMapper;
import org.dromara.payment.operator.service.IBusOperatorAgreementService;

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

/**
 * 运营商协议Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-28
 */
@RequiredArgsConstructor
@Service
public class BusOperatorAgreementServiceImpl implements IBusOperatorAgreementService {

    private final BusOperatorAgreementMapper baseMapper;

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

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

    /**
     * 查询运营商协议列表
     */
    @Override
    public List<BusOperatorAgreementVo> queryList(BusOperatorAgreementBo bo) {
        LambdaQueryWrapper<BusOperatorAgreement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

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

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

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

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