package com.zhyc.module.produce.breed.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.produce.breed.domain.ScMiscarriageRecord;
import com.zhyc.module.produce.breed.service.IScMiscarriageRecordService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 流产记录Controller
 *
 * @author ruoyi
 * @date 2025-08-23
 */
@RestController
@RequestMapping("/miscarriage/miscarriage")
public class ScMiscarriageRecordController extends BaseController
{
    @Autowired
    private IScMiscarriageRecordService scMiscarriageRecordService;

    /**
     * 查询流产记录列表
     */
    @PreAuthorize("@ss.hasPermi('miscarriage:miscarriage:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScMiscarriageRecord scMiscarriageRecord)
    {
        startPage();
        List<ScMiscarriageRecord> list = scMiscarriageRecordService.selectScMiscarriageRecordList(scMiscarriageRecord);
        return getDataTable(list);
    }

    /**
     * 导出流产记录列表
     */
    @PreAuthorize("@ss.hasPermi('miscarriage:miscarriage:export')")
    @Log(title = "流产记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScMiscarriageRecord scMiscarriageRecord)
    {
        List<ScMiscarriageRecord> list = scMiscarriageRecordService.selectScMiscarriageRecordList(scMiscarriageRecord);
        ExcelUtil<ScMiscarriageRecord> util = new ExcelUtil<ScMiscarriageRecord>(ScMiscarriageRecord.class);
        util.exportExcel(response, list, "流产记录数据");
    }

    /**
     * 获取流产记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('miscarriage:miscarriage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scMiscarriageRecordService.selectScMiscarriageRecordById(id));
    }

    /**
     * 根据耳号查询羊只信息
     */
    @GetMapping(value = "/sheep/{manageTags}")
    public AjaxResult getSheepInfo(@PathVariable("manageTags") String manageTags)
    {
        Map<String, Object> sheepInfo = scMiscarriageRecordService.selectSheepByManageTags(manageTags);
        return success(sheepInfo);
    }

    /**
     * 获取流产原因字典
     */
    @GetMapping("/reasonOptions")
    public AjaxResult getReasonOptions()
    {
        // 返回流产原因选项
        return success(new String[]{
                "利斯特氏菌病",
                "子宫积脓",
                "布鲁氏菌",
                "弧菌性流产传染性流产",
                "未知",
                "滴虫病",
                "细螺旋体病",
                "霉菌性流产"
        });
    }

    /**
     * 新增流产记录
     */
    @PreAuthorize("@ss.hasPermi('miscarriage:miscarriage:add')")
    @Log(title = "流产记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScMiscarriageRecord scMiscarriageRecord)
    {
        return toAjax(scMiscarriageRecordService.insertScMiscarriageRecord(scMiscarriageRecord));
    }

    /**
     * 修改流产记录
     */
    @PreAuthorize("@ss.hasPermi('miscarriage:miscarriage:edit')")
    @Log(title = "流产记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScMiscarriageRecord scMiscarriageRecord)
    {
        return toAjax(scMiscarriageRecordService.updateScMiscarriageRecord(scMiscarriageRecord));
    }

    /**
     * 删除流产记录
     */
    @PreAuthorize("@ss.hasPermi('miscarriage:miscarriage:remove')")
    @Log(title = "流产记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scMiscarriageRecordService.deleteScMiscarriageRecordByIds(ids));
    }

    /**
     * 模糊查询母羊耳号列表
     */
    @PreAuthorize("@ss.hasPermi('breed:lambing_records:query')") // 根据实际权限修改
    @GetMapping("/search_ear_numbers")
    public AjaxResult searchEarNumbers(@RequestParam("query") String query) {
        try {
            List<String> earNumbers = scMiscarriageRecordService.searchEarNumbers(query);
            return success(earNumbers);
        } catch (Exception e) {
            logger.error("搜索耳号异常", e);
            return error("搜索耳号失败：" + e.getMessage());
        }
    }
}