package org.dromara.payment.invoice.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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.payment.invoice.domain.bo.MerInvoiceDetailBo;
import org.dromara.payment.invoice.domain.vo.MerInvoiceDetailVo;
import org.dromara.payment.invoice.domain.MerInvoiceDetail;
import org.dromara.payment.invoice.mapper.MerInvoiceDetailMapper;
import org.dromara.payment.invoice.service.IMerInvoiceDetailService;

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

/**
 * 开票明细Service业务层处理
 *
 * @author LionLi
 * @date 2024-05-06
 */
@RequiredArgsConstructor
@Service
public class MerInvoiceDetailServiceImpl implements IMerInvoiceDetailService {

    private final MerInvoiceDetailMapper baseMapper;

    /**
     * 查询开票明细
     */
    @Override
    public MerInvoiceDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询开票明细列表
     */
    @Override
    public TableDataInfo<MerInvoiceDetailVo> queryPageList(MerInvoiceDetailBo bo, PageQuery pageQuery) {
        QueryWrapper<MerInvoiceDetail> lqw = buildQueryWrapper(bo);
        Page<MerInvoiceDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询开票明细列表
     */
    @Override
    public List<MerInvoiceDetailVo> queryList(MerInvoiceDetailBo bo) {
        QueryWrapper<MerInvoiceDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<MerInvoiceDetail> buildQueryWrapper(MerInvoiceDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<MerInvoiceDetail> lqw = new QueryWrapper<>();
        lqw.eq(bo.getInvId() != null, "inv_id", bo.getInvId());
        lqw.eq(StringUtils.isNotBlank(bo.getInvNo()), "inv_no", bo.getInvNo());
        lqw.eq(bo.getInvAmount() != null, "inv_amount", bo.getInvAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailNo()), "detail_no", bo.getDetailNo());
        lqw.eq(StringUtils.isNotBlank(bo.getInvPic()), "inv_pic", bo.getInvPic());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), "category", bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), "add_by", bo.getAddBy());
        lqw.eq(bo.getAddTime() != null, "add_time", bo.getAddTime());
        return lqw;
    }

    /**
     * 新增开票明细
     */
    @Override
    public Boolean insertByBo(MerInvoiceDetailBo bo) {
        MerInvoiceDetail add = MapstructUtils.convert(bo, MerInvoiceDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改开票明细
     */
    @Override
    public Boolean updateByBo(MerInvoiceDetailBo bo) {
        MerInvoiceDetail update = MapstructUtils.convert(bo, MerInvoiceDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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