package com.ruoyi.oa.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 com.ruoyi.oa.domain.SysOaClaimDetail;
import com.ruoyi.oa.domain.bo.SysOaClaimDetailBo;
import com.ruoyi.oa.domain.vo.SysOaClaimDetailVo;
import com.ruoyi.oa.mapper.SysOaClaimDetailMapper;
import com.ruoyi.oa.service.ISysOaClaimDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


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

/**
 * 报销明细Service业务层处理
 *
 * @author hdka
 * @date 2025-02-21
 */
@RequiredArgsConstructor
@Service
public class SysOaClaimDetailServiceImpl implements ISysOaClaimDetailService {

    private final SysOaClaimDetailMapper baseMapper;

    /**
     * 查询报销明细
     */
    @Override
    public SysOaClaimDetailVo queryById(Long claimDetailId){
        return baseMapper.selectVoById(claimDetailId);
    }

    /**
     * 查询报销明细列表
     */
    @Override
    public TableDataInfo<SysOaClaimDetailVo> queryPageList(SysOaClaimDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOaClaimDetail> lqw = buildQueryWrapper(bo);
        Page<SysOaClaimDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报销明细列表
     */
    @Override
    public List<SysOaClaimDetailVo> queryList(SysOaClaimDetailBo bo) {
        LambdaQueryWrapper<SysOaClaimDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaClaimDetail> buildQueryWrapper(SysOaClaimDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaClaimDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getClaimId() != null, SysOaClaimDetail::getClaimId, bo.getClaimId());
        lqw.eq(bo.getClaimType() != null, SysOaClaimDetail::getClaimType, bo.getClaimType());
        lqw.eq(bo.getBeginTime() != null, SysOaClaimDetail::getBeginTime, bo.getBeginTime());
        lqw.eq(bo.getEndTime() != null, SysOaClaimDetail::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLodgingAddress()), SysOaClaimDetail::getLodgingAddress, bo.getLodgingAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getFileIds()), SysOaClaimDetail::getFileIds, bo.getFileIds());
        lqw.eq(bo.getCost() != null, SysOaClaimDetail::getCost, bo.getCost());
        lqw.eq(StringUtils.isNotBlank(bo.getBigCost()), SysOaClaimDetail::getBigCost, bo.getBigCost());
        return lqw;
    }

    /**
     * 新增报销明细
     */
    @Override
    public Boolean insertByBo(SysOaClaimDetailBo bo) {
        SysOaClaimDetail add = BeanUtil.toBean(bo, SysOaClaimDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClaimDetailId(add.getClaimDetailId());
        }
        return flag;
    }

    /**
     * 修改报销明细
     */
    @Override
    public Boolean updateByBo(SysOaClaimDetailBo bo) {
        SysOaClaimDetail update = BeanUtil.toBean(bo, SysOaClaimDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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