package org.dromara.app.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.app.domain.vo.AppWateringLogVo;
import org.dromara.app.domain.bo.AppWateringLogBo;
import org.dromara.app.service.IAppWateringLogService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 浇水记录
 *
 * @author Lion Li
 * @date 2026-04-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/wateringLog")
public class AppWateringLogController extends BaseController {

    private final IAppWateringLogService appWateringLogService;

    /**
     * 查询浇水记录列表
     */
    @SaCheckPermission("app:wateringLog:list")
    @GetMapping("/list")
    public TableDataInfo<AppWateringLogVo> list(AppWateringLogBo bo, PageQuery pageQuery) {
        return appWateringLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出浇水记录列表
     */
    @SaCheckPermission("app:wateringLog:export")
    @Log(title = "浇水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppWateringLogBo bo, HttpServletResponse response) {
        List<AppWateringLogVo> list = appWateringLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "浇水记录", AppWateringLogVo.class, response);
    }

    /**
     * 获取浇水记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:wateringLog:query")
    @GetMapping("/{id}")
    public R<AppWateringLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appWateringLogService.queryById(id));
    }

    /**
     * 新增浇水记录
     */
    @SaCheckPermission("app:wateringLog:add")
    @Log(title = "浇水记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppWateringLogBo bo) {
        return toAjax(appWateringLogService.insertByBo(bo));
    }

    /**
     * 修改浇水记录
     */
    @SaCheckPermission("app:wateringLog:edit")
    @Log(title = "浇水记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppWateringLogBo bo) {
        return toAjax(appWateringLogService.updateByBo(bo));
    }

    /**
     * 删除浇水记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:wateringLog:remove")
    @Log(title = "浇水记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appWateringLogService.deleteWithValidByIds(List.of(ids), true));
    }
}
