package com.hxhq.business.controller;

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

import com.hxhq.business.domain.YqJcgj;
import com.hxhq.business.form.yq.YqForm;
import com.hxhq.business.form.yq.YqSearchForm;
import com.hxhq.business.service.IYqJcgjService;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hxhq.business.domain.Yq;
import com.hxhq.business.service.IYqService;
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 HanLong
 * @date 2025-12-20
 */
@RestController
@RequestMapping("/business/yq")
public class YqController extends BaseController {
    @Autowired
    private IYqService yqService;

    @Autowired
    private IYqJcgjService yqJcgjService;

    /**
     * 查询仪器管理列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:resource:yq:list")
    public TableDataInfo list(YqSearchForm form) {
        startPage();
        List<Yq> list = yqService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 查询仪器稽查轨迹列表
     */
    @RequiresPermissions("business:resource:yq:list")
    @GetMapping("/jcgj/list")
    public TableDataInfo list(Long id) {
        if(id == null ) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        YqJcgj yqJcgj = new YqJcgj();
        yqJcgj.setYqId(id);
        List<YqJcgj> list = yqJcgjService.queryList(yqJcgj);
        return getDataTable(list);
    }

    /**
     * 获取仪器管理详细信息
     */
    @RequiresPermissions("business:resource:yq:xq")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(yqService.getById(id));
    }

    /**
     * 新增、编辑仪器管理信息
     */
    @PostMapping("/save")
    @RequiresPermissions("business:resource:yq:xz")
    public AjaxResult save(@RequestBody @Validated YqForm form) {
        form.setQmrId(SecurityUtils.getUserId());
        yqService.addYq(form);
        return success();
    }

    @PostMapping("/update")
    @RequiresPermissions("business:resource:yq:bj")
    public AjaxResult update(@RequestBody @Validated YqForm form) {
        form.setQmrId(SecurityUtils.getUserId());
        yqService.updateYq(form);
        return success();
    }
}
