package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbCase;
import com.ruoyi.hezhi.domain.TbCaseType;
import com.ruoyi.hezhi.domain.vo.CaseVO;
import com.ruoyi.hezhi.mapper.TbCaseMapper;
import com.ruoyi.hezhi.service.ITbCaseService;
import com.ruoyi.hezhi.service.ITbCaseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 案例Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@Service
public class TbCaseServiceImpl implements ITbCaseService
{
    @Autowired
    private TbCaseMapper tbCaseMapper;

    @Resource
    private ITbCaseTypeService caseTypeService;

    /**
     * 查询案例
     *
     * @param caseId 案例主键
     * @return 案例
     */
    @Override
    public TbCase selectTbCaseByCaseId(Long caseId)
    {
        return tbCaseMapper.selectTbCaseByCaseId(caseId);
    }

    /**
     * 查询案例列表
     *
     * @param tbCase 案例
     * @return 案例
     */
    @Override
    public List<TbCase> selectTbCaseList(TbCase tbCase)
    {
        return tbCaseMapper.selectTbCaseList(tbCase);
    }

    /**
     * 新增案例
     *
     * @param tbCase 案例
     * @return 结果
     */
    @Override
    public int insertTbCase(TbCase tbCase)
    {
        tbCase.setCreateTime(DateUtils.getNowDate());
        return tbCaseMapper.insertTbCase(tbCase);
    }

    /**
     * 修改案例
     *
     * @param tbCase 案例
     * @return 结果
     */
    @Override
    public int updateTbCase(TbCase tbCase)
    {
        tbCase.setUpdateTime(DateUtils.getNowDate());
        return tbCaseMapper.updateTbCase(tbCase);
    }

    /**
     * 批量删除案例
     *
     * @param caseIds 需要删除的案例主键
     * @return 结果
     */
    @Override
    public int deleteTbCaseByCaseIds(Long[] caseIds)
    {
        return tbCaseMapper.deleteTbCaseByCaseIds(caseIds);
    }

    /**
     * 删除案例信息
     *
     * @param caseId 案例主键
     * @return 结果
     */
    @Override
    public int deleteTbCaseByCaseId(Long caseId)
    {
        return tbCaseMapper.deleteTbCaseByCaseId(caseId);
    }

    /**
     * 案例list
     * @param caseTypeId 案例类型id 0全部
     * @return
     */
    @Override
    public List<CaseVO> selectCaseListByType(Long caseTypeId) {
        if(null != caseTypeId && 0 != caseTypeId ){
            TbCaseType tbCaseType = caseTypeService.selectTbCaseTypeByCaseTypeId(caseTypeId);
            if(ObjectUtils.isEmpty(tbCaseType)){
                throw new ServiceException("未查询到案例分类",500);
            }
            if(tbCaseType.getShowStatus() == 1){
                throw new ServiceException("未查询到案例分类",500);
            }
        }
        return tbCaseMapper.selectCaseListByType(caseTypeId);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public CaseVO getDetail(Long id) {
        TbCase detail = tbCaseMapper.selectTbCaseByCaseId(id);
        CaseVO vo = BeanUtil.toBean(detail,CaseVO.class);
        if(ObjectUtils.isEmpty(vo)){
            throw new ServiceException("查询案例详情异常",500);
        }else{
            return vo;
        }
    }
}
