package cn.com.ty.lift.system.user.service.impl;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.utils.ProjectUtils;
import cn.com.ty.lift.system.constants.CommonConstants;
import cn.com.ty.lift.system.user.dao.entity.model.*;
import cn.com.ty.lift.system.user.dao.mapper.MtCompanyUserMapper;
import cn.com.ty.lift.system.user.dao.mapper.UserAccountMapper;
import cn.com.ty.lift.system.user.service.IProjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangyuan
 * @date 2019-12-17
 * @description
 */
@Service
public class ProjectService implements IProjectService {

    @Resource
    private MtCompanyUserMapper mtCompanyUserMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public Map<String, String> judgeKickOutTeam(ProjectUser projectUser) {
        Map<String, String> returnMsg = new HashMap<>();
        //获取用户在当前公司的项目
        Long companyProjectCount = mtCompanyUserMapper.getCompanyUserProject(projectUser);
        if (companyProjectCount != null && companyProjectCount > 0L) {
            returnMsg.put(CommonConstants.RETURN_CODE_FIELD, ApiConstants.RESULT_ERROR);
            returnMsg.put(CommonConstants.RETURN_MSG_FIELD, "无法踢出团队，请先取消用户关联项目");
            return returnMsg;
        }
        //获取用户在当前公司有没有急修任务
        Long companyEmergencyCount = mtCompanyUserMapper.getCompanyUserEmergency(projectUser);
        if (companyEmergencyCount != null && companyEmergencyCount > 0L) {
            returnMsg.put(CommonConstants.RETURN_CODE_FIELD, ApiConstants.RESULT_ERROR);
            returnMsg.put(CommonConstants.RETURN_MSG_FIELD, "无法踢出团队，用户有正在进行的急修项目");
            return returnMsg;
        }

        returnMsg.put(CommonConstants.RETURN_CODE_FIELD, ApiConstants.RESULT_SUCCESS);
        return returnMsg;
    }

    @Override
    public List<LiftCertificate> getLiftCertificateList(ProjectUser projectUser) {
        return mtCompanyUserMapper.getLiftCertificateList(projectUser);
    }

    @Override
    public LiftCertificate getLiftCertificateByUserIdAndCompanyId(ProjectUser projectUser) {
        return mtCompanyUserMapper.getLiftCertificateByUserIdAndCompanyId(projectUser);
    }

    @Override
    public Map<Long, LiftCertificate> getUserIdToLiftCertificateByUserIdListAndCompanyId(List<Long> userIdList, Long companyId) {
        List<LiftCertificate> liftCertificateList = mtCompanyUserMapper.getLiftCertificateList(new ProjectUser(companyId, userIdList));
        return ProjectUtils.attrToObjMap(liftCertificateList, "userId", null);
    }

    @Override
    public ProjectUser getProjectUserByUserIdAndProjectId(ProjectUser projectUser) {
        return userAccountMapper.getProjectUserByUserIdAndProjectId(projectUser);
    }

    @Override
    public void saveProjectUser(ProjectUser projectUser) {
        userAccountMapper.saveProjectUser(projectUser);
    }

    @Override
    public List<ProjectUser> getProjectUserList(PropertyUserRequest propertyUserRequest) {
        return userAccountMapper.getProjectUserList(propertyUserRequest);
    }

    @Override
    public Long getProjectUserCount(PropertyUserRequest propertyUserRequest) {
        return userAccountMapper.getProjectUserCount(propertyUserRequest);
    }

    @Override
    public Map<Long, ProjectInfo> getProjectIdToProjectByProjectIdList(List<Long> projectIdList) {
        Map<Long, ProjectInfo> projectIdToProjectInfo = new HashMap<>();
        //获取项目信息
        List<ProjectInfo> projectInfoList = userAccountMapper.getProjectInfoList(projectIdList);
        if (projectInfoList != null && projectInfoList.size() > 0) {
            //获取区域信息
            List<Long> regionIdList = ProjectUtils.getAttrList(projectInfoList, "regionId", null);
            List<RegionInfo> regionInfoList = userAccountMapper.getRegionInfoList(regionIdList);
            Map<Long, RegionInfo> regionIdToRegionInfo = ProjectUtils.attrToObjMap(regionInfoList,
                    "regionId", null);
            for (ProjectInfo projectInfo : projectInfoList) {
                //封装区域信息
                RegionInfo regionInfo = regionIdToRegionInfo.get(projectInfo.getRegionId());
                if (regionInfo != null) {
                    projectInfo.setAreaName(regionInfo.getAreaName());
                }
            }
            projectIdToProjectInfo = ProjectUtils.attrToObjMap(projectInfoList,
                    "projectId", null);
        }
        return projectIdToProjectInfo;
    }

    @Override
    public Long deleteProjectUser(PropertyUserRequest propertyUserRequest) {
        return userAccountMapper.deleteProjectUser(propertyUserRequest);
    }

    @Override
    public ProjectInfo getProjectById(Long projectId) {
        return null;
    }

}
