package com.ym.mec.web.controller.education;

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.enums.UpdateAttendanceEnum;
import com.ym.mec.biz.dal.page.TeacherAttendanceComplaintsQueryInfo;
import com.ym.mec.biz.service.TeacherAttendanceService;
import com.ym.mec.common.controller.BaseController;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;

@RequestMapping("eduTeacherAttendance")
@Api(tags = "教师考勤服务")
@RestController
public class EduTeacherAttendanceController extends BaseController {

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

    @ApiOperation(value = "获取教师考勤申述列表")
    @PostMapping("/queryTeacherAttendanceComplaints")
    public Object queryTeacherAttendanceComplaints(TeacherAttendanceComplaintsQueryInfo 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("非法请求");
            }
        }
        return succeed(teacherAttendanceService.queryTeacherAttendanceComplaints(queryInfo));
    }

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

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

    @ApiOperation(value = "提交考勤申述")
    @PostMapping("/addComplaints")
    public Object addComplaints(Long courseScheduleId,String content,String url,Integer userId, UpdateAttendanceEnum complaintsType){
        teacherAttendanceService.addComplaints(courseScheduleId,content,url,userId,complaintsType);
        return succeed();
    }

    @ApiOperation(value = "撤销考勤申述")
    @PostMapping("/repealComplaints")
    public Object repealComplaints(Long courseScheduleId,Integer userId){
        teacherAttendanceService.repealComplaints(courseScheduleId,userId);
        return succeed();
    }
}
