package com.fs.his.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fs.common.core.domain.R;
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.his.domain.FsPhysicalReportTemplate;
import com.fs.his.dto.FsPhysicalReportTemplateFieldDTO;
import com.fs.his.mapper.FsPhysicalReportTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsPhysicalReportTemplateFieldMapper;
import com.fs.his.domain.FsPhysicalReportTemplateField;
import com.fs.his.service.IFsPhysicalReportTemplateFieldService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 体检报告模板字段Service业务层处理
 *
 * @author fs
 * @date 2025-07-24
 */
@Service
public class FsPhysicalReportTemplateFieldServiceImpl extends ServiceImpl<FsPhysicalReportTemplateFieldMapper, FsPhysicalReportTemplateField> implements IFsPhysicalReportTemplateFieldService {

    @Autowired
    private FsPhysicalReportTemplateMapper physicalReportTemplateMapper;


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

    /**
     * 查询体检报告模板字段列表
     *
     * @param fsPhysicalReportTemplateField 体检报告模板字段
     * @return 体检报告模板字段
     */
    @Override
    public List<FsPhysicalReportTemplateField> selectFsPhysicalReportTemplateFieldList(FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        return baseMapper.selectFsPhysicalReportTemplateFieldList(fsPhysicalReportTemplateField);
    }

    /**
     * 新增体检报告模板字段
     *
     * @param fsPhysicalReportTemplateField 体检报告模板字段
     * @return 结果
     */
    @Override
    public int insertFsPhysicalReportTemplateField(FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        fsPhysicalReportTemplateField.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsPhysicalReportTemplateField(fsPhysicalReportTemplateField);
    }

    /**
     * 修改体检报告模板字段
     *
     * @param fsPhysicalReportTemplateField 体检报告模板字段
     * @return 结果
     */
    @Override
    public int updateFsPhysicalReportTemplateField(FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        fsPhysicalReportTemplateField.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsPhysicalReportTemplateField(fsPhysicalReportTemplateField);
    }

    /**
     * 批量删除体检报告模板字段
     *
     * @param ids 需要删除的体检报告模板字段主键
     * @return 结果
     */
    @Override
    public int deleteFsPhysicalReportTemplateFieldByIds(String[] ids) {
        return baseMapper.deleteFsPhysicalReportTemplateFieldByIds(ids);
    }

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

    @Override
    public R getTemplateField(String templateId) {
        //查询模板信息是否存在
        FsPhysicalReportTemplate reportTemplate = physicalReportTemplateMapper.selectById(templateId);
        if (StringUtils.isNull(reportTemplate)) {
            return R.error("操作失败，模板信息不存在！");
        } else if (!reportTemplate.getStatus().equals("1")) {
            return R.error("操作失败，当前模板已禁用！");
        }

        //查询模板下自定义数据信息
        return R.ok().put("data",baseMapper.selectList(new LambdaQueryWrapper<FsPhysicalReportTemplateField>().eq(FsPhysicalReportTemplateField::getTemplateId, templateId).orderByAsc(FsPhysicalReportTemplateField::getSort)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveFsPhysicalReportTemplateField(FsPhysicalReportTemplateFieldDTO fieldDTO) {
        //查询模板信息是否存在
        FsPhysicalReportTemplate reportTemplate = physicalReportTemplateMapper.selectById(fieldDTO.getTemplateId());
        if (StringUtils.isNull(reportTemplate)) {
            throw new ServiceException("操作失败，模板信息不存在！");
        } else if (!reportTemplate.getStatus().equals("1")) {
            throw new ServiceException("操作失败，当前模板已禁用！");
        }

        //验证数据主要数据是否存在
        AtomicInteger num = new AtomicInteger();
        Date date=new Date();
        fieldDTO.getTemplateFieldList().forEach(t -> {
            num.getAndIncrement();
            if (StringUtils.isNull(t.getComponentId())) {
                throw new ServiceException("操作失败，模板ID不能为空！");
            } else if (StringUtils.isNull(t.getLabel())) {
                throw new ServiceException("操作失败，组件名称不能为空！");
            } else if (StringUtils.isNull(t.getType())) {
                throw new ServiceException("操作失败，字段类型不能为空！");
            }
            t.setCreateBy(fieldDTO.getCreateBy());
            t.setTemplateId(reportTemplate.getId());
            t.setCreateTime(date);
            t.setSort(num.get());
        });

        //删除相关数据
        baseMapper.deleteTemplateFieldByTemplateId(reportTemplate.getId());
        //批量插入
        baseMapper.batchInsertFsPhysicalReportTemplateField(fieldDTO.getTemplateFieldList());
        return 1;
    }
}
