package com.izouma.awesomeadmin.service.impl;


import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.SysRoleMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.SysRole;
import com.izouma.awesomeadmin.service.SysRoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * sys_role service接口实现类
 * Tue Apr 10 18:09:56 CST 2018  Suo Chen Cheng
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    private static Logger logger = Logger.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRole> getSysRoleList(SysRole record) {

        logger.info("getSysRoleList");
        try {

            return sysRoleMapper.queryAllSysRole(record);
        } catch (Exception e) {
            logger.error("getSysRoleList", e);
        }

        return null;
    }

    @Override
    public List<SysRole> getSysRoleByPage(Page page, SysRole record) {

        logger.info("getSysRoleByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return sysRoleMapper.querySysRolesByPage(parameter);
        } catch (Exception e) {
            logger.error("getSysRoleByPage", e);
        }

        return null;
    }

    @Override
    public SysRole getSysRoleById(String id) {

        logger.info("getSysRoleById");
        try {

            return sysRoleMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getSysRoleById", e);
        }

        return null;
    }

    @Override
    public List<SysRole> getSysRoleByIds(String ids) {
        logger.info("getSysRoleByIds");
        try {

            return sysRoleMapper.selectByPrimaryKeys(ids);
        } catch (Exception e) {
            logger.error("getSysRoleByIds", e);
        }

        return null;
    }

    @Override
    public SysRole getSysRole(SysRole record) {

        logger.info("getSysRole");
        try {

            return sysRoleMapper.querySysRole(record);
        } catch (Exception e) {
            logger.error("getSysRole", e);
        }

        return null;
    }

    @Override
    public boolean createSysRole(SysRole record) {

        logger.info("createSysRole");
        try {

            int updates = sysRoleMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createSysRole", e);
        }

        return false;
    }

    @Override
    public boolean deleteSysRole(String id) {

        logger.info("deleteSysRole");
        try {

            int updates = sysRoleMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteSysRole", e);
        }

        return false;
    }

    @Override
    public boolean updateSysRole(SysRole record) {

        logger.info("updateSysRole");
        try {

            int updates = sysRoleMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateSysRole", e);
        }

        return false;
    }

    @Override
    public List<SysRole> getUserRoles(Integer userId) {
        logger.info("getUserRoles");
        List<SysRole> roles = new ArrayList<>();
        try {
            roles = sysRoleMapper.getUserRoles(userId);
            return roles;
        } catch (Exception e) {
            logger.error("getUserRoles", e);
        }
        return roles;
    }

    @Override
    public boolean setUserRoles(Integer userId, String roles) {
        logger.info("setUserRoles");
        try {
            sysRoleMapper.clearUserRoles(userId);
            List<String> roleList = Arrays.asList(roles.split(","));
            sysRoleMapper.setUserRoles(userId, roleList);
            return true;
        } catch (Exception e) {
            logger.error("setUserRoles", e);
        }
        return false;
    }

    @Override
    public boolean addUserRole(Integer userId, Integer roleId) {
        logger.info("addUserRole");
        try {
            return sysRoleMapper.addUserRole(userId, roleId) > 0;
        } catch (Exception e) {
            logger.error("addUserRole", e);
        }
        return false;
    }

    @Override
    public boolean removeUserRole(Integer userId, Integer roleId) {
        logger.info("removeUserRole");
        try {
            return sysRoleMapper.removeUserRole(userId, roleId) > 0;
        } catch (Exception e) {
            logger.error("removeUserRole", e);
        }
        return false;
    }

}

