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.PayBaohuoEmployeeBo;
import com.ruoyi.pay.domain.vo.PayBaohuoEmployeeVo;
import com.ruoyi.pay.domain.PayBaohuoEmployee;
import com.ruoyi.pay.mapper.PayBaohuoEmployeeMapper;
import com.ruoyi.pay.service.IPayBaohuoEmployeeService;

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

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

    private final PayBaohuoEmployeeMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<PayBaohuoEmployee> buildQueryWrapper(PayBaohuoEmployeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayBaohuoEmployee> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayBaohuoEmployee::getTenantId, bo.getTenantId());
        lqw.eq(bo.getWoId() != null, PayBaohuoEmployee::getWoId, bo.getWoId());
        lqw.eq(bo.getEmpId() != null, PayBaohuoEmployee::getEmpId, bo.getEmpId());
        lqw.eq(bo.getDeptId() != null, PayBaohuoEmployee::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getEmpName()), PayBaohuoEmployee::getEmpName, bo.getEmpName());
        lqw.eq(bo.getAmount() != null, PayBaohuoEmployee::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getMonth()), PayBaohuoEmployee::getMonth, bo.getMonth());
        lqw.eq(bo.getHelper() != null, PayBaohuoEmployee::getHelper, bo.getHelper());
        lqw.eq(bo.getIntime() != null, PayBaohuoEmployee::getIntime, bo.getIntime());
        return lqw;
    }

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

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

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

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