package cn.com.ctop.kuaishou.modules.clean.service.impl;

import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.clean.entity.ProjectMonitorWhiteList;
import cn.com.ctop.kuaishou.modules.clean.mapper.ProjectMonitorWhiteListMapper;
import cn.com.ctop.kuaishou.modules.clean.service.IProjectMonitorWhiteListService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 监测链接预警-项目白名单
 *
 * @author jeecg-boot
 * 2021-09-23
 * @version V1.0
 */
@Service
public class ProjectMonitorWhiteListServiceImpl extends ServiceImpl<ProjectMonitorWhiteListMapper, ProjectMonitorWhiteList> implements IProjectMonitorWhiteListService {
    @Autowired
    private ProjectMonitorWhiteListMapper monitorWhiteListMapper;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private IUserAllocationService userAllocationService;

    @Override
    public List<JSONObject> getProjectAndAccountIds(String userId) {
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        List<String> userIds = new ArrayList<>();
        if ("CompanyManager".equals(roleCode)) {
            userIds = sysRoleService.getCompanyUsers(roleCode, userId);
        } else if ("bytedanceDirector".equals(roleCode)) {
            userIds = sysRoleService.getCompanyUsers(roleCode, userId);
        } else if ("kuaishouDirector".equals(roleCode)) {
            userIds = sysRoleService.getCompanyUsers(roleCode, userId);
        } else if (roleCode.equals("admin")) {
            userId = null;
        }else{
            userIds.add(userId);
        }
        List<JSONObject> projectIds = monitorWhiteListMapper.getProjectIds(userIds);
        if (Check.isNull(projectIds)) {
            return null;
        }
        for (int i = 0; i < projectIds.size(); i++) {
            JSONObject projectJson = projectIds.get(i);
            Long projectId = projectJson.getLong("projectId");
            List<JSONObject> accountListByProject = userAllocationService.getAccountListByProjectAndUserId(projectId, userId);
            projectJson.put("accountList", accountListByProject);
        }
        return projectIds;
    }
}
