package com.hxhq.business.controller;

import java.util.Arrays;
import java.util.List;

import com.hxhq.business.domain.StudyRoomHistory;
import com.hxhq.business.domain.StudyRoomJcgj;
import com.hxhq.business.form.study.StudyRoomForm;
import com.hxhq.business.form.study.StudyRoomHistorySearchForm;
import com.hxhq.business.form.study.StudyRoomSearchForm;
import com.hxhq.business.service.IStudyRoomHistoryService;
import com.hxhq.business.service.IStudyRoomJcgjService;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hxhq.business.domain.StudyRoom;
import com.hxhq.business.service.IStudyRoomService;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;


/**
 * 试验-饲养间Controller
 *
 * @author hxhq
 * @date 2025-12-29
 */
@RestController
@RequestMapping("/business/studyRoom")
public class StudyRoomController extends BaseController
{
    @Autowired
    private IStudyRoomService studyRoomService;

    @Autowired
    private IStudyRoomHistoryService studyRoomHistoryService;

    @Autowired
    private IStudyRoomJcgjService studyRoomJcgjService;

    /**
     * 查询试验-饲养间列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:studyRoom:list")
    public TableDataInfo list(StudyRoomSearchForm form)
    {
        startPage();
        List<StudyRoom> list = studyRoomService.queryList(form);
        return getDataTable(list);
    }

    @GetMapping("/jcgjList")
    public TableDataInfo jcgjList(StudyRoomJcgj form)
    {
        startPage();
        List<StudyRoomJcgj> list = studyRoomJcgjService.queryList(form);
        return getDataTable(list);
    }


    @GetMapping("/historyList")
    @RequiresPermissions("business:studyRoom:ljjl")
    public TableDataInfo historyList(StudyRoomHistorySearchForm form)
    {
        startPage();
        List<StudyRoomHistory> list = studyRoomHistoryService.queryList(form);
        return getDataTable(list);
    }


    /**
     * 获取试验-饲养间详细信息
     */
    @GetMapping(value = "/info")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(studyRoomService.getById(id));
    }

    /**
     * 新增试验-饲养间信息
     */
    @PostMapping("/save")
    @RequiresPermissions("business:studyRoom:add")
    public AjaxResult save(@RequestBody @Validated StudyRoomForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        studyRoomService.add(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 更新笼具
     * @return
     */
    @PostMapping("/update")
    @RequiresPermissions("business:studyRoom:update")
    public AjaxResult updateLj(@RequestBody StudyRoomForm form) {
        form.setQmrId(SecurityUtils.getUserId());
        studyRoomService.updateLj(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 结束
     * @param form
     * @return
     */
    @RequiresPermissions("business:studyRoom:js")
    @PostMapping("/js")
    public AjaxResult js(@RequestBody StudyRoomForm form) {
        if(StringUtils.isEmpty(form.getJssyyl())) {
            return error("请输入结束使用原因");
        }
        form.setQmrId(SecurityUtils.getUserId());
        studyRoomService.js(form);
        return AjaxResult.success("操作成功");
    }

}
