package org.dromara.system.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.SysOss;
import org.dromara.system.mapper.SysOssMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysHospitalBo;
import org.dromara.system.domain.vo.SysHospitalVo;
import org.dromara.system.domain.SysHospital;
import org.dromara.system.mapper.SysHospitalMapper;
import org.dromara.system.service.ISysHospitalService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 医院管理Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysHospitalServiceImpl implements ISysHospitalService {

    private final SysHospitalMapper baseMapper;
    private final SysOssMapper ossMapper;

    @Override
    public SysHospitalVo getOneHospital() {
        return baseMapper.selectVoOne(Wrappers.lambdaQuery(SysHospital.class));
    }

    /**
     * 查询医院管理
     *
     * @param hospitalId 主键
     * @return 医院管理
     */
    @Override
    public SysHospitalVo queryById(Long hospitalId) {
        return baseMapper.selectVoById(hospitalId);
    }

    /**
     * 分页查询医院管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 医院管理分页列表
     */
    @Override
    public TableDataInfo<SysHospitalVo> queryPageList(SysHospitalBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysHospital> lqw = buildQueryWrapper(bo);
        Page<SysHospitalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的医院管理列表
     *
     * @param bo 查询条件
     * @return 医院管理列表
     */
    @Override
    public List<SysHospitalVo> queryList(SysHospitalBo bo) {
        LambdaQueryWrapper<SysHospital> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysHospital> buildQueryWrapper(SysHospitalBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysHospital> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SysHospital::getHospitalId);
        lqw.like(StringUtils.isNotBlank(bo.getHospitalName()), SysHospital::getHospitalName, bo.getHospitalName());
        lqw.eq(StringUtils.isNotBlank(bo.getHospitalLogo()), SysHospital::getHospitalLogo, bo.getHospitalLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getHospitalLevel()), SysHospital::getHospitalLevel, bo.getHospitalLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), SysHospital::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getBedSpace()), SysHospital::getBedSpace, bo.getBedSpace());
        lqw.eq(StringUtils.isNotBlank(bo.getRegion()), SysHospital::getRegion, bo.getRegion());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), SysHospital::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysHospital::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增医院管理
     *
     * @param bo 医院管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysHospitalBo bo) {
        SysHospital add = MapstructUtils.convert(bo, SysHospital.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHospitalId(add.getHospitalId());
        }
        return flag;
    }

    /**
     * 修改医院管理
     *
     * @param bo 医院管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysHospitalBo bo) {
        SysHospital update = MapstructUtils.convert(bo, SysHospital.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysHospital entity) {
        if (StrUtil.isBlank(entity.getHospitalName())) {
            throw new ServiceException("医院名称不能为空！");
        }
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除医院管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
