package com.ruoyi.web.modules.video.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.modules.video.domain.BizLivestockMonitor;
import com.ruoyi.web.modules.video.service.IBizLivestockMonitorService;

/**
 * 畜牧监控管理 HTTP 接口。
 * <p>
 * 提供监控点位台账 CRUD、所属主体下拉、第三方全量同步。数据表 {@code biz_livestock_monitor}，
 * 权限前缀 {@code videoSurveillance:livestockMonitor}。详细字段与同步流程见
 * {@code doc/视频监控系统/对接畜牧监控/畜牧监控技术方案.md}。
 * </p>
 *
 * @see IBizLivestockMonitorService
 */
@RestController
@RequestMapping("/videoSurveillance/livestockMonitor")
public class BizLivestockMonitorController extends BaseController
{
    @Autowired
    private IBizLivestockMonitorService bizLivestockMonitorService;

    /**
     * 分页列表。支持监控名称模糊、所属主体（{@code ownerType}+{@code ownerId}）精确筛选。
     * 仅返回 {@code del_flag=0} 记录，按创建时间倒序。
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizLivestockMonitor query)
    {
        startPage();
        List<BizLivestockMonitor> list = bizLivestockMonitorService.selectBizLivestockMonitorList(query);
        return getDataTable(list);
    }

    /**
     * 所属主体下拉选项。合并「正常」养殖主体与交易市场，支持 {@code keyword} 模糊检索。
     * 供列表筛选与新增/编辑表单共用。
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:list')")
    @GetMapping("/ownerOptions")
    public AjaxResult ownerOptions(@RequestParam(value = "keyword", required = false) String keyword)
    {
        return success(bizLivestockMonitorService.selectOwnerOptions(keyword));
    }

    /**
     * 实时播放地址。按 {@code monitorId}（摄像头 ID）调用第三方 play-url 接口，不落库。
     * 须为本地已登记且未删除的监控点位。
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:query')")
    @GetMapping("/playUrl")
    public AjaxResult playUrl(@RequestParam("monitorId") String monitorId)
    {
        return success(bizLivestockMonitorService.getPlayUrlByMonitorId(monitorId));
    }

    /**
     * 详情。仅未逻辑删除记录可查询。
     *
     * @param id 主键 {@code biz_livestock_monitor.id}
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizLivestockMonitorService.selectBizLivestockMonitorById(id));
    }

    /**
     * 新增监控点位。必填：{@code monitorId}、{@code monitorName}、{@code videoSource}；
     * 所属主体、圈舍、安装位置、播放地址可选。
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:add')")
    @Log(title = "畜牧监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BizLivestockMonitor row)
    {
        row.setCreateBy(getUsername());
        return toAjax(bizLivestockMonitorService.insertBizLivestockMonitor(row));
    }

    /**
     * 修改监控点位。已逻辑删除记录不可修改。
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:edit')")
    @Log(title = "畜牧监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BizLivestockMonitor row)
    {
        row.setUpdateBy(getUsername());
        return toAjax(bizLivestockMonitorService.updateBizLivestockMonitor(row));
    }

    /**
     * 逻辑删除。路径参数为逗号分隔的主键 id，与若依惯例一致。
     *
     * @param ids 待删除主键数组
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:remove')")
    @Log(title = "畜牧监控", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizLivestockMonitorService.deleteBizLivestockMonitorByIds(ids, getUsername()));
    }

    /**
     * 第三方同步。拉取监控列表按 {@code monitor_id} upsert。
     * 返回 {@link com.ruoyi.web.modules.industryservice.domain.vo.YakAssetSyncResultVo} 统计摘要。
     */
    @PreAuthorize("@ss.hasPermi('videoSurveillance:livestockMonitor:sync')")
    @Log(title = "畜牧监控", businessType = BusinessType.OTHER)
    @PostMapping("/sync")
    public AjaxResult sync()
    {
        return success(bizLivestockMonitorService.syncFromThirdParty(getUsername()));
    }
}
