package com.xinelu.manage.service.departmentdiseasetype.impl;

import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.manage.domain.departmentdiseasetype.DepartmentDiseaseType;
import com.xinelu.manage.mapper.departmentdiseasetype.DepartmentDiseaseTypeMapper;
import com.xinelu.manage.service.departmentdiseasetype.IDepartmentDiseaseTypeService;
import com.xinelu.manage.vo.department.DepartmentVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 科室病种信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-02-26
 */
@Service
public class DepartmentDiseaseTypeServiceImpl implements IDepartmentDiseaseTypeService {
    @Resource
    private DepartmentDiseaseTypeMapper departmentDiseaseTypeMapper;

    /**
     * 查询科室病种信息
     *
     * @param id 科室病种信息主键
     * @return 科室病种信息
     */
    @Override
    public DepartmentDiseaseType selectDepartmentDiseaseTypeById(Long id) {
        return departmentDiseaseTypeMapper.selectDepartmentDiseaseTypeById(id);
    }

    /**
     * 查询科室病种信息列表
     *
     * @param departmentDiseaseType 科室病种信息
     * @return 科室病种信息
     */
    @Override
    public List<DepartmentDiseaseType> selectDepartmentDiseaseTypeList(DepartmentDiseaseType departmentDiseaseType) {
        return departmentDiseaseTypeMapper.selectDepartmentDiseaseTypeList(departmentDiseaseType);
    }

    /**
     * 新增科室病种信息
     *
     * @param departmentDiseaseType 科室病种信息
     * @return 结果
     */
    @Override
    public int insertDepartmentDiseaseType(DepartmentDiseaseType departmentDiseaseType) {
        departmentDiseaseType.setCreateTime(LocalDateTime.now());
        return departmentDiseaseTypeMapper.insertDepartmentDiseaseType(departmentDiseaseType);
    }

    /**
     * 修改科室病种信息
     *
     * @param departmentDiseaseType 科室病种信息
     * @return 结果
     */
    @Override
    public int updateDepartmentDiseaseType(DepartmentDiseaseType departmentDiseaseType) {
        departmentDiseaseType.setUpdateTime(LocalDateTime.now());
        return departmentDiseaseTypeMapper.updateDepartmentDiseaseType(departmentDiseaseType);
    }

    /**
     * 批量删除科室病种信息
     *
     * @param ids 需要删除的科室病种信息主键
     * @return 结果
     */
    @Override
    public int deleteDepartmentDiseaseTypeByIds(Long[] ids) {
        return departmentDiseaseTypeMapper.deleteDepartmentDiseaseTypeByIds(ids);
    }

    /**
     * 删除科室病种信息信息
     *
     * @param id 科室病种信息主键
     * @return 结果
     */
    @Override
    public int deleteDepartmentDiseaseTypeById(Long id) {
        return departmentDiseaseTypeMapper.deleteDepartmentDiseaseTypeById(id);
    }

    /**
     * 科室病种数量
     *
     * @param departmentName 科室名称
     * @return DepartmentVO
     */
    @Override
    public AjaxResult selectDiseaseTypeCount(String departmentName) {
        DepartmentVO departmentVO = new DepartmentVO();
        List<DepartmentVO> department = new ArrayList<>();
        departmentVO.setDepartmentName("全部");
        departmentVO.setCountNum(0);
        List<DepartmentVO> departmentVOS = departmentDiseaseTypeMapper.selectDiseaseTypeCount(departmentName);
        if (CollectionUtils.isNotEmpty(departmentVOS)) {
            Integer result = departmentVOS.stream().mapToInt(DepartmentVO::getCountNum).sum();
            departmentVO.setCountNum(result);
            department.add(departmentVO);
            department.addAll(departmentVOS);
        }
        return AjaxResult.success(department);
    }
}
