package com.koobietech.eas.controller;

import com.github.pagehelper.PageHelper;
import com.koobietech.eas.common.result.JsonPageResult;
import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.mbg.model.EasArcTlsAttendance;
import com.koobietech.eas.service.EasArcTlsAttendanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Tag(name = "签到模块")
@RestController
@RequestMapping("/attendance")
public class EasArcTlsAttendanceController {

    @Resource
    private EasArcTlsAttendanceService easArcTlsAttendanceService;

    @GetMapping("/getStudentList")
    @Operation(summary = "获取学生列表", description = "签到的时候会根据课表ID来获取学生列表（scheduleId）")
    public JsonResult getStudentList(@RequestParam Long scheduleId) {
        return JsonResult.data(easArcTlsAttendanceService.getStudentList(scheduleId));
    }

    @PostMapping("/query")
    @Operation(summary = "查询签到记录", description = "前端构造查询条件，可以都为空，根据参数查询签到表信息（调用了分页组件）")
    public JsonPageResult query(@RequestBody EasArcTlsAttendance attendance,
                                @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageDataResult ret = easArcTlsAttendanceService.query(attendance);
        return JsonPageResult.data(ret);
    }

    @PostMapping("/add")
    @Operation(summary = "添加签到记录", description = "用于添加签到信息")
    public JsonResult add(@RequestBody EasArcTlsAttendance attendance) {
        //增加的时候，加一个判断，如果已经存在，就转到更新方法（“/update”）
        boolean isExist = easArcTlsAttendanceService.isExist(attendance);

        if (isExist) {
            System.out.println("！！！！！已经存在，转到更新方法");
            return update(attendance);
        }else{
            System.out.println("！！！！！不存在，正在执行添加方法");
            boolean isAdded = easArcTlsAttendanceService.add(attendance);
            if (isAdded) {
                return JsonResult.ok("添加签到记录成功");
            }
            return JsonResult.fail("添加签到记录失败");
        }
    }



    @DeleteMapping("/delete")
    @Operation(summary = "删除签到记录", description = "删除签到记录数据根据时间和学员student_number")
    public JsonResult delete(@RequestBody EasArcTlsAttendance attendance) {
        Boolean ret = easArcTlsAttendanceService.delete(attendance);
        if (ret) {
            return JsonResult.ok("删除签到记录成功");
        }
        return JsonResult.fail("删除签到记录失败");
    }


    @PutMapping("/update")
    @Operation(summary = "更新签到记录", description = "更新签到记录")
    public JsonResult update(@RequestBody EasArcTlsAttendance attendance) {
        Boolean ret = easArcTlsAttendanceService.update(attendance);
        if (ret) {
            return JsonResult.ok("更新签到记录成功");
        }
        return JsonResult.fail("更新签到记录失败");
    }
}
