package com.ruoyi.cs.service.impl;

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

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsMessageRecordMapper;
import com.ruoyi.cs.domain.CsMessageRecord;
import com.ruoyi.cs.service.ICsMessageRecordService;

import javax.annotation.Resource;

/**
 * 短信通知记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class CsMessageRecordServiceImpl implements ICsMessageRecordService
{
    @Resource
    private CsMessageRecordMapper csMessageRecordMapper;

    /**
     * 查询短信通知记录
     *
     * @param messageRecordId 短信通知记录主键
     * @return 短信通知记录
     */
    @Override
    public CsMessageRecord selectCsMessageRecordByMessageRecordId(String messageRecordId)
    {
        return csMessageRecordMapper.selectCsMessageRecordByMessageRecordId(messageRecordId);
    }

    /**
     * 查询短信通知记录列表
     *
     * @param csMessageRecord 短信通知记录
     * @return 短信通知记录
     */
    @Override
    public List<CsMessageRecord> selectCsMessageRecordList(CsMessageRecord csMessageRecord)
    {
        return csMessageRecordMapper.selectCsMessageRecordList(csMessageRecord);
    }

    /**
     * 新增短信通知记录
     *
     * @param csMessageRecord 短信通知记录
     * @return 结果
     */
    @Override
    public int insertCsMessageRecord(CsMessageRecord csMessageRecord)
    {
        csMessageRecord.setMessageRecordId(IdUtil.getSnowflakeNextIdStr());
        csMessageRecord.setCreateAt(DateUtils.getNowDate());
        csMessageRecord.setDelFlag("0");
        return csMessageRecordMapper.insertCsMessageRecord(csMessageRecord);
    }

    /**
     * 修改短信通知记录
     *
     * @param csMessageRecord 短信通知记录
     * @return 结果
     */
    @Override
    public int updateCsMessageRecord(CsMessageRecord csMessageRecord)
    {
        csMessageRecord.setUpdateAt(DateUtils.getNowDate());
        return csMessageRecordMapper.updateCsMessageRecord(csMessageRecord);
    }

    /**
     * 批量删除短信通知记录
     *
     * @param messageRecordIds 需要删除的短信通知记录主键
     * @return 结果
     */
    @Override
    public int deleteCsMessageRecordByMessageRecordIds(String[] messageRecordIds)
    {
        return csMessageRecordMapper.deleteCsMessageRecordByMessageRecordIds(messageRecordIds);
    }

    /**
     * 删除短信通知记录信息
     *
     * @param messageRecordId 短信通知记录主键
     * @return 结果
     */
    @Override
    public int deleteCsMessageRecordByMessageRecordId(String messageRecordId)
    {
        return csMessageRecordMapper.deleteCsMessageRecordByMessageRecordId(messageRecordId);
    }

    /**
     * 批量发送短信
     *
     * @param csMessageRecord 短信通知记录
     * @return 结果
     */
    @Override
    public AjaxResult bachSendMessage(CsMessageRecord csMessageRecord) {
        List<CsMessageRecord> bachSendMessageList = new ArrayList<>();
        Date nowDate = DateUtils.getNowDate();
        String[] phones = csMessageRecord.getPhones().split("\n");
        for (String phone : phones) {
            CsMessageRecord messageRecord = new CsMessageRecord();
            messageRecord.setMessageRecordId(IdUtil.getSnowflakeNextIdStr());
            messageRecord.setMessageClassId(csMessageRecord.getMessageClassId());
            messageRecord.setMessageUserId(csMessageRecord.getMessageUserId());
            messageRecord.setPhone(phone);
            messageRecord.setMessageInfo(csMessageRecord.getMessagePrefix() + csMessageRecord.getMessageInfo());
            messageRecord.setMessageTime(nowDate);
            messageRecord.setCreateAt(DateUtils.getNowDate());
            messageRecord.setMessageStatus("0");
            messageRecord.setDelFlag("0");
            bachSendMessageList.add(messageRecord);
        }
        if (bachSendMessageList.size() > 0) {
            csMessageRecordMapper.bachInsertCsMessageRecordByMessageList(bachSendMessageList);
        }
        return AjaxResult.success();
    }
}
