package com.yida.data.school.facility.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.school.EduDormitory;
import com.yida.data.common.core.entity.system.enums.DeptTypeEnum;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.index.PageDormDTO;
import com.yida.data.school.facility.service.EduDormitoryRoomService;
import com.yida.data.school.facility.service.EduDormitoryService;
import com.yida.data.school.vo.index.DormInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宿舍楼 Controller
 *
 * @author wjm
 * @date 2021-09-14 17:42:11
 */
@Api(tags = "宿舍-宿舍楼")
@Slf4j
@Validated
@RestController
@RequestMapping("/facility/dorm")
@RequiredArgsConstructor
public class EduDormitoryController {

    private final EduDormitoryService eduDormitoryService;
    private final EduDormitoryRoomService eduDormitoryRoomService;


    @ApiOperation("根据宿管id查询宿舍楼")
    @GetMapping("/listDormByMaster")
    public ResultBean listDormByMaster(@ApiParam("宿管id") Long masterId) {
        return ResultBean.buildSuccess(eduDormitoryService.listDormByMaster(masterId));
    }

    @ApiOperation("根据学校查询宿舍楼")
    @GetMapping("/listDormBySchool")
    ResultBean<List<EduDormitory>> listDormBySchool(@ApiParam("学校id") @RequestParam(required = false) Long schoolId) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                schoolId = currentUser.getDeptId();
            }
        }
        LambdaQueryWrapper<EduDormitory> condition = Wrappers.<EduDormitory>lambdaQuery();
        if (schoolId != null) {
            condition.eq(EduDormitory::getSchoolId, schoolId);
        }
        return ResultBean.buildSuccess(eduDormitoryService.list(condition));
    }

    @ApiOperation("分页查询宿舍楼")
    @GetMapping("/listDormPage")
    public ResultBean<IPage<DormInfoVO>> listDormPage(PageDormDTO dto) {
        if (dto.getSchoolId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (DeptTypeEnum.SCHOOL_TYPE.getValue().equals(currentUser.getDeptType())) {
                dto.setSchoolId(currentUser.getDeptId());
            } else {
                throw new FebsException("请选择学校");
            }
        }
        return ResultBean.buildSuccess(eduDormitoryService.listDormPage(dto));
    }

    @ApiOperation("保存宿舍楼")
    @PostMapping("/saveDorm")
    public ResultBean saveDorm(@RequestBody EduDormitory dorm) {
        eduDormitoryService.saveDorm(dorm);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除宿舍楼")
    @PostMapping("/delDormBatch")
    public ResultBean delDormBatch(@RequestBody List<Long> ids) {
        eduDormitoryService.delDormBatch(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据名称查询寝室楼")
    @GetMapping("/getDormByName")
    ResultBean<EduDormitory> getDormByName(@RequestParam("name") String name,
                                           @RequestParam("schoolId") Long schoolId) {
        return ResultBean.buildSuccess(eduDormitoryService.getOne(Wrappers.<EduDormitory>lambdaQuery()
                .eq(EduDormitory::getSchoolId, schoolId)
                .eq(EduDormitory::getName, name)));
    }

    @ApiOperation("获取宿舍楼")
    @GetMapping("/getDorm")
    ResultBean<EduDormitory> getDorm(@RequestParam("id") Long id) {
        return ResultBean.buildSuccess(eduDormitoryService.getDorm(id));
    }
}
