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

import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.manage.domain.phonedialrecord.PhoneDialRecord;
import com.xinelu.manage.mapper.phonedialrecord.PhoneDialRecordMapper;
import com.xinelu.manage.service.phonedialrecord.IPhoneDialRecordService;
import com.xinelu.manage.vo.phonedialrecord.BillPhoneDialVo;
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-29
 */
@Service
public class PhoneDialRecordServiceImpl implements IPhoneDialRecordService {
    @Resource
    private PhoneDialRecordMapper phoneDialRecordMapper;

    /**
     * 查询电话拨打记录
     *
     * @param id 电话拨打记录主键
     * @return 电话拨打记录
     */
    @Override
    public PhoneDialRecord selectPhoneDialRecordById(Long id) {
        return phoneDialRecordMapper.selectPhoneDialRecordById(id);
    }

    /**
     * 查询电话拨打记录列表
     *
     * @param phoneDialRecord 电话拨打记录
     * @return 电话拨打记录
     */
    @Override
    public List<BillPhoneDialVo> selectPhoneDialRecordList(BillPhoneDialVo phoneDialRecord) {
        return phoneDialRecordMapper.selectPhoneDialRecords(phoneDialRecord);
    }

    /**
     * 新增电话拨打记录
     *
     * @param phoneDialRecord 电话拨打记录
     * @return 结果
     */
    @Override
    public int insertPhoneDialRecord(PhoneDialRecord phoneDialRecord) {
        phoneDialRecord.setCreateTime(LocalDateTime.now());
        return phoneDialRecordMapper.insertPhoneDialRecord(phoneDialRecord);
    }

    /**
     * 修改电话拨打记录
     *
     * @param phoneDialRecord 电话拨打记录
     * @return 结果
     */
    @Override
    public int updatePhoneDialRecord(PhoneDialRecord phoneDialRecord) {
        phoneDialRecord.setUpdateTime(LocalDateTime.now());
        return phoneDialRecordMapper.updatePhoneDialRecord(phoneDialRecord);
    }

    /**
     * 批量删除电话拨打记录
     *
     * @param ids 需要删除的电话拨打记录主键
     * @return 结果
     */
    @Override
    public int deletePhoneDialRecordByIds(Long[] ids) {
        return phoneDialRecordMapper.deletePhoneDialRecordByIds(ids);
    }

    /**
     * 删除电话拨打记录信息
     *
     * @param id 电话拨打记录主键
     * @return 结果
     */
    @Override
    public int deletePhoneDialRecordById(Long id) {
        return phoneDialRecordMapper.deletePhoneDialRecordById(id);
    }

    /**
     * 电话推送统计
     */
    @Override
    public AjaxResult phoneCount() {
        PushCount pushCount = phoneDialRecordMapper.phoneCount();
        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);
    }
}
