package com.fs.qw.service.impl;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.domain.QwExternalContactTransferLog;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.mapper.QwExternalContactTransferLogMapper;
import com.fs.qw.param.QwExternalContactTransferLogParam;
import com.fs.qw.service.IQwExternalContactTransferLogService;
import com.fs.qw.vo.QwExternalContactTransferLogListVO;
import com.fs.qwApi.domain.QwGetTransferResult;
import com.fs.qwApi.param.QwGetTransferParam;
import com.fs.qwApi.service.QwApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 转接记录Service业务层处理
 *
 * @author fs
 * @date 2024-06-27
 */
@Service
public class QwExternalContactTransferLogServiceImpl implements IQwExternalContactTransferLogService
{
    @Autowired
    private QwExternalContactTransferLogMapper qwExternalContactTransferLogMapper;
    @Autowired
    private QwApiService qwApiService;

    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    /**
     * 查询转接记录
     *
     * @param id 转接记录主键
     * @return 转接记录
     */
    @Override
    public QwExternalContactTransferLog selectQwExternalContactTransferLogById(Long id)
    {
        return qwExternalContactTransferLogMapper.selectQwExternalContactTransferLogById(id);
    }

    /**
     * 查询转接记录列表
     *
     * @param qwExternalContactTransferLog 转接记录
     * @return 转接记录
     */
    @Override
    public List<QwExternalContactTransferLog> selectQwExternalContactTransferLogList(QwExternalContactTransferLog qwExternalContactTransferLog)
    {
        return qwExternalContactTransferLogMapper.selectQwExternalContactTransferLogList(qwExternalContactTransferLog);
    }

    /**
     * 新增转接记录
     *
     * @param qwExternalContactTransferLog 转接记录
     * @return 结果
     */
    @Override
    public int insertQwExternalContactTransferLog(QwExternalContactTransferLog qwExternalContactTransferLog)
    {
        qwExternalContactTransferLog.setCreateTime(DateUtils.getNowDate());
        return qwExternalContactTransferLogMapper.insertQwExternalContactTransferLog(qwExternalContactTransferLog);
    }

    /**
     * 修改转接记录
     *
     * @param qwExternalContactTransferLog 转接记录
     * @return 结果
     */
    @Override
    public int updateQwExternalContactTransferLog(QwExternalContactTransferLog qwExternalContactTransferLog)
    {
        return qwExternalContactTransferLogMapper.updateQwExternalContactTransferLog(qwExternalContactTransferLog);
    }

    /**
     * 批量删除转接记录
     *
     * @param ids 需要删除的转接记录主键
     * @return 结果
     */
    @Override
    public int deleteQwExternalContactTransferLogByIds(Long[] ids)
    {
        return qwExternalContactTransferLogMapper.deleteQwExternalContactTransferLogByIds(ids);
    }

    /**
     * 删除转接记录信息
     *
     * @param id 转接记录主键
     * @return 结果
     */
    @Override
    public int deleteQwExternalContactTransferLogById(Long id)
    {
        return qwExternalContactTransferLogMapper.deleteQwExternalContactTransferLogById(id);
    }

    @Override
    public R syncQwExternalContactTransferLog(String corpId) {
        QwExternalContactTransferLog qwExternalContactTransferLog = new QwExternalContactTransferLog();
//        qwExternalContactTransferLog.setCompanyId(companyId);
        qwExternalContactTransferLog.setStatus(2);
        List<QwExternalContactTransferLog> qwExternalContactTransferLogs = qwExternalContactTransferLogMapper.selectQwExternalContactTransferLogList(qwExternalContactTransferLog);
        for (QwExternalContactTransferLog externalContactTransferLog : qwExternalContactTransferLogs) {
            QwGetTransferParam getTransferParam = new QwGetTransferParam();
            getTransferParam.setTakeover_userid(externalContactTransferLog.getTakeoverUserId());
            getTransferParam.setHandover_userid(externalContactTransferLog.getHandoverUserId());
            QwGetTransferResult transfer = qwApiService.getTransfer(getTransferParam, corpId);
            List<QwGetTransferResult.Customer> customers = transfer.getCustomer();
            for (QwGetTransferResult.Customer customer: customers) {
                if (customer.getExternal_userid().equals(externalContactTransferLog.getExternalUserId())){
                    if (customer.getStatus()!=2){
                        externalContactTransferLog.setStatus(customer.getStatus());
                        qwExternalContactTransferLogMapper.updateQwExternalContactTransferLog(externalContactTransferLog);
                        QwExternalContact qwExternalContact = new QwExternalContact();
                        qwExternalContact.setId(externalContactTransferLog.getExternalContactId());
                        qwExternalContact.setTransferStatus(customer.getStatus());
                        if (customer.getStatus()==1){
                            qwExternalContact.setUserId(externalContactTransferLog.getTakeoverUserId());
                            qwExternalContact.setQwUserId(externalContactTransferLog.getQwUserId());
                            qwExternalContact.setCompanyUserId(externalContactTransferLog.getCompanyUserId());
                        }
                        qwExternalContactMapper.updateQwExternalContact(qwExternalContact);
                    }
                }
            }

        }
        return null;
    }

    @Override
    public List<QwExternalContactTransferLogListVO> selectQwExternalContactTransferLogListVO(QwExternalContactTransferLogParam qwExternalContactTransferLog) {
        return qwExternalContactTransferLogMapper.selectQwExternalContactTransferLogListVO(qwExternalContactTransferLog);
    }

    @Override
    public void updateQwExternalContactTransferLogByStatus() {

        qwExternalContactTransferLogMapper.updateQwExternalContactByStatus();
        qwExternalContactTransferLogMapper.updateQwExternalContactTransferLogByStatus();
    }
}
