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.TpOrderModelBo;
import org.dromara.work.domain.vo.TpOrderModelVo;
import org.dromara.work.service.ITpOrderModelService;
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/orderModel")
public class TpOrderModelController extends BaseController {

    private final ITpOrderModelService tpOrderModelService;

    /**
     * 查询订单模型列表
     */
    @SaCheckPermission("work:orderModel:list")
    @GetMapping("/list")
    public TableDataInfo<TpOrderModelVo> list(TpOrderModelBo bo, PageQuery pageQuery) {
        return tpOrderModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单模型列表
     */
    @SaCheckPermission("work:orderModel:export")
    @Log(title = "订单模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderModelBo bo, HttpServletResponse response) {
        List<TpOrderModelVo> list = tpOrderModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单模型", TpOrderModelVo.class, response);
    }

    /**
     * 获取订单模型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:orderModel:query")
    @GetMapping("/{id}")
    public R<TpOrderModelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpOrderModelService.queryById(id));
    }

    /**
     * 新增订单模型
     */
    @SaCheckPermission("work:orderModel:add")
    @Log(title = "订单模型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpOrderModelBo bo) {
        return toAjax(tpOrderModelService.insertByBo(bo));
    }

    /**
     * 修改订单模型
     */
    @SaCheckPermission("work:orderModel:edit")
    @Log(title = "订单模型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpOrderModelBo bo) {
        return toAjax(tpOrderModelService.updateByBo(bo));
    }

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