package com.storlead.centre.vo;

import com.alibaba.fastjson.JSON;
import com.storlead.centre.entity.AttendanceSignRecordEntity;
import com.storlead.centre.entity.HrmschedulesignEntity;
import com.storlead.centre.entity.HrmschedulesignRemindEntity;
import com.storlead.wx.pojo.vo.CheckinDataBO;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: storlead-centre-platform
 * @description:
 * @author: chenkq
 * @create: 2026-01-09 10:51
 */
@Log4j2
public class SignDataConvert {

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static List<HrmschedulesignEntity> attendanceListToOaSignVoList(List<AttendanceSignRecordEntity> vals) {
        if (CollectionUtils.isEmpty(vals)) {
            return null;
        }
        List<HrmschedulesignEntity> departmentTrees = vals.stream().map(SignDataConvert::attendanceToOaSignVo).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return departmentTrees;
    }

    public static HrmschedulesignEntity  attendanceToOaSignVo(AttendanceSignRecordEntity attendance) {
        if (null == attendance) {
            return null;
        }
        try {
            HrmschedulesignEntity entity = new HrmschedulesignEntity();
            entity.setUserid(attendance.getUserId());
            // 1 签到，2：签退
            if ("上班打卡".equals(attendance.getCheckinType())) {
                entity.setSigntype("1");
            } else if ("下班打卡".equals(attendance.getCheckinType())){
                entity.setSigntype("2");
            }
            String checkData = attendance.getCheckinDate().format(formatter);
            entity.setUsertype("1");
            entity.setSigndate(checkData);
            entity.setSigntime(attendance.getCheckinTime());
            entity.setClientAddress(attendance.getLocationTitle());
            entity.setIsincom("1");
            if ("外出打卡".equals(attendance.getCheckinType())) {
                entity.setSignfrom("Wechat_out");
            } else {
                entity.setSignfrom("Wechat");
            }
            entity.setLatitude(attendance.getLatitude());
            entity.setLongitude(attendance.getLongitude());
            entity.setAddr(attendance.getLocationTitle());
            entity.setTimeZone("GMT+8");
            entity.setBelongdate(checkData);
            Map map = new HashMap();
            map.put("deviceId",attendance.getDeviceId());
            entity.setDeviceInfo(JSON.toJSONString(map));
            return entity;
        }catch (Exception e) {
            return null;
        }
    }


    public static List<HrmschedulesignRemindEntity> attendanceListToOaSignRemindVoList(List<AttendanceSignRecordEntity> vals) {
        if (CollectionUtils.isEmpty(vals)) {
            return null;
        }
        List<HrmschedulesignRemindEntity> departmentTrees = vals.stream().map(SignDataConvert::attendanceToOaSignRemindVo).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return departmentTrees;
    }

    public static HrmschedulesignRemindEntity  attendanceToOaSignRemindVo(AttendanceSignRecordEntity attendance) {
        if (null == attendance) {
            return null;
        }
        try {
            HrmschedulesignRemindEntity entity = new HrmschedulesignRemindEntity();
            entity.setUserid(attendance.getUserId().intValue());
            // 1 签到，2：签退
            if ("上班打卡".equals(attendance.getCheckinType())) {
                entity.setSigntype("1");
            } else if ("下班打卡".equals(attendance.getCheckinType())){
                entity.setSigntype("2");
            }
            String checkData = attendance.getCheckinDate().format(formatter);
            entity.setSigndate(checkData);
            entity.setSigntime(attendance.getCheckinTime());
            entity.setBelongdate(checkData);
            return entity;
        }catch (Exception e) {
            return null;
        }
    }
}
