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.TpOrderCdBo;
import org.dromara.work.domain.vo.TpOrderCdVo;
import org.dromara.work.service.ITpOrderCdService;
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/orderCd")
public class TpOrderCdController extends BaseController {

    private final ITpOrderCdService tpOrderCdService;

    /**
     * 查询拆单记录列表
     */
    @SaCheckPermission("work:orderCd:list")
    @GetMapping("/list")
    public TableDataInfo<TpOrderCdVo> list(TpOrderCdBo bo, PageQuery pageQuery) {
        return tpOrderCdService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出拆单记录列表
     */
    @SaCheckPermission("work:orderCd:export")
    @Log(title = "拆单记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderCdBo bo, HttpServletResponse response) {
        List<TpOrderCdVo> list = tpOrderCdService.queryList(bo);
        ExcelUtil.exportExcel(list, "拆单记录", TpOrderCdVo.class, response);
    }

    /**
     * 获取拆单记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:orderCd:query")
    @GetMapping("/{id}")
    public R<TpOrderCdVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpOrderCdService.queryById(id));
    }

    /**
     * 新增拆单记录
     */
    @SaCheckPermission("work:orderCd:add")
    @Log(title = "拆单记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpOrderCdBo bo) {
        return toAjax(tpOrderCdService.insertByBo(bo));
    }

    /**
     * 修改拆单记录
     */
    @SaCheckPermission("work:orderCd:edit")
    @Log(title = "拆单记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpOrderCdBo bo) {
        return toAjax(tpOrderCdService.updateByBo(bo));
    }

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