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.TpOrderBo;
import org.dromara.work.domain.vo.SkillOrderSumVo;
import org.dromara.work.domain.vo.SkillOrderVo;
import org.dromara.work.domain.vo.TpOrderVo;
import org.dromara.work.service.ITpOrderService;
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/skill/order")
public class SkillOrderController extends BaseController {

    private final ITpOrderService tpOrderService;

    /**
     * 查询订单管理列表
     */
    @SaCheckPermission("work:skillOrder:list")
    @GetMapping("/list")
    public TableDataInfo<SkillOrderVo> list(TpOrderBo bo, PageQuery pageQuery) {
        return tpOrderService.querySkillPageList(bo, pageQuery);
    }

    /**
     * 查询订单统计（技术）
     */
    @SaCheckPermission("work:skillOrder:sum")
    @GetMapping("/sum")
    public R<SkillOrderSumVo> sum(TpOrderBo bo) {
        return R.ok(tpOrderService.querySkillSum(bo));
    }

    /**
     * 导出技术订单
     */
    @SaCheckPermission("work:skillOrder:export")
    @Log(title = "导出技术订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpOrderBo bo, HttpServletResponse response) {
        List<TpOrderVo> list = tpOrderService.queryListJS(bo);
        ExcelUtil.exportExcel(list, "导出技术订单", TpOrderVo.class, response);
    }

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

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

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

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