package com.qmth.distributed.print.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.teachcloud.common.bean.params.BasicCampusParams;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.service.BasicCampusService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 基础校区表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-08-02
 */
@Api(tags = "校区管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.basic}/campus")
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class BasicCampusController {
    @Resource
    private BasicCampusService basicCampusService;

    @ApiOperation(value = "校区管理-查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findBasicCampusList() {
        Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
        return ResultUtil.ok(basicCampusService.basicCampusList(schoolId));
    }

    @ApiOperation(value = "校区管理-新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicCampus(@Valid @RequestBody BasicCampusParams basicCampusParams, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(basicCampusService.saveBasicCampus(basicCampusParams));
    }

    @ApiOperation(value = "校区管理-删除（逻辑）")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteBasicCampus(@ApiParam(value = "校区id", required = true) @RequestParam String id) {
        return ResultUtil.ok(basicCampusService.removeBasicCampusById(SystemConstant.convertIdToLong(id)));
    }

}
