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

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

import com.zhyc.module.base.domain.BasSheep;
import com.zhyc.module.base.service.IBasSheepService;
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.manage_sheep.domain.ScChangeEar;
import com.zhyc.module.produce.manage_sheep.service.IScChangeEarService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 修改电子耳号记录Controller
 * 
 * @author ruoyi
 * @date 2025-07-24
 */
@RestController
@RequestMapping("/changeEar/changeEar")
public class ScChangeEarController extends BaseController
{
    @Autowired
    private IScChangeEarService scChangeEarService;

    @Autowired
    private IBasSheepService basSheepService;
    /**
     * 查询修改电子耳号记录列表
     */
    @PreAuthorize("@ss.hasPermi('changeEar:changeEar:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScChangeEar scChangeEar)
    {
        startPage();
        List<ScChangeEar> list = scChangeEarService.selectScChangeEarList(scChangeEar);
        return getDataTable(list);
    }

    /**
     * 导出修改电子耳号记录列表
     */
    @PreAuthorize("@ss.hasPermi('changeEar:changeEar:export')")
    @Log(title = "修改电子耳号记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScChangeEar scChangeEar)
    {
        List<ScChangeEar> list = scChangeEarService.selectScChangeEarList(scChangeEar);
        ExcelUtil<ScChangeEar> util = new ExcelUtil<ScChangeEar>(ScChangeEar.class);
        util.exportExcel(response, list, "修改电子耳号记录数据");
    }

    /**
     * 获取修改电子耳号记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('changeEar:changeEar:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        ScChangeEar changeEar = scChangeEarService.selectScChangeEarById(id);
        // 关联查询羊只表，获取旧耳号
        BasSheep sheep = basSheepService.selectBasSheepById(changeEar.getSheepId());
        if (sheep != null) {
            // 根据耳号类型，设置旧耳号
            if (changeEar.getEarType() == 0) {
                changeEar.setOldTag(sheep.getElectronicTags());
            } else {
                changeEar.setOldTag(sheep.getManageTags());
            }
        }
        return success(changeEar);
    }

    /**
     * 新增修改电子耳号记录
     */
    @PreAuthorize("@ss.hasPermi('changeEar:changeEar:add')")
    @Log(title = "修改电子耳号记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScChangeEar scChangeEar)
    {
        scChangeEar.setUserId(getUserId());
        scChangeEar.setDeptId(getDeptId());
        return toAjax(scChangeEarService.insertScChangeEar(scChangeEar));
    }

    /**
     * 修改修改电子耳号记录
     */
    @PreAuthorize("@ss.hasPermi('changeEar:changeEar:edit')")
    @Log(title = "修改电子耳号记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScChangeEar scChangeEar)
    {
        return toAjax(scChangeEarService.updateScChangeEar(scChangeEar));
    }

    /**
     * 删除修改电子耳号记录
     */
    @PreAuthorize("@ss.hasPermi('changeEar:changeEar:remove')")
    @Log(title = "修改电子耳号记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(scChangeEarService.deleteScChangeEarByIds(ids));
    }

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