package com.yanzhu.manage.service.impl;

import java.util.*;

import com.yanzhu.common.core.context.SecurityContextHolder;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.StringUtils;
import com.yanzhu.manage.domain.AttendanceUbiData;
import com.yanzhu.manage.mapper.AttendanceUbiDataMapper;
import com.yanzhu.manage.service.IAttendanceUbiDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 考勤管理Service业务层处理
 * 
 * @author yanzhu
 * @date 2024-10-12
 */
@Service
public class AttendanceUbiDataServiceImpl implements IAttendanceUbiDataService
{
    @Autowired
    private AttendanceUbiDataMapper attendanceUbiDataMapper;


    /**
     * 查询考勤管理
     * 
     * @param id 考勤管理主键
     * @return 考勤管理
     */
    @Override
    public AttendanceUbiData selectAttendanceUbiDataById(Long id)
    {
        return attendanceUbiDataMapper.selectAttendanceUbiDataById(id);
    }

    /**
     * 查询考勤管理列表
     * 
     * @param attendanceUbiData 考勤管理
     * @return 考勤管理
     */
    @Override
    public List<AttendanceUbiData> selectAttendanceUbiDataList(AttendanceUbiData attendanceUbiData)
    {
        return attendanceUbiDataMapper.selectAttendanceUbiDataList(attendanceUbiData);
    }

    @Override
    public List<AttendanceUbiData> queryAttendaceInfo(AttendanceUbiData where) {
        return attendanceUbiDataMapper.queryAttendaceInfo(where);
    }

    /**
     * 新增考勤管理
     * 
     * @param attendanceUbiData 考勤管理
     * @return 结果
     */
    @Override
    public int insertAttendanceUbiData(AttendanceUbiData attendanceUbiData)
    {
        attendanceUbiData.setCreateBy(SecurityContextHolder.getUserName());
        attendanceUbiData.setCreateTime(DateUtils.getNowDate());
        return attendanceUbiDataMapper.insertAttendanceUbiData(attendanceUbiData);
    }

    /**
     * 修改考勤管理
     * 
     * @param attendanceUbiData 考勤管理
     * @return 结果
     */
    @Override
    public int updateAttendanceUbiData(AttendanceUbiData attendanceUbiData)
    {
        attendanceUbiData.setUpdateBy(SecurityContextHolder.getUserName());
        attendanceUbiData.setUpdateTime(DateUtils.getNowDate());
        return attendanceUbiDataMapper.updateAttendanceUbiData(attendanceUbiData);
    }

    /**
     * 批量删除考勤管理
     * 
     * @param ids 需要删除的考勤管理主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceUbiDataByIds(Long[] ids)
    {
        return attendanceUbiDataMapper.deleteAttendanceUbiDataByIds(ids);
    }

    /**
     * 删除考勤管理信息
     * 
     * @param id 考勤管理主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceUbiDataById(Long id)
    {
        return attendanceUbiDataMapper.deleteAttendanceUbiDataById(id);
    }

    @Override
    public List<AttendanceUbiData> queryStatistics(AttendanceUbiData attendanceUbiData) {
        return attendanceUbiDataMapper.queryStatistics(attendanceUbiData);
    }

    @Override
    public List<AttendanceUbiData> queryStatisticsByUserIds(AttendanceUbiData attendanceUbiData) {
        return attendanceUbiDataMapper.queryStatisticsByUserIds(attendanceUbiData);
    }

    @Override
    public List<AttendanceUbiData> procStatistics(Date inTime, List<AttendanceUbiData> datas, List<AttendanceUbiData> userDatas) {
        List<AttendanceUbiData> users=new ArrayList<>();
        if(datas.size()==0){
            return users;
        }
        Calendar calendar = new GregorianCalendar(inTime.getYear(), inTime.getMonth(), 1);
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for(AttendanceUbiData ad :datas){
            List<String> days=makeEmptyDays(daysInMonth);
            List<AttendanceUbiData> findDatas=findUserDatas(ad,userDatas);
            int j=0;
            for(AttendanceUbiData fd:findDatas){
                if(j==0){
                    fd.setDays(days);
                    users.add(fd);
                }
                j++;
                Date dt=fd.getInTime();
                String inStr=fd.getInTime()!=null?DateUtils.parseDateToStr("HH:MM:SS",fd.getInTime()):"";
                String outStr=fd.getOutTime()!=null?DateUtils.parseDateToStr("HH:MM:SS",fd.getOutTime()):"";
                if(dt==null){
                    dt=fd.getOutTime();
                }
                if(dt==null){
                    continue;
                }
                int day=dt.getDate();
                if(StringUtils.isNotEmpty(inStr) || StringUtils.isNotEmpty(outStr)){
                    days.set(day-1,inStr+"-"+outStr);
                }
            }
        }
        return  users;
    }

    private List<AttendanceUbiData> findUserDatas(AttendanceUbiData ad, List<AttendanceUbiData> userDatas) {
        List<AttendanceUbiData> list=new ArrayList<>();
        for(AttendanceUbiData it :userDatas){
            if(it.getUserId().equals(ad.getUserId())){
                list.add(it);
            }
        }
        return  list;
    }

    private List<String> makeEmptyDays(int daysInMonth) {
        List<String> days=new ArrayList<>();
        for(int i=0;i<daysInMonth;i++){
            days.add("");
        }
        return  days;
    }
}
