package com.zhyc.module.sale.controller;

import java.util.List;
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.sale.domain.SxSheepSale;
import com.zhyc.module.sale.service.ISxSheepSaleService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 羊只销售记录Controller
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/saleRecord/saleRecord")
public class SxSheepSaleController extends BaseController {
    @Autowired
    private ISxSheepSaleService sxSheepSaleService;

    /**
     * 查询羊只销售记录列表
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SxSheepSale sxSheepSale) {
        startPage();
        List<SxSheepSale> list = sxSheepSaleService.selectSxSheepSaleList(sxSheepSale);
        return getDataTable(list);
    }

    /**
     * 导出羊只销售记录列表
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:export')")
    @Log(title = "羊只销售记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SxSheepSale sxSheepSale) {
        List<SxSheepSale> list = sxSheepSaleService.selectSxSheepSaleList(sxSheepSale);
        ExcelUtil<SxSheepSale> util = new ExcelUtil<SxSheepSale>(SxSheepSale.class);
        util.exportExcel(response, list, "羊只销售记录数据");
    }

    /**
     * 获取羊只销售记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        SxSheepSale sxSheepSale = sxSheepSaleService.selectSxSheepSaleById(id);
        // 将数据库中的逗号分隔的耳号字符串转换为列表
        if (sxSheepSale.getBsManageTags() != null && !sxSheepSale.getBsManageTags().isEmpty()) {
            sxSheepSale.setBsManageTagsList(java.util.Arrays.asList(sxSheepSale.getBsManageTags().split(",")));
        }
        return success(sxSheepSale);
    }

    /**
     * 新增羊只销售记录
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:add')")
    @Log(title = "羊只销售记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SxSheepSale sxSheepSale) {
        // 【新增】自动填充当前登录用户的 userId 和 deptId
        sxSheepSale.setUserId(getUserId());
        sxSheepSale.setDeptId(getDeptId());
        return toAjax(sxSheepSaleService.insertSxSheepSale(sxSheepSale));
    }

    /**
     * 修改羊只销售记录
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:edit')")
    @Log(title = "羊只销售记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxSheepSale sxSheepSale) {
        return toAjax(sxSheepSaleService.updateSxSheepSale(sxSheepSale));
    }

    /**
     * 删除羊只销售记录
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:remove')")
    @Log(title = "羊只销售记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sxSheepSaleService.deleteSxSheepSaleByIds(ids));
    }

    /**
     * 【新增】根据耳号查询羊只信息
     */
    @PreAuthorize("@ss.hasPermi('saleRecord:saleRecord:add')" + "|| @ss.hasPermi('saleRecord:saleRecord:edit')")
    @GetMapping("/getSheepInfo")
    public AjaxResult getSheepInfo(@RequestParam String bsManageTags) {
        // 调用Service方法查询信息
        SxSheepSale sheepInfo = sxSheepSaleService.selectSheepInfoByTag(bsManageTags);
        if (sheepInfo == null) {
            return AjaxResult.error("未找到耳号为 [" + bsManageTags + "] 的羊只信息");
        }
        return AjaxResult.success(sheepInfo);
    }
}