package com.fs.hisStore.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.company.domain.CompanyUser;
import com.fs.company.mapper.CompanyUserMapper;
import com.fs.hisStore.domain.FsUserProjectTagScrm;
import com.fs.hisStore.mapper.FsUserProjectTagScrmMapper;
import com.fs.hisStore.param.h5.TagListParam;
import com.fs.hisStore.service.IFsUserProjectTagScrmService;
import com.fs.hisStore.vo.h5.CompanyUserTagListVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FsUserProjectTagScrmServiceImpl extends ServiceImpl<FsUserProjectTagScrmMapper, FsUserProjectTagScrm> implements IFsUserProjectTagScrmService {

    @Resource
    private CompanyUserMapper companyUserMapper;

    /**
     * 添加用户项目标签
     * @param id        用户项目ID
     * @param tagIds    标签ID集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addUserProjectTag(Long id, List<Long> tagIds) {
        // 删除原有标签
        LambdaUpdateWrapper<FsUserProjectTagScrm> deleteWrapper = Wrappers.<FsUserProjectTagScrm>lambdaUpdate()
                .eq(FsUserProjectTagScrm::getUserCompanyUserId, id);
        baseMapper.delete(deleteWrapper);

        // 添加新标签
        List<FsUserProjectTagScrm> tagList = tagIds.stream().distinct().map(tagId -> {
            FsUserProjectTagScrm userProjectTag = new FsUserProjectTagScrm();
            userProjectTag.setUserCompanyUserId(id);
            userProjectTag.setTagId(tagId);
            userProjectTag.setCreateTime(LocalDateTime.now());
            return userProjectTag;
        }).collect(Collectors.toList());

        if (!tagList.isEmpty()) {
            saveBatch(tagList);
        }
    }

    /**
     * 添加用户项目标签
     * @param ids       用户项目ID集合
     * @param tagIds    标签ID集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addUserProjectTag(List<Long> ids, List<Long> tagIds) {
        // 删除原有标签
        LambdaUpdateWrapper<FsUserProjectTagScrm> deleteWrapper = Wrappers.<FsUserProjectTagScrm>lambdaUpdate()
                .in(FsUserProjectTagScrm::getUserCompanyUserId, ids);
        baseMapper.delete(deleteWrapper);

        if (tagIds.isEmpty()) {
            return;
        }

        // 添加新标签
        List<FsUserProjectTagScrm> tagList = ids.stream().map(id ->
                tagIds.stream().distinct().map(tagId -> {
            FsUserProjectTagScrm userProjectTag = new FsUserProjectTagScrm();
            userProjectTag.setUserCompanyUserId(id);
            userProjectTag.setTagId(tagId);
            userProjectTag.setCreateTime(LocalDateTime.now());
            return userProjectTag;
        }).collect(Collectors.toList())).flatMap(List::stream).collect(Collectors.toList());

        if (!tagList.isEmpty()) {
            saveBatch(tagList);
        }
    }

    /**
     * 查询用户项目标签列表
     * @param param     参数
     * @return  list
     */
    @Override
    public List<CompanyUserTagListVO> getTagList(TagListParam param) {
        Map<String, Object> params = new HashMap<>();
        params.put("keywords", Optional.ofNullable(param.getKeyword()).map(keyword -> keyword.split(",")).orElse(null));

        CompanyUser companyUser = companyUserMapper.selectCompanyUserById(param.getUserId());
        if (Objects.nonNull(companyUser) && companyUser.isAdmin()) {
            params.put("companyId", companyUser.getCompanyId());
        } else {
            params.put("companyUserId", param.getUserId());
        }

        return baseMapper.getTagList(params);
    }
}
