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 com.ruoyi.oa.domain.SysOaTaskItem;
import com.ruoyi.oa.domain.bo.SysOaTaskItemBo;
import com.ruoyi.oa.service.ISysOaTaskItemService;
import com.ruoyi.oa.service.ISysOaTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.SysOaTaskUserBo;
import com.ruoyi.oa.domain.vo.SysOaTaskUserVo;
import com.ruoyi.oa.domain.SysOaTaskUser;
import com.ruoyi.oa.mapper.SysOaTaskUserMapper;
import com.ruoyi.oa.service.ISysOaTaskUserService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 文件共享表Service业务层处理
 *
 * @author hdka
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class SysOaTaskUserServiceImpl implements ISysOaTaskUserService {

    private final SysOaTaskUserMapper baseMapper;

    private final ISysOaTaskService taskService;

    private final ISysOaTaskItemService taskItemService;

    /**
     * 查询文件共享表
     */
    @Override
    public SysOaTaskUserVo queryById(Long taskUserId){
        return baseMapper.selectVoById(taskUserId);
    }

    /**
     * 查询文件共享表列表
     */
    @Override
    public TableDataInfo<SysOaTaskUserVo> queryPageList(SysOaTaskUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOaTaskUser> lqw = buildQueryWrapper(bo);
        Page<SysOaTaskUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件共享表列表
     */
    @Override
    public List<SysOaTaskUserVo> queryList(SysOaTaskUserBo bo) {
        LambdaQueryWrapper<SysOaTaskUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaTaskUser> buildQueryWrapper(SysOaTaskUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaTaskUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId() != null, SysOaTaskUser::getTaskId, bo.getTaskId());
        lqw.eq(bo.getItemId() != null, SysOaTaskUser::getItemId, bo.getItemId());
        lqw.eq(bo.getUserId() != null, SysOaTaskUser::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增文件共享表
     */
    @Override
    public Boolean insertByBo(SysOaTaskUserBo bo) {
        bo.getUserList().forEach(user -> {
            SysOaTaskUserBo add = new SysOaTaskUserBo();
            add.setUserId(user.getUserId());
            add.setTaskId(bo.getTaskId());
            if (bo.getItemId() != null) {
                add.setItemId(bo.getItemId());
            }
            if (queryList(add).isEmpty()) {
                SysOaTaskUser bean = BeanUtil.toBean(add, SysOaTaskUser.class);
                baseMapper.insert(bean);
            }
        });

        return true;
    }

    /**
     * 修改文件共享表
     */
    @Override
    public Boolean updateByBo(SysOaTaskUserBo bo) {
        SysOaTaskUser update = BeanUtil.toBean(bo, SysOaTaskUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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