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.PayWorkTimeBo;
import com.ruoyi.pay.domain.vo.PayWorkTimeVo;
import com.ruoyi.pay.domain.PayWorkTime;
import com.ruoyi.pay.mapper.PayWorkTimeMapper;
import com.ruoyi.pay.service.IPayWorkTimeService;

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

/**
 * 员工工时时间段Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayWorkTimeServiceImpl implements IPayWorkTimeService {

    private final PayWorkTimeMapper baseMapper;

    /**
     * 查询员工工时时间段
     */
    @Override
    public PayWorkTimeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询员工工时时间段列表
     */
    @Override
    public TableDataInfo<PayWorkTimeVo> queryPageList(PayWorkTimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayWorkTime> lqw = buildQueryWrapper(bo);
        Page<PayWorkTimeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工工时时间段列表
     */
    @Override
    public List<PayWorkTimeVo> queryList(PayWorkTimeBo bo) {
        LambdaQueryWrapper<PayWorkTime> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增员工工时时间段
     */
    @Override
    public Boolean insertByBo(PayWorkTimeBo bo) {
        PayWorkTime add = BeanUtil.toBean(bo, PayWorkTime.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改员工工时时间段
     */
    @Override
    public Boolean updateByBo(PayWorkTimeBo bo) {
        PayWorkTime update = BeanUtil.toBean(bo, PayWorkTime.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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