package com.ruoyi.pay.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.pay.domain.bo.PayBaohuoWorktimeBo;
import com.ruoyi.pay.domain.vo.PayBaohuoWorktimeVo;
import com.ruoyi.pay.domain.PayBaohuoWorktime;
import com.ruoyi.pay.mapper.PayBaohuoWorktimeMapper;
import com.ruoyi.pay.service.IPayBaohuoWorktimeService;

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

/**
 * 包活工单工时关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayBaohuoWorktimeServiceImpl implements IPayBaohuoWorktimeService {

    private final PayBaohuoWorktimeMapper baseMapper;

    /**
     * 查询包活工单工时关联
     */
    @Override
    public PayBaohuoWorktimeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询包活工单工时关联列表
     */
    @Override
    public TableDataInfo<PayBaohuoWorktimeVo> queryPageList(PayBaohuoWorktimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayBaohuoWorktime> lqw = buildQueryWrapper(bo);
        Page<PayBaohuoWorktimeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询包活工单工时关联列表
     */
    @Override
    public List<PayBaohuoWorktimeVo> queryList(PayBaohuoWorktimeBo bo) {
        LambdaQueryWrapper<PayBaohuoWorktime> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayBaohuoWorktime> buildQueryWrapper(PayBaohuoWorktimeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayBaohuoWorktime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayBaohuoWorktime::getTenantId, bo.getTenantId());
        lqw.eq(bo.getWoId() != null, PayBaohuoWorktime::getWoId, bo.getWoId());
        lqw.eq(bo.getEmpId() != null, PayBaohuoWorktime::getEmpId, bo.getEmpId());
        lqw.eq(bo.getBheId() != null, PayBaohuoWorktime::getBheId, bo.getBheId());
        lqw.eq(bo.getStime() != null, PayBaohuoWorktime::getStime, bo.getStime());
        lqw.eq(bo.getEtime() != null, PayBaohuoWorktime::getEtime, bo.getEtime());
        lqw.eq(bo.getValidHours() != null, PayBaohuoWorktime::getValidHours, bo.getValidHours());
        lqw.eq(bo.getDate() != null, PayBaohuoWorktime::getDate, bo.getDate());
        lqw.eq(bo.getIntime() != null, PayBaohuoWorktime::getIntime, bo.getIntime());
        return lqw;
    }

    /**
     * 新增包活工单工时关联
     */
    @Override
    public Boolean insertByBo(PayBaohuoWorktimeBo bo) {
        PayBaohuoWorktime add = BeanUtil.toBean(bo, PayBaohuoWorktime.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改包活工单工时关联
     */
    @Override
    public Boolean updateByBo(PayBaohuoWorktimeBo bo) {
        PayBaohuoWorktime update = BeanUtil.toBean(bo, PayBaohuoWorktime.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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