package com.keao.edu.user.controller;


import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.tenant.TenantContextHolder;
import com.keao.edu.user.entity.Subject;
import com.keao.edu.user.page.SubjectQueryInfo;
import com.keao.edu.user.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("subject")
@Api(tags = "科目服务")
@RestController
public class SubjectController extends BaseController {

    @Autowired
    private SubjectService subjectService;

    @ApiOperation(value = "修改、新增科目")
    @PostMapping("/upset")
    @PreAuthorize("@pcs.hasPermissions('subject/upset')")
    public HttpResponseResult update(@RequestBody Subject subject){
        subject.setTenantId(TenantContextHolder.getTenantId().toString());
        subjectService.upSetSubject(subject);
        return succeed();
    }

    @ApiOperation(value = "根据科目编号查询科目")
    @GetMapping("/get/{id}")
    @PreAuthorize("@pcs.hasPermissions('subject/get')")
    public HttpResponseResult<Subject> get(@ApiParam(value = "科目编号", required = true) @PathVariable("id") Integer id){
        return succeed(subjectService.get(id));
    }

    @ApiOperation(value = "分页查询科目列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('subject/queryPage')")
    public HttpResponseResult<PageInfo<Subject>> queryPage(SubjectQueryInfo queryInfo){
        return succeed(subjectService.queryPage(queryInfo));
    }

    @ApiOperation(value = "分页查询科目树状列表")
    @GetMapping("/queryPageTree")
    @PreAuthorize("@pcs.hasPermissions('subject/queryPageTree')")
    public HttpResponseResult<PageInfo<Subject>> queryPageTree(SubjectQueryInfo queryInfo){
        return succeed(subjectService.queryPageTree(queryInfo));
    }

    @ApiOperation(value = "获取子集科目列表")
    @GetMapping("/findSubSubjects")
    @PreAuthorize("@pcs.hasPermissions('subject/findSubSubjects')")
    public HttpResponseResult<List<Subject>> findSubSubjects(){
        return succeed(subjectService.findSubSubjects(Integer.parseInt(TenantContextHolder.getTenantId().toString())));
    }
}
