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.TpOrderRecordBo;
import org.dromara.work.domain.vo.TpOrderRecordVo;
import org.dromara.work.service.ITpOrderRecordService;
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/orderRecord")
public class TpOrderRecordController extends BaseController {

    private final ITpOrderRecordService tpOrderRecordService;

    /**
     * 查询操作历史列表
     */
    @SaCheckPermission("work:orderRecord:list")
    @GetMapping("/list")
    public TableDataInfo<TpOrderRecordVo> list(TpOrderRecordBo bo, PageQuery pageQuery) {
        return tpOrderRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出操作历史列表
     */
    @SaCheckPermission("work:orderRecord:export")
    @Log(title = "操作历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderRecordBo bo, HttpServletResponse response) {
        List<TpOrderRecordVo> list = tpOrderRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "操作历史", TpOrderRecordVo.class, response);
    }

    /**
     * 获取操作历史详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:orderRecord:query")
    @GetMapping("/{id}")
    public R<TpOrderRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpOrderRecordService.queryById(id));
    }

    /**
     * 新增操作历史
     */
    @SaCheckPermission("work:orderRecord:add")
    @Log(title = "操作历史", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpOrderRecordBo bo) {
        return toAjax(tpOrderRecordService.insertByBo(bo));
    }

    /**
     * 修改操作历史
     */
    @SaCheckPermission("work:orderRecord:edit")
    @Log(title = "操作历史", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpOrderRecordBo bo) {
        return toAjax(tpOrderRecordService.updateByBo(bo));
    }

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