package com.njcn.gather.user.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.user.user.mapper.SysUserSignatureMapper;
import com.njcn.gather.user.user.pojo.constant.SysFileConst;
import com.njcn.gather.user.user.pojo.po.SysFile;
import com.njcn.gather.user.user.pojo.po.SysUser;
import com.njcn.gather.user.user.pojo.po.SysUserSignature;
import com.njcn.gather.user.user.service.ISysFileService;
import com.njcn.gather.user.user.service.ISysUserSignatureService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 用户签名服务实现。
 */
@Service
@RequiredArgsConstructor
public class SysUserSignatureServiceImpl extends ServiceImpl<SysUserSignatureMapper, SysUserSignature>
        implements ISysUserSignatureService {

    private final ISysFileService sysFileService;

    @Override
    public void fillSignatureInfo(List<SysUser> users) {
        if (CollUtil.isEmpty(users)) {
            return;
        }
        Map<String, SysUserSignature> signatureMap = new LinkedHashMap<String, SysUserSignature>();
        List<String> userIds = users.stream().map(SysUser::getId).collect(Collectors.toList());
        for (SysUserSignature signature : listActiveByUserIds(userIds)) {
            signatureMap.put(signature.getUserId(), signature);
        }
        Map<String, SysFile> sysFileMap = new LinkedHashMap<String, SysFile>();
        List<String> fileIds = signatureMap.values().stream().map(SysUserSignature::getFileId).collect(Collectors.toList());
        for (SysFile sysFile : sysFileService.listActiveByIds(fileIds)) {
            sysFileMap.put(sysFile.getId(), sysFile);
        }
        for (SysUser user : users) {
            SysUserSignature signature = signatureMap.get(user.getId());
            if (signature == null) {
                user.setSignatureFileId(null);
                user.setSignatureFileName(null);
                continue;
            }
            user.setSignatureFileId(signature.getFileId());
            SysFile sysFile = sysFileMap.get(signature.getFileId());
            user.setSignatureFileName(sysFile == null ? null : sysFile.getFileName());
        }
    }

    @Override
    public SysUserSignature getActiveByUserId(String userId) {
        if (StrUtil.isBlank(userId)) {
            return null;
        }
        return this.lambdaQuery()
                .eq(SysUserSignature::getUserId, userId)
                .eq(SysUserSignature::getStatus, SysFileConst.STATUS_NORMAL)
                .one();
    }

    @Override
    public SysUserSignature saveOrUpdateSignature(String userId, String userName, String fileId) {
        SysUserSignature existing = getActiveByUserId(userId);
        if (existing == null) {
            SysUserSignature signature = new SysUserSignature();
            signature.setId(UUID.randomUUID().toString().replace("-", ""));
            signature.setUserId(userId);
            signature.setUserName(userName);
            signature.setFileId(fileId);
            signature.setStatus(SysFileConst.STATUS_NORMAL);
            this.save(signature);
            return null;
        }
        SysUserSignature oldSignature = copySignature(existing);
        existing.setUserName(userName);
        existing.setFileId(fileId);
        existing.setStatus(SysFileConst.STATUS_NORMAL);
        this.updateById(existing);
        return oldSignature;
    }

    @Override
    public void syncUserName(String userId, String userName) {
        SysUserSignature signature = getActiveByUserId(userId);
        if (signature == null || StrUtil.equals(signature.getUserName(), userName)) {
            return;
        }
        signature.setUserName(userName);
        this.updateById(signature);
    }

    @Override
    public List<SysUserSignature> listActiveByUserIds(List<String> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return Collections.emptyList();
        }
        return this.lambdaQuery()
                .in(SysUserSignature::getUserId, userIds)
                .eq(SysUserSignature::getStatus, SysFileConst.STATUS_NORMAL)
                .list();
    }

    @Override
    public void markDeleted(List<SysUserSignature> signatures) {
        if (CollUtil.isEmpty(signatures)) {
            return;
        }
        for (SysUserSignature signature : signatures) {
            signature.setStatus(SysFileConst.STATUS_DELETED);
            this.updateById(signature);
        }
    }

    private SysUserSignature copySignature(SysUserSignature source) {
        SysUserSignature copy = new SysUserSignature();
        copy.setId(source.getId());
        copy.setUserId(source.getUserId());
        copy.setUserName(source.getUserName());
        copy.setFileId(source.getFileId());
        copy.setStatus(source.getStatus());
        return copy;
    }
}
