package com.storlead.centre.dispatch;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.centre.entity.AttendanceSignRecordEntity;
import com.storlead.centre.entity.HrmschedulesignEntity;
import com.storlead.centre.entity.HrmschedulesignRemindEntity;
import com.storlead.centre.service.AttendanceSignRecordService;
import com.storlead.centre.service.HrmschedulesignRemindService;
import com.storlead.centre.service.HrmschedulesignService;
import com.storlead.centre.vo.SignDataConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: 同步考勤数据到OA
 * @description:
 * @author: chenkq
 * @create: 2026-01-08 11:12
 */
@Slf4j
@Component
public class SyncAttendanceSignToOaTask {

    @Resource
    private AttendanceSignRecordService attendanceSignRecordService;
    @Resource
    private HrmschedulesignRemindService hrmschedulesignRemindService;
    @Resource
    private HrmschedulesignService hrmschedulesignService;

    @Scheduled(cron ="0 */5 6-23 * * ? ")
    public void syncSignData() throws ParseException {
        // 获取未同步的数据到
        LambdaQueryWrapper<AttendanceSignRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttendanceSignRecordEntity::getSyncOa,Integer.valueOf(0));
        List<AttendanceSignRecordEntity> attendances = attendanceSignRecordService.list(lambdaQueryWrapper);

        if (CollectionUtils.isEmpty(attendances)) {
            return;
        }
        List<Long> ids = attendances.stream().map(AttendanceSignRecordEntity::getId).collect(Collectors.toList());

        LambdaUpdateWrapper<AttendanceSignRecordEntity> oawp = new LambdaUpdateWrapper<>();
        oawp.set(AttendanceSignRecordEntity::getSyncOa,Integer.valueOf(1));
        oawp.in(AttendanceSignRecordEntity::getId,ids);
        attendanceSignRecordService.update(oawp);
        //同步OA数据
        List<HrmschedulesignEntity> hrmschedulesignsDb = new ArrayList<>();
        List<HrmschedulesignEntity> hrmschedulesigns = SignDataConvert.attendanceListToOaSignVoList(attendances);
        if (!CollectionUtils.isEmpty(hrmschedulesigns)) {
            List<Long> bmd = Arrays.asList(408L);
            if (!CollectionUtils.isEmpty(bmd)) {
                hrmschedulesignsDb = hrmschedulesigns.stream().filter(e ->bmd.contains(e.getUserid())).collect(Collectors.toList());
            } else {
                hrmschedulesignsDb = hrmschedulesigns;
            }
            if (!CollectionUtils.isEmpty(hrmschedulesignsDb)) {
                hrmschedulesignService.saveBatch(hrmschedulesignsDb);
            }
        }

        List<HrmschedulesignRemindEntity> hrmschedulesignremindsDb = new ArrayList<>();
        List<HrmschedulesignRemindEntity> hrmschedulesignreminds = SignDataConvert.attendanceListToOaSignRemindVoList(attendances);
        if (!CollectionUtils.isEmpty(hrmschedulesignreminds)) {
            List<Integer> bmd = Arrays.asList(408);
            if (!CollectionUtils.isEmpty(bmd)) {
                hrmschedulesignremindsDb = hrmschedulesignreminds.stream().filter(e ->bmd.contains(e.getUserid())).collect(Collectors.toList());
            } else {
                hrmschedulesignremindsDb = hrmschedulesignreminds;
            }
            if (!CollectionUtils.isEmpty(hrmschedulesignremindsDb)) {
                hrmschedulesignRemindService.saveBatch(hrmschedulesignremindsDb);
            }
        }
    }

}
