
package com.tmerclub.cloud.distribution.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.platform.feign.SysUserFeignClient;
import com.tmerclub.cloud.api.platform.vo.SysUserVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.distribution.dto.DistributionUserBanDTO;
import com.tmerclub.cloud.distribution.mapper.DistributionUserBanMapper;
import com.tmerclub.cloud.distribution.model.DistributionUserBan;
import com.tmerclub.cloud.distribution.service.DistributionUserBanService;
import com.tmerclub.cloud.distribution.vo.DistributionUserBanVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 分销封禁记录
 *
 * @author cl
 * @date 2021-08-09 14:14:08
 */
@Service
public class DistributionUserBanServiceImpl implements DistributionUserBanService {

    @Autowired
    private DistributionUserBanMapper distributionUserBanMapper;

    @DubboReference
    private SysUserFeignClient sysUserFeignClient;

    @Override
    public PageVO<DistributionUserBanVO> page(PageDTO pageDTO, DistributionUserBanDTO distributionUserBanDTO) {
        PageVO<DistributionUserBanVO> banPage = PageUtil.doPage(pageDTO, () -> distributionUserBanMapper.list(distributionUserBanDTO));
        List<DistributionUserBanVO> banPageList = banPage.getList();
        if (CollUtil.isEmpty(banPageList)) {
            return banPage;
        }
        List<Long> modifiers = banPageList.stream().map(DistributionUserBanVO::getModifier).distinct().collect(Collectors.toList());
        if (CollUtil.isEmpty(modifiers)) {
            return banPage;
        }
        ServerResponseEntity<List<SysUserVO>> sysUserData = sysUserFeignClient.getSysUserList(modifiers);
        if (!sysUserData.isSuccess()) {
            throw new LuckException(sysUserData.getMsg());
        }
        List<SysUserVO> sysUserList = sysUserData.getData();
        Map<Long, String> modifierMap = sysUserList.stream().collect(Collectors.toMap(SysUserVO::getSysUserId, SysUserVO::getNickName));
        for (DistributionUserBanVO distributionUserBanVO : banPageList) {
            Long modifier = distributionUserBanVO.getModifier();
            if (Objects.nonNull(modifier)) {
                String modifierName = modifierMap.get(modifier);
                if (Objects.nonNull(modifierName)) {
                    distributionUserBanVO.setModifierName(modifierName);
                }
            }
        }
        return banPage;
    }

    @Override
    public DistributionUserBan getByBanId(Long banId) {
        return distributionUserBanMapper.getByBanId(banId);
    }

    @Override
    public void save(DistributionUserBan distributionUserBan) {
        distributionUserBanMapper.save(distributionUserBan);
    }

    @Override
    public void update(DistributionUserBan distributionUserBan) {
        distributionUserBanMapper.update(distributionUserBan);
    }

    @Override
    public void deleteById(Long banId) {
        distributionUserBanMapper.deleteById(banId);
    }
}
