package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.manage.entity.AbnormalClockInRecord;
import com.ssm.manage.entity.JobContentList;
import com.ssm.manage.mapper.AbnormalClockInRecordMapper;
import com.ssm.manage.mapper.JobContentListMapper;
import com.ssm.manage.query.AbnormalClockInRecordPageQuery;
import com.ssm.manage.service.AbnormalClockInRecordService;
import com.ssm.manage.vo.AbnormalClockInRecordDetail;
import com.ssm.manage.vo.AbnormalClockInRecordsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AbnormalClockInRecordServiceImpl implements AbnormalClockInRecordService {
    public static final String ENTER_TIME = "enterTime";
    public static final Duration MIN_INTERVAL_TIME = Duration.ofHours(4);

    public static final List<String> EQUIPMENT_NUMS = new ArrayList<>();

    static {
        EQUIPMENT_NUMS.add("TBB5235000125");
        EQUIPMENT_NUMS.add("TBB5235000149");
        EQUIPMENT_NUMS.add("TBB5235000151");
        EQUIPMENT_NUMS.add("TBB5235000152");
        EQUIPMENT_NUMS.add("TBB5235000243");
        EQUIPMENT_NUMS.add("TBB5235000254");
    }

    private final AbnormalClockInRecordMapper abnormalClockInRecordMapper;
    private final JobContentListMapper jobContentListMapper;

    @Override
    public PageInfo<AbnormalClockInRecord> page(@NotNull AbnormalClockInRecordPageQuery pageQuery) {
        int page = pageQuery.getPage();
        int size = pageQuery.getSize();

        Example acrExample = new Example(AbnormalClockInRecord.class);
        acrExample.setOrderByClause("attendance_time DESC");
        Example.Criteria acrExampleCriteria = acrExample.createCriteria();

        Date enterStartTime = pageQuery.getEnterStartTime();
        Date enterEndTime = pageQuery.getEnterEndTime();
        // 时间查询区间
        if (enterStartTime != null && enterEndTime != null) {
            // [enterStartTime, enterEndTime]
            acrExampleCriteria.andLessThanOrEqualTo("attendanceTime", enterEndTime);
            acrExampleCriteria.andGreaterThanOrEqualTo("attendanceTime", enterStartTime);
        } else if (enterEndTime != null) {
            // (-infinity, enterEndTime]
            acrExampleCriteria.andLessThanOrEqualTo("attendanceTime", enterEndTime);
        } else if (enterStartTime != null) {
            // [enterStartTime, +infinity)
            acrExampleCriteria.andGreaterThanOrEqualTo("attendanceTime", enterStartTime);
        }
        if(pageQuery.getType()!=null){
            acrExampleCriteria.andEqualTo("type", pageQuery.getType());
        }
        if(pageQuery.getStaffName()!=null){
            acrExampleCriteria.andLike("staffName", pageQuery.getStaffName());
        }


        // 获取分页
        PageHelper.startPage(page, size);
        List<AbnormalClockInRecord> abnormalClockInRecords = abnormalClockInRecordMapper.selectByExample(acrExample);
        PageInfo<AbnormalClockInRecord> tempPage = new PageInfo<>(abnormalClockInRecords);

////        // 处理分页
//        List<AbnormalClockInRecordsVo> abnormalClockInRecordsVos = abnormalClockInRecords.stream()
//                .map(AbnormalClockInRecordsVo::new).collect(Collectors.toList());
//        PageInfo<AbnormalClockInRecordsVo> pageInfo = new PageInfo<>();
//        BeanUtils.copyProperties(tempPage, pageInfo);
//        pageInfo.setList(abnormalClockInRecordsVos);

//        abnormalClockInRecordsVos.forEach(vo -> {
//            Date enterTime = vo.getEnterTime();
//            Date leaveTime =vo.getLeaveTime();
//            // 查询详细异常打卡记录
//            List<JobContentList> jobContentLists = jobContentListMapper
//                    .selectAbnormalRecords(vo.getIdentityCard(), enterTime, leaveTime);
//
//            // 判断异常打卡原因
//            List<AbnormalClockInRecordDetail> records = jobContentLists.stream().map(jobContentList -> {
//                AbnormalClockInRecordDetail detail = new AbnormalClockInRecordDetail(jobContentList);
//
//                Date checkEndTime = detail.getCheckEndTime();
//                if (checkEndTime == null) {
//                    detail.setReason("下班未打卡");
//                } else {
//                    detail.setReason("上下班打卡时间未超过4小时");
//                }
//
//                return detail;
//            }).collect(Collectors.toList());
//
//            // 判断是否有未打卡
//            Integer totalClockInCount = vo.getTotalClockInCount();
//            // 计算天数(向上取整，仅精确到小时)
//            Duration duration = Duration.between(enterTime.toInstant(), leaveTime.toInstant());
//            long days = duration.toDays() + (duration.toHours() % 24 > 0 ? 1 : 0);
//            // 共打卡次数少于在职天数
//            if (totalClockInCount < days) {
//                AbnormalClockInRecordDetail detail = new AbnormalClockInRecordDetail();
//                detail.setReason("打卡次数少于在职天数");
//                records.add(detail);
//            }
//
//            vo.setRecords(records);
//        });

        return tempPage;
    }
}
