package com.fs.xiaoshouyi.service;

import com.fs.xiaoshouyi.mapper.XsyUserBindMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 企微销售与销售易账号绑定 Service
 */
@Service
@RequiredArgsConstructor
public class XsyUserBindService {

    private final XsyUserBindMapper xsyUserBindMapper;

    /**
     * 绑定销售易账号
     *
     * @param companyUserId 企微销售ID
     * @param accountId     销售易账号ID
     */
    public void bind(Long companyUserId, Long accountId) {
        if (companyUserId == null) {
            throw new RuntimeException("companyUserId不能为空");
        }
        if (accountId == null) {
            throw new RuntimeException("accountId不能为空");
        }

        // 一个企微销售只绑定一个销售易账号
        xsyUserBindMapper.deleteByCompanyUserId(companyUserId);
        xsyUserBindMapper.insertBind(companyUserId, accountId);
    }

    /**
     * 解绑销售易账号
     *
     * @param companyUserId 企微销售ID
     */
    public void unbind(Long companyUserId) {
        if (companyUserId == null) {
            throw new RuntimeException("companyUserId不能为空");
        }

        xsyUserBindMapper.deleteByCompanyUserId(companyUserId);
    }

    /**
     * 查询绑定的销售易账号ID
     *
     * @param companyUserId 企微销售ID
     * @return 销售易账号ID
     */
    public Long getBindAccountId(Long companyUserId) {
        if (companyUserId == null) {
            return null;
        }
        return xsyUserBindMapper.selectAccountIdByCompanyUserId(companyUserId);
    }
}