package com.usoftchina.bi.server.service.user;

import com.usoftchina.bi.core.base.BaseContextHolder;
import com.usoftchina.bi.server.dao.dashboard.DashboardsMapper;
import com.usoftchina.bi.server.dao.user.UserMapper;
import com.usoftchina.bi.server.dao.user.UserPowerMapper;
import com.usoftchina.bi.server.model.vo.configVo.BatchUserPowerInfo;
import com.usoftchina.bi.server.model.vo.configVo.UserPowerInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardPowerDTO;
import com.usoftchina.bi.server.service.common.MessageLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author chenwei
 * @Date 2019-05-13
 */
@Service
public class UserPowerService {

    @Autowired
    private UserPowerMapper userPowerMapper;
    @Autowired
    private MessageLogService messageLogService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DashboardsMapper dashboardsMapper;

    /**
     * 新增用户(组)权限
     * @param userPowerInfo
     */
    public void save(UserPowerInfo userPowerInfo){
        userPowerMapper.insert(userPowerInfo);
        String content = getLogContent(userPowerInfo, "新增");
        messageLogService.save("权限管理", null, BaseContextHolder.getUserName(), content);
    }

    /**
     * 获取用户(组)所拥有的权限
     * @param type
     * @param id
     */
    public List<DashboardPowerDTO> read(int type, int id){
        return userPowerMapper.selectDashBoards(type, id, BaseContextHolder.getUserId());
    }

    /**
     * 删除用户(组)权限
     * @param userPowerInfo
     */
    public void delete(UserPowerInfo userPowerInfo){
        userPowerMapper.delete(userPowerInfo);
        String content = getLogContent(userPowerInfo, "删除");
        messageLogService.delete("权限管理", null, BaseContextHolder.getUserName(), content);
    }

    /**
     * 批量更新权限
     * @param batchUserPowerInfo
     */
    public void batchAdd(BatchUserPowerInfo batchUserPowerInfo) {
        String[] ids = batchUserPowerInfo.getDashboardIds().split(",");
        if (batchUserPowerInfo.getType() == 0) {
            String dashboardsName = userPowerMapper.getDashboardsNameByUserGroup(batchUserPowerInfo.getDashboardIds(), batchUserPowerInfo.getBizId());
            String name = userMapper.getUserGroup(batchUserPowerInfo.getBizId()).getUserGroupName();

            userPowerMapper.deleteByUserGroupId(batchUserPowerInfo.getDashboardIds(),batchUserPowerInfo.getBizId());
            userPowerMapper.insertByUserGroupId(ids, batchUserPowerInfo.getBizId());
            messageLogService.save("权限管理", null, BaseContextHolder.getUserName(), "批量新增 " + name + "(用户组) 查看报表 " + dashboardsName + " 的权限");
        }else {
            String dashboardsName = userPowerMapper.getDashboardsNameByUser(batchUserPowerInfo.getDashboardIds(), batchUserPowerInfo.getBizId());
            String name = userMapper.getUserById(batchUserPowerInfo.getBizId()).getUserName();

            userPowerMapper.deleteByUserId(batchUserPowerInfo.getDashboardIds(), batchUserPowerInfo.getBizId());
            userPowerMapper.insertByUserId(ids, batchUserPowerInfo.getBizId());

            messageLogService.save("权限管理", null, BaseContextHolder.getUserName(), "批量新增 " + name + "(用户) 查看报表 " + dashboardsName + " 的权限");
        }
    }

    /**
     * 批量删除权限
     * @param batchUserPowerInfo
     */
    public void batchDelete(BatchUserPowerInfo batchUserPowerInfo){
        if (batchUserPowerInfo.getType() == 0) {
            String dashboardsName = userPowerMapper.getDashboardsByIds(batchUserPowerInfo.getDashboardIds());
            String name = userMapper.getUserGroup(batchUserPowerInfo.getBizId()).getUserGroupName();
            userPowerMapper.deleteByUserGroupId(batchUserPowerInfo.getDashboardIds(),batchUserPowerInfo.getBizId());
            messageLogService.delete("权限管理", null, BaseContextHolder.getUserName(), "批量删除 " + name + "(用户组) 查看报表 " + dashboardsName + " 的权限");
        }else {
            String dashboardsName = userPowerMapper.getDashboardsByIds(batchUserPowerInfo.getDashboardIds());
            String name = userMapper.getUserById(batchUserPowerInfo.getBizId()).getUserName();
            userPowerMapper.deleteByUserId(batchUserPowerInfo.getDashboardIds(), batchUserPowerInfo.getBizId());
            messageLogService.delete("权限管理", null, BaseContextHolder.getUserName(), "批量删除 " + name + "(用户) 查看报表 " + dashboardsName + " 的权限");
        }
    }

    /**
     * 获取日志内容
     * @param userPowerInfo
     * @return
     */
    private String getLogContent(UserPowerInfo userPowerInfo, String operation){
        String dashboardName = dashboardsMapper.getDashboardsWithoutLimit(userPowerInfo.getDashboardId()).getBdName();
        StringBuilder sb = new StringBuilder();
        if (userPowerInfo.getType() == 0) {
            String name = userMapper.getUserGroup(userPowerInfo.getBizId()).getUserGroupName();
            sb.append(operation + "用户组 " + name + "查看报表 " + dashboardName + "的权限");
        }else {
            String name = userMapper.getUserById(userPowerInfo.getBizId()).getUserName();
            sb.append(operation + "用户 " + name + "查看报表 " + dashboardName + "的权限");
        }
        return sb.toString();
    }

}
