package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpMonthBo;
import org.dromara.work.domain.vo.TpMonthVo;
import org.dromara.work.service.ITpMonthService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 月目标
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/month")
public class TpMonthController extends BaseController {

    private final ITpMonthService tpMonthService;

    /**
     * 查询月目标列表
     */
    @SaCheckPermission("work:month:list")
    @GetMapping("/list")
    public TableDataInfo<TpMonthVo> list(TpMonthBo bo, PageQuery pageQuery) {
        return tpMonthService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出月目标列表
     */
    @SaCheckPermission("work:month:export")
    @Log(title = "月目标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpMonthBo bo, HttpServletResponse response) {
        List<TpMonthVo> list = tpMonthService.queryList(bo);
        ExcelUtil.exportExcel(list, "月目标", TpMonthVo.class, response);
    }

    /**
     * 获取月目标详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:month:query")
    @GetMapping("/{id}")
    public R<TpMonthVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpMonthService.queryById(id));
    }

    /**
     * 新增月目标
     */
    @SaCheckPermission("work:month:add")
    @Log(title = "月目标", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpMonthBo bo) {
        return toAjax(tpMonthService.insertByBo(bo));
    }

    /**
     * 修改月目标
     */
    @SaCheckPermission("work:month:edit")
    @Log(title = "月目标", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpMonthBo bo) {
        return toAjax(tpMonthService.updateByBo(bo));
    }

    /**
     * 删除月目标
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:month:remove")
    @Log(title = "月目标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpMonthService.deleteWithValidByIds(List.of(ids), true));
    }
}
