package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmSysRoleUserDao;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.SysRole;
import com.ssm.manage.entity.SysRolePower;
import com.ssm.manage.entity.SysRoleUser;
import com.ssm.manage.mapper.SysRoleUserMapper;
import com.ssm.manage.query.RoleUserRequest;
import com.ssm.manage.query.UserRequest;
import com.ssm.manage.response.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

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

@Component
@Slf4j
public class SsmSysRoleUserDaoImpl extends BaseDaoImpl<SysRoleUser> implements SsmSysRoleUserDao {
    @Autowired
    private SysRoleUserMapper mapper;

    @Override
    public SysRoleUserMapper getMapper() {
        return mapper;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int addRoleUser(RoleUserRequest roleUserRequest) throws SmsException {
        SysRoleUser roleUser = new SysRoleUser();
        roleUser.setUserId(roleUserRequest.getUserId());
        List<SysRole> checkList = roleUserRequest.getCheckList();
        List<SysRoleUser> roleUserList = new ArrayList<>();

        for (SysRole sysRole : checkList){
            SysRoleUser sysRoleUser = new SysRoleUser();
            sysRoleUser.setUserId(roleUserRequest.getUserId());
            sysRoleUser.setUserName(roleUserRequest.getUserName());
            sysRoleUser.setRoleId(sysRole.getRoleId());
            sysRoleUser.setRoleName(sysRole.getRoleName());
            roleUserList.add(sysRoleUser);
        }
        //删除当前用户所有角色
        int i = 0;
        try {
            i = mapper.delete(roleUser);
        }catch (Exception e){
            log.error("SsmSysRoleUserDaoImpl.addRoleUser", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.FAILED);
        }
        //将前台重新勾选的角色对当前用户进行重新关联

            try {
                i = mapper.addRoleUser(roleUserList);
            }catch (Exception e){
                log.error("SsmSysRoleUserDaoImpl.addRoleUser", e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.FAILED);
            }

        return i;
    }


    @Override
    public PageInfo<SysRoleUser> rolePage(RoleUserRequest roleUserRequest) {
        Example example = new Example(SysRolePower.class);
        Example.Criteria criteria = example.createCriteria();
        if (roleUserRequest.getUserId() != null) {
            criteria.andEqualTo("userId", roleUserRequest.getUserId());
        }
        if (roleUserRequest.getRoleId() != null) {
            criteria.andEqualTo("roleId", roleUserRequest.getRoleId());
        }
        PageHelper.startPage(roleUserRequest.getPageNum(),roleUserRequest.getPageSize(),roleUserRequest.getOrderAndSort());
        List<SysRoleUser> list =  mapper.selectByExample(example);
        PageInfo<SysRoleUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
