package com.hzs.member.base.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.member.base.CuMemberTeam;
import com.hzs.common.domain.member.ext.CuMemberTeamExt;
import com.hzs.member.base.mapper.CuMemberTeamMapper;
import com.hzs.member.base.param.MemberTeamInfoParam;
import com.hzs.member.base.service.ICuMemberTeamService;
import com.hzs.member.base.vo.MemberTeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 隶属团队列表 服务实现类
 */
@Service
public class CuMemberTeamServiceImpl extends ServiceImpl<CuMemberTeamMapper, CuMemberTeam> implements ICuMemberTeamService {

    @Autowired
    private RedisService redisService;

    @Override
    public CuMemberTeamExt getCuMemberTeamById(Long pkMember) {
        CuMemberTeamExt cuMemberTeamExt = redisService.getCacheObject(CacheConstants.CU_MEMBER_TEAM_ID + pkMember);
        if (cuMemberTeamExt == null) {
            CuMemberTeamExt cuMemberTeamExtParam = new CuMemberTeamExt();
            cuMemberTeamExtParam.setPkMember(pkMember);
            List<CuMemberTeamExt> cmtExtList = baseMapper.queryCuMemberTeam(cuMemberTeamExtParam);
            if (null != cmtExtList && cmtExtList.size() > 0) {
                cuMemberTeamExt = cmtExtList.get(0);
                redisService.setCacheObject(CacheConstants.CU_MEMBER_TEAM_ID + pkMember, cuMemberTeamExt, CacheConstants.MEMBER_REFRESH_TIME, TimeUnit.DAYS);
            }
        }
        return cuMemberTeamExt;
    }

    @Override
    public List<CuMemberTeamExt> queryCuMemberTeam(CuMemberTeamExt cuMemberTeamExt) {
        return baseMapper.queryCuMemberTeam(cuMemberTeamExt);
    }

    @Override
    public List<CuMemberTeamExt> queryCuMemberTeam(Integer pkCountry) {
        List<CuMemberTeamExt> cmtExtList = redisService.getCacheList(CacheConstants.CU_MEMBER_TEAM + pkCountry);
        if (cmtExtList == null || cmtExtList.size() == 0) {
            CuMemberTeamExt cuMemberTeamExt = new CuMemberTeamExt();
            cuMemberTeamExt.setPkCountry(pkCountry);
            cmtExtList = baseMapper.queryCuMemberTeam(cuMemberTeamExt);
            if (null != cmtExtList && cmtExtList.size() != 0) {
                redisService.setCacheList(CacheConstants.CU_MEMBER_TEAM + pkCountry, cmtExtList);
            }
        }
        return cmtExtList;
    }

    @Override
    public Boolean checkVertex(Integer pkVertex) {
        QueryWrapper<CuMemberTeam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_BD_VERTEX", pkVertex);
        return this.count(queryWrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCuMemberTeamByVertex(Long pkTeamCode) {
        // 查询体系下的所有团队，团队的编号代表没有团队
        CuMemberTeam cuMemberTeam = baseMapper.selectById(pkTeamCode);
        if (cuMemberTeam != null) {
            // 更新自己
            baseMapper.updateSelfCuMemberTeam(cuMemberTeam.getPkMember());
            // 查询自己及伞下的团队
            List<CuMemberTeam> cuMemberTeamExtList = baseMapper.queryCuMemberUmbrella(cuMemberTeam.getPkMember());
            if (CollectionUtil.isNotEmpty(cuMemberTeamExtList)) {
                cuMemberTeamExtList.forEach(cuMemberTeamExt -> {
                    // 更新团队下所属团队
                    baseMapper.updateCuMemberUnderUmbrella(cuMemberTeamExt);
                });
            } else {
                // 体系所有人都修改为null
                baseMapper.updateCuMemberUnderUmbrellaNull(cuMemberTeam.getPkMember());
            }
        }
        return Boolean.TRUE;
    }

    @Override
    public List<MemberTeamVo> queryMemberTeamInfoList(MemberTeamInfoParam memberTeamInfoParam) {
        return baseMapper.queryMemberTeamInfoList(memberTeamInfoParam);
    }

}
