package com.ruoyi.finance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 com.ruoyi.finance.domain.bo.FinanceServiceFeeBo;
import com.ruoyi.finance.domain.vo.FinanceServiceFeeVo;
import com.ruoyi.finance.domain.FinanceServiceFee;
import com.ruoyi.finance.mapper.FinanceServiceFeeMapper;
import com.ruoyi.finance.service.IFinanceServiceFeeService;

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

/**
 * 劳务费Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-16
 */
@RequiredArgsConstructor
@Service
public class FinanceServiceFeeServiceImpl implements IFinanceServiceFeeService {

    private final FinanceServiceFeeMapper baseMapper;

    /**
     * 查询劳务费
     */
    @Override
    public FinanceServiceFeeVo queryById(Long serviceFeeId){
        return baseMapper.selectVoById(serviceFeeId);
    }

    /**
     * 查询劳务费列表
     */
    @Override
    public TableDataInfo<FinanceServiceFeeVo> queryPageList(FinanceServiceFeeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FinanceServiceFee> lqw = buildQueryWrapper(bo);
        Page<FinanceServiceFeeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询劳务费列表
     */
    @Override
    public List<FinanceServiceFeeVo> queryList(FinanceServiceFeeBo bo) {
        LambdaQueryWrapper<FinanceServiceFee> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FinanceServiceFee> buildQueryWrapper(FinanceServiceFeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FinanceServiceFee> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), FinanceServiceFee::getName, bo.getName());
        lqw.eq(bo.getPayableAmount() != null, FinanceServiceFee::getPayableAmount, bo.getPayableAmount());
        lqw.eq(bo.getTax() != null, FinanceServiceFee::getTax, bo.getTax());
        lqw.eq(bo.getPaidAmount() != null, FinanceServiceFee::getPaidAmount, bo.getPaidAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getCardId()), FinanceServiceFee::getCardId, bo.getCardId());
        lqw.eq(StringUtils.isNotBlank(bo.getBank()), FinanceServiceFee::getBank, bo.getBank());
        lqw.eq(StringUtils.isNotBlank(bo.getBankNo()), FinanceServiceFee::getBankNo, bo.getBankNo());
        return lqw;
    }

    /**
     * 新增劳务费
     */
    @Override
    public Boolean insertByBo(FinanceServiceFeeBo bo) {
        FinanceServiceFee add = BeanUtil.toBean(bo, FinanceServiceFee.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setServiceFeeId(add.getServiceFeeId());
        }
        return flag;
    }

    /**
     * 修改劳务费
     */
    @Override
    public Boolean updateByBo(FinanceServiceFeeBo bo) {
        FinanceServiceFee update = BeanUtil.toBean(bo, FinanceServiceFee.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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