package com.lightinit.hsdatashow.impl;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.service.IRoleService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static java.util.stream.Collectors.toList;

/**
 * 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;
    @Autowired
    private RoleModuleActionMapper roleModuleActionMapper;
    @Autowired
    private ModuleActionMapper moduleActionMapper;

    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) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SentryRoleExample example1 = new SentryRoleExample();
        SentryRoleExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(id);
        return sentryRoleMapper.deleteByExample(example1) > 0;
    }

    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);
    }

    @Transactional
    public boolean SaveRoleModule(long roleid, List<Long> moduleIds, List<Long> actionIds) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoleModuleExample example1=new RoleModuleExample();
        RoleModuleExample.Criteria criteria1=example1.createCriteria();
        criteria1.andRoleidEqualTo(roleid);
        List<RoleModule> roleModuleList = roleModuleMapper.selectByExample(example1);
        List<Long> roleModuleIdList = new ArrayList<>();
        for (RoleModule roleModule: roleModuleList) {
            roleModuleIdList.add(roleModule.getId());
        }

        RoleModuleActionExample example2 = new RoleModuleActionExample();
        RoleModuleActionExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andRolemoduleidIn(roleModuleIdList);
        roleModuleActionMapper.deleteByExample(example2);
        roleModuleMapper.deleteByExample(example1);

        ModuleActionExample example3 = new ModuleActionExample();
        ModuleActionExample.Criteria criteria3 = example3.createCriteria();
        List<ModuleAction> allModuleActionList = moduleActionMapper.selectByExample(example3);

        int resultCount=0;
        if (moduleIds != null){
            for (Long moduleId : moduleIds) {
                RoleModule roleModule = new RoleModule();
                roleModule.setModuleid(moduleId);
                roleModule.setRoleid(roleid);
                resultCount += roleModuleMapper.insert(roleModule);
                if (actionIds != null){
                    List<Long> currentModuleActionIds = allModuleActionList.stream().filter(q -> q.getModuleId().equals(moduleId)).map(q -> q.getId()).collect(toList());
                    for (Long actionId : actionIds) {
                        if (currentModuleActionIds.contains(actionId)){
                            RoleModuleAction roleModuleAction = new RoleModuleAction();
                            roleModuleAction.setRolemoduleid(roleModule.getId());
                            roleModuleAction.setActionid(actionId);
                            resultCount += roleModuleActionMapper.insert(roleModuleAction);
                        }
                    }
                }
            }
        }

        int expectedSize = (moduleIds == null ? 0 : moduleIds.size()) + (actionIds == null ? 0 : actionIds.size());
        return resultCount == expectedSize;
    }

    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.andRoleidIn(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);
    }
}
