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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.casic.missiles.modular.system.model.DeptPermission;
import com.casic.missiles.modular.system.service.IDeptPermissionService;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.modular.system.dao.DeptPermissionMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 组织机构业务支持配置 服务实现类
 * </p>
 *
 * @author lwh123
 * @since 2019-05-28
 */
@Service
public class DeptPermissionServiceImpl extends ServiceImpl<DeptPermissionMapper, DeptPermission> implements IDeptPermissionService {
    @Resource
    private AbstractPermissionContext service;

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