package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.TeacherAttendance;
import com.ym.mec.biz.dal.page.TeacherAttendanceComplaintsQueryInfo;
import com.ym.mec.biz.dal.page.TeacherCloseQueryInfo;
import com.ym.mec.biz.service.TeacherAttendanceService;
import com.ym.mec.common.controller.BaseController;

@RequestMapping("teacherAttendance")
@Api(tags = "教师签到服务")
@RestController
public class TeacherAttendanceController extends BaseController {

    @Autowired
    private TeacherAttendanceService teacherAttendanceService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    @ApiOperation(value = "查询教师签到（上课）记录")
    @GetMapping("/getTeacherPersonalAttendances")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/getTeacherPersonalAttendances')")
    public Object getTeacherPersonalAttendances(TeacherCloseQueryInfo queryInfo){
        return succeed(teacherAttendanceService.getTeacherPersonalAttendances(queryInfo));
    }

    @ApiOperation(value = "获取教师考勤记录")
    @PostMapping("/queryTeacherAttendances")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/queryTeacherAttendances')")
    public Object queryTeacherAttendances(TeacherCloseQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            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("非法请求");
                }
            }
        }
        return succeed(teacherAttendanceService.queryTeacherAttendances(queryInfo));
    }

    @ApiOperation(value = "查询某节课教师签到记录")
    @GetMapping("/getTeacherPersonalAttendance")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/getTeacherPersonalAttendance')")
    public Object getTeacherPersonalAttendances(Integer courseScheduleId){
        return succeed(teacherAttendanceService.getTeacherPersonalAttendance(courseScheduleId));
    }

    @ApiOperation(value = "获取教师签到详情")
    @GetMapping("/getTeacherPersonalAttendanceDetail")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/getTeacherPersonalAttendanceDetail')")
    public Object getTeacherPersonalAttendanceDetail(Integer courseScheduleId){
        return succeed(teacherAttendanceService.getTeacherPersonalAttendanceDetail(courseScheduleId));
    }

    @ApiOperation(value = "更新教师签到记录")
    @PostMapping("/updateTeacherAttendance")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/updateTeacherAttendance')")
    public Object updateTeacherAttendance(TeacherAttendance teacherAttendance){
        teacherAttendanceService.updateTeacherAttendance(teacherAttendance);
        return succeed();
    }

    @ApiOperation(value = "获取教师考勤申述列表")
    @PostMapping("/queryTeacherAttendanceComplaints")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/queryTeacherAttendanceComplaints')")
    public Object queryTeacherAttendanceComplaints(TeacherAttendanceComplaintsQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            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("非法请求");
                }
            }
        }
        return succeed(teacherAttendanceService.queryTeacherAttendanceComplaints(queryInfo));
    }

    @ApiOperation(value = "同意教师异常考勤申述")
    @PostMapping("/agreeTeacherAttendanceComplaints")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/agreeTeacherAttendanceComplaints')")
    public Object agreeTeacherAttendanceComplaints(Integer signInStatus, Integer signOutStatus, long teacherAttendanceId, String content){
        teacherAttendanceService.agreeTeacherAttendanceComplaints(signInStatus,signOutStatus,teacherAttendanceId,content);
        return succeed();
    }

    @ApiOperation(value = "拒绝教师异常考勤申述")
    @PostMapping("/rejectTeacherAttendanceComplaints")
    @PreAuthorize("@pcs.hasPermissions('teacherAttendance/rejectTeacherAttendanceComplaints')")
    public Object rejectTeacherAttendanceComplaints(long teacherAttendanceId,String content){
        teacherAttendanceService.rejectTeacherAttendanceComplaints(teacherAttendanceId,content);
        return succeed();
    }
}
