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

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhyc.common.utils.StringUtils;
import com.zhyc.module.produce.other.domain.ScFixHoof;
import com.zhyc.module.produce.other.service.IScFixHoofService;
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.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 修蹄Controller
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@RestController
@RequestMapping("/produce/other/fixHoof")
public class ScFixHoofController extends BaseController
{
    @Autowired
    private IScFixHoofService scFixHoofService;

    /**
     * 查询修蹄列表
     */
    @PreAuthorize("@ss.hasPermi('produce:fixHoof:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScFixHoof scFixHoof)
    {
        startPage();
        List<ScFixHoof> list = scFixHoofService.selectScFixHoofList(scFixHoof);
        return getDataTable(list);

    }

    /**
     * 导出修蹄列表
     */
    @PreAuthorize("@ss.hasPermi('produce:fixHoof:export')")
    @Log(title = "修蹄", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScFixHoof scFixHoof)
    {
        List<ScFixHoof> list = scFixHoofService.selectScFixHoofList(scFixHoof);
        ExcelUtil<ScFixHoof> util = new ExcelUtil<ScFixHoof>(ScFixHoof.class);
        util.exportExcel(response, list, "修蹄数据");
    }

    /**
     * 获取修蹄详细信息
     */
    @PreAuthorize("@ss.hasPermi('produce:fixHoof:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(scFixHoofService.selectScFixHoofById(id));
    }

    /**
     * 新增修蹄记录（支持批量）
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('produce:fixHoof:add')")
    @Log(title = "修蹄", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<ScFixHoof> dtos) {
        for (ScFixHoof dto : dtos) {
            if (dto.getSheepId() == null && StringUtils.isNotBlank(dto.getManageTags())) {
                Long sheepId = scFixHoofService.findIdByManageTags(dto.getManageTags());
                dto.setSheepId(sheepId.intValue());
            }
            dto.setUserId(getUserId());
            dto.setDeptId(getDeptId());
            // 保存修蹄记录
            scFixHoofService.insertScFixHoof(dto);
        }
        return toAjax(dtos.size());
    }

    /**
     * 修改修蹄
     */
    @PreAuthorize("@ss.hasPermi('produce:fixHoof:edit')")
    @Log(title = "修蹄", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScFixHoof dto){
        dto.setSheepId(scFixHoofService.findIdByManageTags(dto.getManageTags()).intValue());
        return toAjax(scFixHoofService.updateScFixHoof(dto));
    }

    /**
     * 删除修蹄
     */
    @PreAuthorize("@ss.hasPermi('produce:fixHoof:remove')")
    @Log(title = "修蹄", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(scFixHoofService.deleteScFixHoofByIds(ids));
    }

    @GetMapping("/search_ear_numbers")
    public AjaxResult searchEarNumbers(@RequestParam("query") String query){
        return success(scFixHoofService.searchEarNumbers(query.trim()));
    }

}

