package com.ruoyi.school.room.service;

import com.ruoyi.school.course.domain.ScClaTime;
import com.ruoyi.school.course.service.IScClaTimeService;
import com.ruoyi.school.room.domain.req.ReqRoomSelect;
import com.ruoyi.school.room.domain.req.ReqSearchScRoom;
import com.ruoyi.school.room.domain.resp.RespRoomInfo;
import com.ruoyi.school.room.domain.resp.RespRoomSelect;
import com.ruoyi.school.room.mapper.ScRoomMapper;
import com.ruoyi.school.room.domain.ScRoom;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.page.RespPage;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.core.api.APIResponse;
import com.ruoyi.core.api.ApiResEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author ：zhangbaoyu
 * @date ：Created in 2020-01-14 17:24
 */
@Service
@Transactional
public class BusinessScRoomService {

    @Autowired
    private IScRoomService scRoomService;
    @Autowired
    private ScRoomMapper roomMapper;
    @Autowired
    private IScClaTimeService claTimeService;

    /**
     * 查询
     *
     * @param reqSearchScRoom
     * @return
     */
    public APIResponse searchList(ReqSearchScRoom reqSearchScRoom) {
        RespPage<RespRoomInfo> page = new RespPage<>(reqSearchScRoom.getPageNum(), reqSearchScRoom.getPageSize());
        List<RespRoomInfo> roomSelectList = roomMapper.selectList(reqSearchScRoom, page);
        page.setRows(roomSelectList);
        return APIResponse.toAPIResponse(page);
    }

    /**
     * 前端select
     *
     * @return
     */
    public APIResponse select(ReqRoomSelect roomSelect) {
        roomSelect.setTenantId(SecurityUtils.getLoginUser().getNowTenantId());
        List<RespRoomSelect> list = scRoomService.selectRoomSelect(roomSelect);
        return APIResponse.toAPIResponse(list);
    }

    /**
     * 详情
     *
     * @param roomId
     * @return
     */
    public APIResponse detailById(Long roomId) {
        if (null == roomId) {
            return APIResponse.toAPIResponse(null);
        }
        ScRoom detailInfo = scRoomService.getById(roomId);
        return APIResponse.toAPIResponse(detailInfo);
    }

    /**
     * 添加
     *
     * @param scRoom
     * @return
     */
    public APIResponse addScRoom(ScRoom scRoom) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        scRoom.setTenantId(SecurityUtils.getLoginUser().getNowTenantId());
        scRoom.setCreateUser(loginUser.getUserId());
        boolean addScRoom = scRoomService.save(scRoom);
        if (addScRoom) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    /**
     * 更新
     *
     * @param scRoom
     * @return
     */
    public APIResponse updateScRoom(ScRoom scRoom) {
        if (null == scRoom.getRoomId()) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        scRoom.setLastUpdateUser(loginUser.getUserId());
        scRoom.setLastUpdateTime(new Date());
        boolean updateScRoom = scRoomService.updateById(scRoom);
        if (updateScRoom) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    /**
     * 删除
     *
     * @param roomIds
     * @return
     */
    public APIResponse deleteById(Long[] roomIds) {
        if (null == roomIds || roomIds.length == 0) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }

        // 教室有 排课未上课 不允许删除
        QueryWrapper<ScClaTime> qw = new QueryWrapper<>();
        qw.in("room_id", roomIds);
        qw.eq("status", "1");
        int unBeginClaTimeCount = claTimeService.count(qw);
        if (unBeginClaTimeCount != 0) {
            return APIResponse.toExceptionResponse("该教室有'"+unBeginClaTimeCount+"'待上课的排课,无法删除教室");
        }

        boolean deleteScRoom = scRoomService.removeByIds(Arrays.asList(roomIds));
        if (deleteScRoom) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }
}
