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.OaSalaryItemBo;
import com.ruoyi.oa.domain.vo.OaSalaryItemVo;
import com.ruoyi.oa.domain.OaSalaryItem;
import com.ruoyi.oa.mapper.OaSalaryItemMapper;
import com.ruoyi.oa.service.IOaSalaryItemService;

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

/**
 * 薪水详情Service业务层处理
 *
 * @author hdka
 * @date 2025-03-06
 */
@RequiredArgsConstructor
@Service
public class OaSalaryItemServiceImpl implements IOaSalaryItemService {

    private final OaSalaryItemMapper baseMapper;

    /**
     * 查询薪水详情
     */
    @Override
    public OaSalaryItemVo queryById(Long salaryItemId){
        return baseMapper.selectVoById(salaryItemId);
    }

    /**
     * 查询薪水详情列表
     */
    @Override
    public TableDataInfo<OaSalaryItemVo> queryPageList(OaSalaryItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaSalaryItem> lqw = buildQueryWrapper(bo);
        Page<OaSalaryItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询薪水详情列表
     */
    @Override
    public List<OaSalaryItemVo> queryList(OaSalaryItemBo bo) {
        LambdaQueryWrapper<OaSalaryItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaSalaryItem> buildQueryWrapper(OaSalaryItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaSalaryItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, OaSalaryItem::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), OaSalaryItem::getReason, bo.getReason());
        lqw.eq(bo.getPrice() != null, OaSalaryItem::getPrice, bo.getPrice());
        lqw.eq(bo.getFlag() != null, OaSalaryItem::getFlag, bo.getFlag());
        lqw.eq(bo.getSalaryId() != null, OaSalaryItem::getSalaryId, bo.getSalaryId());
        lqw.eq(bo.getSignTime() != null, OaSalaryItem::getSignTime, bo.getSignTime());
        return lqw;
    }

    /**
     * 新增薪水详情
     */
    @Override
    public Boolean insertByBo(OaSalaryItemBo bo) {
        OaSalaryItem add = BeanUtil.toBean(bo, OaSalaryItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSalaryItemId(add.getSalaryItemId());
        }
        return flag;
    }

    /**
     * 修改薪水详情
     */
    @Override
    public Boolean updateByBo(OaSalaryItemBo bo) {
        OaSalaryItem update = BeanUtil.toBean(bo, OaSalaryItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public Boolean checkFile(OaSalaryItemBo bo) {
        LambdaQueryWrapper<OaSalaryItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(OaSalaryItem::getSignTime,bo.getSignTime());
        lambdaQueryWrapper.eq(OaSalaryItem::getType,3L);
        return baseMapper.selectList(lambdaQueryWrapper).isEmpty();
    }
}
