package com.openhis.administration.service.impl;

import java.util.List;

import com.openhis.administration.domain.Practitioner;
import com.openhis.administration.dto.OrgDataDto;
import com.openhis.common.enums.DelFlag;
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.Organization;
import com.openhis.administration.mapper.OrganizationMapper;
import com.openhis.administration.service.IOrganizationService;
import com.openhis.common.enums.AccountStatus;

import javax.annotation.Resource;

/**
 * 机构管理Service业务层处理
 *
 * @author system
 * @date 2025-02-21
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization>
    implements IOrganizationService {
    @Resource
    private OrganizationMapper organizationMapper;
    /**
     * 机构启用
     *
     * @param orgId 机构信息id
     * @return 操作结果
     */
    @Override
    public boolean activeOrg(Long orgId) {
        int updateCount = baseMapper.update(new Organization().setActiveFlag(AccountStatus.ACTIVE.getValue()),
            new LambdaUpdateWrapper<Organization>().eq(Organization::getId, orgId));
        return updateCount > 0;
    }

    /**
     * 机构停用
     *
     * @param orgId 机构信息id
     * @return 操作结果
     */
    @Override
    public boolean inactiveOrg(Long orgId) {
        int updateCount = baseMapper.update(new Organization().setActiveFlag(AccountStatus.INACTIVE.getValue()),
            new LambdaUpdateWrapper<Organization>().eq(Organization::getId, orgId));
        return updateCount > 0;
    }

    /**
     * 获取机构下拉列表
     *
     * @param organizationType 机构类型
     * @param organizationClass 机构分类
     * @return 机构下拉列表
     */
    @Override
    public List<Organization> getList(Integer organizationType, Integer organizationClass) {
        return baseMapper.selectList(new LambdaQueryWrapper<Organization>()
            .select(Organization::getId, Organization::getName).eq(Organization::getTypeEnum, organizationType)
            .eq(organizationClass != null, Organization::getClassEnum, organizationClass));
    }

    /**
     * 根据id查询科室集合
     *
     * @param collect id集合
     * @return 科室集合
     */
    @Override
    public List<Organization> getOrganizationListById(List<Long> collect) {
        return baseMapper.selectList(new LambdaQueryWrapper<Organization>().in(Organization::getId, collect)
                .eq(Organization::getDeleteFlag, DelFlag.NO.getCode()));
    }

    @Override
    public List<OrgDataDto> searchOrgDataByHealth() {
        return this.organizationMapper.searchOrgDataByHealth();
    }
}