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.OcrUrlConfigConvert;
import com.docus.server.dto.segmentation.ocrurlconfig.AddOcrUrlConfigDTO;
import com.docus.server.dto.segmentation.ocrurlconfig.DeleteOcrUrlConfigDTO;
import com.docus.server.dto.segmentation.ocrurlconfig.EditOcrUrlConfigDTO;
import com.docus.server.entity.segmentation.OcrUrlConfig;
import com.docus.server.infrastructure.dao.IOcrUrlConfigDao;
import com.docus.server.service.IOcrUrlConfigService;
import com.docus.server.vo.segmentation.ocrurlconfig.OcrUrlConfigVO;
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 OcrUrlConfigServiceImpl implements IOcrUrlConfigService {
    @Resource
    private IOcrUrlConfigDao iOcrUrlConfigDao;
    @Resource
    private IdService idService;

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

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

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

        //PageResult<OcrUrlConfigVO> result = OcrUrlConfigConvert.INSTANCE.convertVO(iOcrUrlConfigDao.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 addOcrUrlConfigDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddOcrUrlConfigDTO addOcrUrlConfigDTO) {
        OcrUrlConfig ocrUrlConfig = OcrUrlConfigConvert.INSTANCE.convertDO(addOcrUrlConfigDTO);
        ocrUrlConfig.setId(idService.getDateSeq());
        return iOcrUrlConfigDao.add(ocrUrlConfig);

        // String name = addOcrUrlConfigDTO.getName();

        //OcrUrlConfig ocrUrlConfig = iOcrUrlConfigDao.findOneBy("name", name);

        //if (Func.notNull(ocrUrlConfig)) {
        // throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "ocr路径配置表已经存在");
        // }

        // OcrUrlConfig ocrUrlConfig =OcrUrlConfigConvert.INSTANCE.convertDO(addOcrUrlConfigDTO);
        // ocrUrlConfig.setId(idService.getDateSeq());

        // return iOcrUrlConfigDao.add(schCollector);

    }

    /**
     * 编辑
     *
     * @param editOcrUrlConfigDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EditOcrUrlConfigDTO editOcrUrlConfigDTO) {
        OcrUrlConfig ocrUrlConfig = OcrUrlConfigConvert.INSTANCE.convertDO(editOcrUrlConfigDTO);
        return iOcrUrlConfigDao.edit(ocrUrlConfig);

        // Long id = editOcrUrlConfigDTO.getId();

        //  OcrUrlConfig ocrUrlConfig = iOcrUrlConfigDao.findById(id);

        // if (Func.isNull(ocrUrlConfig)) {
        //     throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "ocr路径配置表，无法更新！");
        // }

        // ocrUrlConfig.setUpdateTime(LocalDateTime.now());
        // return iOcrUrlConfigDao.edit(ocrUrlConfig);
    }

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

