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.ReProductInvoiceBo;
import org.dromara.payment.operator.domain.vo.ReProductInvoiceVo;
import org.dromara.payment.operator.domain.ReProductInvoice;
import org.dromara.payment.operator.mapper.ReProductInvoiceMapper;
import org.dromara.payment.operator.service.IReProductInvoiceService;

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

/**
 * 产品开票类目Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-28
 */
@RequiredArgsConstructor
@Service
public class ReProductInvoiceServiceImpl implements IReProductInvoiceService {

    private final ReProductInvoiceMapper baseMapper;

    /**
     * 查询产品开票类目
     */
    @Override
    public ReProductInvoiceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品开票类目列表
     */
    @Override
    public TableDataInfo<ReProductInvoiceVo> queryPageList(ReProductInvoiceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReProductInvoice> lqw = buildQueryWrapper(bo);
        Page<ReProductInvoiceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品开票类目列表
     */
    @Override
    public List<ReProductInvoiceVo> queryList(ReProductInvoiceBo bo) {
        LambdaQueryWrapper<ReProductInvoice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReProductInvoice> buildQueryWrapper(ReProductInvoiceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReProductInvoice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProType() != null, ReProductInvoice::getProType, bo.getProType());
        lqw.eq(StringUtils.isNotBlank(bo.getProNo()), ReProductInvoice::getProNo, bo.getProNo());
        lqw.eq(bo.getProId() != null, ReProductInvoice::getProId, bo.getProId());
        lqw.eq(bo.getInvoiceTypeId() != null, ReProductInvoice::getInvoiceTypeId, bo.getInvoiceTypeId());
        lqw.eq(bo.getBusId() != null, ReProductInvoice::getBusId, bo.getBusId());
        lqw.eq(bo.getBusType() != null, ReProductInvoice::getBusType, bo.getBusType());
        return lqw;
    }

    /**
     * 新增产品开票类目
     */
    @Override
    public Boolean insertByBo(ReProductInvoiceBo bo) {
        ReProductInvoice add = MapstructUtils.convert(bo, ReProductInvoice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品开票类目
     */
    @Override
    public Boolean updateByBo(ReProductInvoiceBo bo) {
        ReProductInvoice update = MapstructUtils.convert(bo, ReProductInvoice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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