package com.ruoyi.web.modules.industryservice.research.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.BizPasture;
import com.ruoyi.web.modules.industryservice.domain.BizYakAsset;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenApiPageResult;
import com.ruoyi.web.modules.industryservice.research.config.ResearchOpenApiProperties;
import com.ruoyi.web.modules.industryservice.research.domain.dto.ResearchTokenVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchPastureVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakAssetVo;
import com.ruoyi.web.modules.industryservice.research.service.IResearchOpenApiService;
import com.ruoyi.web.modules.industryservice.research.support.ResearchOpenApiSupport;
import com.ruoyi.web.modules.industryservice.research.support.ResearchOpenApiTokenService;
import com.ruoyi.web.modules.industryservice.service.IBizPastureService;
import com.ruoyi.web.modules.industryservice.service.IBizYakAssetService;

@Service
public class ResearchOpenApiServiceImpl implements IResearchOpenApiService
{
    @Autowired
    private ResearchOpenApiProperties properties;

    @Autowired
    private ResearchOpenApiTokenService tokenService;

    @Autowired
    private IBizPastureService bizPastureService;

    @Autowired
    private IBizYakAssetService bizYakAssetService;

    @Override
    public ResearchTokenVo authenticate(String appKey, String appSecret)
    {
        return tokenService.issueToken(appKey, appSecret);
    }

    @Override
    public OpenApiPageResult<ResearchPastureVo> listPastures(int pageNum, int pageSize, String keyword,
            String farmType)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizPasture query = new BizPasture();
        query.setKeyword(keyword);
        query.setFarmType(farmType);
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizPasture> rows = bizPastureService.selectBizPastureList(query);
        PageInfo<BizPasture> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toPastureVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchYakAssetVo> listYakAssets(int pageNum, int pageSize,
            Integer assetStatus, String gender, Long pastureId)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizYakAsset query = new BizYakAsset();
        query.setAssetStatus(assetStatus);
        query.setGender(gender);
        query.setPastureId(pastureId);
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizYakAsset> rows = bizYakAssetService.selectBizYakAssetList(query);
        PageInfo<BizYakAsset> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toYakAssetVo).collect(Collectors.toList()));
    }

    private int normalizePageNum(int pageNum)
    {
        return pageNum > 0 ? pageNum : 1;
    }

    private int normalizePageSize(int pageSize)
    {
        if (pageSize <= 0)
        {
            return 20;
        }
        int max = properties.getMaxPageSize() > 0 ? properties.getMaxPageSize() : 200;
        if (pageSize > max)
        {
            throw new ServiceException("pageSize 不能超过 " + max, 400);
        }
        return pageSize;
    }

    private static <T> OpenApiPageResult<T> toPageResult(int pageNum, int pageSize, long total, List<T> records)
    {
        OpenApiPageResult<T> result = new OpenApiPageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(total);
        result.setRecords(records);
        return result;
    }
}
