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.OaUserActiveBo;
import com.ruoyi.oa.domain.vo.OaUserActiveVo;
import com.ruoyi.oa.domain.OaUserActive;
import com.ruoyi.oa.mapper.OaUserActiveMapper;
import com.ruoyi.oa.service.IOaUserActiveService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.time.LocalDate;
import java.sql.Date;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

/**
 * 用户活跃统计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-30
 */
@RequiredArgsConstructor
@Service
public class OaUserActiveServiceImpl implements IOaUserActiveService {

    private final OaUserActiveMapper baseMapper;

    /**
     * 查询用户活跃统计
     */
    @Override
    public OaUserActiveVo queryById(Long activeId){
        return baseMapper.selectVoById(activeId);
    }

    /**
     * 查询用户活跃统计列表
     */
    @Override
    public TableDataInfo<OaUserActiveVo> queryPageList(OaUserActiveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaUserActive> lqw = buildQueryWrapper(bo);
        Page<OaUserActiveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户活跃统计列表
     */
    @Override
    public List<OaUserActiveVo> queryList(OaUserActiveBo bo) {
        LambdaQueryWrapper<OaUserActive> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaUserActive> buildQueryWrapper(OaUserActiveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaUserActive> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, OaUserActive::getUserId, bo.getUserId());
        lqw.eq(bo.getActiveDate() != null, OaUserActive::getActiveDate, bo.getActiveDate());
        lqw.eq(bo.getLoginCount() != null, OaUserActive::getLoginCount, bo.getLoginCount());
        lqw.orderByDesc(OaUserActive::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户活跃统计
     */
    @Override
    public Boolean insertByBo(OaUserActiveBo bo) {
        OaUserActive add = BeanUtil.toBean(bo, OaUserActive.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setActiveId(add.getActiveId());
        }
        return flag;
    }

    /**
     * 修改用户活跃统计
     */
    @Override
    public Boolean updateByBo(OaUserActiveBo bo) {
        OaUserActive update = BeanUtil.toBean(bo, OaUserActive.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public boolean hasActiveToday(Long userId) {
        Date today = Date.valueOf(LocalDate.now());
        LambdaQueryWrapper<OaUserActive> lqw = Wrappers.lambdaQuery();
        lqw.eq(OaUserActive::getUserId, userId)
           .eq(OaUserActive::getActiveDate, today);
        return baseMapper.selectCount(lqw) > 0;
    }

    @Override
    public void recordTodayLogin(Long userId) {
        Date today = Date.valueOf(LocalDate.now());
        LambdaUpdateWrapper<OaUserActive> uw = Wrappers.lambdaUpdate();
        uw.eq(OaUserActive::getUserId, userId)
          .eq(OaUserActive::getActiveDate, today)
          .setSql("login_count = login_count + 1");
        int rows = baseMapper.update(null, uw);
        if (rows == 0) {
            OaUserActive entity = new OaUserActive();
            entity.setUserId(userId);
            entity.setActiveDate(today);
            entity.setLoginCount(1L);
            baseMapper.insert(entity);
        }
    }
}
