package com.ruoyi.demo.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.demo.entity.Order;
import com.ruoyi.demo.entity.bo.OrderAddBo;
import com.ruoyi.demo.entity.bo.PlanBo;
import com.ruoyi.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    // 获取sign——用于测试
    @PostMapping("/getSign")
    public R getSign(@RequestBody OrderAddBo bo){
        String sign = orderService.getSign(bo);
        if (sign.equals("error"))
            return R.fail("无效的appkey");
        return R.ok(sign);
    }

    // 添加订单
    @PostMapping()
    public R addOrder(@RequestBody @Validated(value = {AddGroup.class}) OrderAddBo bo){
        int i = orderService.addOrder(bo);
        if (i == 1)
            return R.ok("添加成功");
        if (i == -1)
            return R.fail("无效的appkey");
        if (i == -3)
            return R.fail("校验sign出错");
        return R.fail("添加失败");
    }

    // 查询订单列表
    @PostMapping("/list")
    public R orderList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        return R.ok(orderService.getOrderList(bo));
    }
}
