package com.fs.hisStore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.CustomException;
import com.fs.hisStore.domain.FsUserCompanyUserScrm;
import com.fs.hisStore.mapper.FsUserCompanyUserScrmMapper;
import com.fs.hisStore.service.IFsUserCompanyUserScrmService;
import com.fs.qw.dto.UserProjectDTO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 微信用户和销售关系Service业务层处理
 *
 * @author fs
 * @date 2025-05-09
 */
@Service
public class FsUserCompanyUserScrmServiceImpl extends ServiceImpl<FsUserCompanyUserScrmMapper, FsUserCompanyUserScrm> implements IFsUserCompanyUserScrmService {

    /**
     * 查询微信用户和销售关系
     *
     * @param id 微信用户和销售关系主键
     * @return 微信用户和销售关系
     */
    @Override
    public FsUserCompanyUserScrm selectFsUserCompanyUserById(Long id)
    {
        return baseMapper.selectFsUserCompanyUserById(id);
    }

    /**
     * 查询微信用户和销售关系列表
     *
     * @param fsUserCompanyUser 微信用户和销售关系
     * @return 微信用户和销售关系
     */
    @Override
    public List<FsUserCompanyUserScrm> selectFsUserCompanyUserList(FsUserCompanyUserScrm fsUserCompanyUser)
    {
        return baseMapper.selectFsUserCompanyUserList(fsUserCompanyUser);
    }

    /**
     * 根据销售id查询出项目id
     *
     * @param companyUserId 销售id
     * @return 微信用户和销售关系
     */
    @Override
    public List<Long> selectFsUserCompanyUserList(Map<String,Object> param) {

        return baseMapper.selectFsUserCompanyUserListByMap(param);
    }
    /**
     * 新增微信用户和销售关系
     *
     * @param fsUserCompanyUser 微信用户和销售关系
     * @return 结果
     */
    @Override
    public int insertFsUserCompanyUser(FsUserCompanyUserScrm fsUserCompanyUser)
    {
        return baseMapper.insertFsUserCompanyUser(fsUserCompanyUser);
    }

    /**
     * 修改微信用户和销售关系
     *
     * @param fsUserCompanyUser 微信用户和销售关系
     * @return 结果
     */
    @Override
    public int updateFsUserCompanyUser(FsUserCompanyUserScrm fsUserCompanyUser)
    {
        return baseMapper.updateFsUserCompanyUser(fsUserCompanyUser);
    }

    /**
     * 批量删除微信用户和销售关系
     *
     * @param ids 需要删除的微信用户和销售关系主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCompanyUserByIds(Long[] ids)
    {
        return baseMapper.deleteFsUserCompanyUserByIds(ids);
    }

    /**
     * 删除微信用户和销售关系信息
     *
     * @param id 微信用户和销售关系主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCompanyUserById(Long id)
    {
        return baseMapper.deleteFsUserCompanyUserById(id);
    }

    /**
     * 根据用户ID和项目ID查询微信用户与销售的关系
     * @param userId            用户ID
     * @param projectId   项目ID
     * @return FsUserCompanyUser
     */
    @Override
    public FsUserCompanyUserScrm selectByUserIdAndProjectId(Long userId, Long projectId) {
        LambdaQueryWrapper<FsUserCompanyUserScrm> queryWrapper = Wrappers.<FsUserCompanyUserScrm>lambdaQuery()
                .eq(FsUserCompanyUserScrm::getUserId, userId)
                .eq(FsUserCompanyUserScrm::getProjectId, projectId);
        return getOne(queryWrapper);
    }

    /**
     * 查询是否已绑定关系
     * @param userId            用户ID
     * @param companyUserId     销售ID
     * @return  boolean
     */
    @Override
    public boolean hasBind(Long userId, Long companyUserId) {
        LambdaQueryWrapper<FsUserCompanyUserScrm> queryWrapper = Wrappers.<FsUserCompanyUserScrm>lambdaQuery()
                .eq(FsUserCompanyUserScrm::getUserId, userId)
                .eq(FsUserCompanyUserScrm::getCompanyUserId, companyUserId);
        return count(queryWrapper) > 0;
    }

    /**
     * 获取当前销售的所有重粉会员
     * @param userId 销售ID
     * @return list
     */
    @Override
    public List<FsUserCompanyUserScrm> selectRepeatUser(Long userId) {
        return baseMapper.selectRepeatUser(userId);
    }

    /**
     * 获取会员的重粉的所属销售
     * @param userIds   会员ID集合
     * @return  list
     */
    @Override
    public List<FsUserCompanyUserScrm> selectRepeatCompanyUserName(List<Long> userIds) {
        return baseMapper.selectRepeatCompanyUserName(userIds);
    }

    /**
     * 绑定会员-项目-销售关系
     *
     * @param userId        会员ID
     * @param projectId     项目ID
     * @param companyId     公司ID
     * @param companyUserId 销售ID
     * @param status        状态 0小黑屋 1正常
     */
    @Override
    public FsUserCompanyUserScrm bindRelationship(Long userId, Long projectId, Long companyId, Long companyUserId, int status) {
        FsUserCompanyUserScrm userCompanyUser = new FsUserCompanyUserScrm();
        userCompanyUser.setUserId(userId);
        userCompanyUser.setProjectId(projectId);
        userCompanyUser.setCompanyId(companyId);
        userCompanyUser.setCompanyUserId(companyUserId);
//        boolean hasBind = hasBind(userId, companyUserId);
//        userCompanyUser.setIsRepeatFans(hasBind ? 1 : 0);
        userCompanyUser.setIsRepeatFans(0);
        userCompanyUser.setStatus(status);
        userCompanyUser.setCreateTime(LocalDateTime.now());
        save(userCompanyUser);
        return userCompanyUser;
    }

    /**
     * 修改会员-项目-销售关系
     * @param userId        会员ID
     * @param projectId     项目ID
     * @param companyId     公司ID
     * @param companyUserId 销售ID
     */
    @Override
    public void changeRelationship(Long userId, Long projectId, Long companyId, Long companyUserId) {
        LambdaQueryWrapper<FsUserCompanyUserScrm> queryWrapper = Wrappers.<FsUserCompanyUserScrm>lambdaQuery()
                .eq(FsUserCompanyUserScrm::getUserId, userId)
                .eq(FsUserCompanyUserScrm::getProjectId, projectId)
                .last("limit 1");
        FsUserCompanyUserScrm userCompanyUser = getOne(queryWrapper);
        if (Objects.nonNull(userCompanyUser)) {
            userCompanyUser.setCompanyId(companyId);
            userCompanyUser.setCompanyUserId(companyUserId);
            updateById(userCompanyUser);
        }
    }

    /**
     * 修改会员-项目关系 状态
     * @param ids       参数
     * @param status    状态 0小黑屋 1正常 2拉黑
     * @return Boolean
     */
    @Override
    public Boolean batchUpdateUserProjectStatus(List<UserProjectDTO> ids, int status) {
        if (Objects.isNull(ids) || ids.isEmpty()) {
            throw new CustomException("请先选择会员");
        }
        return baseMapper.changeUserProjectStatus(ids, status) > 0;
    }



    /**
     * 修改会员-项目关系 状态
     * @param ids       参数
     * @param status    状态 0小黑屋 1正常 2拉黑
     * @return Boolean
     */
    @Override
    public Boolean batchUpdateStatus(List<Long> ids, int status) {
        if (Objects.isNull(ids) || ids.isEmpty()) {
            throw new CustomException("请先选择会员");
        }
        return baseMapper.batchUpdateStatus(ids, status) > 0;
    }
}
