package com.ruoyi.ehsDocument.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

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

import com.ruoyi.ehsDocument.mapper.EhsDocumentMapper;
import com.ruoyi.ehsDocument.domain.EhsDocument;
import com.ruoyi.ehsDocument.service.IEhsDocumentService;

/**
 * 公文管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-07
 */
@Service
public class EhsDocumentServiceImpl extends ServiceImpl<EhsDocumentMapper, EhsDocument> implements IEhsDocumentService {

    @Autowired
    private EhsDocumentMapper ehsDocumentMapper;

    /**
     * 查询公文管理
     *
     * @param id 公文管理主键
     * @return 公文管理
     */
    @Override
    public EhsDocument selectEhsDocumentById(Long id) {
        return ehsDocumentMapper.selectEhsDocumentById(id);
    }

    /**
     * 查询公文管理列表
     *
     * @param ehsDocument 公文管理
     * @return 公文管理
     */
    @Override
    public List<EhsDocument> selectEhsDocumentList(EhsDocument ehsDocument) {
        // 按部门区分权限,局端没有查询全部的权限
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        ehsDocument.setDeptId(currentUser.getDept().getDeptId().longValue());
        return ehsDocumentMapper.selectEhsDocumentList(ehsDocument);
    }
    /*@Override
    public List<EhsDocument> selectEhsDocumentList(EhsDocument ehsDocument) {
        // 如果登录用户不是县级部门(即不是根节点parent_id=0),那么只能查询本部门的;县级部门(根节点)则查询全部
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        Long deptParentId = currentUser.getDept().getParentId();
        if (deptParentId != null &&
                deptParentId.longValue() != 0) {
            ehsDocument.setDeptId(currentUser.getDept().getDeptId().longValue());
        }
        return ehsDocumentMapper.selectEhsDocumentList(ehsDocument);
    }*/

    /**
     * 获取拟稿部门下拉列表
     *
     * @return
     */
    @Override
    public List<String> getAllDraftDept() {
        return ehsDocumentMapper.getAllDraftDept();
    }


    private LambdaQueryWrapper<EhsDocument> buildQueryWrapper(EhsDocument query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<EhsDocument> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getDocName()), EhsDocument::getDocName, query.getDocName());
        lqw.eq(query.getDocType() != null, EhsDocument::getDocType, query.getDocType());
        lqw.orderByDesc(EhsDocument::getCreateTime);
        return lqw;
    }

}
