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

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

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

/**
 * 断奶记录Controller
 */
@RestController
@RequestMapping("/Weaning/weaning_record")
public class ScWeanRecordController extends BaseController {
    @Autowired
    private IScWeanRecordService scWeanRecordService;

    /**
     * 查询断奶记录列表
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScWeanRecord scWeanRecord) {
        startPage();
        List<ScWeanRecord> list = scWeanRecordService.selectScWeanRecordList(scWeanRecord);
        return getDataTable(list);
    }

    /**
     * 【新增】模糊查询耳号列表 (用于前端下拉框远程搜索)
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:list')")
    @GetMapping("/search_ear_numbers")
    public AjaxResult searchEarNumbers(@RequestParam("query") String query) {
        List<String> list = scWeanRecordService.searchEarNumbers(query);
        return success(list);
    }

    /**
     * 导出断奶记录列表
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:export')")
    @Log(title = "断奶记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScWeanRecord scWeanRecord) {
        List<ScWeanRecord> list = scWeanRecordService.selectScWeanRecordList(scWeanRecord);
        ExcelUtil<ScWeanRecord> util = new ExcelUtil<ScWeanRecord>(ScWeanRecord.class);
        util.exportExcel(response, list, "断奶记录数据");
    }

    /**
     * 获取断奶记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scWeanRecordService.selectScWeanRecordById(id));
    }

    /**
     * 根据耳号查询羊只ID
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:query')")
    @GetMapping(value = "/sheepId/{earNumber}")
    public AjaxResult getSheepIdByEarNumber(@PathVariable("earNumber") String earNumber) {
        Long sheepId = scWeanRecordService.selectSheepIdByEarNumber(earNumber);
        if (sheepId != null) {
            return success(sheepId);
        } else {
            return error("未找到对应的羊只信息");
        }
    }

    /**
     * 新增断奶记录
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:add')")
    @Log(title = "断奶记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScWeanRecord scWeanRecord) {
        if (scWeanRecord.getEarNumber() != null) {
            Long sheepId = scWeanRecordService.selectSheepIdByEarNumber(scWeanRecord.getEarNumber());
            if (sheepId == null) {
                return error("耳号不存在，请检查后重新输入");
            }
            scWeanRecord.setSheepId(sheepId);
        }
        if (scWeanRecord.getSheepId() == null) return error("羊只信息不能为空");
        if (scWeanRecord.getDatetime() == null) return error("断奶日期不能为空");
        if (scWeanRecord.getWeight() == null) return error("断奶重量不能为空");
        if (scWeanRecord.getStatus() == null) return error("是否留养不能为空");

        scWeanRecord.setCreateBy(getUsername());
        return toAjax(scWeanRecordService.insertScWeanRecord(scWeanRecord));
    }

    /**
     * 修改断奶记录
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:edit')")
    @Log(title = "断奶记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScWeanRecord scWeanRecord) {
        if (scWeanRecord.getEarNumber() != null) {
            Long sheepId = scWeanRecordService.selectSheepIdByEarNumber(scWeanRecord.getEarNumber());
            if (sheepId == null) return error("耳号不存在");
            scWeanRecord.setSheepId(sheepId);
        }
        return toAjax(scWeanRecordService.updateScWeanRecord(scWeanRecord));
    }

    /**
     * 删除断奶记录
     */
    @PreAuthorize("@ss.hasPermi('Weaning:weaning_record:remove')")
    @Log(title = "断奶记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scWeanRecordService.deleteScWeanRecordByIds(ids));
    }


}