package jnpf.attendance.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.attendance.service.AppStatisticsService;
import jnpf.base.ActionResult;
import jnpf.exception.QueryException;
import jnpf.model.attendance.dto.*;
import jnpf.model.attendance.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 考勤统计APP
 */
@Slf4j
@RestController
@RequestMapping(value = "/attendance/app/statistics")
public class AppStatisticsController {

    @Resource
    private AppStatisticsService appStatisticsService;

    @Operation(summary = "我的考勤-主页")
    @GetMapping(value = "/home")
    public ActionResult<AppStatisticsListVo> getHomeData(@Valid AppStatisticsListDto req) throws QueryException {
        try {
            log.info("我的考勤-主页，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            AppStatisticsListVo listVo = appStatisticsService.getAppHomeData(req);
            long ent = System.currentTimeMillis();
            log.info("我的考勤-主页，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(listVo);
        } catch (QueryException e) {
            if (e.getMessage().contains("用户暂无考勤组")) {
                return ActionResult.success(e.getMessage());
            } else {
                throw new QueryException(e.getMessage());
            }
        }
    }

    @Operation(summary = "我的考勤-出勤情况")
    @GetMapping("/record")
    public ActionResult<List<AppStatisticsRecordVo>> getRecordData(@Valid AppStatisticsRecordDto req) throws QueryException {
        try {
            List<AppStatisticsRecordVo> list = appStatisticsService.getRecordData(req);
            return ActionResult.success(list);
        } catch (Exception e) {
            if (e.getMessage().contains("暂无数据") || e.getMessage().contains("用户暂无考勤组") || e.getMessage().contains("用户暂无排班")) {
                return ActionResult.success("暂无数据");
            } else {
                throw new QueryException(e.getMessage());
            }
        }
    }

    @Operation(summary = "我的考勤-更多统计-默认")
    @GetMapping("/moreDefault")
    public ActionResult<AppStatisticsMoreVo> getMoreDefaultData(@Valid AppStatisticsMoreDto req) throws Exception {
        log.info("我的考勤-更多统计-默认，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        AppStatisticsMoreVo moreVo = appStatisticsService.getMoreDefaultData(req);
        long ent = System.currentTimeMillis();
        log.info("我的考勤-更多统计-默认，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(moreVo);
    }

    @Operation(summary = "我的考勤-更多统计-展开")
    @GetMapping("/moreExpand")
    public ActionResult<AppStatisticsMoreInfoVo> getMoreExpandData(@Valid AppStatisticsMoreInfoDto req) throws Exception {
        log.info("我的考勤-更多统计-展开，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        AppStatisticsMoreInfoVo moreInfoVo = appStatisticsService.getMoreExpandData(req);
        long ent = System.currentTimeMillis();
        log.info("我的考勤-更多统计-展开，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(moreInfoVo);
    }

    @Operation(summary = "团队考勤-首页")
    @PostMapping("/team/home")
    public ActionResult<AppStatisticsTeamListVo> getTeamHomeData(@Valid @RequestBody AppStatisticsTeamListDto req) throws QueryException {
        log.info("团队考勤-首页，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        AppStatisticsTeamListVo teamListVo = appStatisticsService.getTeamHomeData(req);
        long ent = System.currentTimeMillis();
        log.info("团队考勤-首页，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(teamListVo);
    }

    @Operation(summary = "团队考勤-tab列表数据")
    @PostMapping("/team/tabList")
    public ActionResult<List<AppStatisticsTeamTabVo>> getTabListData(@Valid @RequestBody AppTeamStatisticsTabDto req) {
        log.info("团队考勤-tab列表数据，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        List<AppStatisticsTeamTabVo> teamTabVoList = appStatisticsService.getTabListData(req);
        long ent = System.currentTimeMillis();
        log.info("团队考勤-tab列表数据，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(teamTabVoList);
    }

    @Operation(summary = "团队考勤-团队统计")
    @PostMapping("/team/statistics")
    public ActionResult<AppTeamStatisticsVo> getStatisticsData(@Valid @RequestBody AppTeamStatisticsDto req) throws ParseException {
        log.info("团队考勤-团队统计，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        AppTeamStatisticsVo statisticsVo = appStatisticsService.getStatisticsData(req);
        long ent = System.currentTimeMillis();
        log.info("团队考勤-团队统计，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(statisticsVo);
    }

    @Operation(summary = "团队考勤-团队统计-详情列表")
    @PostMapping("/team/statisticsList")
    public ActionResult<List<AppTeamStatisticsListVo>> getStatisticsListData(@Valid @RequestBody AppTeamStatisticsListDto req) throws QueryException, ParseException {
        log.info("团队考勤-团队统计-详情列表，入参=>{}", JSONObject.toJSON(req));
        long st = System.currentTimeMillis();
        List<AppTeamStatisticsListVo> statisticsListVoList = appStatisticsService.getStatisticsListData(req);
        long ent = System.currentTimeMillis();
        log.info("团队考勤-团队统计-详情列表，耗时=>{}", ent - st + " 毫秒");
        return ActionResult.success(statisticsListVoList);
    }
}
