package com.agri.system.controller;

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

import com.agri.common.utils.StringUtils;
import com.agri.system.domain.vo.AgriTermVo;
import org.apache.commons.collections4.CollectionUtils;
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.RestController;
import com.agri.common.annotation.Log;
import com.agri.common.core.controller.BaseController;
import com.agri.common.core.domain.AjaxResult;
import com.agri.common.enums.BusinessType;
import com.agri.system.domain.SysAutoTerm;
import com.agri.system.service.ISysAutoTermService;
import com.agri.common.utils.poi.ExcelUtil;
import com.agri.common.core.page.TableDataInfo;

/**
 * 卷膜运行条件Controller
 * 
 * @author lld
 * @date 2026-02-27
 */
@RestController
@RequestMapping("/control/autoTerm")
public class SysAutoTermController extends BaseController
{
    @Autowired
    private ISysAutoTermService sysAutoTermService;

    /**
     * 查询卷膜运行条件列表
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAutoTerm sysAutoTerm)
    {
        startPage();
        List<SysAutoTerm> list = sysAutoTermService.selectSysAutoTermList(sysAutoTerm);
        return getDataTable(list);
    }

    /**
     * 导出卷膜运行条件列表
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:export')")
    @Log(title = "卷膜运行条件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAutoTerm sysAutoTerm)
    {
        List<SysAutoTerm> list = sysAutoTermService.selectSysAutoTermList(sysAutoTerm);
        ExcelUtil<SysAutoTerm> util = new ExcelUtil<SysAutoTerm>(SysAutoTerm.class);
        util.exportExcel(response, list, "卷膜运行条件数据");
    }

    /**
     * 获取卷膜运行条件详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysAutoTermService.selectSysAutoTermById(id));
    }

    /**
     * 新增卷膜运行条件
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:add')")
    @Log(title = "卷膜运行条件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAutoTerm sysAutoTerm)
    {
        return toAjax(sysAutoTermService.insertSysAutoTerm(sysAutoTerm));
    }

    /**
     * 修改卷膜运行条件
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:edit')")
    @Log(title = "卷膜运行条件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAutoTerm sysAutoTerm)
    {
        return toAjax(sysAutoTermService.updateSysAutoTerm(sysAutoTerm));
    }

    /**
     * 删除卷膜运行条件
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:remove')")
    @Log(title = "卷膜运行条件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysAutoTermService.deleteSysAutoTermByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('control:autoTerm:saveAgriTerm')")
    @Log(title = "保存自动化条件", businessType = BusinessType.INSERT)
    @PutMapping("/saveAgriTerm")
    public AjaxResult saveAgriTerm(@RequestBody List<AgriTermVo> agriTerms) {
        if (CollectionUtils.isEmpty(agriTerms)) {
            return error("保存失败！自动化条件设置为空！");
        } else {
            String validateTips = sysAutoTermService.validate(agriTerms);
            if (StringUtils.isNotEmpty(validateTips)) {
                return error(validateTips);
            }
        }
        sysAutoTermService.saveAgriTerm(agriTerms);
        return success();
    }

    /**
     * 获取当前设备下所有条件
     * @param imei
     * @return
     */
    @PreAuthorize("@ss.hasPermi('control:autoTerm:query')")
    @GetMapping("/getAgriTerm/{imei}")
    public AjaxResult getAgriTerm(@PathVariable String imei) {

        return success(sysAutoTermService.getAgriTerm(imei));
    }
}
