package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.CourseScheduleEndDto;
import com.ym.mec.biz.dal.dto.IndexErrorDataExportDto;
import com.ym.mec.biz.dal.entity.InspectionItem;
import com.ym.mec.biz.dal.entity.InspectionItemPlan;
import com.ym.mec.biz.dal.page.EndCourseScheduleQueryInfo;
import com.ym.mec.biz.dal.page.IndexErrRecordQueryInfo;
import com.ym.mec.biz.dal.page.InspectionItemPlanQueryInfo;
import com.ym.mec.biz.dal.page.InspectionQueryInfo;
import com.ym.mec.biz.service.IndexErrDataRecordService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * @Author Joburgess
 * @Date 2021/5/28 0028
 */
@Api(tags = "异常数据")
@RequestMapping("indexErrDataRecord")
@RestController
public class IndexErrDataRecordController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private IndexErrDataRecordService indexErrDataRecordService;

    @ApiOperation(value = "历史异常记录")
    @GetMapping("/queryRecord")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/queryRecord')")
    public HttpResponseResult<PageInfo<IndexErrorDataExportDto>> queryRecord(IndexErrRecordQueryInfo queryInfo){
        queryInfo.setOrganIds(organizationService.getEmployeeOrgan(queryInfo.getOrganIds()));
        return succeed(indexErrDataRecordService.queryRecord(queryInfo));
    }

    @ApiOperation(value = "异常记录汇总")
    @GetMapping("/errDataSummarizing")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/errDataSummarizing')")
    public HttpResponseResult errDataSummarizing(IndexErrRecordQueryInfo queryInfo){
        queryInfo.setOrganIds(organizationService.getEmployeeOrgan(queryInfo.getOrganIds()));
        return succeed(indexErrDataRecordService.errDataSummarizing(queryInfo));
    }

    @ApiOperation(value = "获取所有异常相关教务人员")
    @GetMapping("/getAllEducationUsers")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/getAllEducationUsers')")
    public HttpResponseResult getAllEducationUsers(String organIds){
        organIds = organizationService.getEmployeeOrgan(organIds);
        return succeed(indexErrDataRecordService.getAllEducationUsers(organIds));
    }

    @ApiOperation(value = "乐团巡查事项异常/乐团巡查任务未提交")
    @GetMapping("/queryErrInspectionData")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/queryErrInspectionData')")
    public HttpResponseResult<PageInfo<InspectionItemPlan>> queryErrInspectionData(InspectionItemPlanQueryInfo queryInfo){
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        if (queryInfo.getStartTime() != null) {
            queryInfo.setStartTime(DateUtil.trunc(queryInfo.getStartTime()));
        }
        if (queryInfo.getEndTime() != null) {
            queryInfo.setEndTime(DateUtil.getLastTimeWithDay(queryInfo.getEndTime()));
        }
        String startTime = DateUtil.format(DateUtil.getFirstDayOfMonth(DateUtil.addMonths(new Date(), -2)), DateUtil.ISO_EXPANDED_DATE_FORMAT);
        queryInfo.setSearchStartTime(startTime);

        return succeed(indexErrDataRecordService.queryErrInspectionData(queryInfo));
    }

    @ApiOperation(value = "回访任务未完成")
    @GetMapping("/queryErrStudentVisit")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/queryErrStudentVisit')")
    public HttpResponseResult<PageInfo<InspectionItem>> queryErrStudentVisit(InspectionQueryInfo queryInfo){
        queryInfo = organizationService.onlyEducation(queryInfo);
        String startTime = DateUtil.format(DateUtil.getFirstDayOfMonth(DateUtil.addMonths(new Date(), -2)),DateUtil.ISO_EXPANDED_DATE_FORMAT);
        queryInfo.setSearchStartTime(startTime);
        return succeed(indexErrDataRecordService.queryErrStudentVisit(queryInfo));
    }

    @ApiOperation(value = "课程考勤异常/课程异常")
    @GetMapping("/queryHistoryErrCourseData")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/queryHistoryErrCourseData')")
    public HttpResponseResult<PageInfo<CourseScheduleEndDto>> queryHistoryErrCourseData(EndCourseScheduleQueryInfo queryInfo){
        queryInfo.setOrganIdList(organizationService.getEmployeeOrgan(queryInfo.getOrganIdList()));
        return succeed(indexErrDataRecordService.queryHistoryErrCourseData(queryInfo));
    }
}
