package com.fs.course.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.uuid.UUID;
import com.fs.course.domain.FsMessTemp;
import com.fs.course.mapper.FsMessTempMapper;
import com.fs.course.service.IFsMessTempService;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.vo.QwSopCourseFinishTempSetting;
import com.fs.sop.domain.QwSopLogs;
import com.fs.sop.domain.SopUserLogsInfo;
import com.fs.sop.mapper.SopUserLogsInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 群发模板Service业务层处理
 *
 * @author fs
 * @date 2025-01-09
 */
@Service
public class FsMessTempServiceImpl implements IFsMessTempService
{
    @Autowired
    private FsMessTempMapper fsMessTempMapper;

    @Autowired
    private SopUserLogsInfoMapper sopUserLogsInfoMapper;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询群发模板
     *
     * @param id 群发模板主键
     * @return 群发模板
     */
    @Override
    public FsMessTemp selectFsMessTempById(Long id)
    {
        return fsMessTempMapper.selectFsMessTempById(id);
    }

    /**
     * 查询群发模板列表
     *
     * @param fsMessTemp 群发模板
     * @return 群发模板
     */
    @Override
    public List<FsMessTemp> selectFsMessTempList(FsMessTemp fsMessTemp)
    {
        return fsMessTempMapper.selectFsMessTempList(fsMessTemp);
    }

    /**
     * 新增群发模板
     *
     * @param fsMessTemp 群发模板
     */
    @Override
    public void insertFsMessTemp(FsMessTemp fsMessTemp)
    {
        fsMessTemp.setCreateTime(DateUtils.getNowDate());
        fsMessTempMapper.insertFsMessTemp(fsMessTemp);
//        SopUserLogsInfo sopUserLogsInfo = new SopUserLogsInfo();
//        sopUserLogsInfo.setUserLogsId(fsMessTemp.getSopUserLogId());
//        List<SopUserLogsInfo> sopUserLogsInfos = sopUserLogsInfoMapper.selectSopUserLogsInfoList(sopUserLogsInfo);
//        QwSopCourseFinishTempSetting setting = new QwSopCourseFinishTempSetting();
//        for (SopUserLogsInfo info : sopUserLogsInfos) {
//            String externalContactId = info.getExternalContactId();
//            String corpId = info.getCorpId();
//            QwExternalContact qwExternalContact = new QwExternalContact();
//            qwExternalContact.setExternalUserId(externalContactId);
//            qwExternalContact.setCorpId(corpId);
//            List<QwExternalContact> qwExternalContacts = qwExternalContactMapper.selectQwExternalContactList(qwExternalContact);
//            for (QwExternalContact externalContact : qwExternalContacts) {
//                // 设置时间格式
//                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                // 获取当前时间
//                LocalDateTime currentTime = LocalDateTime.now();
//                // 添加两分钟
//                LocalDateTime newTime = currentTime.plusMinutes(2);
//                // 格式化为字符串
//                String newTimeString = newTime.format(formatter);
//                QwSopLogs sopLogs = new QwSopLogs();
//                sopLogs.setSendTime(newTimeString);
//                sopLogs.setQwUserid(externalContact.getUserId());
//                sopLogs.setCorpId(externalContact.getCorpId());
//                sopLogs.setLogType(2);
//                sopLogs.setSendType(1);
//                sopLogs.setSendStatus(3L);
//                sopLogs.setReceivingStatus(0L);
//                sopLogs.setSort(3);
//                sopLogs.setCompanyId(fsMessTemp.getCompanyId());
//                sopLogs.setSopId(info.getSopId());
//                sopLogs.setExternalUserId(externalContact.getExternalUserId());
//                sopLogs.setExternalUserName(externalContact.getName());
//                sopLogs.setFsUserId(info.getFsUserId());
//                List<QwSopCourseFinishTempSetting.Setting> list = JSONArray.parseArray(fsMessTemp.getSetting(),QwSopCourseFinishTempSetting.Setting.class);
//                setting.setSetting(list);
//                sopLogs.setContentJson(JSON.toJSONString(setting));
//                redisCache.setCacheObject("mess:course:"+ UUID.randomUUID(),sopLogs);
//            }
//        }

    }

    /**
     * 修改群发模板
     *
     * @param fsMessTemp 群发模板
     * @return 结果
     */
    @Override
    public int updateFsMessTemp(FsMessTemp fsMessTemp)
    {
        fsMessTemp.setUpdateTime(DateUtils.getNowDate());
        return fsMessTempMapper.updateFsMessTemp(fsMessTemp);
    }

    /**
     * 批量删除群发模板
     *
     * @param ids 需要删除的群发模板主键
     * @return 结果
     */
    @Override
    public int deleteFsMessTempByIds(Long[] ids)
    {
        return fsMessTempMapper.deleteFsMessTempByIds(ids);
    }

    /**
     * 删除群发模板信息
     *
     * @param id 群发模板主键
     * @return 结果
     */
    @Override
    public int deleteFsMessTempById(Long id)
    {
        return fsMessTempMapper.deleteFsMessTempById(id);
    }
}
