package com.ruoyi.web.modules.news.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource;
import com.ruoyi.web.modules.diagnosis.mapper.BizMedicalResourceMapper;
import com.ruoyi.web.modules.farming.domain.BizTechResource;
import com.ruoyi.web.modules.farming.mapper.BizTechResourceMapper;
import com.ruoyi.web.modules.news.domain.LivestockResourceListVo;
import com.ruoyi.web.modules.news.domain.LivestockResourceQuery;
import com.ruoyi.web.modules.news.domain.LivestockResourceRow;
import com.ruoyi.web.modules.news.mapper.LivestockResourceMapper;
import com.ruoyi.web.modules.news.service.ILivestockResourceService;
import com.ruoyi.web.modules.news.support.LivestockResourceDetailSupport;
import com.ruoyi.web.modules.news.support.LivestockResourceQuerySupport;
import com.ruoyi.web.modules.news.support.LivestockResourceRules;

@Service
public class LivestockResourceServiceImpl implements ILivestockResourceService
{
    @Autowired
    private LivestockResourceMapper livestockResourceMapper;

    @Autowired
    private BizMedicalResourceMapper bizMedicalResourceMapper;

    @Autowired
    private BizTechResourceMapper bizTechResourceMapper;

    @Override
    public List<LivestockResourceListVo> selectLivestockResourceList(LivestockResourceQuery query)
    {
        LivestockResourceQuery effective = LivestockResourceQuerySupport.normalize(query);
        List<LivestockResourceRow> rows = livestockResourceMapper.selectLivestockResourceList(effective);
        if (rows == null || rows.isEmpty())
        {
            return Collections.emptyList();
        }
        List<LivestockResourceListVo> result = new ArrayList<>(rows.size());
        for (LivestockResourceRow row : rows)
        {
            result.add(toListVo(row));
        }
        return result;
    }

    @Override
    public Object selectLivestockResourceById(Long id, Integer type)
    {
        if (id == null)
        {
            throw new ServiceException(LivestockResourceRules.MSG_ID_REQUIRED);
        }
        if (LivestockResourceRules.SOURCE_MEDICAL == type)
        {
            BizMedicalResource row = bizMedicalResourceMapper.selectBizMedicalResourceById(id);
            LivestockResourceDetailSupport.assertVisibleMedical(row);
            LivestockResourceDetailSupport.enrichMedical(row);
            return row;
        }
        if (LivestockResourceRules.SOURCE_TECH == type)
        {
            BizTechResource row = bizTechResourceMapper.selectBizTechResourceById(id);
            LivestockResourceDetailSupport.assertVisibleTech(row);
            LivestockResourceDetailSupport.enrichTech(row);
            return row;
        }
        throw new ServiceException(LivestockResourceRules.MSG_SOURCE_TYPE_INVALID);
    }

    private static LivestockResourceListVo toListVo(LivestockResourceRow row)
    {
        LivestockResourceListVo vo = new LivestockResourceListVo();
        vo.setId(row.getId());
        vo.setSourceType(row.getSourceType());
        vo.setTitle(row.getTitle());
        vo.setIntroduction(row.getIntroduction());
        vo.setType(row.getType());
        vo.setCoverFileUrl(row.getCoverFileUrl());
        vo.setContentFileUrl(row.getContentFileUrl());
        vo.setPublishTime(row.getPublishTime());
        return vo;
    }
}
