package com.yida.data.device.telephone.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.telephone.EduTelephone;
import com.yida.data.common.core.entity.telephone.EduTelephoneCard;
import com.yida.data.common.core.entity.telephone.EduTelephoneRecord;
import com.yida.data.common.core.utils.ExcelUtil;
import com.yida.data.device.dto.telephone.RecordPageDTO;
import com.yida.data.device.telephone.mapper.EduTelephoneOrderMapper;
import com.yida.data.device.telephone.mapper.EduTelephoneRecordMapper;
import com.yida.data.device.telephone.service.EduTelephoneCardService;
import com.yida.data.device.telephone.service.EduTelephoneRecordService;
import com.yida.data.device.telephone.service.EduTelephoneService;
import com.yida.data.device.vo.telephone.FamilyRecordVO;
import com.yida.data.device.vo.telephone.RecordInfoVO;
import com.yida.data.device.vo.telephone.TelephoneSummaryVO;
import com.yida.data.school.vo.news.TelephoneCountVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 亲情电话-通话记录 Service实现
 *
 * @author zhanghaijun
 * @date 2021-08-10 17:01:40
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduTelephoneRecordServiceImpl extends ServiceImpl
        <EduTelephoneRecordMapper, EduTelephoneRecord> implements EduTelephoneRecordService {

    private final EduTelephoneRecordMapper eduTelephoneRecordMapper;

    @Resource
    private EduTelephoneService eduTelephoneService;

    @Resource
    private EduTelephoneOrderMapper eduTelephoneOrderMapper;

    @Resource
    private EduTelephoneCardService eduTelephoneCardService;

    @Override
    public IPage<RecordInfoVO> listRecordPage(RecordPageDTO dto) {
        return baseMapper.listRecordPage(dto.toPage(), dto);
    }

    /**
     * 导出通话记录
     *
     * @param dto
     * @param response
     */
    @Override
    public void exportRecord(RecordPageDTO dto, HttpServletResponse response) {
        List<RecordInfoVO> voList = CollUtil.isNotEmpty(dto.getRecordList()) ? dto.getRecordList() :
                baseMapper.listRecord(dto);
        if (CollUtil.isNotEmpty(voList)) {
            for (RecordInfoVO vo : voList) {
                vo.setCallProperty(vo.getCallPhone() + (StrUtil.isNotBlank(vo.getCallName()) ? "(" + vo.getCallName() +
                        ")" : ""));
            }
        }
        ExcelUtil.export("通话记录", RecordInfoVO.class, voList, response);
    }

    /**
     * 查询总数
     *
     * @param schoolId
     * @param type
     * @return
     */
    @Override
    public TelephoneCountVO getTotalNum(Long schoolId, Integer type) {
        long countPhone = eduTelephoneService.count(Wrappers.lambdaQuery(new EduTelephone())
                .eq(EduTelephone::getSchoolId, schoolId)
                .eq(EduTelephone::getOnlineStatus, 0));

        long countPerson = eduTelephoneCardService.count(Wrappers.lambdaQuery(new EduTelephoneCard())
                .eq(EduTelephoneCard::getSchoolId, schoolId)
                .eq(EduTelephoneCard::getStatus, 0));

        BigDecimal totalMoney = eduTelephoneOrderMapper.getTotalMoney(type, schoolId);
        TelephoneCountVO telephoneCountVO = new TelephoneCountVO();
        telephoneCountVO.setCountPerson(countPerson);
        telephoneCountVO.setCountPhone(countPhone);
        telephoneCountVO.setTotalMoney(totalMoney);
        return telephoneCountVO;

    }

    /**
     * 看板统计
     *
     * @param type
     * @param schoolId
     * @return
     */
    @Override
    public List<TelephoneSummaryVO> callSummary(Integer type, Long schoolId) {

        //统计全部
        if (type == 0) {
            List<TelephoneSummaryVO> yearList = baseMapper.callYearSummary(schoolId);
            //总月数
            Integer integer = baseMapper.selectMonth(schoolId);
            if (integer != null) {
                if (integer == 0) {
                    TelephoneSummaryVO telephoneSummaryVO = yearList.get(0);
                    telephoneSummaryVO.setMonth(telephoneSummaryVO.getMonth());
                }
            }
            LocalDateTime now = LocalDateTime.now();
            ArrayList<TelephoneSummaryVO> list = new ArrayList<>();
            for (int i = 0; i < integer + 1; i++) {
                TelephoneSummaryVO telephoneSummaryVO = new TelephoneSummaryVO();
                String b = now.getYear() + "-" + (now.getMonthValue() > 10 ? now.getMonth() : "0" + now.getMonth());
                telephoneSummaryVO.setTime(b);
                list.add(telephoneSummaryVO);
            }
            for (int i = 0; i < list.size(); i++) {
                for (TelephoneSummaryVO telephoneSummaryVO : yearList) {
                    if (telephoneSummaryVO.getMonth().equals(list.get(i).getMonth())) {
                        telephoneSummaryVO.setTime(telephoneSummaryVO.getMonth());
                        list.set(i, telephoneSummaryVO);
                        now = now.plusMonths(-1);
                    }
                }

            }


            return list;
        }
        //统计最近一天，24个小时
        if (type == 3) {
            List<TelephoneSummaryVO> hoursList = baseMapper.callDaySummary(schoolId);

            LocalDateTime now = LocalDateTime.now();
            ArrayList<TelephoneSummaryVO> list = new ArrayList<>(24);
            for (int i = 0; i < 24; i++) {
                TelephoneSummaryVO telephoneSummaryVO = new TelephoneSummaryVO();
                String b = (now.getHour() > 9 ? now.getHour() : +now.getHour()) + "";
                telephoneSummaryVO.setTime(b);
                list.add(telephoneSummaryVO);
                now = now.plusHours(-1);
            }
            for (int i = 0; i < 24; i++) {
                for (TelephoneSummaryVO telephoneSummaryVO : hoursList) {
                    if (telephoneSummaryVO.getHours().toString().equals(list.get(i).getTime())) {
                        telephoneSummaryVO.setTime(telephoneSummaryVO.getHours().toString());
                        list.set(i, telephoneSummaryVO);
                    }
                }
            }
            Collections.reverse(list);
            return list;
        }

        //统计一周或一个月
        int a = 0;
        switch (type) {
            case 1:
                a = 30;
                break;
            case 2:
                a = 7;
                break;
            default:
        }
        List<TelephoneSummaryVO> listObj = baseMapper.callSummary(type, schoolId);
        LocalDate now = LocalDate.now();
        ArrayList<TelephoneSummaryVO> list = new ArrayList<>(a);
        for (int i = 0; i < a; i++) {
            TelephoneSummaryVO telephoneSummaryVO = new TelephoneSummaryVO();
            int monthValue = now.getMonthValue();
            int dayOfMonth = now.getDayOfMonth();
            String b = (monthValue > 10 ? monthValue : "0" + monthValue) + "-" + (dayOfMonth > 10 ? dayOfMonth : "0" + dayOfMonth);
            telephoneSummaryVO.setTime(b);
            now = now.plusDays(-1);
            list.add(telephoneSummaryVO);
        }
        for (int i = 0; i < a; i++) {
            for (TelephoneSummaryVO ot : listObj) {
                if (ot.getDay().equals(list.get(i).getTime())) {
                    ot.setTime(list.get(i).getTime());
                    list.set(i, ot);
                }
            }
        }

        Collections.reverse(list);
        return list;
    }

    /**
     * 查询亲情电话
     *
     * @param cardId
     * @return
     */
    @Override
    public List<FamilyRecordVO> listFamilyRecord(Long cardId) {
        return baseMapper.listFamilyRecord(cardId);
    }

}