package com.sdut.labex.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdut.labex.Factory.SubmitRecordsFactory;
import com.sdut.labex.entity.SubmitRecord;
import com.sdut.labex.service.SubmitRecordsService;
import com.sdut.labex.utils.ResVo;
import com.sdut.labex.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * File: SubmitRecords
 * Created: 2023/6/21
 * Author: springforest
 * Description:
 */
@RestController
@CrossOrigin
@Slf4j
public class SubmitRecordsController {
    @Resource
    private SubmitRecordsService submitRecordsService;

    @PostMapping("/records")
    public ResVo addRecord(@RequestBody JSONObject jsonObject) {
        SubmitRecord submitRecord = SubmitRecordsFactory.create(jsonObject);
        return submitRecordsService.addRecords(submitRecord);
    }

    @PostMapping("/getRecordST/{pageNum}/{pageSize}")
    public ResVo getRecordST(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody JSONObject jsonObject) {
        String className = jsonObject.getString("className");
        String stId = jsonObject.getString("studentId");
        String applyUser = jsonObject.getString("applyUser");
        if (UserHolder.getUser().getRole().equals("student")) {
            className = UserHolder.getUser().getClassName();
            stId = UserHolder.getUser().getId();
        } else if (UserHolder.getUser().getRole().equals("teacher")) {
            applyUser = UserHolder.getUser().getUsername();
        }

        return submitRecordsService.selectRecords(pageNum, pageSize, className, stId, applyUser);
    }

    @DeleteMapping("/records/{id}")
    public ResVo deleteRecord(@PathVariable Integer id) {
        return submitRecordsService.deleteRecord(id);
    }

    @PutMapping("/records")
    public ResVo updateRecord(@RequestBody JSONObject jsonObject) {
        log.info("updateRecords:{}", jsonObject);
        SubmitRecord submitRecord = SubmitRecordsFactory.create(jsonObject);
        return submitRecordsService.updateRecords(submitRecord);
    }

}
