package com.docus.server.service.impl;

import com.docus.infrastructure.redis.service.IdService;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.convert.OcrFileInfoTestConvert;
import com.docus.server.dto.segmentation.ocrfileinfotest.AddOcrFileInfoTestDTO;
import com.docus.server.dto.segmentation.ocrfileinfotest.DeleteOcrFileInfoTestDTO;
import com.docus.server.dto.segmentation.ocrfileinfotest.EditOcrFileInfoTestDTO;
import com.docus.server.entity.segmentation.OcrFileInfoTest;
import com.docus.server.infrastructure.dao.IOcrFileInfoTestDao;
import com.docus.server.service.IOcrFileInfoTestService;
import com.docus.server.vo.segmentation.ocrfileinfotest.OcrFileInfoTestVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * ocr文件信息-调试 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-08-29
 */
@Service
public class OcrFileInfoTestServiceImpl implements IOcrFileInfoTestService {
    @Resource
    private IOcrFileInfoTestDao iOcrFileInfoTestDao;
    @Resource
    private IdService idService;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public OcrFileInfoTestVO findById(Long id) {
        return OcrFileInfoTestConvert.INSTANCE.convertVO(iOcrFileInfoTestDao.findById(id));
    }

    /**
     * 查询所有
     *
     * @return 实体
     */
    @Override
    public List<OcrFileInfoTestVO> findAll() {
        return OcrFileInfoTestConvert.INSTANCE.convertVO(iOcrFileInfoTestDao.findAll());
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<OcrFileInfoTestVO> search(SearchDTO searchDTO) {
        return OcrFileInfoTestConvert.INSTANCE.convertVO(iOcrFileInfoTestDao.search(searchDTO));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBatch(List<AddOcrFileInfoTestDTO> files) {
        List<OcrFileInfoTest> ocrFileInfos = OcrFileInfoTestConvert.INSTANCE.convertDO(files);
        ocrFileInfos.forEach(p -> {
            p.setId(idService.getDateSeq());
        });
        iOcrFileInfoTestDao.saveBatch(ocrFileInfos, 500);
    }

    @Override
    public OcrFileInfoTest findOneBy(String patientId) {
        return iOcrFileInfoTestDao.findOneBy("patientId", patientId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long saveOrUpdate(AddOcrFileInfoTestDTO files) {
        OcrFileInfoTest ocrFileInfos = OcrFileInfoTestConvert.INSTANCE.convertDO(files);
        ocrFileInfos.setId(idService.getDateSeq());
        ocrFileInfos.setPatientId(String.valueOf(ocrFileInfos.getId()));

        iOcrFileInfoTestDao.saveOrUpdate(ocrFileInfos);

        return ocrFileInfos.getId();
    }

    /**
     * 新增
     *
     * @param addOcrFileInfoTestDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddOcrFileInfoTestDTO addOcrFileInfoTestDTO) {
        OcrFileInfoTest ocrFileInfoTest = OcrFileInfoTestConvert.INSTANCE.convertDO(addOcrFileInfoTestDTO);
        ocrFileInfoTest.setId(idService.getDateSeq());
        return iOcrFileInfoTestDao.add(ocrFileInfoTest);

        // String name = addOcrFileInfoTestDTO.getName();

        //OcrFileInfoTest ocrFileInfoTest = iOcrFileInfoTestDao.findOneBy("name", name);

        //if (Func.notNull(ocrFileInfoTest)) {
        // throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "ocr文件信息-调试已经存在");
        // }

        // OcrFileInfoTest ocrFileInfoTest =OcrFileInfoTestConvert.INSTANCE.convertDO(addOcrFileInfoTestDTO);
        // ocrFileInfoTest.setId(idService.getDateSeq());

        // return iOcrFileInfoTestDao.add(schCollector);

    }

    /**
     * 编辑
     *
     * @param editOcrFileInfoTestDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EditOcrFileInfoTestDTO editOcrFileInfoTestDTO) {
        OcrFileInfoTest ocrFileInfoTest = OcrFileInfoTestConvert.INSTANCE.convertDO(editOcrFileInfoTestDTO);
        return iOcrFileInfoTestDao.edit(ocrFileInfoTest);

        // Long id = editOcrFileInfoTestDTO.getId();

        //  OcrFileInfoTest ocrFileInfoTest = iOcrFileInfoTestDao.findById(id);

        // if (Func.isNull(ocrFileInfoTest)) {
        //     throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "ocr文件信息-调试，无法更新！");
        // }

        // ocrFileInfoTest.setUpdateTime(LocalDateTime.now());
        // return iOcrFileInfoTestDao.edit(ocrFileInfoTest);
    }

    /**
     * 批量删除
     *
     * @param deleteOcrFileInfoTestDTO 删除参数
     * @return 成功或失败
     */
    @Override
    public int delete(DeleteOcrFileInfoTestDTO deleteOcrFileInfoTestDTO) {
        return iOcrFileInfoTestDao.delete(deleteOcrFileInfoTestDTO.getIds());
    }
}

