package org.dromara.payment.taxRecords.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.taxRecords.domain.bo.MerTaxRecordsDetailBo;
import org.dromara.payment.taxRecords.domain.vo.MerTaxRecordsDetailVo;
import org.dromara.payment.taxRecords.domain.MerTaxRecordsDetail;
import org.dromara.payment.taxRecords.mapper.MerTaxRecordsDetailMapper;
import org.dromara.payment.taxRecords.service.IMerTaxRecordsDetailService;

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

/**
 * 完税证明Service业务层处理
 *
 * @author LionLi
 * @date 2024-05-06
 */
@RequiredArgsConstructor
@Service
public class MerTaxRecordsDetailServiceImpl implements IMerTaxRecordsDetailService {

    private final MerTaxRecordsDetailMapper baseMapper;

    /**
     * 查询完税证明
     */
    @Override
    public MerTaxRecordsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询完税证明列表
     */
    @Override
    public TableDataInfo<MerTaxRecordsDetailVo> queryPageList(MerTaxRecordsDetailBo bo, PageQuery pageQuery) {
        QueryWrapper<MerTaxRecordsDetail> lqw = new QueryWrapper<>();
        lqw.eq("t.tr_id",bo.getTrId());
        lqw.orderByDesc("t.create_time");
        Page<MerTaxRecordsDetailVo> result = baseMapper.selTaxRecordDetails(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询完税证明列表
     */
    @Override
    public List<MerTaxRecordsDetailVo> queryList(MerTaxRecordsDetailBo bo) {
        QueryWrapper<MerTaxRecordsDetail> lqw = new QueryWrapper<>();
        lqw.eq("t.tr_id",bo.getTrId());
        lqw.orderByDesc("t.create_time");
        return baseMapper.selTaxRecordDetails(lqw);
    }

    private QueryWrapper<MerTaxRecordsDetail> buildQueryWrapper(MerTaxRecordsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<MerTaxRecordsDetail> lqw = new QueryWrapper<>();
        lqw.eq(bo.getTrId() != null, "tr_id", bo.getTrId());
        lqw.eq(bo.getSydId() != null, "syd_id", bo.getSydId());
        lqw.eq(StringUtils.isNotBlank(bo.getSydNo()), "syd_no", bo.getSydNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOpNo()), "op_no", bo.getOpNo());
        lqw.eq(bo.getOpId() != null, "op_id", bo.getOpId());
        lqw.eq(bo.getChannelId() != null, "channel_id", bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNo()), "channel_no", bo.getChannelNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelCode()), "channel_code", bo.getChannelCode());
        lqw.eq(bo.getMerId() != null, "mer_id", bo.getMerId());
        lqw.eq(StringUtils.isNotBlank(bo.getMerNo()), "mer_no", bo.getMerNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMonth()), "month", bo.getMonth());
        lqw.eq(bo.getWorkerId() != null, "worker_id", bo.getWorkerId());
        lqw.like(StringUtils.isNotBlank(bo.getWorkerName()), "worker_name", bo.getWorkerName());
        lqw.eq(StringUtils.isNotBlank(bo.getCardId()), "card_id", bo.getCardId());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), "bank_type", bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), "bank_card", bo.getBankCard());
        lqw.eq(bo.getAmount() != null, "amount", bo.getAmount());
        return lqw;
    }

    /**
     * 新增完税证明
     */
    @Override
    public Boolean insertByBo(MerTaxRecordsDetailBo bo) {
        MerTaxRecordsDetail add = MapstructUtils.convert(bo, MerTaxRecordsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改完税证明
     */
    @Override
    public Boolean updateByBo(MerTaxRecordsDetailBo bo) {
        MerTaxRecordsDetail update = MapstructUtils.convert(bo, MerTaxRecordsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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