package cn.com.tenlion.systembase.controller.app.api.school;

import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO;
import cn.com.tenlion.systembase.pojo.vos.school.SchoolVO;
import cn.com.tenlion.systembase.service.school.ISchoolService;
import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SchoolAppController
 * @Description: 学校
 * @Author: CodeFactory
 * @Date: 2021-10-12 17:11:37
 * @Version: 3.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "学校接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/school")
public class SchoolAppController extends DefaultBaseController {

    @Autowired
    private ISchoolService schoolService;

    @ApiOperation(value = "新增学校", notes = "新增学校接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestHeader("token") String token, @RequestBody SchoolVO schoolVO) {
        schoolService.save(token, schoolVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除学校(id列表)", notes = "删除学校(id列表)接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
        @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@RequestHeader("token") String token, @PathVariable("ids") String ids) {
        schoolService.remove(token, Arrays.asList(ids.split("\\_")), ids);
        return new SuccessResult();
    }

    @ApiOperation(value = "修改学校", notes = "修改学校接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
        @ApiImplicitParam(name = "schoolId", value = "学校ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updateschool/{schoolId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateSchool(@RequestHeader("token") String token, @PathVariable("schoolId") String schoolId, @RequestBody SchoolVO schoolVO) {
        schoolService.update(token, schoolId, schoolVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "学校详情(通过ID)", notes = "学校详情(通过ID)接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
        @ApiImplicitParam(name = "schoolId", value = "学校ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{schoolId}")
    public SchoolDTO get(@RequestHeader("token") String token, @PathVariable("schoolId") String schoolId) {
        return schoolService.get(schoolId);
    }

    @ApiOperation(value = "学校列表", notes = "学校列表接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<SchoolDTO> list(@RequestHeader("token") String token) {
        Map<String, Object> params = requestParams();
        return schoolService.list(params);
    }

    @ApiOperation(value = "学校分页列表", notes = "学校分页列表接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
        @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
        @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
        @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpageschool")
    public SuccessResultList<List<SchoolDTO>> listPage(@RequestHeader("token") String token, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return schoolService.listPage(page);
    }

    @ApiOperation(value = "学校统计", notes = "学校统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        return new SuccessResultData<>(schoolService.count(params));
    }

}