package com.tofly.gpsboot.handler.plan;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.gpsboot.common.system.SignTypeEnum;
import com.tofly.gpsboot.service.sign.PdasignService;
import com.tofly.gpsboot.service.sign.PdasignhisService;
import com.tofly.pojo.plan.Pdasign;
import com.tofly.pojo.plan.Pdasignhis;
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.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 自动签退
 *
 * @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) {
        List<String> signTypeList = new ArrayList<>();
        signTypeList.add(SignTypeEnum.SIGN_TYPE_YES.getCode());
        signTypeList.add(SignTypeEnum.SIGN_TYPE_WORK_OVERTIME_YES.getCode());
        List<Pdasign> pdasignList = pdasignService.list(new QueryWrapper<Pdasign>().lambda().in(Pdasign::getSignType, signTypeList));
        if(CollUtil.isNotEmpty(pdasignList)){
            Calendar calendarSignNo = Calendar.getInstance();
            String hourStr = DateUtil.format(calendarSignNo.getTime(), "HH:mm:ss");
            String signType;
            if (hourStr.compareTo("06:30:00") >= 0
                    && hourStr.compareTo("07:00:00") < 0) {
                calendarSignNo.set(Calendar.MINUTE, 30);
                calendarSignNo.set(Calendar.SECOND, 0);
                signType = SignTypeEnum.SIGN_TYPE_YES.getCode();
            } else if (hourStr.compareTo("08:00:00") >= 0
                    && hourStr.compareTo("08:30:00") < 0) {
                calendarSignNo.set(Calendar.MINUTE, 0);
                calendarSignNo.set(Calendar.SECOND, 0);
                signType = SignTypeEnum.SIGN_TYPE_WORK_OVERTIME_YES.getCode();
            } else {
                return ReturnT.SUCCESS;
            }
            Date date = DateUtil.parse(DateUtil.format(calendarSignNo.getTime(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            // 把签到上班记录的地址坐标作为自动签退的地址坐标
            String finalSignType = signType;
            pdasignList.forEach(pdasign -> {
                pdasign.setSignTime(date);
                if (!pdasign.getSignType().equals(finalSignType)) {
                    return;
                }
                if (finalSignType.equals(SignTypeEnum.SIGN_TYPE_YES.getCode())) {
                    pdasign.setSignType(SignTypeEnum.SIGN_TYPE_NO.getCode());
                } else if (finalSignType.equals(SignTypeEnum.SIGN_TYPE_WORK_OVERTIME_YES.getCode())) {
                    pdasign.setSignType(SignTypeEnum.SIGN_TYPE_WORK_OVERTIME_NO.getCode());
                }
                pdasign.setNote("自动签退");
                // 登录历史表中记录数据
                Pdasignhis pdasignhis = JSONObject.parseObject(JSON.toJSONString(pdasign), Pdasignhis.class);
                pdasignhisService.save(pdasignhis);
            });
            pdasignService.updateBatchById(pdasignList);
        }
        return ReturnT.SUCCESS;
    }

}
