package com.younike.server.controller;

import java.util.List;

import com.younike.framework.config.AnonymousAccess;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.younike.common.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.system.domain.Order;
import com.younike.system.service.IOrderService;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.common.core.page.TableDataInfo;

/**
 * 订单主Controller
 * 
 * @author elev
 * @date 2022-01-07
 */
@Api("订单主接口")
@RestController
@RequestMapping("/server/order")
public class ServerOrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单主列表
     */
    @ApiOperation("查询订单主列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单主列表
     */
    @ApiOperation("导出订单主列表")
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Order order) {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "订单主数据");
    }

    /**
     * 获取订单主详细信息
     */
    @ApiOperation("获取订单主详细信息")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(orderService.selectOrderByOrderId(orderId));
    }

    /**
     * 新增订单主
     */
    @ApiOperation("新增订单主")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单主
     */
    @ApiOperation("修改订单主")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单主
     */
    @ApiOperation("删除订单主")
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(orderService.deleteOrderByOrderIds(orderIds));
    }

    /**
     * 获取服务端首页本周数值
     */
    @ApiOperation("修改订单状态")
    @PostMapping("/changeOrderState")
    @AnonymousAccess
    public AjaxResult changeOrderState(@RequestBody Order order) {
        int num = orderService.updateOrderCustom(order);
        if(num == 999){
            return AjaxResult.error("距离超过规定范围,不能开始服务!");
        }
        if(num == 0){
            return toAjax(0);
        }
        return toAjax(1);
    }


}
