package com.ym.mec.web.controller;

import com.netflix.discovery.converters.Auto;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.auth.api.entity.SysUserRole;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.CourseScheduleEndDto;
import com.ym.mec.biz.dal.dto.IndexErrorDataExportDto;
import com.ym.mec.biz.dal.entity.Employee;
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.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.Arrays;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private IndexErrDataRecordService indexErrDataRecordService;

    @ApiOperation(value = "历史异常记录")
    @GetMapping("/queryRecord")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/queryRecord')")
    public HttpResponseResult<PageInfo<IndexErrorDataExportDto>> queryRecord(IndexErrRecordQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganIds())) {
            queryInfo.setOrganIds(employee.getOrganIdList());
        } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
            return failed("用户所在分部异常");
        } else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if (!list.containsAll(Arrays.asList(queryInfo.getOrganIds().split(",")))) {
                return failed("非法请求");
            }
        }
        return succeed(indexErrDataRecordService.queryRecord(queryInfo));
    }

    @ApiOperation(value = "异常记录汇总")
    @GetMapping("/errDataSummarizing")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/errDataSummarizing')")
    public HttpResponseResult errDataSummarizing(IndexErrRecordQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganIds())) {
            queryInfo.setOrganIds(employee.getOrganIdList());
        } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
            return failed("用户所在分部异常");
        } else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if (!list.containsAll(Arrays.asList(queryInfo.getOrganIds().split(",")))) {
                return failed("非法请求");
            }
        }
        return succeed(indexErrDataRecordService.errDataSummarizing(queryInfo));
    }

    @ApiOperation(value = "获取所有异常相关教务人员")
    @GetMapping("/getAllEducationUsers")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/getAllEducationUsers')")
    public HttpResponseResult getAllEducationUsers(String organIds){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(organIds)) {
            organIds = employee.getOrganIdList();
        } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
            return failed("用户所在分部异常");
        } else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if (!list.containsAll(Arrays.asList(organIds.split(",")))) {
                return failed("非法请求");
            }
        }
        return succeed(indexErrDataRecordService.getAllEducationUsers(organIds));
    }

    @ApiOperation(value = "乐团巡查事项异常/乐团巡查任务未提交")
    @GetMapping("/queryErrInspectionData")
    @PreAuthorize("@pcs.hasPermissions('indexErrDataRecord/queryErrInspectionData')")
    public HttpResponseResult<PageInfo<InspectionItemPlan>> queryErrInspectionData(InspectionItemPlanQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(employee.getOrganIdList());
        } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
            return failed("用户所在分部异常");
        } else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if (!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))) {
                return failed("非法请求");
            }
        }
        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){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(employee.getOrganIdList());
        } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
            return failed("用户所在分部异常");
        } else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if (!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))) {
                return failed("非法请求");
            }
        }
        List<Integer> userRole = employeeDao.queryUserRole(sysUser.getId());
        if (!userRole.contains(SysUserRole.SECTION_MANAGER) && !sysUser.getIsSuperAdmin()) {
            queryInfo.setUserId(sysUser.getId());
        }
        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){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (org.apache.commons.lang3.StringUtils.isEmpty(queryInfo.getOrganIdList())) {
            queryInfo.setOrganIdList(employee.getOrganIdList());
        }else if(org.apache.commons.lang3.StringUtils.isEmpty(employee.getOrganIdList())){
            return failed("用户所在分部异常");
        }else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if(!list.containsAll(Arrays.asList(queryInfo.getOrganIdList().split(",")))){
                return failed("非法请求");
            }
        }
        return succeed(indexErrDataRecordService.queryHistoryErrCourseData(queryInfo));
    }
}
