package jnpf.attendance.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.attendance.AttendanceConfirmApi;
import jnpf.attendance.service.AttendanceConfirmService;
import jnpf.attendance.service.AttendanceConfirmSettingService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.attendance.dto.ConfirmPageListDto;
import jnpf.model.attendance.dto.ConfirmSettingSubmitDto;
import jnpf.model.attendance.dto.ConfirmStatisticsDto;
import jnpf.model.attendance.vo.attendance.ConfirmDetailsVo;
import jnpf.model.attendance.vo.attendance.ConfirmPageListVo;
import jnpf.model.attendance.vo.attendance.ConfirmSettingInfoVo;
import jnpf.model.attendance.vo.attendance.ConfirmStatisticsVo;
import jnpf.model.thousandsfaces.TodayWorkVo;
import jnpf.util.FtbUtil;
import jnpf.util.NoDataSourceBind;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 考勤确认
 */
@Slf4j
@RestController
@RequestMapping(value = "/attendance/confirm")
public class AttendanceConfirmController implements AttendanceConfirmApi {
    @Resource
    private AttendanceConfirmService attendanceConfirmService;
    @Resource
    private AttendanceConfirmSettingService confirmSettingService;

    @SneakyThrows
    @Operation(summary = "获取考勤确认设置")
    @GetMapping(value = "/getConfirmSetting")
    public ActionResult<ConfirmSettingInfoVo> getConfirmSetting() {
        long st = System.currentTimeMillis();
        ConfirmSettingInfoVo result = confirmSettingService.getConfirmSetting(Boolean.TRUE);
        long ent = System.currentTimeMillis();
        log.info("获取考勤确认设置，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @SneakyThrows
    @Operation(summary = "考勤确认设置提交")
    @PostMapping(value = "/confirmSettingSubmit")
    public ActionResult<Boolean> confirmSettingSubmit(@Valid @RequestBody ConfirmSettingSubmitDto dto) {
        log.info("考勤确认设置提交，入参=>{}", JSONObject.toJSON(dto));
        long st = System.currentTimeMillis();
        Boolean result = confirmSettingService.confirmSettingSubmit(dto);
        long ent = System.currentTimeMillis();
        log.info("考勤确认设置提交，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @SneakyThrows
    @Operation(summary = "获取考勤月份")
    @GetMapping(value = "/getConfirmMonth")
    public ActionResult<String> getConfirmMonth() {
        long st = System.currentTimeMillis();
        String result = attendanceConfirmService.getConfirmMonth();
        long ent = System.currentTimeMillis();
        log.info("获取考勤月份，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success("获取成功", result);
    }

    @SneakyThrows
    @Operation(summary = "考勤确认聚合统计")
    @PostMapping(value = "/getStatistics")
    public ActionResult<ConfirmStatisticsVo> getStatistics(@Valid @RequestBody ConfirmStatisticsDto dto) {
        log.info("考勤确认聚合统计，入参=>{}", JSONObject.toJSON(dto));
        long st = System.currentTimeMillis();
        ConfirmStatisticsVo result = attendanceConfirmService.getStatistics(dto);
        long ent = System.currentTimeMillis();
        log.info("考勤确认聚合统计，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @SneakyThrows
    @Operation(summary = "考勤确认列表展示")
    @PostMapping(value = "/getPageList")
    public ActionResult<PageListVO<ConfirmPageListVo>> getPageList(@Valid @RequestBody ConfirmPageListDto dto) {
        log.info("考勤确认列表展示，入参=>{}", JSONObject.toJSON(dto));
        long st = System.currentTimeMillis();
        PageInfo<ConfirmPageListVo> page = attendanceConfirmService.getPageList(dto);
        long ent = System.currentTimeMillis();
        log.info("考勤确认列表展示，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    @SneakyThrows
    @Operation(summary = "获取考勤确认详情")
    @GetMapping(value = "/getConfirmDetails/{id}")
    public ActionResult<ConfirmDetailsVo> getConfirmDetails(@PathVariable("id") String id) {
        log.info("获取考勤确认详情，入参=>{}", id);
        long st = System.currentTimeMillis();
        ConfirmDetailsVo result = attendanceConfirmService.getConfirmDetails(id);
        long ent = System.currentTimeMillis();
        log.info("获取考勤确认详情，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @Override
    @SneakyThrows
    @Operation(summary = "自动生成考勤确认数据")
    @GetMapping(value = "/autoConfirm")
    public ActionResult<Boolean> autoCreateConfirm() {
        long st = System.currentTimeMillis();
        boolean result = attendanceConfirmService.autoCreateConfirm();
        long ent = System.currentTimeMillis();
        log.info("自动生成考勤确认数据，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @Override
    @SneakyThrows
    @Operation(summary = "逾期自动确认")
    @GetMapping(value = "/confirmAutoSlippage")
    public ActionResult<Boolean> confirmAutoSlippage() {
        long st = System.currentTimeMillis();
        boolean result = attendanceConfirmService.confirmAutoSlippage();
        long ent = System.currentTimeMillis();
        log.info("逾期自动确认，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @Override
    @Operation(summary = "今日工作-考勤确认列表(0-待确认 1-已确认 2-已逾期)")
    @GetMapping(value = "/getTodayWorkConfirmList")
    public List<TodayWorkVo> getTodayWorkConfirmList() {
        long st = System.currentTimeMillis();
        List<TodayWorkVo> result = attendanceConfirmService.getTodayWorkConfirmList();
        long ent = System.currentTimeMillis();
        log.info("获取App考勤确认详情，耗时=>{}", ent - st + " 毫秒");
        return result;
    }

    @SneakyThrows
    @Operation(summary = "获取App考勤确认详情")
    @GetMapping(value = "/getAppConfirmDetails")
    public ActionResult<ConfirmDetailsVo> getAppConfirmDetails(@RequestParam(value = "id", required = false) String id) {
        long st = System.currentTimeMillis();
        ConfirmDetailsVo result = attendanceConfirmService.getAppConfirmDetails(id);
        long ent = System.currentTimeMillis();
        log.info("获取App考勤确认详情，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @SneakyThrows
    @Operation(summary = "App温馨提示关闭")
    @GetMapping(value = "/tipsClos/{id}")
    public ActionResult<Boolean> tipsClos(@PathVariable("id") String id) {
        log.info("温馨提示关闭，入参=>{}", id);
        long st = System.currentTimeMillis();
        Boolean result = attendanceConfirmService.tipsClos(id);
        long ent = System.currentTimeMillis();
        log.info("温馨提示关闭，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @SneakyThrows
    @Operation(summary = "App已查看")
    @GetMapping(value = "/look/{id}")
    public ActionResult<Boolean> look(@PathVariable("id") String id) {
        log.info("App已查看，入参=>{}", id);
        long st = System.currentTimeMillis();
        Boolean result = attendanceConfirmService.look(id);
        long ent = System.currentTimeMillis();
        log.info("App已查看，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }

    @SneakyThrows
    @Operation(summary = "App考勤确认提交")
    @GetMapping(value = "/confirmDetailsSubmit/{id}")
    public ActionResult<Boolean> confirmDetailsSubmit(@PathVariable("id") String id) {
        log.info("App考勤确认提交，入参=>{}", id);
        long st = System.currentTimeMillis();
        Boolean result = attendanceConfirmService.confirmDetailsSubmit(id);
        long ent = System.currentTimeMillis();
        log.info("App考勤确认提交，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(result);
    }
}
