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

import cn.hutool.core.util.ObjectUtil;
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.userLeave.EduLeaveRequestUser;
import com.yida.data.school.dto.userLeave.AuditRequestDTO;
import com.yida.data.school.dto.userLeave.LeaveRequestPageDTO;
import com.yida.data.school.userLeave.service.EduLeaveRequestUserService;
import com.yida.data.school.vo.userLeave.EduLeaveRequestStatisticsVO;
import com.yida.data.school.vo.userLeave.EduLeaveRequestUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * Controller
 *
 * @author wjm
 * @date 2022-12-01 10:51:19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("eduLeaveRequestUser")
@RequiredArgsConstructor
@Api(tags = "请假管理")
public class EduLeaveRequestUserController {

    private final EduLeaveRequestUserService eduLeaveRequestUserService;

    @ApiOperation("查询请假统计信息")
    @PostMapping("selectLeaveRequestStatistics")
    public ResultBean<EduLeaveRequestStatisticsVO> selectLeaveRequestStatistics() {
        EduLeaveRequestStatisticsVO vo = eduLeaveRequestUserService.selectLeaveRequestStatistics();
        return ResultBean.buildSuccess(vo);
    }

    @ApiOperation("查询请假记录列表")
    @PostMapping("findPageList")
    public ResultBean<Page<EduLeaveRequestUser>> findPageList(@RequestBody LeaveRequestPageDTO dto) {
        List<Integer> statusList = new ArrayList<>();
        if (dto.getStatus() == 4) {
            statusList.add(1);
            statusList.add(2);
        } else {
            statusList.add(dto.getStatus());
        }
        dto.setStatusList(statusList);
        return ResultBean
                .buildSuccess(
                        eduLeaveRequestUserService.findPageList(dto));
    }

    @ApiOperation("新增请假")
    @PostMapping("add")
    public ResultBean addLeave(@RequestBody EduLeaveRequestUser leaveRequestUser) {
        //判断是否包含表情
        if (EmojiManager.containsEmoji(leaveRequestUser.getRequestReason())) {
            return ResultBean.buildError("请假理由中不能包含表情，请重新输入请假理由");
        }
        eduLeaveRequestUserService.addLeaveRequest(leaveRequestUser);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("请假审核")
    @PostMapping("audit")
    public ResultBean auditRequest(@RequestBody AuditRequestDTO dto) {
        eduLeaveRequestUserService.auditRequest(dto);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("获取单条请假详情")
    @GetMapping("get")
    public ResultBean<EduLeaveRequestUserVO> getRequest(@RequestParam(required = true) @ApiParam("请假主体ID") Long requestId) {
        return ResultBean.buildSuccess(eduLeaveRequestUserService.getRequest(requestId));
    }


    @ApiOperation("我申请的请假条")
    @GetMapping("findUserLeaveList")
    public ResultBean<Page<EduLeaveRequestUser>> findUserLeaveList(
            @RequestParam(required = true) @ApiParam("学生id") Long studentId,
            @RequestParam(required = true) @ApiParam("请假类型：0教师请假，1学生请假") Integer requestType,
            @RequestParam(required = true) @ApiParam("所属学校ID") Long schoolId,
            @RequestParam(required = false) @ApiParam("0为未审核，空为全部，1为已通过，2为未通过，3审核中，4已失效,5全部") Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        LeaveRequestPageDTO dto = new LeaveRequestPageDTO();
        dto.setStudentIdList(Collections.singletonList(studentId));
        dto.setRequestType(requestType);
        dto.setSchoolId(schoolId);
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);

        if (ObjectUtil.isNotNull(status)) {
            dto.setStatusList(Collections.singletonList(status));
        }

        return ResultBean.buildSuccess(eduLeaveRequestUserService.findPageList(dto));
    }


    @ApiOperation("我审批的请假条")
    @GetMapping("findApprovalUserLeaveList")
    public ResultBean<Page<EduLeaveRequestUser>> findApprovalUserLeaveList(
            @RequestParam(required = true) @ApiParam("教师ID") Long userId,
            @RequestParam(required = false) @ApiParam("请假类型：0教师请假，1学生请假") Integer requestType,
            @RequestParam(required = false) @ApiParam("状态，0未处理，1已处理") Integer currentUserStatus,
            @RequestParam(required = true) @ApiParam("所属学校ID") Long schoolId,
            @RequestParam(required = false) @ApiParam("0未审核，1已通过，2未通过，3审核中，4已失效, 5全部") Integer status,
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize) {
        Page<EduLeaveRequestUser> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

//        List<Integer> statusList = new ArrayList<>();
//        if (ObjectUtil.isNotNull(status)) {
//            if (status == 5) {
//                statusList.add(0);
//                statusList.add(1);
//                statusList.add(2);
//                statusList.add(3);
//                statusList.add(4);
//            } else {
//                statusList = Collections.singletonList(status);
//            }
//        } else {
//            if(ObjectUtil.isNotNull(currentUserStatus)){
//                if (currentUserStatus == 0) {
//                    //待处理
//                    statusList.add(0);
//                    statusList.add(3);
//                } else if (currentUserStatus == 1) {
//                    statusList = null;
//                }
//            }else {
//                statusList = null;
//            }
//        }

        return ResultBean
                .buildSuccess(
                        eduLeaveRequestUserService
                                .findApprovalUserLeaveList(page, userId, requestType, schoolId, null, currentUserStatus));
    }

    @ApiOperation("测试模板消息")
    @GetMapping("testTamplateCardNotice")
    public ResultBean testTamplateCardNotice(String wxId, Long schoolId) {
        eduLeaveRequestUserService.testTamplateCardNotice(wxId, schoolId);
        return ResultBean.buildSuccess();
    }


}
