package com.jkcredit.invoice.hub.service.userRolePermission;

import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.mapper.RolePermissionMapper;
import com.jkcredit.invoice.hub.mapper.UserRolePermissionMapper;
import com.jkcredit.invoice.hub.model.dto.userRolePermission.UserRolePermissionDto;
import com.jkcredit.invoice.hub.model.po.userRolePermission.UserRolePermissionPo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/13 17:45
 * @version: V1.0
 **/
@Service
@Slf4j
public class UserRolePermissionServiceImpl extends BaseService implements UserRolePermissionService {
    @Resource
    UserRolePermissionMapper mapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;

    @Override
    public void saveUserRolePermission(UserRolePermissionDto dto) {
        validate(dto);
        // 此处为用户授权接口，需要根据roleId 查询rolePermission表中的所以permissionId,循环放入userRolePermission
        List<Long> permissions = rolePermissionMapper.getPermissionIdByRoleId(dto.getRoleId());

        try {
            for (Long permission : permissions) {
                UserRolePermissionPo userRolePermissionPo = new UserRolePermissionPo();
                userRolePermissionPo.setUserId(dto.getUserId());
                userRolePermissionPo.setRoleId(dto.getRoleId());
                userRolePermissionPo.setPermissionId(permission);
                userRolePermissionPo.setCreateTime(new Date());
                mapper.insert(userRolePermissionPo);
            }
        } catch (Exception e) {
            log.error("新增用户权限失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增用户权限失败");
        }
    }

    @Override
    public void saveUserRolePermissionByRoleId(Long roleId, List<Long> permissionIdList) {
        try {
            List<Long> userIdList = mapper.getUserIdByRoleId(roleId);
            for (Long userId : userIdList) {
                for (Long permissionId : permissionIdList) {
                    UserRolePermissionPo userRolePermissionPo = new UserRolePermissionPo();
                    userRolePermissionPo.setUserId(userId);
                    userRolePermissionPo.setRoleId(roleId);
                    userRolePermissionPo.setPermissionId(permissionId);
                    userRolePermissionPo.setCreateTime(new Date());
                    mapper.insert(userRolePermissionPo);
                }
            }
        } catch (Exception e) {
            log.error("新增用户权限失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增用户权限失败");
        }
    }

    @Override
    public void updateUserRolePermissionByRoleId(Long roleId, List<Long> permissionIdList) {
        try {
            List<UserRolePermissionPo> userRolePermissionPoList = mapper.getUserRolePermissionByRoleId(roleId);
            for (UserRolePermissionPo userRolePermissionPo : userRolePermissionPoList) {
                for (Long permissionId : permissionIdList) {
                    if (userRolePermissionPo.getPermissionId().equals(permissionId)) {
                        userRolePermissionPo.setActivated(CommonConstant.ACTIVATED_DELETED);
                        userRolePermissionPo.setUpdateTime(new Date());
                        mapper.updateById(userRolePermissionPo);
                    }
                }
            }
        } catch (Exception e) {
            log.error("删除用户权限失败，失败原因：{}", e.getMessage());
            throw new ServiceException("删除用户权限失败");
        }

    }
}
