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.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.common.exception.ServiceException;
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)
    {
        try {
            startPage();
            List<ScSheepDeath> list = scSheepDeathService.selectScSheepDeathList(scSheepDeath);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error("查询羊只死淘记录列表失败", e);
            return getDataTable(new java.util.ArrayList<>());
        }
    }

    /**
     * 根据管理耳号查询羊只信息
     */
    @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) {
            logger.error("查询羊只信息失败，管理耳号: " + manageTags, e);
            return error("查询羊只信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取疾病树形列表
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:query')")
    @GetMapping("/disease/tree")
    public AjaxResult getDiseaseTree()
    {
        try {
            List<SwDisease> diseaseList = swDiseaseService.selectSwDiseaseList(new SwDisease());
            return success(diseaseList);
        } catch (Exception e) {
            logger.error("获取疾病树形列表失败", e);
            return error("获取疾病列表失败: " + e.getMessage());
        }
    }

    /**
     * 导出羊只死淘记录列表
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:export')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScSheepDeath scSheepDeath)
    {
        try {
            List<ScSheepDeath> list = scSheepDeathService.selectScSheepDeathList(scSheepDeath);
            ExcelUtil<ScSheepDeath> util = new ExcelUtil<ScSheepDeath>(ScSheepDeath.class);
            util.exportExcel(response, list, "羊只死淘记录数据");
        } catch (Exception e) {
            logger.error("导出羊只死淘记录失败", e);
            // 可以在这里返回错误响应
        }
    }

    /**
     * 获取羊只死淘记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        try {
            if (id == null) {
                return error("记录ID不能为空");
            }

            ScSheepDeath result = scSheepDeathService.selectScSheepDeathById(id);
            if (result == null) {
                return error("记录不存在");
            }

            return success(result);
        } catch (Exception e) {
            logger.error("获取羊只死淘记录详细信息失败，ID: " + id, e);
            return error("获取记录详细信息失败: " + e.getMessage());
        }
    }

    /**
     * 新增羊只死淘记录
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:add')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScSheepDeath scSheepDeath)
    {
        try {
            // 基础参数校验
            if (scSheepDeath == null) {
                return error("请求参数不能为空");
            }

            if (scSheepDeath.getManageTags() == null || scSheepDeath.getManageTags().trim().isEmpty()) {
                return error("管理耳号不能为空");
            }

            if (scSheepDeath.getDeathDate() == null) {
                return error("死淘日期不能为空");
            }

            int result = scSheepDeathService.insertScSheepDeath(scSheepDeath);
            if (result > 0) {
                return success("新增成功");
            } else {
                return error("新增失败");
            }
        } catch (ServiceException e) {
            logger.warn("新增羊只死淘记录业务异常: " + e.getMessage());
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("新增羊只死淘记录失败", e);
            return error("新增失败: " + e.getMessage());
        }
    }

    /**
     * 修改羊只死淘记录
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:edit')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScSheepDeath scSheepDeath)
    {
        try {
            // 基础参数校验
            if (scSheepDeath == null) {
                return error("请求参数不能为空");
            }

            if (scSheepDeath.getId() == null) {
                return error("记录ID不能为空");
            }

            if (scSheepDeath.getManageTags() == null || scSheepDeath.getManageTags().trim().isEmpty()) {
                return error("管理耳号不能为空");
            }

            if (scSheepDeath.getDeathDate() == null) {
                return error("死淘日期不能为空");
            }

            int result = scSheepDeathService.updateScSheepDeath(scSheepDeath);
            if (result > 0) {
                return success("修改成功");
            } else {
                return error("修改失败，记录可能不存在");
            }
        } catch (ServiceException e) {
            logger.warn("修改羊只死淘记录业务异常: " + e.getMessage());
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("修改羊只死淘记录失败", e);
            return error("修改失败: " + e.getMessage());
        }
    }

    /**
     * 删除羊只死淘记录
     */
    @PreAuthorize("@ss.hasPermi('sheep_death:death:remove')")
    @Log(title = "羊只死淘记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        try {
            if (ids == null || ids.length == 0) {
                return error("删除的记录ID不能为空");
            }

            int result = scSheepDeathService.deleteScSheepDeathByIds(ids);
            if (result > 0) {
                return success("删除成功");
            } else {
                return error("删除失败，记录可能不存在");
            }
        } catch (ServiceException e) {
            logger.warn("删除羊只死淘记录业务异常: " + e.getMessage());
            return error(e.getMessage());
        } catch (Exception e) {
            logger.error("删除羊只死淘记录失败", e);
            return error("删除失败: " + e.getMessage());
        }
    }
}