package com.casic.missiles.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.casic.missiles.modular.system.model.DeptPermission;
import com.casic.missiles.modular.system.service.IDeptPermissionService;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.modular.system.dao.DeptPermissionMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
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 lwh123
 * @since 2019-05-28
 */
@Service
public class DeptPermissionServiceImpl extends ServiceImpl<DeptPermissionMapper, DeptPermission> implements IDeptPermissionService {
    @Autowired
    private ICommonPermissionService service;

    @Transactional
    @Override
    public DeptPermission selectDeptPermission(Long deptId) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("DEPTID", deptId);
        DeptPermission deptPermission = this.selectOne(wrapper);
        if (deptPermission == null) {
            List<Long> deptIds = service.getParentDeptIds(deptId);
            for (int i = deptIds.size() - 1; i > -1; i--) {
                Long depId = deptIds.get(i);
                EntityWrapper query = new EntityWrapper();
                query.eq("DEPTID", depId);
                deptPermission = this.selectOne(query);
                if (deptPermission != null) {
                    return deptPermission;
                }
            }
        }
        return deptPermission;
    }
}
