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.MerInvoiceBillBo;
import org.dromara.payment.invoice.domain.vo.MerInvoiceBillVo;
import org.dromara.payment.invoice.domain.MerInvoiceBill;
import org.dromara.payment.invoice.mapper.MerInvoiceBillMapper;
import org.dromara.payment.invoice.service.IMerInvoiceBillService;

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

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

    private final MerInvoiceBillMapper baseMapper;

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

    /**
     * 查询发票明细列表
     */
    @Override
    public TableDataInfo<MerInvoiceBillVo> queryPageList(MerInvoiceBillBo bo, PageQuery pageQuery) {
        QueryWrapper<MerInvoiceBill> lqw = buildQueryWrapper(bo);
        Page<MerInvoiceBillVo> result = baseMapper.queryPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发票明细列表
     */
    @Override
    public List<MerInvoiceBillVo> queryList(MerInvoiceBillBo bo) {
        QueryWrapper<MerInvoiceBill> lqw = buildQueryWrapper(bo);
        return baseMapper.queryList(lqw);
    }

    private QueryWrapper<MerInvoiceBill> buildQueryWrapper(MerInvoiceBillBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<MerInvoiceBill> lqw = new QueryWrapper<>();
        lqw.eq(bo.getInvId() != null, "t.inv_id", bo.getInvId());
        lqw.eq(bo.getInvAmount() != null, "t.inv_amount", bo.getInvAmount());
        lqw.eq(bo.getStatus() != null, "t.status", bo.getStatus());
        lqw.eq(bo.getMoney() != null, "t.money", bo.getMoney());
        lqw.eq(bo.getServiceChargeAmount() != null, "t.service_charge_amount", bo.getServiceChargeAmount());
        lqw.eq(bo.getTaskId() != null, "t.task_id", bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskNo()), "t.task_no", bo.getTaskNo());
        lqw.eq(bo.getSydId() != null, "t.syd_id", bo.getSydId());
        lqw.eq(StringUtils.isNotBlank(bo.getSydNo()), "t.syd_no", bo.getSydNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOpNo()), "t.op_no", bo.getOpNo());
        lqw.eq(bo.getOpId() != null, "t.op_id", bo.getOpId());
        lqw.eq(bo.getChannelId() != null, "t.channel_id", bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNo()), "t.channel_no", bo.getChannelNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelCode()), "t.channel_code", bo.getChannelCode());
        lqw.eq(bo.getMerId() != null, "t.mer_id", bo.getMerId());
        lqw.eq(StringUtils.isNotBlank(bo.getMerNo()), "t.mer_no", bo.getMerNo());
        lqw.eq(bo.getBillId() != null, "t.bill_id", bo.getBillId());
        lqw.eq(StringUtils.isNotBlank(bo.getBillNo()), "t.bill_no", bo.getBillNo());
        return lqw;
    }

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

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

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

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