package com.agri.system.controller;

import com.agri.common.annotation.Log;
import com.agri.common.core.controller.BaseController;
import com.agri.common.core.domain.AjaxResult;
import com.agri.common.core.page.TableDataInfo;
import com.agri.common.enums.AgriEnum;
import com.agri.common.enums.BusinessType;
import com.agri.common.utils.SecurityUtils;
import com.agri.common.utils.poi.ExcelUtil;
import com.agri.system.domain.SysAgriInfo;
import com.agri.system.domain.SysUserAgri;
import com.agri.system.domain.vo.AgriInfoView;
import com.agri.system.service.ISysAgriInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 大棚管理Controller
 * 
 * @author agri
 * @date 2026-01-08
 */
@RestController
@RequestMapping("/assets/agri")
public class SysAgriInfoController extends BaseController
{
    @Autowired
    private ISysAgriInfoService sysAgriInfoService;

    /**
     * 查询大棚管理列表
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAgriInfo sysAgriInfo)
    {
        startPage();
        List<SysAgriInfo> list = sysAgriInfoService.findAgriByUser(sysAgriInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('assets:agri:list')")
    @GetMapping("/findAgriByUser")
    public AjaxResult findAgriByUser(SysAgriInfo sysAgriInfo) {
        List<SysAgriInfo> list = sysAgriInfoService.findAgriByUser(sysAgriInfo);
        return success(list);
    }
    /**
     * 导出大棚管理列表
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:export')")
    @Log(title = "大棚管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAgriInfo sysAgriInfo)
    {
        List<SysAgriInfo> list = sysAgriInfoService.selectSysAgriInfoList(sysAgriInfo);
        ExcelUtil<SysAgriInfo> util = new ExcelUtil<SysAgriInfo>(SysAgriInfo.class);
        util.exportExcel(response, list, "大棚管理数据");
    }

    /**
     * 获取大棚管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysAgriInfoService.selectSysAgriInfoById(id));
    }

    /**
     * 新增大棚管理
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:add')")
    @Log(title = "大棚管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAgriInfo sysAgriInfo)
    {
        return toAjax(sysAgriInfoService.insertSysAgriInfo(sysAgriInfo));
    }

    /**
     * 修改大棚管理
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:edit')")
    @Log(title = "大棚管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAgriInfo sysAgriInfo)
    {
        return toAjax(sysAgriInfoService.updateSysAgriInfo(sysAgriInfo));
    }

    /**
     * 删除大棚管理
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:remove')")
    @Log(title = "大棚管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysAgriInfoService.deleteSysAgriInfoByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('assets:agri:list')")
    @GetMapping("/findAgriInfoByUser")
    public AjaxResult findAgriInfoByUser(SysAgriInfo sysAgriInfo) {
        List<AgriInfoView> list = sysAgriInfoService.findAgriInfoByUser(sysAgriInfo);
        return success(list);
    }

    /**
     * 通过移动端添加大棚信息，默认添加当前用户为所有者
     * @param sysAgriInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:addAgriFromMobile')")
    @PostMapping("/addAgriFromMobile")
    public AjaxResult addAgriFromMobile(@RequestBody SysAgriInfo sysAgriInfo) {

        return success(sysAgriInfoService.addAgriFromMobile(sysAgriInfo));
    }

    @PreAuthorize("@ss.hasPermi('assets:agri:switchAgriMode')")
    @PostMapping("/switchAgriMode/{imei}")
    public AjaxResult switchAgriMode(@PathVariable String imei, @RequestParam("code") Integer code) {

        if (StringUtils.isEmpty(imei) || code == null) {
            return error();
        }
        sysAgriInfoService.lambdaUpdate()
                .eq(SysAgriInfo::getImei, imei)
                .set(SysAgriInfo::getWorkMode, code)
                .update();
        return success();
    }

    /**
     * 修改大棚名称
     */
    @PreAuthorize("@ss.hasPermi('assets:agri:edit')")
    @Log(title = "大棚管理", businessType = BusinessType.UPDATE)
    @PutMapping("/renameAgriName")
    public AjaxResult renameAgriName(@RequestParam("imei") String imei, @RequestParam("newAgriName") String newAgriName)
    {
        boolean update = sysAgriInfoService.lambdaUpdate()
                .eq(SysAgriInfo::getImei, imei)
                .set(SysAgriInfo::getAgriName, newAgriName)
                .update();
        return update? success():error();
    }
}
