package com.fs.qw.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.his.domain.FsFirstDiagnosis;
import com.fs.his.domain.FsUser;
import com.fs.his.mapper.FsFirstDiagnosisMapper;
import com.fs.his.service.IFsUserService;
import com.fs.qw.domain.QwExternalContactInfo;
import com.fs.qw.mapper.QwExternalContactInfoMapper;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.service.IQwExternalContactInfoService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 外部联系人信息表Service业务层处理
 *
 * @author fs
 * @date 2024-11-22
 */
@Slf4j
@Service
public class QwExternalContactInfoServiceImpl implements IQwExternalContactInfoService
{
    @Autowired
    private QwExternalContactInfoMapper qwExternalContactInfoMapper;
    @Autowired
    QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    private ICompanyUserService companyUserService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private FsFirstDiagnosisMapper diagnosisMapper;
    /**
     * 查询外部联系人信息表
     *
     * @param id 外部联系人信息表主键
     * @return 外部联系人信息表
     */
    @Override
    public QwExternalContactInfo selectQwExternalContactInfoById(Long id)
    {
        return qwExternalContactInfoMapper.selectQwExternalContactInfoById(id);
    }

    /**
     * 查询外部联系人信息表列表
     *
     * @param qwExternalContactInfo 外部联系人信息表
     * @return 外部联系人信息表
     */
    @Override
    public List<QwExternalContactInfo> selectQwExternalContactInfoList(QwExternalContactInfo qwExternalContactInfo)
    {
        return qwExternalContactInfoMapper.selectQwExternalContactInfoList(qwExternalContactInfo);
    }

    /**
     * 新增外部联系人信息表
     *
     * @param qwExternalContactInfo 外部联系人信息表
     * @return 结果
     */
    @Override
    public int insertQwExternalContactInfo(QwExternalContactInfo qwExternalContactInfo)
    {
        qwExternalContactInfo.setCreateTime(DateUtils.getNowDate());
        return qwExternalContactInfoMapper.insertQwExternalContactInfo(qwExternalContactInfo);
    }

    /**
     * 修改外部联系人信息表
     *
     * @param qwExternalContactInfo 外部联系人信息表
     * @return 结果
     */
    @Override
    public int updateQwExternalContactInfo(QwExternalContactInfo qwExternalContactInfo)
    {
        qwExternalContactInfo.setUpdateTime(DateUtils.getNowDate());
        return qwExternalContactInfoMapper.updateQwExternalContactInfo(qwExternalContactInfo);
    }

    /**
     * 批量删除外部联系人信息表
     *
     * @param ids 需要删除的外部联系人信息表主键
     * @return 结果
     */
    @Override
    public int deleteQwExternalContactInfoByIds(Long[] ids)
    {
        return qwExternalContactInfoMapper.deleteQwExternalContactInfoByIds(ids);
    }

    /**
     * 删除外部联系人信息表信息
     *
     * @param id 外部联系人信息表主键
     * @return 结果
     */
    @Override
    public int deleteQwExternalContactInfoById(Long id)
    {
        return qwExternalContactInfoMapper.deleteQwExternalContactInfoById(id);
    }

    @Override
    public QwExternalContactInfo selectQwExternalContactInfoByExternalContactId(Long id) {
        return qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(id);
    }

    @Override
    public List<QwExternalContactInfo> selectQwExternalContactInfoListByExternalContactIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return Collections.emptyList();
        }
        List<QwExternalContactInfo> qwExternalContactInfos =qwExternalContactInfoMapper.selectQwExternalContactInfoListByExternalContactIds(ids);
        if (CollectionUtils.isEmpty(qwExternalContactInfos)){
            return Collections.emptyList();
        }
        return qwExternalContactInfos;
    }

    @Override
    public int updateQwExternalContactInfoByIds(Long[] ids) {
        for (Long id : ids) {
            qwExternalContactInfoMapper.updateQwExternalContactInfoByExtId(id);
        }
        return 1;
    }
    @Async
    @Override
    public void updateQwExternalContactInfoBytalk(String talkType, Long externalId) {
        QwExternalContactInfo info = qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(externalId);
        QwExternalContactInfo in = new QwExternalContactInfo();
        in.setTalk(talkType);
        in.setExternalContactId(externalId);
        if (info==null){
            in.setCreateTime(new Date());
            qwExternalContactInfoMapper.insertQwExternalContactInfo(in);
          }else {
            in.setId(info.getId());
            qwExternalContactInfoMapper.updateQwExternalContactInfo(in);
        }
    }

    @Override
    public int updateQwExternalContactInfoByQwUserId(Long qwUserId) {
        List<Long> qwExternalContactsIds = qwExternalContactMapper.selectExternalUserByQwUserId(qwUserId);
        for (Long id : qwExternalContactsIds) {
            qwExternalContactInfoMapper.updateQwExternalContactInfoByExtId(id);
        }

        return 1;
    }

    @Override
    public void updateQwExternalContactInfoByExternalContactId(QwExternalContactInfo qwExternalContactInfo) {
        qwExternalContactInfoMapper.updateQwExternalContactInfoByExternalContactId(qwExternalContactInfo);
    }

    @Override
    public Boolean generateDiagnosis(Long exId, Long companyUserId, Long userId) {
        log.info("生成初诊单参数=============>exId:{},companyUserId:{},userId:{}", exId, companyUserId, userId);
        if (exId == null || companyUserId == null || userId == null) {
            throw new CustomException("参数异常");
        }
        QwExternalContactInfo qwExternalContactInfo = qwExternalContactInfoMapper.selectQwExternalContactInfoByExternalContactId(exId);
        if (qwExternalContactInfo == null) {
            throw new CustomException("外部联系人信息不存在");
        }
        log.info("外部联系人信息:{}", qwExternalContactInfo);
        CompanyUser companyUser = companyUserService.selectCompanyUserById(companyUserId);
        if (companyUser == null) {
            throw new CustomException("公司用户信息不存在");
        }
        log.info("销售信息:{}", companyUser);
        FsUser fsUser = userService.selectFsUserByUserId(userId);
        if (fsUser == null) {
            throw new CustomException("用户信息不存在");
        }
        log.info("用户信息:{}", fsUser);
        FsFirstDiagnosis diagnosis = getFsFirstDiagnosis(companyUserId, userId, qwExternalContactInfo);
        int i = diagnosisMapper.insertFsFirstDiagnosis(diagnosis);
        return i > 0;
    }

    private static @NotNull FsFirstDiagnosis getFsFirstDiagnosis(Long companyUserId, Long userId, QwExternalContactInfo qwExternalContactInfo) {
        FsFirstDiagnosis diagnosis = new FsFirstDiagnosis();
        diagnosis.setUserId(userId);
        diagnosis.setQwUserId(companyUserId);
        diagnosis.setAge(qwExternalContactInfo.getAge());
        long sex = 0L;
        if ("男".equals(qwExternalContactInfo.getSex())) {
            sex = 1L;
        } else if ("女".equals(qwExternalContactInfo.getSex())) {
            sex = 2L;
        }
        diagnosis.setGender(sex);
        diagnosis.setPhone(qwExternalContactInfo.getPhone());
        diagnosis.setPatientName(qwExternalContactInfo.getName());
        diagnosis.setPhysicalCondition(qwExternalContactInfo.getBody());
        return diagnosis;
    }
}
