package com.xinyilu.system.service.common.impl;

import com.xinyilu.common.constant.Constants;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.domain.entity.SysRole;
import com.xinyilu.common.utils.SecurityUtils;
import com.xinyilu.system.domain.kindergarteninfo.KindergartenInfo;
import com.xinyilu.system.domain.vo.SysUserVO;
import com.xinyilu.system.domain.vo.common.RoleInfoVO;
import com.xinyilu.system.mapper.SysUserMapper;
import com.xinyilu.system.mapper.classinfo.ClassInfoMapper;
import com.xinyilu.system.mapper.kindergarteninfo.KindergartenInfoMapper;
import com.xinyilu.system.mapper.teacherInfo.TeacherInfoMapper;
import com.xinyilu.system.service.common.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 通用业务实现类
 * @Author 纪寒
 * @Date 2022-08-09 16:44:00
 */
@Service
@Slf4j
public class CommonServiceImpl implements CommonService {

    @Resource
    private ClassInfoMapper classInfoMapper;
    @Resource
    private TeacherInfoMapper teacherInfoMapper;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private KindergartenInfoMapper kindergartenInfoMapper;

    /**
     * 获取用户角色信息
     *
     * @return 角色信息集合
     */
    @Override
    public AjaxResult getAnalysisInfoByRole() {
        RoleInfoVO roleInfoVO = new RoleInfoVO();
        //获取系统用户角色列表信息
        List<String> roleKeys = SecurityUtils.getLoginUser().getUser().getRoles()
                .stream().filter(Objects::nonNull)
                .filter(item -> StringUtils.isNotBlank(item.getRoleKey()))
                .map(SysRole::getRoleKey).collect(Collectors.toList());
        SysUserVO sysUserVO = userMapper.selectUserById(SecurityUtils.getUserId());
        //如果角色只是园长，那么将其所属的幼儿园id返回
        if (roleKeys.contains(Constants.ENCHOU_ROLE) && roleKeys.size() == 1) {
            //角色只是园长
            KindergartenInfo byPrincipalId = classInfoMapper.getKindergartenNameByPrincipalId(SecurityUtils.getUserId());
            if (Objects.nonNull(byPrincipalId)) {
                roleInfoVO.setKindergartenId(Objects.isNull(byPrincipalId.getId()) ? 999999L : byPrincipalId.getId());
                roleInfoVO.setKindergartenName(StringUtils.isBlank(byPrincipalId.getKindergartenName()) ? "" : byPrincipalId.getKindergartenName());
            } else {
                //没有所属的幼儿园给定默认值0
                roleInfoVO.setKindergartenId(999999L);
                roleInfoVO.setKindergartenName("");
            }
        } else if (roleKeys.contains(Constants.HEALTHCARE_ROLE) && roleKeys.size() == 1) {
            //角色是：保健师，查找出其所属的幼儿园信息 获取幼儿园的名称
            setKindergartenInfo(sysUserVO, roleInfoVO);
        } else if ((roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.size() == 1)) {
            //角色只是老师 角色是：老师，查找出其所属的幼儿园id
            setKindergartenInfo(sysUserVO, roleInfoVO);
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 2) {
            //角色只是老师 角色是：老师，查找出其所属的幼儿园id
            setKindergartenInfo(sysUserVO, roleInfoVO);
        }
        roleInfoVO.setRoleKeys(roleKeys);
        return AjaxResult.success(roleInfoVO);
    }

    /**
     * 设置幼儿园信息
     *
     * @param sysUserVO  用户信息
     * @param roleInfoVO 角色信息
     */
    private void setKindergartenInfo(SysUserVO sysUserVO, RoleInfoVO roleInfoVO) {
        if (Objects.nonNull(sysUserVO) && Objects.nonNull(sysUserVO.getKindergartenId())) {
            KindergartenInfo kindergartenInfo = kindergartenInfoMapper.selectKindergartenInfoById(sysUserVO.getKindergartenId());
            if (Objects.nonNull(kindergartenInfo)) {
                roleInfoVO.setKindergartenId(Objects.isNull(kindergartenInfo.getId()) ? 999999L : kindergartenInfo.getId());
                roleInfoVO.setKindergartenName(StringUtils.isBlank(kindergartenInfo.getKindergartenName()) ? "" : kindergartenInfo.getKindergartenName());
            } else {
                //没有所属的幼儿园给定默认值0
                roleInfoVO.setKindergartenId(999999L);
                roleInfoVO.setKindergartenName("");
            }
        }
    }
}
