package com.fs.aiChat.service.impl;

import com.fs.aiChat.param.QwSopParam;
import com.fs.aiChat.service.IQwSopImService;
import com.fs.aiChat.vo.QwSopImCustomerVO;
import com.fs.aiChat.vo.QwSopLogsListVO;
import com.fs.aiChat.vo.QwSopLogsVO;
import com.fs.aiChat.vo.QwSopListVO;
import com.fs.common.BeanCopyUtils;
import com.fs.sop.domain.QwSopLogs;
import com.fs.qw.domain.QwUser;
import com.fs.sop.mapper.QwSopMapper;
import com.fs.sop.mapper.QwSopLogsMapper;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.sop.params.QwSopLogsAiParam;
import com.fs.sop.params.QwSopsListParamByCompany;
import com.fs.sop.params.QwSopsListParamByExternal;
import com.fs.sop.vo.QwSopLogsListVOByUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * sopImService业务层处理
 *
 * @author fs
 * @date 2024-07-31
 */
@Service
public class QwSopImServiceImpl implements IQwSopImService
{
    @Autowired
    private QwSopMapper qwSopMapper;

    @Autowired
    private QwSopLogsMapper qwSopLogsMapper;

    @Autowired
    private QwUserMapper qwUserMapper;


    @Override
    public List<QwSopListVO> selectSopListVO(QwSopParam qwSopParam) {

        QwUser qwUser = qwUserMapper.selectQwUserByCompanyUserId(qwSopParam.getUserId(), qwSopParam.getCorpId());

        qwSopParam.setQwUserId(qwUser.getId());

        List<QwSopListVO> sopList = qwSopMapper.selectSopListVO(qwSopParam);


        return sopList;
    }


    @Override
    public List<QwSopImCustomerVO> getSopCustomerList(QwSopParam qwSopParam) {
        return qwSopMapper.getSopCustomerList(qwSopParam);
    }

    @Override
    public List<QwSopLogsListVO> selectSopSendCustomerLogsListVO(QwSopParam qwSopParam) {

        QwSopLogs qwSopLogs=new QwSopLogs();
        qwSopLogs.setCompanyId(qwSopParam.getCompanyId());
        qwSopLogs.setQwUserid(String.valueOf(qwSopParam.getQwUserId()));
        qwSopLogs.setSendType(2);
        qwSopLogs.setSendStatus(qwSopLogs.getSendStatus());
        qwSopLogs.setReceivingStatus(qwSopLogs.getReceivingStatus());
        qwSopLogs.setSopId(qwSopLogs.getSopId());
        qwSopLogs.setCorpId(qwSopLogs.getCorpId());

        List<QwSopLogs> qwSopLogsList = qwSopLogsMapper.selectQwSopLogsList(qwSopLogs);

        List<QwSopLogsListVO> qwSopLogsListVOList=new ArrayList<>();

        qwSopLogsList.forEach(logs -> {

            QwSopLogsListVO qwSopLogsListVO=new QwSopLogsListVO();

            BeanCopyUtils.copy(logs, qwSopLogsListVO);

            QwSopsListParamByExternal param1=new QwSopsListParamByExternal();
            param1.setQwUserid(logs.getQwUserid());
            param1.setExternalUserId(logs.getExternalUserId());
            QwSopLogsListVOByUserInfo info = qwSopLogsMapper.selectSopSendCustomerLogsListVO(param1);

            qwSopLogsListVO.setQwUserName(info.getQwUserName());
            qwSopLogsListVO.setUserName(info.getUserName());
            qwSopLogsListVO.setQwId(info.getQwId());
            qwSopLogsListVO.setCompanyUserId(info.getCompanyUserId());
            qwSopLogsListVO.setExternalUserName(info.getExternalUserName());
            qwSopLogsListVO.setMobile(info.getMobile());

            qwSopLogsListVOList.add(qwSopLogsListVO);
        });


        return qwSopLogsListVOList;
    }

    /**
    * 更新AI发送状态
    */
    @Override
    public Integer updateQwSopLogsSendType(QwSopLogs qwSopLogs) {

        return  qwSopLogsMapper.updateQwSopLogs(qwSopLogs);
    }

    @Override
    public QwSopLogsVO getQwSopLogsDetailVO(QwSopLogsAiParam param) {

        QwSopLogsVO qwSopLogsVO=new QwSopLogsVO();

        QwSopLogs qwSopLogs = qwSopLogsMapper.selectQwSopLogsById(param.getId());

        QwSopsListParamByCompany paramByCompany=new QwSopsListParamByCompany();
        paramByCompany.setCompanyUserId(param.getUserId());
        paramByCompany.setExternalUserId(qwSopLogs.getExternalUserId());
        QwSopLogsListVOByUserInfo detailVO = qwSopLogsMapper.getQwSopLogsDetailVO(paramByCompany);

        BeanCopyUtils.copy(qwSopLogs,qwSopLogsVO);
        qwSopLogsVO.setQwUserName(detailVO.getQwUserName());
        qwSopLogsVO.setUserName(detailVO.getUserName());
        qwSopLogsVO.setQwId(detailVO.getQwId());
        qwSopLogsVO.setCompanyUserId(detailVO.getCompanyUserId());
        qwSopLogsVO.setExternalUserName(detailVO.getExternalUserName());
        qwSopLogsVO.setMobile(detailVO.getMobile());


        return qwSopLogsVO;
    }

}
