package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.mapper.AdminRoleMapper;
import com.lightinit.hsdataportal.mapper.RoleModuleMapper;
import com.lightinit.hsdataportal.mapper.SentryRoleMapper;
import com.lightinit.hsdataportal.service.IRoleService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Dictionary;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/24.
 */
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private SentryRoleMapper sentryRoleMapper;
    @Autowired
    private RoleModuleMapper roleModuleMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;

    public List<SentryRole> QueryList(int page, int rows, Dictionary<String, String> conditions) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SentryRoleExample example=new SentryRoleExample();
        SentryRoleExample.Criteria criteria=example.createCriteria();
        criteria.andParentidIsNull();
        BaseExample.Page _page=new BaseExample.Page();
        _page.setPageNo(page);
        _page.setPageSize(rows);
        example.setPage(_page);
        return sentryRoleMapper.selectByExample(example);
    }

    public long QueryCount(Dictionary<String, String> conditions) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SentryRoleExample example=new SentryRoleExample();
        SentryRoleExample.Criteria criteria=example.createCriteria();
        criteria.andParentidIsNull();
        return sentryRoleMapper.countByExample(example);
    }

    public SentryRole QueryOne(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return sentryRoleMapper.selectByPrimaryKey(id);
    }

    public boolean InsertRole(SentryRole role) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return sentryRoleMapper.insert(role)>0;
    }

    public boolean DeleteRole(long id) {
        return false;
    }

    public boolean UpdateRole(SentryRole role) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SentryRoleExample example=new SentryRoleExample();
        SentryRoleExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(role.getId());
        return sentryRoleMapper.updateByExample(role,example)>0;
    }

    public List<RoleModule> QueryModuleList(long roleid) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoleModuleExample example=new RoleModuleExample();
        RoleModuleExample.Criteria criteria=example.createCriteria();
        criteria.andRoleidEqualTo(roleid);
        return roleModuleMapper.selectByExample(example);
    }

    public List<AdminRole> QueryRoleList(long uid) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminRoleExample example=new AdminRoleExample();
        AdminRoleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        return adminRoleMapper.selectByExample(example);
    }

    @Override
    public List<SentryRole> QueryRoleList(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return sentryRoleMapper.selectByUserName(username);
    }

    public boolean SaveRoleModule(long roleid, List<Long> ids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoleModuleExample example=new RoleModuleExample();
        RoleModuleExample.Criteria criteria=example.createCriteria();
        criteria.andRoleidEqualTo(roleid);
        roleModuleMapper.deleteByExample(example);
        int resultCount=0;
        for (Long id:ids) {
            RoleModule record=new RoleModule();
            record.setModuleid(id);
            record.setRoleid(roleid);
            resultCount+=roleModuleMapper.insert(record);
        }
        return resultCount==ids.size();
    }

    public boolean IsExitsRole(String code) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SentryRoleExample example=new SentryRoleExample();
        SentryRoleExample.Criteria criteria=example.createCriteria();
        criteria.andRolecodeEqualTo(code.toLowerCase());
        return sentryRoleMapper.countByExample(example)>0;
    }

    public boolean CheckRoleHasUser(List<Long> ids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminRoleExample example=new AdminRoleExample();
        AdminRoleExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(ids);
        return adminRoleMapper.countByExample(example)>0;
    }
    public boolean CheckRoleHasModules(List<Long> ids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoleModuleExample example=new RoleModuleExample();
        RoleModuleExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(ids);
        return roleModuleMapper.countByExample(example)>0;
    }

    public boolean SaveUserRoles(long uid, List<Long> ids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminRoleExample example=new AdminRoleExample();
        AdminRoleExample.Criteria criteria=example.createCriteria();
        criteria.andRoleidEqualTo(uid);
        adminRoleMapper.deleteByExample(example);
        int resultCount=0;
        for (Long id:ids) {
            AdminRole record=new AdminRole();
            record.setUserid(uid);
            record.setRoleid(id);
            resultCount+=adminRoleMapper.insert(record);
        }
        return resultCount==ids.size();
    }

    public boolean CheckUserHasRole(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminRoleExample example=new AdminRoleExample();
        AdminRoleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(id);
        return adminRoleMapper.countByExample(example)>0;
    }

    public List<SentryRole> QueryManyRole(List<Long> list) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SentryRoleExample example=new SentryRoleExample();
        SentryRoleExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(list);
        return sentryRoleMapper.selectByExample(example);
    }

    public List<RoleModule> QueryModuleList(List<Long> roleids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoleModuleExample example=new RoleModuleExample();
        RoleModuleExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(roleids);
        return roleModuleMapper.selectByExample(example);
    }

    public List<RoleModule> QueryUserModuleList(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return roleModuleMapper.selectByUserMenu(username);
    }
}
