package com.yonge.cooleshow.biz.dal.service.impl;

import com.yonge.cooleshow.biz.dal.dao.HomeDao;
import com.yonge.cooleshow.biz.dal.dto.req.TotalReq;
import com.yonge.cooleshow.biz.dal.enums.PeriodEnum;
import com.yonge.cooleshow.biz.dal.service.HomeService;
import com.yonge.cooleshow.biz.dal.vo.res.HomeTotalStudent;
import com.yonge.cooleshow.biz.dal.vo.res.HomeTotalTeacher;
import com.yonge.cooleshow.biz.dal.vo.res.HomeUserToDoNum;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * @Author: liweifan
 * @Data: 2022/3/30 18:08
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeDao baserMapper;

    @Override
    public HomeUserToDoNum getUserToDoNum() {
        HomeUserToDoNum userToDoNum = baserMapper.getUserToDoNum();
        userToDoNum.setEntryNum(null == userToDoNum.getEntryNum() ? 0 : userToDoNum.getEntryNum());
        userToDoNum.setMusicianNum(null == userToDoNum.getMusicianNum() ? 0 : userToDoNum.getMusicianNum());
        userToDoNum.setMusicNum(null == userToDoNum.getMusicNum() ? 0 : userToDoNum.getMusicNum());
        return userToDoNum;
    }

    @Override
    public HttpResponseResult<HomeTotalTeacher> totalTeacher(TotalReq query) {
        //参数处理
        try {
            LocalDateTime firstDay;
            LocalDateTime lastDay;
            if (PeriodEnum.YEAR.equals(query.getTimeType())) {
                LocalDate date = LocalDate.of(Integer.parseInt(query.getDateTime()), 1, 1);
                firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
                lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
            } else {
                String[] classDateSp = query.getDateTime().split("-");
                LocalDate date = LocalDate.of(Integer.parseInt(classDateSp[0]), Integer.parseInt(classDateSp[1]), 1);

                firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
                lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
            }
            query.setStartTime(firstDay);
            query.setEndTime(lastDay);
        } catch (Exception e) {
            throw new BizException("查询时间格式不正确 [" + query.getDateTime() + "]");
        }

        //统计查询
        List<HomeTotalTeacher> totalList = baserMapper.totalTeacher(query.getTimeType().getCode(), query);
        Integer registerNum = 0;
        Integer entryNum = 0;
        Integer liveNum = 0;
        Integer musicianNum = 0;
        for (HomeTotalTeacher totalTeacher : totalList) {
            totalTeacher.setRegisterNum(null == totalTeacher.getRegisterNum() ? 0 : totalTeacher.getRegisterNum());
            totalTeacher.setEntryNum(null == totalTeacher.getEntryNum() ? 0 : totalTeacher.getEntryNum());
            totalTeacher.setLiveNum(null == totalTeacher.getLiveNum() ? 0 : totalTeacher.getLiveNum());
            totalTeacher.setMusicianNum(null == totalTeacher.getMusicianNum() ? 0 : totalTeacher.getMusicianNum());

            registerNum += totalTeacher.getRegisterNum();
            entryNum += totalTeacher.getEntryNum();
            liveNum += totalTeacher.getLiveNum();
            musicianNum += totalTeacher.getMusicianNum();
        }
        HomeTotalTeacher total = new HomeTotalTeacher();
        total.setRegisterNum(registerNum);
        total.setEntryNum(entryNum);
        total.setLiveNum(liveNum);
        total.setMusicianNum(musicianNum);
        total.setInfoList(totalList);
        return HttpResponseResult.succeed(total);
    }

    @Override
    public HttpResponseResult<HomeTotalStudent> totalStudent(TotalReq query) {
        //参数处理
        try {
            LocalDateTime firstDay;
            LocalDateTime lastDay;
            if (PeriodEnum.YEAR.equals(query.getTimeType())) {
                LocalDate date = LocalDate.of(Integer.parseInt(query.getDateTime()), 1, 1);
                firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
                lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
            } else {
                String[] classDateSp = query.getDateTime().split("-");
                LocalDate date = LocalDate.of(Integer.parseInt(classDateSp[0]), Integer.parseInt(classDateSp[1]), 1);

                firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
                lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
            }
            query.setStartTime(firstDay);
            query.setEndTime(lastDay);
        } catch (Exception e) {
            throw new BizException("查询时间格式不正确 [" + query.getDateTime() + "]");
        }

        List<HomeTotalStudent> totalList = baserMapper.totalStudent(query.getTimeType().getCode(), query);
        Integer registerNum = 0;
        Integer vipNum = 0;
        Integer courseNum = 0;
        Integer shoppingNum = 0;

        for (HomeTotalStudent totalStudent : totalList) {
            totalStudent.setRegisterNum(null == totalStudent.getRegisterNum() ? 0 : totalStudent.getRegisterNum());
            totalStudent.setVipNum(null == totalStudent.getVipNum() ? 0 : totalStudent.getVipNum());
            totalStudent.setCourseNum(null == totalStudent.getCourseNum() ? 0 : totalStudent.getCourseNum());
            totalStudent.setShoppingNum(null == totalStudent.getShoppingNum() ? 0 : totalStudent.getShoppingNum());

            registerNum += totalStudent.getRegisterNum();
            vipNum += totalStudent.getVipNum();
            courseNum += totalStudent.getCourseNum();
            shoppingNum += totalStudent.getShoppingNum();
        }
        HomeTotalStudent total = new HomeTotalStudent();
        total.setRegisterNum(registerNum);
        total.setVipNum(vipNum);
        total.setCourseNum(courseNum);
        total.setShoppingNum(shoppingNum);
        total.setInfoList(totalList);
        return HttpResponseResult.succeed(total);
    }
}
