package com.docus.server.service.impl;

import com.docus.core.util.ListUtils;
import com.docus.core.util.property.Setters;
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.SchCollectorVersionConvert;
import com.docus.server.dto.scheduling.management.schcollectorversion.AddSchCollectorVersionDTO;
import com.docus.server.dto.scheduling.management.schcollectorversion.DeleteSchCollectorVersionDTO;
import com.docus.server.dto.scheduling.management.schcollectorversion.EditSchCollectorVersionDTO;
import com.docus.server.entity.scheduling.management.SchCollectorVersion;
import com.docus.server.entity.scheduling.management.SchSystemParams;
import com.docus.server.infrastructure.dao.ISchCollectorVersionDao;
import com.docus.server.service.ISchCollectorVersionService;
import com.docus.server.service.ISchSystemParamsService;
import com.docus.server.vo.scheduling.management.schcollectorversion.SchCollectorVersionVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 采集器版本列表管理 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Service
public class SchCollectorVersionServiceImpl implements ISchCollectorVersionService {
    @Resource
    private ISchCollectorVersionDao iSchCollectorVersionDao;
    @Resource
    private ISchSystemParamsService iSchSystemParamsService;
    @Resource
    private IdService idService;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public SchCollectorVersionVO findById(String id) {
        return SchCollectorVersionConvert.INSTANCE.convertVO(iSchCollectorVersionDao.findById(id));
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<SchCollectorVersionVO> search(SearchDTO searchDTO) {

        PageResult<SchCollectorVersionVO> result = SchCollectorVersionConvert.INSTANCE.convertVO(iSchCollectorVersionDao.search(searchDTO));

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

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

        Setters.<SchCollectorVersionVO>instance().list(result.getList()).cycleSetProperties(p -> {
            String collectorId = String.valueOf(p.getCollectorId());
            if (map.containsKey(collectorId)) {
                p.setCollectorName(map.get(collectorId).getParamName());
            }
        });

        return result;
    }

    @Override
    public SchCollectorVersion findByVersion(Long collectorId, String collectorVersionNo) {
        return iSchCollectorVersionDao.findByVersion(collectorId, collectorVersionNo);
    }

    /**
     * 新增
     *
     * @param addSchCollectorVersionDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean add(AddSchCollectorVersionDTO addSchCollectorVersionDTO) {
        SchCollectorVersion schCollectorVersion = SchCollectorVersionConvert.INSTANCE.convertDO(addSchCollectorVersionDTO);
        schCollectorVersion.setId(idService.getDateSeq());
        return iSchCollectorVersionDao.add(schCollectorVersion);
    }

    /**
     * 编辑
     *
     * @param editSchCollectorVersionDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean edit(EditSchCollectorVersionDTO editSchCollectorVersionDTO) {
        SchCollectorVersion schCollectorVersion = SchCollectorVersionConvert.INSTANCE.convertDO(editSchCollectorVersionDTO);
        return iSchCollectorVersionDao.edit(schCollectorVersion);
    }

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

