package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.SysOaWarehouseLogBo;
import com.ruoyi.oa.domain.vo.SysOaWarehouseLogVo;
import com.ruoyi.oa.domain.SysOaWarehouseLog;
import com.ruoyi.oa.mapper.SysOaWarehouseLogMapper;
import com.ruoyi.oa.service.ISysOaWarehouseLogService;

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

/**
 * 日志Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-28
 */
@RequiredArgsConstructor
@Service
public class SysOaWarehouseLogServiceImpl implements ISysOaWarehouseLogService {

    private final SysOaWarehouseLogMapper baseMapper;

    /**
     * 查询日志
     */
    @Override
    public SysOaWarehouseLogVo queryById(Long logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 查询日志列表
     */
    @Override
    public TableDataInfo<SysOaWarehouseLogVo> queryPageList(SysOaWarehouseLogBo bo, PageQuery pageQuery) {
        QueryWrapper<SysOaWarehouseLog> lqw = buildQueryWrapper(bo);
        Page<SysOaWarehouseLogVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询日志列表
     */
    @Override
    public List<SysOaWarehouseLogVo> queryList(SysOaWarehouseLogBo bo) {
        QueryWrapper<SysOaWarehouseLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoListPlus(lqw);
    }

    private QueryWrapper<SysOaWarehouseLog> buildQueryWrapper(SysOaWarehouseLogBo bo) {
        QueryWrapper<SysOaWarehouseLog> qw = new QueryWrapper<>();
        qw.eq(bo.getMasterId() != null, "log.master_id", bo.getMasterId());
        qw.eq(bo.getWarehouseId() != null, "log.warehouse_id", bo.getWarehouseId());
        qw.eq(bo.getNum() != null, "log.num", bo.getNum());
        // 新增模糊查询
        qw.like(org.apache.commons.lang3.StringUtils.isNotBlank(bo.getMasterNum()), "master.master_num", bo.getMasterNum());
        qw.like(org.apache.commons.lang3.StringUtils.isNotBlank(bo.getWarehouseName()), "ware.name", bo.getWarehouseName());
        return qw;
    }

    /**
     * 新增日志
     */
    @Override
    public Boolean insertByBo(SysOaWarehouseLogBo bo) {
        SysOaWarehouseLog add = BeanUtil.toBean(bo, SysOaWarehouseLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改日志
     */
    @Override
    public Boolean updateByBo(SysOaWarehouseLogBo bo) {
        SysOaWarehouseLog update = BeanUtil.toBean(bo, SysOaWarehouseLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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