package com.zhongzheng.controller.order;

import java.util.List;
import java.util.Arrays;

import com.zhongzheng.modules.order.bo.OrderAddBo;
import com.zhongzheng.modules.order.bo.OrderEditBo;
import com.zhongzheng.modules.order.bo.OrderQueryBo;
import com.zhongzheng.modules.order.domain.Order;
import com.zhongzheng.modules.order.service.IOrderService;
import com.zhongzheng.modules.order.vo.OrderVo;
import com.zhongzheng.modules.order.vo.UserOrderInFo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 订单Controller
 *
 * @author hjl
 * @date 2021-05-19
 */
@Api(value = "订单控制器", tags = {"订单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    private final IOrderService iOrderService;

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo<OrderVo> list(OrderQueryBo bo) {
        startPage();
        List<OrderVo> list = iOrderService.queryList(bo);
        return getDataTable(list);
    }



    /**
     * 查询订单列表
     */
    @ApiOperation("查询用户订单数据")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/listUserOrder")
    public AjaxResult<UserOrderInFo> listUserOrder(OrderQueryBo bo) {
        UserOrderInFo list = iOrderService.listUserOrder(bo);
        return AjaxResult.success(list);
    }

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单和商品列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/listAll")
    public TableDataInfo<Order> listAll(OrderQueryBo bo) {
        startPage();
        List<Order> list = iOrderService.getOrderGoodsList(bo);
        return getDataTable(list);
    }

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

    /**
     * 获取订单详细信息
     */
    @ApiOperation("获取订单详细信息")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping("/{orderId}")
    public AjaxResult<OrderVo> getInfo(@PathVariable("orderId" ) Long orderId) {
        return AjaxResult.success(iOrderService.queryById(orderId));
    }

    /**
     * 新增订单
     */
   /* @ApiOperation("新增订单")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody OrderAddBo bo) {
        return toAjax(iOrderService.insertByAddBo(bo) ? 1 : 0);
    }*/

    /**
     * 修改订单
     */
    @ApiOperation("修改订单")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@Validated  @RequestBody OrderEditBo bo) {
        return toAjax(iOrderService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除订单
     */
    /*@ApiOperation("删除订单")
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] orderIds) {
        return toAjax(iOrderService.deleteWithValidByIds(Arrays.asList(orderIds), true) ? 1 : 0);
    }*/
}
