package com.yongqi.xinrenli.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.yongqi.xinrenli.domain.vo.DbOrderVo;
import com.yongqi.xinrenli.domain.bo.DbOrderBo;
import com.yongqi.xinrenli.service.IDbOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单
 *
 * @author zyf
 * @date 2023-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xinrenli/order")
public class DbOrderController extends BaseController {

    private final IDbOrderService iDbOrderService;

    /**
     * 查询订单列表
     */
    @SaCheckPermission("xinrenli:order:list")
    @GetMapping("/list")
    public TableDataInfo<DbOrderVo> list(DbOrderBo bo, PageQuery pageQuery) {
        return iDbOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单列表
     */
    @SaCheckPermission("xinrenli:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DbOrderBo bo, HttpServletResponse response) {
        List<DbOrderVo> list = iDbOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", DbOrderVo.class, response);
    }

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

    /**
     * 新增订单
     */
    @SaCheckPermission("xinrenli:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DbOrderBo bo) {
        return toAjax(iDbOrderService.insertByBo(bo));
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("xinrenli:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DbOrderBo bo) {
        return toAjax(iDbOrderService.updateByBo(bo));
    }

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