package com.docus.server.service.impl;

import com.docus.core.util.DateUtil;
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.OcrBasicConvert;
import com.docus.server.dto.segmentation.ocrbasic.AddOcrBasicDTO;
import com.docus.server.dto.segmentation.ocrbasic.DeleteOcrBasicDTO;
import com.docus.server.dto.segmentation.ocrbasic.EditOcrBasicDTO;
import com.docus.server.entity.segmentation.OcrBasic;
import com.docus.server.infrastructure.dao.IOcrBasicDao;
import com.docus.server.service.IOcrBasicService;
import com.docus.server.vo.segmentation.ocrbasic.OcrBasicVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 患者信息表 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-08-29
 */
@Service
public class OcrBasicServiceImpl implements IOcrBasicService {
    @Resource
    private IOcrBasicDao iOcrBasicDao;
    @Resource
    private IdService idService;

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

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

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

        //PageResult<OcrBasicVO> result = OcrBasicConvert.INSTANCE.convertVO(iOcrBasicDao.search(searchDTO));

        //if (CollectionUtils.isEmpty(result.getList())) {
        //return new PageResult<>();
        //}

        //Map<String, SchSystemParams> map = iSchSystemParamsService.find(ListUtils.distinctSelect(result.getList(), SchCollectorVO::getCollectorId));

        //result.getList().forEach(p -> {
        // String collectorId = String.valueOf(p.getCollectorId());
        // if (map.containsKey(collectorId)) {
        //     p.setCollectorName(map.get(collectorId).getParamName());
        // }
        //});

        //return result;
    }

    /**
     * 新增
     *
     * @param addOcrBasicDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddOcrBasicDTO addOcrBasicDTO) {
        OcrBasic ocrBasic = OcrBasicConvert.INSTANCE.convertDO(addOcrBasicDTO);
        ocrBasic.setId(idService.getDateSeq());
        return iOcrBasicDao.add(ocrBasic);

        // String name = addOcrBasicDTO.getName();

        //OcrBasic ocrBasic = iOcrBasicDao.findOneBy("name", name);

        //if (Func.notNull(ocrBasic)) {
        // throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "患者信息表已经存在");
        // }

        // OcrBasic ocrBasic =OcrBasicConvert.INSTANCE.convertDO(addOcrBasicDTO);
        // ocrBasic.setId(idService.getDateSeq());

        // return iOcrBasicDao.add(schCollector);

    }

    /**
     * 编辑
     *
     * @param editOcrBasicDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EditOcrBasicDTO editOcrBasicDTO) {
        OcrBasic ocrBasic = OcrBasicConvert.INSTANCE.convertDO(editOcrBasicDTO);
        return iOcrBasicDao.edit(ocrBasic);

        // Long id = editOcrBasicDTO.getId();

        //  OcrBasic ocrBasic = iOcrBasicDao.findById(id);

        // if (Func.isNull(ocrBasic)) {
        //     throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "患者信息表，无法更新！");
        // }

        // ocrBasic.setUpdateTime(LocalDateTime.now());
        // return iOcrBasicDao.edit(ocrBasic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(AddOcrBasicDTO editOcrBasicDTO) {
        OcrBasic ocrBasic = OcrBasicConvert.INSTANCE.convertDO(editOcrBasicDTO);
        OcrBasic ocrBasicDO = iOcrBasicDao.findOneBy("patientId", ocrBasic.getPatientId());
        if (null == ocrBasicDO) {
            ocrBasic.setId(idService.getDateSeq());
            return iOcrBasicDao.add(ocrBasic);
        } else {
            Long id = ocrBasicDO.getId();
            OcrBasic ocrBasicPO = OcrBasicConvert.INSTANCE.convertDO(editOcrBasicDTO, ocrBasicDO);
            ocrBasicPO.setId(id);
            ocrBasicPO.setUpdateTime(DateUtil.now());
            return iOcrBasicDao.edit(ocrBasicPO);
        }
    }

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

