package jnpf.attendance.controller;


import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.service.AttendanceUserSettingService;
import jnpf.base.ActionResult;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.exception.HandleException;
import jnpf.model.attendance.dto.AppUserSettingQueryDto;
import jnpf.model.attendance.dto.AttendanceAppUserSettingDto;
import jnpf.model.attendance.vo.UserSettingVo;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * app用户个人及考勤组设置
 * @Author huanglinpan
 * @Date 2024/8/8 10:53
 * @Version 1.0 （版本号）
 */
@RestController
@RequestMapping(value = "/attendance/app")
@Slf4j
public class AttendanceUserSettingController {

    @Resource
    private AttendanceUserSettingService attendanceUserSettingService;
    @Resource
    private UserProvider userProvider;

    /**
     * 修改设置
     * @param attendanceAppUserSettingDto 设置信息
     */
    @PutMapping("/setting")
    public ActionResult saveOrUpdate(@RequestBody AttendanceAppUserSettingDto attendanceAppUserSettingDto) {
        attendanceUserSettingService.saveOrUpdate(attendanceAppUserSettingDto);
        return ActionResult.success();
    }

    /**
     * 批量保存设置
     * @param attendanceAppUserSettingDto 保存的对象
     */
    @PutMapping("/saveOrUpdateList")
    public ActionResult saveOrUpdateList(@RequestBody List<AttendanceAppUserSettingDto> attendanceAppUserSettingDto) {
        attendanceUserSettingService.saveOrUpdateList(attendanceAppUserSettingDto);
        return ActionResult.success();
    }

    /**
     * 查询设置列表
     * @param appUserSettingQueryDto 查询信息
     */
    @GetMapping("/setting")
    public ActionResult<List<UserSettingVo>> getList(AppUserSettingQueryDto appUserSettingQueryDto)  {
        return ActionResult.success(attendanceUserSettingService.getList(appUserSettingQueryDto));
    }

    /**
     * 获取用户上下班极速打卡配置
     */
    @GetMapping("/userSetting")
    public ActionResult<List<UserSettingVo>> getUserSettingList()  {
        return ActionResult.success(attendanceUserSettingService.getSettingList(CollUtil.newArrayList(userProvider.get().getUserId()),1, Stream.of(UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode(),
                        UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode())
                .collect(Collectors.toList())));
    }

    /*****************************************考勤V1.9************************************************/

    /**
     * v1.9查询设置列表
     *
     * 因考虑兼容，导致原本的优化接口变为赋值接口并优化逻辑
     * @param appUserSettingQueryDto 查询信息
     */
    @GetMapping("/settingNew")
    public ActionResult<List<UserSettingVo>> getListNew(AppUserSettingQueryDto appUserSettingQueryDto)  {
        return ActionResult.success(attendanceUserSettingService.getListNew(appUserSettingQueryDto));
    }
}
