package com.bonus.system.att.controller;

import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.Log;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.att.entity.AttDataDetailsBean;
import com.bonus.system.att.entity.OrgChangeBean;
import com.bonus.system.att.service.AttCalService;
import com.bonus.system.att.service.OrgChangeService;
import com.bonus.system.att.tasks.AttTasks;
import com.bonus.system.att.tasks.NewAttTask;
import com.bonus.system.att.utils.AttTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤数据拉取控制层
 *
 * @author fly
 */
@RestController
@RequestMapping("/attHisPull")
@Slf4j
public class AttHisPullController extends BaseController {

    @Resource
    private NewAttTask attTask;

    /**
     * 考勤模版数据
     * 某天考勤人员列表
     * 基模版数据生成
     */
    @GetMapping("/getAttTempData")
    @Log(title = "考勤数据拉取->考勤模版数据", businessType = BusinessType.QUERY)
    public AjaxResult getAttTempData(AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            Collections.reverse(dateList);
            for (String date : dateList) {
                attTask.insertAttDateHistory(date);
                attTask.insertAttTempData(date, 2);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 1.数据拉取（考勤）定时器 每隔1个小时获取一次数据
     * 2.考勤数据应用（步骤一：考勤数据更新）
     */
    @GetMapping("/getAttDataPull")
    @Log(title = "考勤数据拉取->数据拉取（考勤）考勤数据更新", businessType = BusinessType.QUERY)
    public AjaxResult getAttDataPull( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            Collections.reverse(dateList);
            for (String date : dateList) {
                attTask.getAttendanceData(date,2);
                attTask.updateAttData(date,2);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 单独子流程，不在主流程中
     * 旷工判断
     */
    @GetMapping("/getAbsenteeismData")
    @Log(title = "考勤数据拉取->旷工判断", businessType = BusinessType.QUERY)
    public AjaxResult getAbsenteeismData( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            Collections.reverse(dateList);
            for (String date : dateList) {
                attTask.updateAbsenteeismData(date,2);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 请假数据应用--法假
     * 每天更新一次一点更新
     */
    @GetMapping("/getLegalHolidayData")
    @Log(title = "考勤数据拉取->请假数据应用--法假", businessType = BusinessType.QUERY)
    public AjaxResult getLegalHolidayData( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            Collections.reverse(dateList);
            for (String date : dateList) {
                attTask.updateLegalHolidayData(date);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 请假数据应用--请假
     * 每天更新两次上午8点，下午18点（15天前数据）
     */
    @GetMapping("/getLeaveData")
    @Log(title = "考勤数据拉取->请假数据应用--请假", businessType = BusinessType.QUERY)
    public AjaxResult getLeaveData( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            String lastDate = dateList.get(dateList.size() - 1);
            attTask.updateLeaveData(lastDate);
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 报表数据生成（日报表）
     * 每次更新数据都要执行(跟随)
     */
    @GetMapping("/insertDayReportData")
    @Log(title = "考勤数据拉取->报表数据生成（日报表）", businessType = BusinessType.QUERY)
    public AjaxResult insertDayReportData( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            Collections.reverse(dateList);
            for (String date : dateList) {
                attTask.insertDayReportData(date);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 报表数据生成（月报表）
     * 月初生成一次
     */
    @GetMapping("/insertMonthReportTempData")
    @Log(title = "考勤数据拉取->报表数据生成（月报表）", businessType = BusinessType.QUERY)
    public AjaxResult insertMonthReportTempData( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            List<String> monthList = dateList.stream()
                    .map(LocalDate::parse)
                    .map(localDate -> localDate.format(DateTimeFormatter.ofPattern("yyyy-MM")))
                    .distinct()
                    .collect(Collectors.toList());
            for (String date : monthList) {
                attTask.insertMonthReportTempData(date);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 报表数据更新--月报表
     * 每日数据变化修改之后修改
     */
    @GetMapping("/updateMonthReportData")
    @Log(title = "考勤数据拉取->报表数据生成--月报表修改", businessType = BusinessType.QUERY)
    public AjaxResult updateMonthReportData( AttDataDetailsBean bean) {
        try{
            List<String> dateList = AttTimeUtil.getStrDateListBetween(bean.getStartDate(), bean.getEndDate());
            List<String> monthList = dateList.stream()
                    .map(LocalDate::parse)
                    .map(localDate -> localDate.format(DateTimeFormatter.ofPattern("yyyy-MM")))
                    .distinct()
                    .collect(Collectors.toList());
            for (String date : monthList) {
                attTask.updateMonthReportData(date);
            }
            return toAjax(true);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }



}
