package com.openhis.administration.service.impl;

import java.util.List;

import com.openhis.administration.dto.LocationDataDto;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.administration.domain.Location;
import com.openhis.administration.mapper.LocationMapper;
import com.openhis.administration.service.ILocationService;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.LocationForm;
import com.openhis.common.enums.LocationStatus;

import javax.annotation.Resource;

/**
 * 位置管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements ILocationService {
    @Resource
    private LocationMapper locationMapper;

    /**
     * 获取药房列表
     *
     * @return 药房列表
     */
    @Override
    public List<Location> getPharmacyList() {
        return baseMapper
            .selectList(new LambdaQueryWrapper<Location>().eq(Location::getFormEnum, LocationForm.PHARMACY.getValue()));
    }

    /**
     * 获取药库列表
     *
     * @return 药库列表
     */
    @Override
    public List<Location> getCabinetList() {
        return baseMapper
            .selectList(new LambdaQueryWrapper<Location>().eq(Location::getFormEnum, LocationForm.CABINET.getValue()));
    }
    /**
     * 获取仓库列表
     *
     * @return 仓库列表
     */
    @Override
    public List<Location> getWarehouseList() {
        return baseMapper
            .selectList(new LambdaQueryWrapper<Location>().eq(Location::getFormEnum, LocationForm.WAREHOUSE.getValue()));
    }

    /**
     * 获取药房药库列表
     *
     * @return 药房药库列表
     */
    @Override
    public List<Location> getPharmacyCabinetList() {
        return baseMapper.selectList(new LambdaQueryWrapper<Location>().in(Location::getFormEnum,
            LocationForm.CABINET.getValue(), LocationForm.PHARMACY.getValue(), LocationForm.WAREHOUSE.getValue()));
    }

    /**
     * 依据id查询
     *
     * @param id
     * @return
     */
    public Location getLocationById(Long id) {

        return baseMapper.selectById(id);

    }

    /**
     * 获取科室对应的病区列表
     *
     * @param id 科室id
     * @return 病区列表
     */
    @Override
    public List<Location> getWardList(Long id) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Location::getId, Location::getName)
            .eq(Location::getStatusEnum, LocationStatus.ACTIVE.getValue())
            .eq(Location::getFormEnum, LocationForm.WARD.getValue());
        if (id != null) {
            queryWrapper.eq(Location::getOrganizationId, id);
        }
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 获取病区下对应的病床列表
     *
     * @param id 病区id
     * @return 病床列表
     */
    @Override
    public List<Location> getBedList(Long id) {
        //
        // Location location = baseMapper.selectById(id);
        // if (location == null || location.getBusNo() == null) {
        // return null;
        // }
        // LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        // queryWrapper.select(Location::getId, Location::getName) // 只查询 id 和 name 字段
        // .likeRight(Location::getBusNo, location.getBusNo()) // 查询病区busNo开头的
        // .eq(Location::getFormEnum, LocationForm.BED.getValue()) // form 值为 8:病床
        // .eq(Location::getOperationalEnum, LocationBedStatus.U.getValue());// 空闲状态

        // 查询满足条件的 Location 对象列表
        return null;
    }

    /**
     * 新增位置信息
     *
     * @param location 位置信息
     * @return 是否成功
     */
    @Override
    public boolean addLocation(Location location) {
        return baseMapper.insert(location) > 0;
    }

    /**
     * 编辑位置信息
     *
     * @param location 位置信息
     * @return 是否成功
     */
    @Override
    public boolean updateLocation(Location location) {
        return baseMapper.update(location,
            new LambdaUpdateWrapper<Location>().eq(Location::getId, location.getId())) > 0;
    }

    /**
     * 根据科室ID集合，查询对应信息
     *
     * @param ids 科室ID集合
     * @return 科室信息列表
     */
    @Override
    public List<Location> getLocationList(List<Long> ids) {
        // // 如果 ids 是空的，获取当前登录用户的 orgId
        // if (ids == null || ids.isEmpty()) {
        // Long orgId = SecurityUtils.getLoginUser().getOrgId();
        // if (orgId != null) {
        // ids = Collections.singletonList(orgId); // 将 orgId 转换为单元素列表
        // }
        // }
        // 创建查询条件
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Location::getId, Location::getName, Location::getFormEnum, Location::getBusNo)
            .in(Location::getId, ids).eq(Location::getDeleteFlag, DelFlag.NO.getCode())
            .eq(Location::getStatusEnum, LocationStatus.ACTIVE.getValue());
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据locationId，更新状态
     *
     * @param id 位置Id
     * @param status 操作状态
     * @return 是否成功
     */
    @Override
    public boolean updateStatusById(Long id, Integer status) {
        Location location = new Location();
        location.setId(id);
        location.setStatusEnum(status);
        return baseMapper.updateById(location) > 0;
    }

    @Override
    public List<LocationDataDto> searchLocationDataByIds(List<Long> locationIds) {
        return this.locationMapper.searchLocationDataByIds(locationIds);
    }

}