package com.sunyard.ssp.modules.user.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunyard.ssp.modules.user.entity.ScDepartment;
import com.sunyard.ssp.modules.user.mapper.ScDepartmentMapper;
import com.sunyard.ssp.modules.user.service.IScDepartmentService;
import com.sunyard.ssp.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tsz
 * @since 2020-03-12
 */
@Slf4j
@Service
@Transactional
public class ScDepartmentServiceImpl extends ServiceImpl<ScDepartmentMapper, ScDepartment> implements IScDepartmentService {


    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public List<ScDepartment> findByParentIdOrderBySortOrder(Long parentId, Boolean openDataFilter) {

        // 数据权限
        List<String> depIds = securityUtil.getDeparmentIds();
        if(depIds!=null&&depIds.size()>0&&openDataFilter){
            return baseMapper.selectList(new QueryWrapper<ScDepartment>().lambda()
                    .eq(ScDepartment::getParentId, parentId)
                    .in(ScDepartment::getId, depIds)
                    .orderByAsc(ScDepartment::getSortOrder));
        }
        return baseMapper.selectList(new QueryWrapper<ScDepartment>().lambda()
                .eq(ScDepartment::getParentId, parentId)
                .orderByAsc(ScDepartment::getSortOrder));
    }

    @Override
    public List<ScDepartment> findByParentIdAndStatusOrderBySortOrder(Long parentId, Integer status) {

        return baseMapper.selectList(new QueryWrapper<ScDepartment>().lambda()
                .eq(ScDepartment::getParentId, parentId)
                .eq(ScDepartment::getStatus, status)
                .orderByAsc(ScDepartment::getSortOrder));
    }

    @Override
    public List<ScDepartment> findByTitleLikeOrderBySortOrder(String title, Boolean openDataFilter) {

        List<String> depIds = securityUtil.getDeparmentIds();
        if(depIds!=null&&depIds.size()>0&&openDataFilter){
            return baseMapper.selectList(new QueryWrapper<ScDepartment>().lambda()
                    .like(ScDepartment::getTitle, title)
                    .in(ScDepartment::getId, depIds)
                    .orderByAsc(ScDepartment::getSortOrder));
        }
        return baseMapper.selectList(new QueryWrapper<ScDepartment>().lambda()
                .like(ScDepartment::getTitle, title)
                .orderByAsc(ScDepartment::getSortOrder));
    }
}
