package com.yida.data.school.leave.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vdurmont.emoji.EmojiManager;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.leave.EduLeaveRequest;
import com.yida.data.school.leave.service.EduLeaveRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 请假表 Controller
 *
 * @author ccl
 * @date 2021-11-22 16:27:42
 */
@Slf4j
@Validated
@RestController
@RequestMapping("request/app/leaveRequest")
@AllArgsConstructor
@Api(tags = "请假条-app-请假管理")
public class EduLeaveRequestController {


    private final EduLeaveRequestService eduLeaveRequestService;


    @ApiOperation("家长新增请假条")
    @PostMapping("saveOrUpLeave")
    public ResultBean saveOrUpLeave(@RequestBody EduLeaveRequest leaveRequest) {
        //判断是否包含表情
        if (EmojiManager.containsEmoji(leaveRequest.getReason())) {
            return ResultBean.buildError("请假理由中不能包含表情，请重试");
        }
        eduLeaveRequestService.saveOrUpLeave(leaveRequest);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("家长查询对应学生请假条列表")
    @GetMapping("findLeaveList")
    public ResultBean<Page<EduLeaveRequest>> findLeaveList(@RequestParam(required = false) @ApiParam("学生id") Long studentId,
                                                           @RequestParam(required = false) @ApiParam("家长id") Long parentId,
                                                           @RequestParam(required = false) @ApiParam("0为未审核，空为全部，1为已通过，2为未通过，3为已失效") Integer status,
                                                           @RequestParam(defaultValue = "1") Long pageNum,
                                                           @RequestParam(defaultValue = "10") Long pageSize) {
        Page<EduLeaveRequest> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        return ResultBean.buildSuccess(eduLeaveRequestService.findLeaveList(parentId,studentId, page, status));
    }

    @ApiOperation("教师查询对应学生请假条列表")
    @GetMapping("findDeptLeaveList")
    public ResultBean<Page<EduLeaveRequest>> findDeptLeaveList(@RequestParam(required = false) @ApiParam("部门id") Long deptId,
                                                               @RequestParam(required = false) @ApiParam("教师id") Long teacherId,
                                                               @RequestParam(required = false) @ApiParam("0为未审核，空为全部，1为已通过，2为未通过，3为已失效，4为已审核") Integer status,
                                                               @RequestParam(required = false) @ApiParam("学生名字") String studentName,
                                                               @RequestParam(defaultValue = "1") Long pageNum,
                                                               @RequestParam(defaultValue = "10") Long pageSize) {

        Page<EduLeaveRequest> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        return ResultBean.buildSuccess(eduLeaveRequestService.findDeptLeaveList(deptId, teacherId, status, page, studentName));
    }

    @ApiOperation("教师审核请假条")
    @GetMapping("updateLeave")
    public ResultBean updateLeave(@RequestParam @ApiParam("请假条id") Long requestId,
                                  @RequestParam @ApiParam("1通过，2拒绝") Integer status,
                                  @RequestParam @ApiParam("学校id") Long schoolId) {
        eduLeaveRequestService.updateLeave(requestId, status, schoolId);
        return ResultBean.buildSuccess();
    }

}
