package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.entity.SmsFix;
import com.ssm.manage.entity.SmsMessages;
import com.ssm.manage.mapper.SmsMessagesMapper;
import com.ssm.manage.query.SmsMessagesRequest;
import com.ssm.manage.response.SmsMessageResponse;
import com.ssm.manage.service.SmsFixService;
import com.ssm.manage.service.SmsMessagesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsMessagesServiceImpl extends BaseServiceImpl<SmsMessages> implements SmsMessagesService {

    @Autowired
    private SmsMessagesMapper smsMessagesMapper;

    @Override
    public BaseDao<SmsMessages> getDao() {
        return null;
    }

    @Override
    public PageInfo<SmsMessages> list(SmsMessagesRequest messages) {
        //根据门店id查询
        if(messages.getPhone()!=null && messages.getPhone().equals("")){
            messages.setPhone(null);
        }
//        int i = 1/0;
        PageHelper.startPage(messages.getPageNum(),messages.getPageSize(),messages.getOrderAndSort());
        List<SmsMessages> smsMessagesList = smsMessagesMapper.selectByDate(messages);
        PageInfo<SmsMessages> pageInfo = new PageInfo<>(smsMessagesList);
        return pageInfo;
    }

    @Override
    public SmsMessageResponse ratio(SmsMessagesRequest messages) {
        //根据门店id查询
        if(messages.getPhone()!=null && messages.getPhone().equals("")){
            messages.setPhone(null);
        }
        List<SmsMessages> smsMessagesList = smsMessagesMapper.selectByDate(messages);
        List<SmsMessages> successMessage = smsMessagesList.stream().filter(item -> item.getStatus() == 0).collect(Collectors.toList());
        SmsMessageResponse successSms = new SmsMessageResponse();
        if(smsMessagesList!=null && smsMessagesList.size()>0){
            SmsMessages smsMessages = smsMessagesList.get(0);
            BeanUtils.copyProperties(smsMessages,successSms);
            successSms.setSuccessRatio(BigDecimal.valueOf(successMessage.size()).divide(BigDecimal.valueOf(smsMessagesList.size()),4,BigDecimal.ROUND_HALF_UP));
            successSms.setFailureRatio(new BigDecimal("1").subtract(successSms.getSuccessRatio()));
            successSms.setSmsTotal(smsMessagesList.size());
        }
        return successSms;
    }
}
