package com.fs.qw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.domain.QwUser;
import com.fs.qw.domain.QwUserVoiceLog;
import com.fs.qw.domain.QwWorkTask;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.qw.mapper.QwUserVoiceLogMapper;
import com.fs.qw.mapper.QwWorkTaskMapper;
import com.fs.qw.service.IQwUserVoiceLogService;
import com.fs.qw.vo.QwUserVoiceLogTotalVo;
import com.fs.qw.vo.QwUserVoiceLogVo;
import com.fs.wxwork.dto.WxWorkResponseDTO;
import com.fs.wxwork.dto.WxWorkVid2UserIdDTO;
import com.fs.wxwork.dto.WxWorkVid2UserIdRespDTO;
import com.fs.wxwork.service.WxWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 企微用户通话记录Service业务层处理
 *
 * @author fs
 * @date 2025-05-08
 */
@Service
public class QwUserVoiceLogServiceImpl extends ServiceImpl<QwUserVoiceLogMapper, QwUserVoiceLog> implements IQwUserVoiceLogService {
    @Autowired
    private QwUserMapper qwUserMapper;

    @Autowired
    private QwWorkTaskMapper qwWorkTaskMapper;

    @Autowired
    WxWorkService wxWorkService;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    /**
     * 查询企微用户通话记录
     *
     * @param id 企微用户通话记录主键
     * @return 企微用户通话记录
     */
    @Override
    public QwUserVoiceLogVo selectQwUserVoiceLogById(Long id)
    {
        return baseMapper.selectQwUserVoiceLogById(id);
    }

    /**
     * 查询企微用户通话记录列表
     *
     * @param qwUserVoiceLog 企微用户通话记录
     * @return 企微用户通话记录
     */
    @Override
    public List<QwUserVoiceLogVo> selectQwUserVoiceLogList(QwUserVoiceLogVo qwUserVoiceLog)
    {
        return baseMapper.selectQwUserVoiceLogList(qwUserVoiceLog);
    }

    /**
     * 新增企微用户通话记录
     *
     * @param qwUserVoiceLog 企微用户通话记录
     * @return 结果
     */
    @Override
    public int insertQwUserVoiceLog(QwUserVoiceLog qwUserVoiceLog)
    {
        qwUserVoiceLog.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertQwUserVoiceLog(qwUserVoiceLog);
    }

    /**
     * 修改企微用户通话记录
     *
     * @param qwUserVoiceLog 企微用户通话记录
     * @return 结果
     */
    @Override
    public int updateQwUserVoiceLog(QwUserVoiceLog qwUserVoiceLog)
    {
        return baseMapper.updateQwUserVoiceLog(qwUserVoiceLog);
    }

    /**
     * 批量删除企微用户通话记录
     *
     * @param ids 需要删除的企微用户通话记录主键
     * @return 结果
     */
    @Override
    public int deleteQwUserVoiceLogByIds(Long[] ids)
    {
        return baseMapper.deleteQwUserVoiceLogByIds(ids);
    }

    /**
     * 删除企微用户通话记录信息
     *
     * @param id 企微用户通话记录主键
     * @return 结果
     */
    @Override
    public int deleteQwUserVoiceLogById(Long id)
    {
        return baseMapper.deleteQwUserVoiceLogById(id);
    }

    @Override
    public List<QwUserVoiceLogTotalVo> selectQwUserVoiceLogTotalList(QwUserVoiceLogTotalVo qwUserVoiceLog) {
        return baseMapper.selectQwUserVoiceLogTotalList(qwUserVoiceLog);
    }

    @Override
    public void addQuUserVoiceByIpadCallback(Long id, Long extUserId, Integer recordType, Integer totalSeconds, String uuid) {
        QwUser sendUser = qwUserMapper.selectQwUserById(id);
        if (sendUser!=null){
            String extId = getExtId(extUserId, uuid, sendUser.getServerId());
            QwExternalContact qwExternalContacts = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(extId, sendUser.getCorpId(),sendUser.getQwUserId());
            if (qwExternalContacts==null){
                return ;
            }

            QwUserVoiceLog qwUserVoiceLog = new QwUserVoiceLog();
            qwUserVoiceLog.setCorpId(sendUser.getCorpId());
            qwUserVoiceLog.setExtId(qwExternalContacts.getId());
            qwUserVoiceLog.setQwUserId(sendUser.getId());
            qwUserVoiceLog.setCompanyId(qwExternalContacts.getCompanyId());
            if(qwExternalContacts.getCompanyUserId() != null){
                qwUserVoiceLog.setCompanyUserId(qwExternalContacts.getCompanyUserId());
            }

            if(recordType == 2){//2是未接通的处理
                qwUserVoiceLog.setStatus(Long.valueOf(recordType));
                qwUserVoiceLog.setTitle("通话拒接");
            }else{
                qwUserVoiceLog.setStatus(1L);
                if(totalSeconds != null){
                    qwUserVoiceLog.setDuration(Long.valueOf(totalSeconds));
                }else{
                    qwUserVoiceLog.setDuration(0L);
                }
                qwUserVoiceLog.setTitle("通话已接听");
            }
            qwUserVoiceLog.setCreateTime(new Date());

            int insert = baseMapper.insertQwUserVoiceLog(qwUserVoiceLog);
            //插入成功的情况下就处理定时任务
            if(insert == 1){
                List<QwWorkTask> qwWorkTaskList = qwWorkTaskMapper.selectQwWorkTaskByExtIdAndQwUserId(qwUserVoiceLog);
                if(qwWorkTaskList != null && !qwWorkTaskList.isEmpty()){
                    for (QwWorkTask qwWorkTask : qwWorkTaskList) {
                        if(qwUserVoiceLog.getStatus() == 1L){//接通电话
                            qwWorkTask.setTrackType(3);//1正常处理 2 未接听 3 已接听
                            qwWorkTask.setDuration(qwUserVoiceLog.getDuration());
                        }else{
                            qwWorkTask.setTrackType(2);//1正常处理 2 未接听 3 已接听
                        }
                        qwWorkTask.setStatus(1);//0 待处理 1 已处理 3 过期
                        qwWorkTask.setUpdateTime(new Date());
                        qwWorkTaskMapper.updateQwWorkTask(qwWorkTask);
                    }
                }
            }
        }
    }

    String getExtId(Long id,String uid,Long serverId){
        WxWorkVid2UserIdDTO wxWorkVid2UserIdDTO = new WxWorkVid2UserIdDTO();
        wxWorkVid2UserIdDTO.setUser_id(Arrays.asList(id));
        wxWorkVid2UserIdDTO.setUuid(uid);
        WxWorkResponseDTO<List<WxWorkVid2UserIdRespDTO>> WxWorkVid2UserIdRespDTO = wxWorkService.Vid2UserId(wxWorkVid2UserIdDTO,serverId);
        List<WxWorkVid2UserIdRespDTO> data = WxWorkVid2UserIdRespDTO.getData();
        if (data==null&& data.isEmpty()){
            System.out.println("未获取到extId");
            return "";
        }
        WxWorkVid2UserIdRespDTO dto = data.get(0);
        return dto.getOpenid();
    }
}
