package com.xinelu.manage.service.shortmessagesendrecord.impl;

import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.manage.domain.shortmessagesendrecord.ShortMessageSendRecord;
import com.xinelu.manage.mapper.shortmessagesendrecord.ShortMessageSendRecordMapper;
import com.xinelu.manage.service.shortmessagesendrecord.IShortMessageSendRecordService;
import com.xinelu.manage.vo.shortmessagesendrecord.ShortMessageSendRecordVo;
import com.xinelu.manage.vo.statistics.PushCount;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 短信发送结果记录Service业务层处理
 *
 * @author xinelu
 * @date 2024-07-30
 */
@Service
public class ShortMessageSendRecordServiceImpl implements IShortMessageSendRecordService {
    @Resource
    private ShortMessageSendRecordMapper shortMessageSendRecordMapper;

    /**
     * 查询短信发送结果记录
     *
     * @param id 短信发送结果记录主键
     * @return 短信发送结果记录
     */
    @Override
    public ShortMessageSendRecord selectShortMessageSendRecordById(Long id) {
        return shortMessageSendRecordMapper.selectShortMessageSendRecordById(id);
    }

    /**
     * 查询短信发送结果记录列表
     *
     * @param shortMessageSendRecord 短信发送结果记录
     * @return 短信发送结果记录
     */
    @Override
    public List<ShortMessageSendRecordVo> selectShortMessageSendRecordList(ShortMessageSendRecordVo shortMessageSendRecord) {
        return shortMessageSendRecordMapper.selectShortMessageSendRecords(shortMessageSendRecord);
    }

    /**
     * 新增短信发送结果记录
     *
     * @param shortMessageSendRecord 短信发送结果记录
     * @return 结果
     */
    @Override
    public int insertShortMessageSendRecord(ShortMessageSendRecord shortMessageSendRecord) {
        shortMessageSendRecord.setCreateTime(LocalDateTime.now());
        return shortMessageSendRecordMapper.insertShortMessageSendRecord(shortMessageSendRecord);
    }

    /**
     * 修改短信发送结果记录
     *
     * @param shortMessageSendRecord 短信发送结果记录
     * @return 结果
     */
    @Override
    public int updateShortMessageSendRecord(ShortMessageSendRecord shortMessageSendRecord) {
        shortMessageSendRecord.setUpdateTime(LocalDateTime.now());
        return shortMessageSendRecordMapper.updateShortMessageSendRecord(shortMessageSendRecord);
    }

    /**
     * 批量删除短信发送结果记录
     *
     * @param ids 需要删除的短信发送结果记录主键
     * @return 结果
     */
    @Override
    public int deleteShortMessageSendRecordByIds(Long[] ids) {
        return shortMessageSendRecordMapper.deleteShortMessageSendRecordByIds(ids);
    }

    /**
     * 删除短信发送结果记录信息
     *
     * @param id 短信发送结果记录主键
     * @return 结果
     */
    @Override
    public int deleteShortMessageSendRecordById(Long id) {
        return shortMessageSendRecordMapper.deleteShortMessageSendRecordById(id);
    }

    /**
     * 短信推送统计
     */
    @Override
    public AjaxResult messageCount() {
        PushCount pushCount = shortMessageSendRecordMapper.messageCount();
        if (Objects.nonNull(pushCount.getTotal()) && pushCount.getTotal() != 0) {
            pushCount.setSuccessRate(new BigDecimal(pushCount.getSuccess()).divide(new BigDecimal(pushCount.getTotal()), 4, RoundingMode.HALF_UP));
        }
        if (Objects.isNull(pushCount.getTotal()) || pushCount.getTotal() == 0){
            pushCount.setSuccessRate(BigDecimal.ZERO);
        }
        return AjaxResult.success(pushCount);
    }
}
