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

import java.util.Date;
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.common.utils.StringUtils;
import com.ruoyi.web.modules.farming.service.IBizTechServicePersonnelService;
import com.ruoyi.web.modules.industryservice.domain.BizCooperativeDevelopment;
import com.ruoyi.web.modules.industryservice.domain.BizDisposableIncome;
import com.ruoyi.web.modules.industryservice.domain.BizFamilyRanch;
import com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold;
import com.ruoyi.web.modules.industryservice.domain.BizLargeLivestockFarmer;
import com.ruoyi.web.modules.industryservice.domain.BizLivestockOwnershipHousehold;
import com.ruoyi.web.modules.industryservice.domain.BizLivestockProductOutput;
import com.ruoyi.web.modules.industryservice.domain.BizPasture;
import com.ruoyi.web.modules.industryservice.domain.BizRuralThreeAssets;
import com.ruoyi.web.modules.industryservice.domain.BizThreeAssetsProblemReport;
import com.ruoyi.web.modules.industryservice.domain.BizVaccinationData;
import com.ruoyi.web.modules.industryservice.domain.BizYakAsset;
import com.ruoyi.web.modules.industryservice.domain.BizYakHerdInventory;
import com.ruoyi.web.modules.industryservice.domain.BizYakOutboundReport;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenApiPageResult;
import com.ruoyi.web.modules.industryservice.mapper.BizCooperativeDevelopmentMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizDisposableIncomeMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizFamilyRanchMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizFarmerHouseholdMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizLargeLivestockFarmerMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizLivestockOwnershipHouseholdMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizLivestockProductOutputMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizRuralThreeAssetsMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizThreeAssetsProblemReportMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizVaccinationDataMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakHerdInventoryMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakOutboundReportMapper;
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.ResearchCooperativeDevelopmentVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchCommonProsperityProjectVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchDisposableIncomeVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchFamilyRanchVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchFarmerHouseholdVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchLargeLivestockFarmerVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchLivestockOwnershipHouseholdVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchLivestockProductOutputVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchPastureVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchRuralThreeAssetsVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchTechServicePersonnelVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchThreeAssetsProblemReportVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchVaccinationDataVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakAssetVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakHerdInventoryVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakOutboundReportVo;
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;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectMapper;

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

    @Autowired
    private ResearchOpenApiTokenService tokenService;

    @Autowired
    private IBizPastureService bizPastureService;

    @Autowired
    private IBizYakAssetService bizYakAssetService;

    @Autowired
    private BizYakHerdInventoryMapper bizYakHerdInventoryMapper;

    @Autowired
    private BizYakOutboundReportMapper bizYakOutboundReportMapper;

    @Autowired
    private BizCooperativeDevelopmentMapper bizCooperativeDevelopmentMapper;

    @Autowired
    private BizLivestockProductOutputMapper bizLivestockProductOutputMapper;

    @Autowired
    private BizFamilyRanchMapper bizFamilyRanchMapper;

    @Autowired
    private BizDisposableIncomeMapper bizDisposableIncomeMapper;

    @Autowired
    private BizFarmerHouseholdMapper bizFarmerHouseholdMapper;

    @Autowired
    private BizCommonProsperityProjectMapper bizCommonProsperityProjectMapper;

    @Autowired
    private BizLargeLivestockFarmerMapper bizLargeLivestockFarmerMapper;

    @Autowired
    private BizVaccinationDataMapper bizVaccinationDataMapper;

    @Autowired
    private BizLivestockOwnershipHouseholdMapper bizLivestockOwnershipHouseholdMapper;

    @Autowired
    private BizRuralThreeAssetsMapper bizRuralThreeAssetsMapper;

    @Autowired
    private BizThreeAssetsProblemReportMapper bizThreeAssetsProblemReportMapper;

    @Autowired
    private IBizTechServicePersonnelService bizTechServicePersonnelService;

    @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()));
    }

    @Override
    public OpenApiPageResult<ResearchYakHerdInventoryVo> listYakHerdInventories(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizYakHerdInventory query = new BizYakHerdInventory();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizYakHerdInventory> rows = bizYakHerdInventoryMapper.selectBizYakHerdInventoryList(query);
        PageInfo<BizYakHerdInventory> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toYakHerdInventoryVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchYakOutboundReportVo> listYakOutboundReports(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizYakOutboundReport query = new BizYakOutboundReport();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizYakOutboundReport> rows = bizYakOutboundReportMapper.selectBizYakOutboundReportList(query);
        PageInfo<BizYakOutboundReport> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toYakOutboundReportVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchCooperativeDevelopmentVo> listCooperativeDevelopments(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName, String cooperativeName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizCooperativeDevelopment query = new BizCooperativeDevelopment();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        if (StringUtils.isNotEmpty(cooperativeName))
        {
            query.setCooperativeName(cooperativeName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizCooperativeDevelopment> rows = bizCooperativeDevelopmentMapper.selectBizCooperativeDevelopmentList(query);
        PageInfo<BizCooperativeDevelopment> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toCooperativeDevelopmentVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchLivestockProductOutputVo> listLivestockProductOutputs(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizLivestockProductOutput query = new BizLivestockProductOutput();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizLivestockProductOutput> rows = bizLivestockProductOutputMapper.selectBizLivestockProductOutputList(query);
        PageInfo<BizLivestockProductOutput> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toLivestockProductOutputVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchFamilyRanchVo> listFamilyRanches(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName, String enterpriseName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizFamilyRanch query = new BizFamilyRanch();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        if (StringUtils.isNotEmpty(enterpriseName))
        {
            query.setEnterpriseName(enterpriseName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizFamilyRanch> rows = bizFamilyRanchMapper.selectBizFamilyRanchList(query);
        PageInfo<BizFamilyRanch> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toFamilyRanchVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchDisposableIncomeVo> listDisposableIncomes(int pageNum, int pageSize,
            Integer statYear)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizDisposableIncome query = new BizDisposableIncome();
        query.setStatYear(statYear);
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizDisposableIncome> rows = bizDisposableIncomeMapper.selectBizDisposableIncomeList(query);
        PageInfo<BizDisposableIncome> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toDisposableIncomeVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchFarmerHouseholdVo> listFarmerHouseholds(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizFarmerHousehold query = new BizFarmerHousehold();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizFarmerHousehold> rows = bizFarmerHouseholdMapper.selectBizFarmerHouseholdList(query);
        PageInfo<BizFarmerHousehold> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toFarmerHouseholdVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchCommonProsperityProjectVo> listCommonProsperityProjects(int pageNum, int pageSize,
            String projectName, Integer projectType, Integer implYear)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizCommonProsperityProject query = new BizCommonProsperityProject();
        if (StringUtils.isNotEmpty(projectName))
        {
            query.setProjectName(projectName.trim());
        }
        query.setProjectType(projectType);
        query.setImplYear(implYear);
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizCommonProsperityProject> rows =
                bizCommonProsperityProjectMapper.selectBizCommonProsperityProjectList(query);
        PageInfo<BizCommonProsperityProject> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toCommonProsperityProjectVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchLargeLivestockFarmerVo> listLargeLivestockFarmers(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName, String enterpriseName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizLargeLivestockFarmer query = new BizLargeLivestockFarmer();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        if (StringUtils.isNotEmpty(enterpriseName))
        {
            query.setEnterpriseName(enterpriseName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizLargeLivestockFarmer> rows = bizLargeLivestockFarmerMapper.selectBizLargeLivestockFarmerList(query);
        PageInfo<BizLargeLivestockFarmer> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toLargeLivestockFarmerVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchVaccinationDataVo> listVaccinationData(int pageNum, int pageSize,
            Integer statYear, Integer statMonth, String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizVaccinationData query = new BizVaccinationData();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizVaccinationData> rows = bizVaccinationDataMapper.selectBizVaccinationDataList(query);
        PageInfo<BizVaccinationData> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toVaccinationDataVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchLivestockOwnershipHouseholdVo> listLivestockOwnershipHouseholds(int pageNum,
            int pageSize, Integer statYear, Integer statMonth, String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizLivestockOwnershipHousehold query = new BizLivestockOwnershipHousehold();
        query.setStatYear(statYear);
        query.setStatMonth(statMonth);
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizLivestockOwnershipHousehold> rows =
                bizLivestockOwnershipHouseholdMapper.selectBizLivestockOwnershipHouseholdList(query);
        PageInfo<BizLivestockOwnershipHousehold> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toLivestockOwnershipHouseholdVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchRuralThreeAssetsVo> listRuralThreeAssets(int pageNum, int pageSize,
            String townName, String villageName)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizRuralThreeAssets query = new BizRuralThreeAssets();
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizRuralThreeAssets> rows = bizRuralThreeAssetsMapper.selectBizRuralThreeAssetsList(query);
        PageInfo<BizRuralThreeAssets> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toRuralThreeAssetsVo).collect(Collectors.toList()));
    }

    @Override
    public OpenApiPageResult<ResearchThreeAssetsProblemReportVo> listThreeAssetsProblemReports(int pageNum,
            int pageSize, String townName, String villageName, Date beginReportTime, Date endReportTime)
    {
        tokenService.assertEnabled();
        int safePageNum = normalizePageNum(pageNum);
        int safePageSize = normalizePageSize(pageSize);
        BizThreeAssetsProblemReport query = new BizThreeAssetsProblemReport();
        if (StringUtils.isNotEmpty(townName))
        {
            query.setTownName(townName.trim());
        }
        if (StringUtils.isNotEmpty(villageName))
        {
            query.setVillageName(villageName.trim());
        }
        // 上报时间区间：开始、结束日期同时有值时才生效
        if (beginReportTime != null && endReportTime != null)
        {
            query.setBeginReportTime(beginReportTime);
            query.setEndReportTime(endReportTime);
        }
        // 开放接口无登录上下文，清空 dataScope，避免走填报部门权限
        query.getParams().put("dataScope", "");
        PageHelper.startPage(safePageNum, safePageSize);
        List<BizThreeAssetsProblemReport> rows =
                bizThreeAssetsProblemReportMapper.selectBizThreeAssetsProblemReportList(query);
        PageInfo<BizThreeAssetsProblemReport> page = new PageInfo<>(rows);
        return toPageResult(safePageNum, safePageSize, page.getTotal(),
                rows.stream().map(ResearchOpenApiSupport::toThreeAssetsProblemReportVo).collect(Collectors.toList()));
    }

    @Override
    public ResearchTechServicePersonnelVo getTechServicePersonnel()
    {
        tokenService.assertEnabled();
        return ResearchOpenApiSupport.toTechServicePersonnelVo(bizTechServicePersonnelService.getCurrentConfig());
    }

    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;
    }
}
