package com.mortals.xhx.module.attendance.service.impl;

import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.module.attendance.model.AttendanceStaffStatEntity;
import com.mortals.xhx.module.attendance.model.AttendanceStaffStatQuery;
import com.mortals.xhx.module.attendance.service.AttendanceStaffStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.attendance.dao.AttendanceStatDao;
import com.mortals.xhx.module.attendance.model.AttendanceStatEntity;
import com.mortals.xhx.module.attendance.service.AttendanceStatService;

import java.util.List;

/**
 * AttendanceStatService
 * 考勤汇总信息 service实现
 *
 * @author zxfei
 * @date 2023-04-08
 */
@Service("attendanceStatService")
public class AttendanceStatServiceImpl extends AbstractCRUDServiceImpl<AttendanceStatDao, AttendanceStatEntity, Long> implements AttendanceStatService {
    @Autowired
    private AttendanceStaffStatService attendanceStaffStatService;

    @Override
    protected void findAfter(AttendanceStatEntity entity, Context context, List<AttendanceStatEntity> list) throws AppException {
        //开始时间
        String startYears =entity.getStartTime().split("-",0)[0];
        Long startYear = Long.valueOf(startYears);
        String startMonths = entity.getStartTime().split("-",0)[1];
        Long startMonth =Long.valueOf(startMonths);
        String startDays= entity.getStartTime().split("-",0)[2];
        Long startDay =Long.valueOf(startDays);
        //结束时间
        String endYears =entity.getEndTime().split("-",0)[0];
        Long endYear = Long.valueOf(endYears);
        String endMonths = entity.getEndTime().split("-",0)[1];
        Long endMonth =Long.valueOf(endMonths);
        String endDays= entity.getEndTime().split("-",0)[2];
        Long endDay =Long.valueOf(endDays);
        list.forEach(item -> {
            AttendanceStaffStatQuery staffStatQuery = new AttendanceStaffStatQuery();
            staffStatQuery.setStaffId(entity.getStaffId());
            if (startYear==endYear){
                staffStatQuery.setYear(startYear.intValue());
            }else {
                staffStatQuery.setYearStart(startYear.intValue());
                staffStatQuery.setYearEnd(endYear.intValue());
            }
            if (startMonth==endMonth){
                staffStatQuery.setMonth(startMonth.intValue());
            }else {
                staffStatQuery.setMonthStart(startMonth.intValue());
                staffStatQuery.setMonthEnd(startMonth.intValue());
            }
            if (startDay==endDay){
                staffStatQuery.setDay(startDay.intValue());
            }else {
                staffStatQuery.setDayStart(startDay.intValue());
                staffStatQuery.setDayEnd(startDay.intValue());
            }
            //比较 year  month day todo
//            staffStatQuery.setYear(year);//相等
//            staffStatQuery.setYearStart();
//            staffStatQuery.setYearEnd();
            //month and day  同上
            List<AttendanceStaffStatEntity> attendanceStaffStatEntities = attendanceStaffStatService.find(staffStatQuery);
           item.setAttendanceStaffStatEntities(attendanceStaffStatEntities);
        });
        super.findAfter(entity, context, list);
    }

    @Override
    protected void findAfter(AttendanceStatEntity params, PageInfo pageInfo, Context context, List<AttendanceStatEntity> list) throws AppException {
        //开始时间
        String startYears =params.getStartTime().split("-",0)[0];
        Long startYear = Long.valueOf(startYears);
        String startMonths = params.getStartTime().split("-",0)[1];
        Long startMonth =Long.valueOf(startMonths);
        String startDays= params.getStartTime().split("-",0)[2];
        Long startDay =Long.valueOf(startDays);
        //结束时间
        String endYears =params.getEndTime().split("-",0)[0];
        Long endYear = Long.valueOf(endYears);
        String endMonths = params.getEndTime().split("-",0)[1];
        Long endMonth =Long.valueOf(endMonths);
        String endDays= params.getEndTime().split("-",0)[2];
        Long endDay =Long.valueOf(endDays);
        list.forEach(item -> {
            AttendanceStaffStatQuery staffStatQuery = new AttendanceStaffStatQuery();
            staffStatQuery.setStaffId(item.getStaffId());
            if (startYear==endYear){
                staffStatQuery.setYear(startYear.intValue());
            }else {
                staffStatQuery.setYearStart(startYear.intValue());
                staffStatQuery.setYearEnd(endYear.intValue());
            }
            if (startMonth==endMonth){
                staffStatQuery.setMonth(startMonth.intValue());
            }else {
                staffStatQuery.setMonthStart(startMonth.intValue());
                staffStatQuery.setMonthEnd(endMonth.intValue());
            }
            if (startDay==endDay){
                staffStatQuery.setDay(startDay.intValue());
            }else {
                staffStatQuery.setDayStart(startDay.intValue());
                staffStatQuery.setDayEnd(endDay.intValue());
            }
            //比较 year  month day todo
//            staffStatQuery.setYear(year);//相等
//            staffStatQuery.setYearStart();
//            staffStatQuery.setYearEnd();
            //month and day  同上
            List<AttendanceStaffStatEntity> attendanceStaffStatEntities = attendanceStaffStatService.find(staffStatQuery);
            item.setAttendanceStaffStatEntities(attendanceStaffStatEntities);
        });
        super.findAfter(params, pageInfo, context, list);
    }
}