package jnpf.attendance.controller;

import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.AttendanceSimulateDataApi;
import jnpf.attendance.service.AttenceMachineService;
import jnpf.attendance.service.AttendanceUserService;
import jnpf.attendance.service.UserFaceService;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.entity.AttendanceGroupUser;
import jnpf.entity.attendance.AttendanceMachineManage;
import jnpf.util.NoDataSourceBind;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 考勤打卡控制器
 *
 * @author yanwenfu
 * @create 2023-11-21
 */
@Slf4j
@RestController
@RequestMapping(value = "/attendance/simulateData")
public class AttendanceSimulatedDataController implements AttendanceSimulateDataApi {

    @Resource
    private AttenceMachineService attendanceClockInService;
    @Autowired
    private AttendanceUserService attendanceUserService;
    @Autowired
    private UserFaceService userFaceService;

    /**
     * 打卡
     * @return java.lang.Object
     */
    @GetMapping(value = "/clockIn")
    @NoDataSourceBind
    public void clockIn(@RequestParam(value = "tenantId") String tenantId) throws Exception {
        Assert.notNull(tenantId, "租户ID不能为空");
        TenantDataSourceUtil.switchTenant(tenantId);
        List<AttendanceGroupUser> attendanceGroupUsers = attendanceUserService.queryByUsersAndGroupFilterSecondment(new Date(), new Date(), null, null);
        UserProvider.getUser().setTenantId(tenantId);
        Map<String, List<AttendanceMachineManage>> userFaceList = userFaceService.getUserFaceList(attendanceGroupUsers.stream().map(AttendanceGroupUser::getUserId).distinct().collect(Collectors.toList()), tenantId);
        attendanceGroupUsers.forEach(attendanceGroupUser -> {
            try {
                List<AttendanceMachineManage> attendanceMachineManages = userFaceList.get(attendanceGroupUser.getUserId());
                if(CollUtil.isEmpty(attendanceMachineManages)){
                    return;
                }
                AttendanceMachineManage attendanceMachineManage = attendanceMachineManages.stream().findFirst().orElse(null);
                if(Objects.isNull(attendanceMachineManage)){
                    return;
                }
                attendanceClockInService.clockIn(attendanceMachineManage.getMac(), attendanceGroupUser.getUserId(), tenantId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

    }

}
