package com.openhis.web.appointmentmanage.appservice.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.openhis.appointmentmanage.domain.ClinicRoom;
import com.openhis.appointmentmanage.service.IClinicRoomService;
import com.openhis.web.appointmentmanage.appservice.IClinicRoomAppService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ClinicRoomAppServiceImpl implements IClinicRoomAppService {

    @Resource
    private IClinicRoomService clinicRoomService;

    @Override
    public R<?> selectClinicRoomPage(Integer pageNum, Integer pageSize, String orgName, String roomName) {
        // 构建查询条件
        ClinicRoom clinicRoom = new ClinicRoom();
        if (orgName != null && ObjectUtil.isNotEmpty(orgName)) {
            clinicRoom.setOrgName(orgName);
        }
        if (roomName != null && ObjectUtil.isNotEmpty(roomName)) {
            clinicRoom.setRoomName(roomName);
        }
        
        // 分页查询
        Page<ClinicRoom> page = new Page<>(pageNum, pageSize);
        Page<ClinicRoom> result = clinicRoomService.selectClinicRoomPage(page, clinicRoom);
        
        return R.ok(result);
    }

    @Override
    public R<?> selectClinicRoomById(Long id) {
        ClinicRoom clinicRoom = clinicRoomService.selectClinicRoomById(id);
        if (clinicRoom == null) {
            return R.fail(404, "诊室不存在");
        }
        return R.ok(clinicRoom);
    }

    @Override
    public R<?> insertClinicRoom(ClinicRoom clinicRoom) {
        // 数据校验
        if (ObjectUtil.isEmpty(clinicRoom.getRoomName())) {
            return R.fail(400, "诊室名称不能为空");
        }
        if (ObjectUtil.isEmpty(clinicRoom.getDepartment())) {
            return R.fail(400, "科室名称不能为空");
        }
        if (clinicRoom.getRoomName().length() > 50) {
            return R.fail(400, "诊室名称长度不能超过50个字符");
        }
        if (clinicRoom.getRemarks() != null && clinicRoom.getRemarks().length() > 500) {
            return R.fail(400, "备注长度不能超过500个字符");
        }
        
        // 新增诊室
        int result = clinicRoomService.insertClinicRoom(clinicRoom);
        if (result > 0) {
            return R.ok(null, "新增成功");
        } else {
            return R.fail("新增失败");
        }
    }

    @Override
    public R<?> updateClinicRoom(ClinicRoom clinicRoom) {
        // 数据校验
        if (ObjectUtil.isEmpty(clinicRoom.getId())) {
            return R.fail(400, "诊室ID不能为空");
        }
        if (ObjectUtil.isEmpty(clinicRoom.getRoomName())) {
            return R.fail(400, "诊室名称不能为空");
        }
        if (ObjectUtil.isEmpty(clinicRoom.getDepartment())) {
            return R.fail(400, "科室名称不能为空");
        }
        if (clinicRoom.getRoomName().length() > 50) {
            return R.fail(400, "诊室名称长度不能超过50个字符");
        }
        if (clinicRoom.getRemarks() != null && clinicRoom.getRemarks().length() > 500) {
            return R.fail(400, "备注长度不能超过500个字符");
        }
        
        // 检查诊室是否存在
        ClinicRoom existingClinicRoom = clinicRoomService.selectClinicRoomById(clinicRoom.getId());
        if (existingClinicRoom == null) {
            return R.fail(404, "诊室不存在");
        }
        
        // 更新诊室
        int result = clinicRoomService.updateClinicRoom(clinicRoom);
        if (result > 0) {
            return R.ok(null, "修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @Override
    public R<?> deleteClinicRoomById(Long id) {
        // 检查诊室是否存在
        ClinicRoom existingClinicRoom = clinicRoomService.selectClinicRoomById(id);
        if (existingClinicRoom == null) {
            return R.fail(404, "诊室不存在");
        }
        
        // 删除诊室
        int result = clinicRoomService.deleteClinicRoomById(id);
        if (result > 0) {
            return R.ok(null, "删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
}
