/**
 * 文件：RoleServiceImpl.java
 * 版本：1.0.0
 * 日期：
 * Copyright &reg;
 * All right reserved.
 */

package com.mortals.xhx.base.system.role.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.base.system.role.dao.RoleDao;
import com.mortals.xhx.base.system.role.model.*;
import com.mortals.xhx.base.system.role.service.RoleAuthService;
import com.mortals.xhx.base.system.role.service.RoleService;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>Title: 角色信息</p>
 * <p>Description: RoleServiceImpl service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author
 * @version 1.0.0
 */
@Service("roleService")
public class RoleServiceImpl extends AbstractCRUDServiceImpl<RoleDao,RoleEntity,Long> implements RoleService {

    @Autowired
    private RoleAuthService roleAuthService;
    @Autowired
    private RoleUserService roleUserService;

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        //删除关联角色
        Arrays.asList(ids).stream().peek(roleId->{
            RoleAuthQuery roleAuthQuery = new RoleAuthQuery();
            roleAuthQuery.setRoleId(roleId);
            Long[] roleAuthIds = roleAuthService.find(roleAuthQuery).stream().map(RoleAuthEntity::getId).toArray(Long[]::new);
            roleAuthService.remove(roleAuthIds,context);

            RoleUserQuery roleUserQuery = new RoleUserQuery();
            roleUserQuery.setRoleId(roleId);

            Long[] roleUserIds = roleUserService.find(roleUserQuery).stream().map(RoleUserEntity::getId).toArray(Long[]::new);
            roleUserService.remove(roleUserIds,context);

        }).count();
        super.removeAfter(ids, context, result);
    }

}