package com.zhyc.module.dairyProducts.controller;

import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.core.page.TableDataInfo;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.module.dairyProducts.domain.NpSheepMilkAnalysis;
import com.zhyc.module.dairyProducts.service.INpSheepMilkAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 奶产量分析控制器
 * 保留：分页列表（只读） + 单条查询 + 导出 + 耳号模糊查询
 */
@RestController
@RequestMapping("/dairyProducts/sheepMilkAnalysis")
public class NpSheepMilkAnalysisController extends BaseController {

    @Autowired
    private INpSheepMilkAnalysisService npSheepMilkAnalysisService;

    /**
     * 查询奶产量分析列表（只读，分页）
     * 支持参数 allEarNumbers（多耳号） 和 screenDays（筛选天数）
     */
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(required = false) List<String> allEarNumbers, // 修改处：接收多耳号参数
            NpSheepMilkAnalysis analysis) {
        startPage();

        // 修改处：将接收到的 list 设置进对象
        if (allEarNumbers != null && !allEarNumbers.isEmpty()) {
            analysis.setAllEarNumbers(allEarNumbers);
        }

        List<NpSheepMilkAnalysis> list = npSheepMilkAnalysisService.selectNpSheepMilkAnalysisList(analysis);
        return getDataTable(list);
    }

    /**
     * 修改处：新增搜索耳号接口
     */
    @GetMapping("/search_ear_numbers")
    public AjaxResult searchEarNumbers(@RequestParam("query") String query) {
        try {
            List<String> earNumbers = npSheepMilkAnalysisService.searchEarNumbers(query);
            return success(earNumbers);
        } catch (Exception e) {
            return error("搜索耳号失败：" + e.getMessage());
        }
    }

    /**
     * 获取单个分析记录详细信息（按sheepId）
     */
    @GetMapping(value = "/{sheepId}")
    public AjaxResult getInfo(@PathVariable("sheepId") String sheepId) {
        return AjaxResult.success(npSheepMilkAnalysisService.selectNpSheepMilkAnalysisBySheepId(sheepId));
    }

    /**
     * 导出奶产量分析记录（Excel）
     * 支持 allEarNumbers 与 screenDays 两个查询条件
     */
    @Log(title = "奶产量分析 导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(
            @RequestParam(required = false) List<String> allEarNumbers, // 修改处：接收多耳号参数
            NpSheepMilkAnalysis analysis) {

        if (allEarNumbers != null && !allEarNumbers.isEmpty()) {
            analysis.setAllEarNumbers(allEarNumbers);
        }

        List<NpSheepMilkAnalysis> list = npSheepMilkAnalysisService.selectNpSheepMilkAnalysisForExport(analysis);
        ExcelUtil<NpSheepMilkAnalysis> util = new ExcelUtil<>(NpSheepMilkAnalysis.class);
        return util.exportExcel(list, "羊奶产量分析数据");
    }
}