package com.ruoyi.school.dashboard.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.school.course.domain.ScCourse;
import com.ruoyi.school.course.domain.req.time.ReqClaTimeCount;
import com.ruoyi.school.course.enums.ClaTimeAttendStatusEnums;
import com.ruoyi.school.course.service.IScClaTimeAttendService;
import com.ruoyi.school.course.service.IScClaTimeService;
import com.ruoyi.school.course.service.IScCourseService;
import com.ruoyi.school.dashboard.domain.resp.DashboardData;
import com.ruoyi.school.member.domain.ScStudent;
import com.ruoyi.school.member.mapper.ScMemberCardMapper;
import com.ruoyi.school.member.service.IScMemberCardService;
import com.ruoyi.school.order.enums.OrderTypeEnum;
import com.ruoyi.school.order.service.IScOrderService;
import com.ruoyi.school.student.service.IScStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author ：zhangbaoyu
 * @date ：Created in 2020/10/18 15:34
 */
@Service
@Transactional
public class DashboardService {

    @Autowired
    private IScMemberCardService memberCardService;
    @Autowired
    private IScCourseService courseService;
    @Autowired
    private IScStudentService studentService;
    @Autowired
    private IScOrderService orderService;



    @Autowired
    private IScClaTimeService claTimeService;

    @Autowired
    private IScClaTimeAttendService attendService;

    @Autowired
    private ScMemberCardMapper memberCardMapper;
//

    /**
     * 首页各项数据
     *
     * @return
     */
    public DashboardData dashboardData() {
        String todayDate = DateTime.now().toString("yyyy-MM-dd");
        String thisMonthBegin = DateUtil.beginOfMonth(DateTime.now()).toString("yyyy-MM-dd");
        String thisMonthEnd = DateUtil.endOfMonth(DateTime.now()).toString("yyyy-MM-dd");
        String tenantId=SecurityUtils.getLoginUser().getNowTenantId();

        int courseCnt = courseService.count(new QueryWrapper<ScCourse>().eq("tenant_id",tenantId));
        int studentCnt = studentService.count(new QueryWrapper<ScStudent>().eq("tenant_id",tenantId));
        //排课数
        ReqClaTimeCount reqClaTimeCount = ReqClaTimeCount.builder().beginDate(todayDate).endDate(todayDate).hadBegin(null).tenantId(tenantId).build();
        Integer todayClaTimeCnt = claTimeService.claTimeCount(reqClaTimeCount);
        // 今日已上课数量
        reqClaTimeCount.setHadBegin(true);
        Integer completeTodayClaTimeCnt = claTimeService.claTimeCount(reqClaTimeCount);
        //今日报名
        Integer todayOrderCnt = orderService.orderCount(todayDate, todayDate, OrderTypeEnum.SIGN_UP.getOrderType());
        // 本月报名
        Integer thisMonthOrderCnt = orderService.orderCount(thisMonthBegin, thisMonthEnd, OrderTypeEnum.SIGN_UP.getOrderType());
        // 今日上课应到人次（预约人数）
        Integer todayNeedAttendCnt = attendService.getNeedAttendCount(todayDate, todayDate);
        // 今日上课实到课人次（签到人数）
        Integer todayRealAttendCnt = attendService.getAttendCount(todayDate, todayDate,
                new String[]{ClaTimeAttendStatusEnums.AT_CLASS.getAttendStatus()});
//        BigDecimal todayNeedCostHour = attendService.getAttendCostHour(todayDate, todayDate, true);
        //今日实收费用
        BigDecimal todayRealCostHour = attendService.getAttendCostHour(todayDate, todayDate);
        //费用少于200元
        Integer feeWillExpireCnt = memberCardMapper.selectFeeWillExpireCards(200);
        //5天内要过期的
        Integer dateWillExpireCnt = memberCardMapper.selectExpiringCards(5).size();
        //还剩10节课以内的
        Integer hourWillExpireCnt = memberCardMapper.selectHourWillExpireCards(10);

        return DashboardData.builder()
                .todayClaTimeCnt(todayClaTimeCnt)
                .completeTodayClaTimeCnt(completeTodayClaTimeCnt)
                .todayOrderCnt(todayOrderCnt)
                .thisMonthOrderCnt(thisMonthOrderCnt)
                .todayRealAttendCnt(todayRealAttendCnt)
                .todayNeedAttendCnt(todayNeedAttendCnt)
//                .todayNeedCostHour(todayNeedCostHour)
                .todayRealCostHour(todayRealCostHour)
                .studentCnt(studentCnt)
//                .claCnt(claCnt)
                .courseCnt(courseCnt)
                .feeWillExpireCnt(feeWillExpireCnt)
                .dateWillExpireCnt(dateWillExpireCnt)
                .hourWillExpireCnt(hourWillExpireCnt)
                .build();
    }

}
