package com.sqx.modules.member.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.Result;
import com.sqx.modules.member.dto.SpecialFavorStudentDTO;
import com.sqx.modules.member.query.SpecialFavorStudentQuery;
import com.sqx.modules.member.service.SpecialFavorStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

@Api(tags = "特殊照顾学生")
@RestController
@RequestMapping("admin/special-favor-student")
@RequiredArgsConstructor
public class SpecialFavorStudentController{
    
    private final SpecialFavorStudentService specialFavorStudentService;

    @GetMapping("pages")
    @ApiOperation("分页查询特殊照顾学生列表")
    public Result pages(@Valid SpecialFavorStudentQuery query) {
        return Result.success().put("data", specialFavorStudentService.pages(query));
    }

    @PostMapping
    @ApiOperation("添加特殊照顾学生")
    public Result add(@RequestBody @Valid SpecialFavorStudentDTO dto) {
        specialFavorStudentService.save(dto);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改特殊照顾学生")
    public Result update(@RequestBody @Valid SpecialFavorStudentDTO dto) {
        if (ObjectUtil.isNull(dto.getId())) {
            throw new SqxException("id不能为空");
        }
        specialFavorStudentService.update(dto);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除特殊照顾学生")
    public Result delete(@RequestBody @ApiParam("id的集合") Long[] ids) {
        specialFavorStudentService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    @PostMapping("import")
    @ApiOperation("导入特殊照顾学生")
    public Result importData(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new SqxException("文件不能为空");
        }

        specialFavorStudentService.importData(file);
        return Result.success();
    }
}
