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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaBindingItemDetailBo;
import com.ruoyi.oa.domain.vo.OaBindingItemDetailVo;
import com.ruoyi.oa.domain.OaBindingItemDetail;
import com.ruoyi.oa.mapper.OaBindingItemDetailMapper;
import com.ruoyi.oa.service.IOaBindingItemDetailService;

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

/**
 * 绑定记录明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@RequiredArgsConstructor
@Service
public class OaBindingItemDetailServiceImpl implements IOaBindingItemDetailService {

    private final OaBindingItemDetailMapper baseMapper;

    /**
     * 查询绑定记录明细
     */
    @Override
    public OaBindingItemDetailVo queryById(Long bindingItemId){
        return baseMapper.selectVoById(bindingItemId);
    }

    /**
     * 查询绑定记录明细列表
     */
    @Override
    public TableDataInfo<OaBindingItemDetailVo> queryPageList(OaBindingItemDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaBindingItemDetail> lqw = buildQueryWrapper(bo);
        Page<OaBindingItemDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询绑定记录明细列表
     */
    @Override
    public List<OaBindingItemDetailVo> queryList(OaBindingItemDetailBo bo) {
        LambdaQueryWrapper<OaBindingItemDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaBindingItemDetail> buildQueryWrapper(OaBindingItemDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaBindingItemDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBindingId() != null, OaBindingItemDetail::getBindingId, bo.getBindingId());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplateType()), OaBindingItemDetail::getTemplateType, bo.getTemplateType());
        lqw.eq(bo.getItemDetailId() != null, OaBindingItemDetail::getItemDetailId, bo.getItemDetailId());
        lqw.eq(bo.getPaidAmount() != null, OaBindingItemDetail::getPaidAmount, bo.getPaidAmount());
        return lqw;
    }

    /**
     * 新增绑定记录明细
     */
    @Override
    public Boolean insertByBo(OaBindingItemDetailBo bo) {
        OaBindingItemDetail add = BeanUtil.toBean(bo, OaBindingItemDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBindingItemId(add.getBindingItemId());
        }
        return flag;
    }

    /**
     * 修改绑定记录明细
     */
    @Override
    public Boolean updateByBo(OaBindingItemDetailBo bo) {
        OaBindingItemDetail update = BeanUtil.toBean(bo, OaBindingItemDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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