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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
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.ScSheepDeath;
import com.zhyc.module.produce.breed.service.IScSheepDeathService;
import com.zhyc.module.biosafety.service.ISwDiseaseService;
import com.zhyc.module.biosafety.domain.SwDisease;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 羊只死淘记录Controller
 *
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/sheep_death/death")
public class ScSheepDeathController extends BaseController
{
    @Autowired
    private IScSheepDeathService scSheepDeathService;

    @Autowired
    private ISwDiseaseService swDiseaseService;

    /**
     * 查询羊只死淘记录列表
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScSheepDeath scSheepDeath)
    {
        startPage();
        List<ScSheepDeath> list = scSheepDeathService.selectScSheepDeathList(scSheepDeath);
        return getDataTable(list);
    }

    /**
     * 搜索管理耳号（用于下拉提示）
     */
    @GetMapping("/search/earNo")
    public AjaxResult searchEarNo(@RequestParam("query") String query) {
        return success(scSheepDeathService.selectDistinctManageTags(query == null ? "" : query));
    }

    /**
     * 搜索技术员（用于下拉提示）
     */
    @GetMapping("/search/technician")
    public AjaxResult searchTechnician(@RequestParam("query") String query) {
        return success(scSheepDeathService.selectDistinctTechnician(query == null ? "" : query));
    }

    /**
     * 搜索处理人（用于下拉提示）
     */
    @GetMapping("/search/handler")
    public AjaxResult searchHandler(@RequestParam("query") String query) {
        return success(scSheepDeathService.selectDistinctHandler(query == null ? "" : query));
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:query')")
    @GetMapping("/sheepInfo/{manageTags}")
    public AjaxResult getSheepInfo(@PathVariable("manageTags") String manageTags)
    {
        try {
            if (manageTags == null || manageTags.trim().isEmpty()) {
                return error("管理耳号不能为空");
            }
            Map<String, Object> sheepInfo = scSheepDeathService.selectSheepFileByManageTags(manageTags.trim());
            if (sheepInfo != null) {
                return success(sheepInfo);
            } else {
                return error("未找到该耳号对应的羊只信息");
            }
        } catch (Exception e) {
            return error("查询羊只信息失败: " + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:query')")
    @GetMapping("/disease/tree")
    public AjaxResult getDiseaseTree()
    {
        List<SwDisease> diseaseList = swDiseaseService.selectSwDiseaseList(new SwDisease());
        return success(diseaseList);
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:export')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScSheepDeath scSheepDeath)
    {
        List<ScSheepDeath> list = scSheepDeathService.selectScSheepDeathList(scSheepDeath);
        ExcelUtil<ScSheepDeath> util = new ExcelUtil<ScSheepDeath>(ScSheepDeath.class);
        util.exportExcel(response, list, "羊只死淘记录数据");
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scSheepDeathService.selectScSheepDeathById(id));
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:add')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScSheepDeath scSheepDeath)
    {
        try {
            if (scSheepDeath == null || scSheepDeath.getManageTags() == null || scSheepDeath.getManageTags().trim().isEmpty()) {
                return error("管理耳号不能为空");
            }
            return toAjax(scSheepDeathService.insertScSheepDeath(scSheepDeath));
        } catch (Exception e) {
            return error("新增失败: " + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:edit')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScSheepDeath scSheepDeath)
    {
        return toAjax(scSheepDeathService.updateScSheepDeath(scSheepDeath));
    }

    @PreAuthorize("@ss.hasPermi('sheep_death:death:remove')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scSheepDeathService.deleteScSheepDeathByIds(ids));
    }
}