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.PayAttendanceBo;
import com.ruoyi.pay.domain.vo.PayAttendanceVo;
import com.ruoyi.pay.domain.PayAttendance;
import com.ruoyi.pay.mapper.PayAttendanceMapper;
import com.ruoyi.pay.service.IPayAttendanceService;

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

/**
 * 考勤Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayAttendanceServiceImpl implements IPayAttendanceService {

    private final PayAttendanceMapper baseMapper;

    /**
     * 查询考勤
     */
    @Override
    public PayAttendanceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询考勤列表
     */
    @Override
    public TableDataInfo<PayAttendanceVo> queryPageList(PayAttendanceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayAttendance> lqw = buildQueryWrapper(bo);
        Page<PayAttendanceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询考勤列表
     */
    @Override
    public List<PayAttendanceVo> queryList(PayAttendanceBo bo) {
        LambdaQueryWrapper<PayAttendance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayAttendance> buildQueryWrapper(PayAttendanceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayAttendance> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayAttendance::getTenantId, bo.getTenantId());
        lqw.eq(bo.getEmployeeId() != null, PayAttendance::getEmployeeId, bo.getEmployeeId());
        lqw.like(StringUtils.isNotBlank(bo.getEmpName()), PayAttendance::getEmpName, bo.getEmpName());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), PayAttendance::getDeptName, bo.getDeptName());
        lqw.eq(bo.getDeptId() != null, PayAttendance::getDeptId, bo.getDeptId());
        lqw.eq(bo.getAmStartTime() != null, PayAttendance::getAmStartTime, bo.getAmStartTime());
        lqw.eq(bo.getAmEndTime() != null, PayAttendance::getAmEndTime, bo.getAmEndTime());
        lqw.eq(bo.getPmStartTime() != null, PayAttendance::getPmStartTime, bo.getPmStartTime());
        lqw.eq(bo.getPmEndTime() != null, PayAttendance::getPmEndTime, bo.getPmEndTime());
        lqw.eq(bo.getKaoqinDate() != null, PayAttendance::getKaoqinDate, bo.getKaoqinDate());
        lqw.eq(StringUtils.isNotBlank(bo.getKaoqinYearMonth()), PayAttendance::getKaoqinYearMonth, bo.getKaoqinYearMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getKaoqinjiNum()), PayAttendance::getKaoqinjiNum, bo.getKaoqinjiNum());
        return lqw;
    }

    /**
     * 新增考勤
     */
    @Override
    public Boolean insertByBo(PayAttendanceBo bo) {
        PayAttendance add = BeanUtil.toBean(bo, PayAttendance.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改考勤
     */
    @Override
    public Boolean updateByBo(PayAttendanceBo bo) {
        PayAttendance update = BeanUtil.toBean(bo, PayAttendance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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