package com.fs.company.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.mapper.CompanyUserMapper;
import com.fs.company.vo.CompanyTagUserVO;
import com.fs.store.domain.FsUser;
import com.fs.store.mapper.FsUserMapper;
import com.fs.store.param.h5.TagListParam;
import com.fs.store.vo.h5.CompanyUserTagListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.mapper.CompanyTagUserMapper;
import com.fs.company.domain.CompanyTagUser;
import com.fs.company.service.ICompanyTagUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * companyService业务层处理
 *
 * @author fs
 * @date 2025-04-02
 */
@Service
public class CompanyTagUserServiceImpl implements ICompanyTagUserService
{
    @Autowired
    private CompanyTagUserMapper companyTagUserMapper;

    @Autowired
    private CompanyUserMapper  companyUserMapper;
    @Autowired
    private FsUserMapper fsUserMapper;

    /**
     * 查询company
     *
     * @param id companyID
     * @return company
     */
    @Override
    public CompanyTagUser selectCompanyTagUserById(Long id)
    {
        return companyTagUserMapper.selectCompanyTagUserById(id);
    }

    /**
     * 查询company列表
     *
     * @param companyTagUser company
     * @return company
     */
    @Override
    public List<CompanyTagUser> selectCompanyTagUserList(CompanyTagUser companyTagUser)
    {
        return companyTagUserMapper.selectCompanyTagUserList(companyTagUser);
    }

    /**
     * 新增company
     *
     * @param companyTagUser company
     * @return 结果
     */
    @Override
    public int insertCompanyTagUser(CompanyTagUser companyTagUser)
    {
        companyTagUser.setCreateTime(DateUtils.getNowDate());
        return companyTagUserMapper.insertCompanyTagUser(companyTagUser);
    }

    /**
     * 修改company
     *
     * @param companyTagUser company
     * @return 结果
     */
    @Override
    public int updateCompanyTagUser(CompanyTagUser companyTagUser)
    {
        return companyTagUserMapper.updateCompanyTagUser(companyTagUser);
    }

    /**
     * 批量删除company
     *
     * @param ids 需要删除的companyID
     * @return 结果
     */
    @Override
    public int deleteCompanyTagUserByIds(Long[] ids)
    {
        return companyTagUserMapper.deleteCompanyTagUserByIds(ids);
    }

    /**
     * 删除company信息
     *
     * @param id companyID
     * @return 结果
     */
    @Override
    public int deleteCompanyTagUserById(Long id)
    {
        return companyTagUserMapper.deleteCompanyTagUserById(id);
    }

    @Override
    public List<CompanyUserTagListVO> getTagList(TagListParam param) {
        String[] keywords = new String[0];
        if (param != null) {
            if (StringUtils.isNotEmpty(param.getKeyword())) {
                keywords = param.getKeyword().split(",");
            }
        }
        //获取所有销售
        List<CompanyUser> companyUsers = companyUserMapper.selectAllCompanyUserAndSelf(param != null ? param.getUserId() : null);
        List<Long> userIds = Collections.emptyList();
        if(companyUsers != null && !companyUsers.isEmpty()){
            userIds = companyUsers.stream().map(CompanyUser::getUserId).collect(Collectors.toList());
        }

        return companyTagUserMapper.getTagList(param, keywords, userIds);
    }

    /**
     * 根据条件查询标签下用户
     * @param params    条件
     * @return  list
     */
    @Override
    public List<CompanyTagUserVO> selectUserListByMap(Map<String, Object> params) {
        return companyTagUserMapper.selectUserListByMap(params);
    }

    /**
     * 修改用户标签
     * @param fsUserIds 用户ID
     * @param tagIds   标签ID集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeUserTags(List<Long> fsUserIds, List<Long> tagIds) {
        fsUserIds.forEach(fsUserId -> {
            FsUser fsUser = fsUserMapper.selectFsUserById(fsUserId);
            if (Objects.isNull(fsUser)) {
                return;
            }

            // 删除原标签
            Map<String, Object> params = new HashMap<>();
            params.put("userId", fsUserId);
            params.put("companyId", fsUser.getCompanyId());
            params.put("companyUserId", fsUser.getCompanyUserId());
            companyTagUserMapper.deleteCompanyTagUserByMap(params);

            // 不为空则添加新标签
            if (Objects.nonNull(tagIds) && !tagIds.isEmpty()) {
                CompanyTagUser companyTagUser = new CompanyTagUser();
                companyTagUser.setUserId(fsUserId);
                companyTagUser.setCompanyId(fsUser.getCompanyId());
                companyTagUser.setCompanyUserId(fsUser.getCompanyUserId());
                companyTagUser.setTagIds(tagIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
                companyTagUser.setCreateTime(new Date());
                companyTagUserMapper.insertCompanyTagUser(companyTagUser);
            }
        });
    }
}
