package com.tofly.gpsboot.handler.plan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.es.PdaDeviceUpload;
import com.tofly.gpsboot.common.system.PlanStateEnum;
import com.tofly.gpsboot.common.system.SignTypeEnum;
import com.tofly.gpsboot.service.es.IPdaDeviceService;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.gpsboot.service.sign.PdasignService;
import com.tofly.gpsboot.service.sign.PdasignhisService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.plan.Pdasign;
import com.tofly.pojo.plan.Pdasignhis;
import com.tofly.pojo.plan.Plan;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 自动签退
 *
 * @author BZ-DJJ
 */
@Component
public class AutomaticallySignBackHandler {

    @Autowired
    private PdasignService pdasignService;
    @Autowired
    private PdasignhisService pdasignhisService;

    @XxlJob("automaticallySignBackHandler")
    @Transactional(rollbackFor = Exception.class)
    public ReturnT<String> automaticallySignBackHandler(String param) {
        // 获取今日23:59分未自动签退的签到信息(定时任务未当天 23点59分)
        List<Pdasign> list = pdasignService.list(new QueryWrapper<Pdasign>().lambda()
                .eq(Pdasign::getSignType, SignTypeEnum.SIGN_TYPE_YES.getCode()));
        if(null!=list && list.size()>0){
            Date date = DateTimeUtil.convertLDTToDate(DateTimeUtil.getTodayEndTime());
            // 把签到上班记录地址 坐标  作为下班记录
            list.forEach(info -> {
                info.setSignTime(date);
                info.setSignType(SignTypeEnum.SIGN_TYPE_NO.getCode());
                info.setNote("自动签退");
                // 登录历史表中记录数据
                Pdasignhis pdasignhis = JSONObject.parseObject(JSON.toJSONString(info), Pdasignhis.class);
                pdasignhisService.save(pdasignhis);
            });
            pdasignService.updateBatchById(list);
        }



        return ReturnT.SUCCESS;
    }

}
