package com.mortals.xhx.module.attendance.web;

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.utils.ServletUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.service.*;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 海康考勤打卡记录汇总信息
*
* @author zxfei
* @date 2023-04-08
*/
@RestController
@RequestMapping("attendance/record/hik")
public class AttendanceRecordHikController extends BaseCRUDJsonBodyMappingController<AttendanceRecordHikService,AttendanceRecordHikEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private AttendanceRecordService attendanceRecordService;
    @Autowired
    private AttendanceRecordHikService attendanceRecordHikService;
    @Autowired
    private AttendanceGroupStaffService attendanceGroupStaffService;
    @Autowired
    private AttendanceGroupService attendanceGroupService;
    @Autowired
    private AttendanceGroupFixedworkService attendanceGroupFixedworkService;
    @Autowired
    private AttendanceClassService attendanceClassService;
    @Autowired
    private AttendanceClassDetailService attendanceClassDetailService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private AttendanceExportRecordService attendanceExportRecordService;
    @Autowired
    private AttendanceRecordErrorService errorService;

    public AttendanceRecordHikController(){
        super.setModuleDesc( "海康考勤打卡记录汇总信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        super.init(model, context);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doImportDataBefore(List<AttendanceRecordHikEntity> list, boolean updateSupport, Context context) throws AppException {


        list.forEach(item->{
            String orgName = item.getOrgName();
            //todo split orgName to deptId and deptName
            String[] split = orgName.split("/");

            String deptName = split[split.length-1];
            DeptQuery deptQuery = new DeptQuery();
            deptQuery.setDeptName(deptName);
            //通过分解的deptName去获取部门信息
            List<DeptEntity> deptEntities = deptService.find(deptQuery);
            Long deptId = 0L;
            //判断查出来的部门信息是否为一个,是的话直接获取id,不是的话获取部门的父级id,通过父级id和deptName去查部门id
            if (StringUtils.isNotNull(deptEntities) && deptEntities.size()==1){
                deptId = deptEntities.get(0).getId();
            }else {
                String parentDeptName = split[split.length - 2];

                deptQuery.setDeptName(parentDeptName);
                DeptEntity deptEntity = deptService.selectOne(deptQuery);

                deptQuery.setDeptName(deptName);
                deptQuery.setParentId(deptEntity.getParentId());
                DeptEntity dept = deptService.selectOne(deptQuery);
                deptId = dept.getId();
            }
            //通过工号去查询员工信息
            String workNum = item.getWorkNum();
            String replace = StrUtil.replace(workNum, "\t", "");
            StaffEntity staffEntity = staffService.selectOne(new StaffQuery().workNum(replace));
            item.setWorkNum(replace);
            item.setStaffId(staffEntity.getId());
            item.setDeptId(deptId);
            item.setDeptName(deptName);
            item.setPositionId(staffEntity.getPositionId());
            item.setPositionName(staffEntity.getPositionName());
        });
        //存入海康打卡原始数据 mortals_xhx_attendance_record_hik
        super.doImportDataBefore(list, updateSupport, context);
    }

    @Override
    protected int batchSaveAfter(List<AttendanceRecordHikEntity> list, Map<String, Object> model, Context context) throws AppException {
        return super.batchSaveAfter(list, model, context);
    }

    @PostMapping(value = "/addAttendanceRecord")
    public void addAttendanceRecord(@RequestBody AttendanceRecordHikEntity hikEntity) throws ParseException {
        List<AttendanceRecordHikEntity> list = hikEntity.getList();
        //对考勤数据根据考勤时间进行排序
        List<AttendanceRecordHikEntity> collect = list.stream().sorted(Comparator.comparing(AttendanceRecordHikEntity::getAttendanceDate)).collect(Collectors.toList());
        //根据工号对考勤数据进行分组
        Map<String, List<AttendanceRecordHikEntity>> listMap = collect.stream().collect(Collectors.groupingBy(AttendanceRecordHikEntity::getWorkNum));
        //map遍历.先把所有键取出来
        Set<String> workNumSet = listMap.keySet();
        //根据键取出value
        for (String workNum : workNumSet) {
            List<AttendanceRecordHikEntity> attendanceRecordHikEntities1 = listMap.get(workNum);
            //对按照工号分组了的数据attendanceRecordHikEntities进行时间分组
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Map<String, List<AttendanceRecordHikEntity>> map = attendanceRecordHikEntities1.stream().collect(Collectors.groupingBy(entity -> entity.getAttendanceDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().format(formatter)));
            Set<String> dateSet = map.keySet();
            for (String s : dateSet) {
                List<AttendanceRecordHikEntity> attendanceRecordHikEntities = map.get(s);
                //对相同工号的人员进行遍历判断考勤规则.
                AttendanceRecordEntity attendanceRecordEntity = new AttendanceRecordEntity();
                List<AttendanceRecordDetailEntity> detailEntity = new ArrayList<>();
                for (AttendanceRecordHikEntity item:attendanceRecordHikEntities) {
                    //对主表的对象进行赋值
                    if (Objects.nonNull(attendanceRecordEntity)){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        attendanceRecordEntity.setStaffId(item.getStaffId());
                        attendanceRecordEntity.setStaffName(item.getStaffName());
                        attendanceRecordEntity.setDeptId(item.getDeptId());
                        attendanceRecordEntity.setDeptName(item.getDeptName());
                        attendanceRecordEntity.setPositionId(item.getPositionId());
                        attendanceRecordEntity.setPositionName(item.getPositionName());
                        attendanceRecordEntity.setWorkNum(item.getWorkNum());
                        IUser user = getContext().getUser();
                        attendanceRecordEntity.setCreateTime(new Date());
                        attendanceRecordEntity.setCreateUserId(user.getId());
                        attendanceRecordEntity.setAttendanceDate(sdf.parse(sdf.format(item.getAttendanceDate())));
                    }
                    //获取考勤打卡时间
                    Date attendanceDate = item.getAttendanceDate();
                    //通过工号查询员工信息
                    StaffQuery staffQuery = new StaffQuery();
                    StaffEntity staffEntity = staffService.selectOne(staffQuery.workNum(item.getWorkNum()));
                    //通过员工id查询考勤人员信息
                    AttendanceGroupStaffQuery attendanceGroupStaffQuery = new AttendanceGroupStaffQuery();
                    List<AttendanceGroupStaffEntity> attendanceGroupStaffEntities = attendanceGroupStaffService.find(attendanceGroupStaffQuery.staffId(staffEntity.getId()));
                    //查询该考勤人员,如果查出来的考勤人员信息list长度为1 则该考勤人员只有一个考勤组
                    if (StringUtils.isNotNull(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size()==1){
                        //查询考勤组
                        AttendanceGroupQuery attendanceGroupQuery = new AttendanceGroupQuery();
                        AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.selectOne(attendanceGroupQuery.id(attendanceGroupStaffEntities.get(0).getGroupId()));
                        //查询考勤组详细信息获取班次
                        AttendanceGroupFixedworkQuery attendanceGroupFixedworkQuery = new AttendanceGroupFixedworkQuery();
                        AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(attendanceGroupFixedworkQuery.groupId(attendanceGroupEntity.getId()));
                        //判断当前考勤日期为周几
                        Date attendanceDate1 = attendanceDate;
                        String week = this.getWeek(attendanceDate1);
                        //通过考勤日期的星期拿到班次id.
                        Long weekClassId = this.getWeekClassId(attendanceGroupFixedworkEntity, week);
                        //weekClassId为-1 则不在考勤
                        if (weekClassId==-1L){
                            //跳过本次循环
                            continue;
                        }
                        //获取班次信息
                        AttendanceClassQuery attendanceClassQuery = new AttendanceClassQuery();
                        AttendanceClassEntity attendanceClassEntity = attendanceClassService.selectOne(attendanceClassQuery.id(weekClassId));
                        //获取班次详细信息
                        AttendanceClassDetailQuery attendanceClassDetailQuery = new AttendanceClassDetailQuery();
                        List<AttendanceClassDetailEntity> attendanceClassDetailEntities = attendanceClassDetailService.find(attendanceClassDetailQuery.classId(attendanceClassEntity.getId()));
                        Integer orderNum = 0;
                        for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntities) {
                            orderNum++;
                            AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                            recordDetailEntity.setShiftsId(classDetailEntity.getId());
                            recordDetailEntity.setShiftsName(classDetailEntity.getClassName());
                            recordDetailEntity.setGoWorkResult(3);
                            recordDetailEntity.setOffWorkResult(3);
                            recordDetailEntity.setOrderNum(orderNum);
                            detailEntity.add(recordDetailEntity);
                        }

                        for (AttendanceClassDetailEntity classDetail :attendanceClassDetailEntities) {
                            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                            //上班打卡前时间
                            Date goWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), -classDetail.getGoWorkDateBefore())));
                            //上班打卡后时间
                            Date goWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), classDetail.getGoWorkDateAfter())));
                            //下班打卡前时间
                            Date offWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), -classDetail.getOffWorkDateBefore())));
                            //下班打卡后时间
                            Date offWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), classDetail.getOffWorkDateAfter())));
                            //判断考勤时间是否在上班打卡区间内  true 在 false 不在
                            boolean goInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore, goWorkDateAfter);
                            //判断考勤时间是否在下班打卡区间内
                            boolean offInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), offWorkDateBefore, offWorkDateAfter);
                            if (goInTime==false && offInTime==false){
                                continue;
                            }
                            if (goInTime) {
                                //如果是上班打卡,判断是否迟到
                                boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore,sdf.parse(sdf.format(classDetail.getGoWorkDate())));
                                if (!inTime) {
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                        if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                            attendanceRecordDetailEntity.setGoWorkResult(4);
                                            attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                        }
                                    }
                                }
                                if (inTime) {
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                        if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                            attendanceRecordDetailEntity.setGoWorkResult(1);
                                            attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                        }
                                    }
                                }
                            }
                            if(offInTime){
                                boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)),sdf.parse(sdf.format( classDetail.getOffWorkDate())), offWorkDateAfter);
                                if (!inTime){
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                        if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                            attendanceRecordDetailEntity.setOffWorkResult(5);
                                            attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                        }
                                    }
                                }
                                if (inTime){
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                        if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                            attendanceRecordDetailEntity.setOffWorkResult(1);
                                            attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //如果考勤人员信息list长度大于1 则该考勤人员有多个考勤组.
                    if (StringUtils.isNotNull(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size()>1){
                        for (AttendanceGroupStaffEntity groupStaffEntity:attendanceGroupStaffEntities){
                            {
                                //查询考勤组
                                AttendanceGroupQuery attendanceGroupQuery = new AttendanceGroupQuery();
                                AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.selectOne(attendanceGroupQuery.id(groupStaffEntity.getGroupId()));
                                //查询考勤组详细信息获取班次
                                AttendanceGroupFixedworkQuery attendanceGroupFixedworkQuery = new AttendanceGroupFixedworkQuery();
                                AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(attendanceGroupFixedworkQuery.groupId(attendanceGroupEntity.getId()));
                                //判断当前考勤日期为周几
                                Date attendanceDate1 = attendanceDate;
                                String week = this.getWeek(attendanceDate1);
                                //通过考勤日期的星期拿到班次id.
                                Long weekClassId = this.getWeekClassId(attendanceGroupFixedworkEntity, week);
                                //weekClassId为-1 则不在考勤
                                if (weekClassId==-1L){
                                    //跳过本次循环
                                    continue;
                                }
                                //获取班次信息
                                AttendanceClassQuery attendanceClassQuery = new AttendanceClassQuery();
                                AttendanceClassEntity attendanceClassEntity = attendanceClassService.selectOne(attendanceClassQuery.id(weekClassId));
                                //获取班次详细信息
                                AttendanceClassDetailQuery attendanceClassDetailQuery = new AttendanceClassDetailQuery();
                                List<AttendanceClassDetailEntity> attendanceClassDetailEntities = attendanceClassDetailService.find(attendanceClassDetailQuery.classId(attendanceClassEntity.getId()));
                                Integer orderNum = 0;
                                for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntities) {
                                    orderNum++;
                                    AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                                    recordDetailEntity.setShiftsId(classDetailEntity.getId());
                                    recordDetailEntity.setShiftsName(classDetailEntity.getClassName());
                                    recordDetailEntity.setGoWorkResult(3);
                                    recordDetailEntity.setOffWorkResult(3);
                                    recordDetailEntity.setOrderNum(orderNum);
                                    detailEntity.add(recordDetailEntity);
                                }
                                for (AttendanceClassDetailEntity classDetail :attendanceClassDetailEntities) {
                                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                                    //上班打卡前时间
                                    Date goWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), -classDetail.getGoWorkDateBefore())));
                                    //上班打卡后时间
                                    Date goWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), classDetail.getGoWorkDateAfter())));
                                    //下班打卡前时间
                                    Date offWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), -classDetail.getOffWorkDateBefore())));
                                    //下班打卡后时间
                                    Date offWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), classDetail.getOffWorkDateAfter())));
                                    //判断考勤时间是否在上班打卡区间内  true 在 false 不在
                                    boolean goInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore, goWorkDateAfter);
                                    //判断考勤时间是否在下班打卡区间内
                                    boolean offInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), offWorkDateBefore, offWorkDateAfter);
                                    if (goInTime==false&&offInTime==false){
                                        continue;
                                    }
                                    if (goInTime) {
                                        //如果是上班打卡,判断是否迟到
                                        boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore,sdf.parse(sdf.format(classDetail.getGoWorkDate())));
                                        if (!inTime) {
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                                    attendanceRecordDetailEntity.setGoWorkResult(4);
                                                    attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                        if (inTime) {
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                                    attendanceRecordDetailEntity.setGoWorkResult(1);
                                                    attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                    }
                                    if(offInTime){
                                        boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)),sdf.parse(sdf.format(classDetail.getOffWorkDate())), offWorkDateAfter);
                                        if (!inTime){
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                                    attendanceRecordDetailEntity.setOffWorkResult(5);
                                                    attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                        if (inTime){
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntity) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getClassId())){
                                                    attendanceRecordDetailEntity.setOffWorkResult(1);
                                                    attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                List<AttendanceRecordErrorEntity> errorEntityList = new ArrayList<>();
                for (AttendanceRecordDetailEntity recordDetailEntity : detailEntity) {
                    Integer goWorkResult = recordDetailEntity.getGoWorkResult();
                    Integer offWorkResult = recordDetailEntity.getOffWorkResult();
                    if (goWorkResult!=1){
                        AttendanceRecordErrorEntity errorEntity = new AttendanceRecordErrorEntity();
                        errorEntity.setStaffId(attendanceRecordEntity.getStaffId());
                        errorEntity.setStaffName(attendanceRecordEntity.getStaffName());
                        errorEntity.setWorkNum(attendanceRecordEntity.getWorkNum());
                        errorEntity.setDeptId(attendanceRecordEntity.getDeptId());
                        errorEntity.setDeptName(attendanceRecordEntity.getDeptName());
                        errorEntity.setPositionId(attendanceRecordEntity.getPositionId());
                        errorEntity.setPositionName(attendanceRecordEntity.getPositionName());
                        errorEntity.setShiftsId(recordDetailEntity.getShiftsId());
                        errorEntity.setShiftsName(recordDetailEntity.getShiftsName());
                        errorEntity.setGoOffDateTime(attendanceClassDetailService.selectOne(new AttendanceClassDetailQuery().id(recordDetailEntity.getShiftsId())).getGoWorkDate());
                        errorEntity.setErrorDateTime(recordDetailEntity.getGoWorkDate());
                        errorEntity.setActualAttendanceDateTime(recordDetailEntity.getGoWorkDate());
                        errorEntity.setErrorStatus(recordDetailEntity.getGoWorkResult());
                        errorEntity.setProcessResult("0");
                        IUser user = getContext().getUser();
                        errorEntity.setCreateTime(new Date());
                        errorEntity.setCreateUserId(user.getId());
                        errorEntityList.add(errorEntity);
                    }
                    if (offWorkResult!=1){
                        AttendanceRecordErrorEntity errorEntity = new AttendanceRecordErrorEntity();
                        errorEntity.setStaffId(attendanceRecordEntity.getStaffId());
                        errorEntity.setStaffName(attendanceRecordEntity.getStaffName());
                        errorEntity.setWorkNum(attendanceRecordEntity.getWorkNum());
                        errorEntity.setDeptId(attendanceRecordEntity.getDeptId());
                        errorEntity.setDeptName(attendanceRecordEntity.getDeptName());
                        errorEntity.setPositionId(attendanceRecordEntity.getPositionId());
                        errorEntity.setPositionName(attendanceRecordEntity.getPositionName());
                        errorEntity.setShiftsId(recordDetailEntity.getShiftsId());
                        errorEntity.setShiftsName(recordDetailEntity.getShiftsName());
                        errorEntity.setGoOffDateTime(attendanceClassDetailService.selectOne(new AttendanceClassDetailQuery().id(recordDetailEntity.getShiftsId())).getOffWorkDate());
                        errorEntity.setErrorDateTime(recordDetailEntity.getOffWorkDate());
                        errorEntity.setActualAttendanceDateTime(recordDetailEntity.getOffWorkDate());
                        errorEntity.setErrorStatus(recordDetailEntity.getOffWorkResult());
                        errorEntity.setProcessResult("0");
                        IUser user = getContext().getUser();
                        errorEntity.setCreateTime(new Date());
                        errorEntity.setCreateUserId(user.getId());
                        errorEntityList.add(errorEntity);
                    }

                }
                if (errorEntityList!=null&&errorEntityList.size()>0){
                    errorService.save(errorEntityList);
                }
                attendanceRecordEntity.setAttendanceRecordDetailList(detailEntity);
                attendanceRecordService.save(attendanceRecordEntity);
            }

        }

    }

    public String getWeek(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        String week = sdf.format(date);
        return week;
    }

    /**
     * 获取通过星期获取班次id  如果返参为-1 则不在考勤
     * @return
     */
    public Long getWeekClassId(AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity,String week){
        Long weekClassId = 0L;
        switch (week){
            case "星期一":
                if (attendanceGroupFixedworkEntity.getMonday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getMondayClassId();
                break;
            case "星期二":
                if (attendanceGroupFixedworkEntity.getTuesday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getTuesdayClassId();
                break;
            case "星期三":
                if (attendanceGroupFixedworkEntity.getWednesday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getWednesdayClassId();
                break;
            case "星期四":
                if (attendanceGroupFixedworkEntity.getThursday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getThursdayClassId();
                break;
            case "星期五":
                if (attendanceGroupFixedworkEntity.getFriday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getFridayClassId();
                break;
            case "星期六":
                if (attendanceGroupFixedworkEntity.getSaturday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getSaturdayClassId();
                break;
            case "星期天":
                if (attendanceGroupFixedworkEntity.getSunday()==0){
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getSundayClassId();
                break;
        }
        return weekClassId;
    }

    /**
     *
     * @param nowTime   当前时间
     * @param startTime	开始时间
     * @param endTime   结束时间
     * @return
     *  判断当前时间在时间区间内
     */
    public static boolean isInTime(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    protected void doListBefore(AttendanceRecordHikEntity query, Map<String, Object> model, Context context) throws AppException {
        if (query.getStaffName()!=null && !query.getStaffName().equals("")){
            String staffName = "%" + query.getStaffName() + "%";
            query.setStaffName(staffName);
        }
        super.doListBefore(query, model, context);
    }

    @Override
    public void doExportFileAfter(byte[] data, AttendanceRecordHikEntity query, Context context) throws AppException {
        InputStream inputStream = new ByteArrayInputStream(data);
        MultipartFile file = ServletUtils.getMultipartFile(inputStream, "kaoqin.xlsx");
        String filePath = uploadService.saveFileUpload(file, "file/fileupload",null);
        AttendanceExportRecordEntity attendanceExportRecordEntity = new AttendanceExportRecordEntity();
        attendanceExportRecordEntity.initAttrValue();
        attendanceExportRecordEntity.setFilePath(filePath);
        if (query.getIdList()!=null){
            List<Long> idList = query.getIdList();
            String recordIdList = "";
            for (Long aLong : idList) {
                if (recordIdList.equals("")){
                    recordIdList = recordIdList + aLong;
                }else {
                    recordIdList = recordIdList + "、" +aLong;
                }
            }
            attendanceExportRecordEntity.setRecordIdList(recordIdList);
        }
        if (query.getAttendanceDateStart()!=null){
            String attendanceDate = query.getAttendanceDateStart() + "~"+query.getAttendanceDateEnd();
            attendanceExportRecordEntity.setAttendanceDate(attendanceDate);
        }
        attendanceExportRecordEntity.setCreateUserId(context.getUser().getId());
        attendanceExportRecordEntity.setCreateTime(new Date());
        attendanceExportRecordEntity.setType(1);
        attendanceExportRecordEntity.setExportOrImport(1);
        attendanceExportRecordService.save(attendanceExportRecordEntity);
        super.doExportFileAfter(data, query, context);
    }

}
