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

import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.agency.Agency;
import com.xinelu.manage.domain.departmentdiseasetype.DepartmentDiseaseType;
import com.xinelu.manage.dto.department.DepartmentDTO;
import com.xinelu.manage.dto.departmentdiseasetype.DepartmentDiseaseTypeDto;
import com.xinelu.manage.mapper.agency.AgencyMapper;
import com.xinelu.manage.mapper.departmentdiseasetype.DepartmentDiseaseTypeMapper;
import com.xinelu.manage.service.departmentdiseasetype.IDepartmentDiseaseTypeService;
import com.xinelu.manage.vo.department.DepartmentBaseVo;
import com.xinelu.manage.vo.department.DepartmentVO;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;


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

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

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

    /**
     * 新增科室病种信息
     *
     * @param departmentDiseaseType 科室病种信息
     * @return 结果
     */
    @Override
    public int insertDepartmentDiseaseType(DepartmentDiseaseType departmentDiseaseType) {
        departmentDiseaseType.setCreateTime(LocalDateTime.now());
        departmentDiseaseType.setDiseaseTypeCode(Constants.DISEASE_TYPE_CODE + generateSystemCodeUtil.generateSystemCode(Constants.DISEASE_TYPE_CODE));
        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 departmentDto 科室名称
     * @return DepartmentVO
     */
    @Override
    public AjaxResult selectDiseaseTypeCount(DepartmentDTO departmentDto) {
	    Agency agency = agencyMapper.selectAgencyById(departmentDto.getHospitalAgencyId());
        List<DepartmentVO> departmentVOS = departmentDiseaseTypeMapper.selectDiseaseTypeCount(departmentDto);
        return AjaxResult.success(DepartmentBaseVo.builder().hospitalAgencyName(ObjectUtils.isEmpty(agency) ? "" : agency.getAgencyName())
	        .deptNumList(departmentVOS).build());
    }
}
