package com.fs.course.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.course.domain.FsUserCompanyUser;
import com.fs.qw.param.FsUserCompanyUserQwParam;
import com.fs.qw.vo.FsUserCompanyUserQwVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserCompanyUserQwMapper;
import com.fs.course.domain.FsUserCompanyUserQw;
import com.fs.course.service.IFsUserCompanyUserQwService;

/**
 * 微信用户和销售关系-企业微信自动的Service业务层处理
 *
 * @author fs
 * @date 2025-10-23
 */
@Service
public class FsUserCompanyUserQwServiceImpl extends ServiceImpl<FsUserCompanyUserQwMapper, FsUserCompanyUserQw> implements IFsUserCompanyUserQwService {


    @Autowired
    private FsUserCompanyUserQwMapper fsUserCompanyUserQwMapper;

    /**
     * 查询微信用户和销售关系-企业微信自动的
     *
     * @param id 微信用户和销售关系-企业微信自动的主键
     * @return 微信用户和销售关系-企业微信自动的
     */
    @Override
    public FsUserCompanyUserQw selectFsUserCompanyUserQwById(Long id)
    {
        return baseMapper.selectFsUserCompanyUserQwById(id);
    }

    @Override
    public FsUserCompanyUserQw selectByUserIdAndProjectId(Long userId, Long projectId) {
        LambdaQueryWrapper<FsUserCompanyUserQw> queryWrapper = Wrappers.<FsUserCompanyUserQw>lambdaQuery()
                .eq(FsUserCompanyUserQw::getUserId, userId)
                .eq(FsUserCompanyUserQw::getProjectId, projectId);
        return getOne(queryWrapper);
    }

    @Override
    public FsUserCompanyUserQw bindRelationship(Long userId, Long projectId, Long companyId, Long companyUserId, int status) {
        FsUserCompanyUserQw companyUserQw = new FsUserCompanyUserQw();
        companyUserQw.setUserId(userId);
        companyUserQw.setProjectId(projectId);
        companyUserQw.setCompanyId(companyId);
        companyUserQw.setCompanyUserId(companyUserId);
        companyUserQw.setIsRepeatFans(0);
        companyUserQw.setStatus(status);
        companyUserQw.setCreateTime(LocalDateTime.now());
        save(companyUserQw);
        return companyUserQw;
    }

    /**
     * 查询微信用户和销售关系-企业微信自动的列表
     *
     * @param fsUserCompanyUserQw 微信用户和销售关系-企业微信自动的
     * @return 微信用户和销售关系-企业微信自动的
     */
    @Override
    public List<FsUserCompanyUserQw> selectFsUserCompanyUserQwList(FsUserCompanyUserQw fsUserCompanyUserQw)
    {
        return baseMapper.selectFsUserCompanyUserQwList(fsUserCompanyUserQw);

    }

    @Override
    public List<FsUserCompanyUserQwVO> selectFsUserCompanyUserQwListVO(FsUserCompanyUserQwParam userQwParam) {
        return fsUserCompanyUserQwMapper.selectFsUserCompanyUserQwListVO(userQwParam);
    }

    /**
     * 新增微信用户和销售关系-企业微信自动的
     *
     * @param fsUserCompanyUserQw 微信用户和销售关系-企业微信自动的
     * @return 结果
     */
    @Override
    public int insertFsUserCompanyUserQw(FsUserCompanyUserQw fsUserCompanyUserQw)
    {
        fsUserCompanyUserQw.setCreateTime(LocalDateTime.now());
        return baseMapper.insertFsUserCompanyUserQw(fsUserCompanyUserQw);
    }

    /**
     * 修改微信用户和销售关系-企业微信自动的
     *
     * @param fsUserCompanyUserQw 微信用户和销售关系-企业微信自动的
     * @return 结果
     */
    @Override
    public int updateFsUserCompanyUserQw(FsUserCompanyUserQw fsUserCompanyUserQw)
    {

        return baseMapper.updateFsUserCompanyUserQw(fsUserCompanyUserQw);
    }

    @Override
    public void changeRelationship(Long userId, Long projectId, Long companyId, Long companyUserId) {
        LambdaQueryWrapper<FsUserCompanyUserQw> queryWrapper = Wrappers.<FsUserCompanyUserQw>lambdaQuery()
                .eq(FsUserCompanyUserQw::getUserId, userId)
                .eq(FsUserCompanyUserQw::getProjectId, projectId)
                .last("limit 1");
        FsUserCompanyUserQw userCompanyUserQw = getOne(queryWrapper);
        if (Objects.nonNull(userCompanyUserQw)) {
            userCompanyUserQw.setCompanyId(companyId);
            userCompanyUserQw.setCompanyUserId(companyUserId);
            updateById(userCompanyUserQw);
        }
    }

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

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