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

import com.github.pagehelper.PageInfo;
import com.xinelu.common.annotation.DataScope;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.constant.HttpStatus;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.PageUtils;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.operationInfo.OperationInfo;
import com.xinelu.manage.dto.operationinfo.BaseInfoQueryDto;
import com.xinelu.manage.dto.operationinfo.OperationInfoDto;
import com.xinelu.manage.mapper.operationInfo.OperationInfoMapper;
import com.xinelu.manage.service.operationInfo.IOperationInfoService;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 手术信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-02-26
 */
@Service
public class OperationInfoServiceImpl implements IOperationInfoService {
    @Resource
    private OperationInfoMapper operationInfoMapper;

    @Resource
    private GenerateSystemCodeUtil systemCodeUtil;

    /**
     * 查询手术信息
     *
     * @param id 手术信息主键
     * @return 手术信息
     */
    @Override
    public OperationInfo selectOperationInfoById(Long id) {
        return operationInfoMapper.selectOperationInfoById(id);
    }

    /**
     * 查询手术信息列表
     *
     * @param operationInfo 手术信息
     * @return 手术信息
     */
    @DataScope(agencyAlias = "d", deptAlias = "oi")
    @Override
    public List<OperationInfo> selectOperationInfoList(OperationInfoDto operationInfo) {
        return operationInfoMapper.selectOperationList(operationInfo);
    }

	/**
     * 新增手术信息
     *
     * @param operationInfo 手术信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOperationInfo(OperationInfo operationInfo) {
        // 检查手术名称是否已存在
        int existingNameCount = operationInfoMapper.countByDepartmentIdAndOperationName(operationInfo.getDepartmentId(), operationInfo.getOperationName());
        if (existingNameCount > 0) {
            throw new ServiceException("手术名称已存在");
        }
        // 设置创建人、创建时间
        operationInfo.setOperationCode(Constants.SURGERY_ENCODING + systemCodeUtil.generateDepartCode(Constants.SURGERY_ENCODING));
        operationInfo.setCreateBy(SecurityUtils.getUsername());
        operationInfo.setCreateTime(LocalDateTime.now());
        return operationInfoMapper.insertOperationInfo(operationInfo);
    }

    /**
     * 修改手术信息
     *
     * @param operationInfo 手术信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOperationInfo(OperationInfo operationInfo) {
        // 检查除当前记录之外是否存在同名的手术名称
        if (operationInfoMapper.countByOperationNameExcludingId(operationInfo.getId(), operationInfo.getDepartmentId(), operationInfo.getOperationName()) > 0) {
            throw new ServiceException("手术名称已存在");
        }
        // 设置修改人，修改时间
        operationInfo.setUpdateBy(SecurityUtils.getUsername());
        operationInfo.setUpdateTime(LocalDateTime.now());
        return operationInfoMapper.updateOperationInfo(operationInfo);
    }

    /**
     * 批量删除手术信息
     *
     * @param ids 需要删除的手术信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOperationInfoByIds(Long[] ids) {
        return operationInfoMapper.deleteOperationInfoByIds(ids);
    }
}
