package com.hhh.cloud.framework.demo.clazz.controller;


import com.alibaba.fastjson.JSONObject;
import com.hhh.cloud.framework.demo.clazz.entity.ClazzStudent;
import com.hhh.cloud.framework.demo.clazz.model.StudentSaveVo;
import com.hhh.cloud.framework.demo.clazz.model.StudentUpdateVo;
import com.hhh.cloud.framework.demo.clazz.service.IClazzStudentService;
import com.hhh.cloud.framework.util.CommonUtil;
import com.hhh.cloud.framework.util.enumeration.CodeEnum;
import com.hhh.cloud.framework.util.result.Result;
import com.hhh.cloud.framework.util.result.TableResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 3hhjs
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/clazzStudent")
public class ClazzStudentController {

    @Autowired
    private IClazzStudentService clazzStudentService;

    /**
     * 新增学生信息
     * @param student 学生信息
     * @return Result<Boolean>
     */
    @PostMapping("/addStudent")
    public Result<Boolean> addStudent(@RequestBody @Valid StudentSaveVo student) {
        ClazzStudent clazzStudent = new ClazzStudent();
        BeanUtils.copyProperties(student, clazzStudent);
        clazzStudent.setId(CommonUtil.GUID());
        return new Result<>(CodeEnum.SUCCESS.get(), clazzStudentService.save(clazzStudent), "");
    }

    /**
     * 修改学生信息
     * @param student 学生信息
     * @return Result<Boolean>
     */
    @PostMapping("updateStudent")
    public Result<Boolean> updateStudent(@RequestBody @Valid StudentUpdateVo student) {
        ClazzStudent clazzStudent = clazzStudentService.getById(student.getId());
        BeanUtils.copyProperties(student, clazzStudent);
        return new Result<>(CodeEnum.SUCCESS.get(), clazzStudentService.updateById(clazzStudent), "");
    }

    @PostMapping("deleteStudent")
    public Result<Boolean> deleteStudent(@RequestBody JSONObject jsonObject) {
        return new Result<>(CodeEnum.SUCCESS.get(), clazzStudentService.removeById(jsonObject.getString("id")), "");
    }

    @PostMapping("checkStudent")
    public Result<Boolean> checkStudent(@RequestBody JSONObject jsonObject) {
        boolean exists = clazzStudentService.existsStudent(jsonObject.getString("name"));
        return exists ? new Result<>(CodeEnum.BUSINESS_ERROR.get(), true, "学生已存在") : new Result<>(CodeEnum.SUCCESS.get(), false, "");
    }

    @PostMapping("getStudent")
    public Result<ClazzStudent> getStudent(@RequestBody JSONObject jsonObject) {
        return new Result<>(CodeEnum.SUCCESS.get(), clazzStudentService.getById(jsonObject.getString("id")), "");
    }

    @PostMapping("pageStudent")
    public TableResult<List<ClazzStudent>> pageStudent(@RequestBody JSONObject jsonObject) {
        ClazzStudent student = jsonObject.toJavaObject(ClazzStudent.class);
        int page = jsonObject.containsKey("page") ? jsonObject.getInteger("page") : 1;
        int limit = jsonObject.containsKey("limit") ? jsonObject.getInteger("limit") : 15;
        return clazzStudentService.pageStudent(student, page, limit);
    }

    @PostMapping("pageStudent2")
    public TableResult<List<ClazzStudent>> pageStudent2(@RequestBody JSONObject jsonObject) {
        ClazzStudent student = jsonObject.toJavaObject(ClazzStudent.class);
        int page = jsonObject.containsKey("page") ? jsonObject.getInteger("page") : 1;
        int limit = jsonObject.containsKey("limit") ? jsonObject.getInteger("limit") : 15;
        return clazzStudentService.pageStudent2(student, page, limit);
    }
}
