package com.fs.his.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.StringUtils;
import com.fs.huifuPay.sdk.opps.core.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsPhysicalReportTemplateMapper;
import com.fs.his.domain.FsPhysicalReportTemplate;
import com.fs.his.service.IFsPhysicalReportTemplateService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 体检报告模板Service业务层处理
 *
 * @author fs
 * @date 2025-07-23
 */
@Service
public class FsPhysicalReportTemplateServiceImpl extends ServiceImpl<FsPhysicalReportTemplateMapper, FsPhysicalReportTemplate> implements IFsPhysicalReportTemplateService {

    /**
     * 查询体检报告模板
     *
     * @param id 体检报告模板主键
     * @return 体检报告模板
     */
    @Override
    public FsPhysicalReportTemplate selectFsPhysicalReportTemplateById(String id) {
        return baseMapper.selectFsPhysicalReportTemplateById(id);
    }

    /**
     * 查询体检报告模板列表
     *
     * @param fsPhysicalReportTemplate 体检报告模板
     * @return 体检报告模板
     */
    @Override
    public List<FsPhysicalReportTemplate> selectFsPhysicalReportTemplateList(FsPhysicalReportTemplate fsPhysicalReportTemplate) {
        return baseMapper.selectFsPhysicalReportTemplateList(fsPhysicalReportTemplate);
    }

    /**
     * 新增体检报告模板
     *
     * @param fsPhysicalReportTemplate 体检报告模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertFsPhysicalReportTemplate(FsPhysicalReportTemplate fsPhysicalReportTemplate) {
        //验证模板是否存在
        if (baseMapper.selectCount(new LambdaQueryWrapper<FsPhysicalReportTemplate>().eq(FsPhysicalReportTemplate::getTemplateName, fsPhysicalReportTemplate.getTemplateName()).eq(FsPhysicalReportTemplate::getStatus, "1")) > 0) {
            throw new ServiceException("操作失败，当前模板：" + fsPhysicalReportTemplate.getTemplateName() + "名称已存在！");
        }

        fsPhysicalReportTemplate.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsPhysicalReportTemplate(fsPhysicalReportTemplate);
    }

    /**
     * 修改体检报告模板
     *
     * @param fsPhysicalReportTemplate 体检报告模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateFsPhysicalReportTemplate(FsPhysicalReportTemplate fsPhysicalReportTemplate) {
        //验证修改数据是否存在
        FsPhysicalReportTemplate reportTemplate = baseMapper.selectById(fsPhysicalReportTemplate.getId());
        if (StringUtils.isNull(reportTemplate)) {
            throw new ServiceException("操作失败，修改数据不存在！");
        }

        //验证模板是否存在
        if (baseMapper.selectCount(new LambdaQueryWrapper<FsPhysicalReportTemplate>().eq(FsPhysicalReportTemplate::getTemplateName, fsPhysicalReportTemplate.getTemplateName()).eq(FsPhysicalReportTemplate::getStatus, "1").ne(FsPhysicalReportTemplate::getId, fsPhysicalReportTemplate.getId())) > 0) {
            throw new ServiceException("操作失败，当前模板：" + fsPhysicalReportTemplate.getTemplateName() + "名称已存在！");
        }

        fsPhysicalReportTemplate.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsPhysicalReportTemplate(fsPhysicalReportTemplate);
    }

    /**
     * 批量删除体检报告模板
     *
     * @param ids 需要删除的体检报告模板主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteFsPhysicalReportTemplateByIds(String[] ids) {
        //验证删除数据是否存在启用数据
        List<FsPhysicalReportTemplate> reportTemplateList = baseMapper.selectList(new LambdaQueryWrapper<FsPhysicalReportTemplate>().in(FsPhysicalReportTemplate::getId, ids));
        if (reportTemplateList.isEmpty() || reportTemplateList.size() != ids.length) {
            throw new ServiceException("操作失败，删除数据不匹配！");
        }
        //校验数据数据状态是否
        String templateName = reportTemplateList.stream().filter(r -> r.getStatus().equals("1")).findFirst().map(FsPhysicalReportTemplate::getTemplateName).orElse(null);
        if (StringUtils.isNotEmpty(templateName)) {
            throw new ServiceException("操作失败，当前模板：" + templateName + "正在启用中，无法删除！");
        }

        return baseMapper.deleteFsPhysicalReportTemplateByIds(ids);
    }

    /**
     * 删除体检报告模板信息
     *
     * @param id 体检报告模板主键
     * @return 结果
     */
    @Override
    public int deleteFsPhysicalReportTemplateById(String id) {
        return baseMapper.deleteFsPhysicalReportTemplateById(id);
    }
}
