package com.zhongzheng.controller.order;

import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.SecurityUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.order.bo.*;
import com.zhongzheng.modules.order.service.IOrderGoodsService;
import com.zhongzheng.modules.order.service.IOrderService;
import com.zhongzheng.modules.order.vo.OrderGoodsVo;
import com.zhongzheng.modules.order.vo.OrderListVo;
import com.zhongzheng.modules.order.vo.OrderVo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    private final IOrderService iOrderService;

    private final IOrderGoodsService iOrderGoodsService;

    private final WxTokenService wxTokenService;

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
    @GetMapping("/list")
    public TableDataInfo<OrderListVo> list(OrderQueryBo bo) {
        startPage();
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<OrderListVo> list = iOrderService.selectListApp(bo);
        return getDataTable(list);
    }

    /**
     * 查询订单商品列表
     */
    @ApiOperation("查询订单商品列表")
    @GetMapping("/goods/list")
    public TableDataInfo<OrderGoodsVo> goodsList(OrderGoodsQueryBo bo) {
        startPage();
        List<OrderGoodsVo> list = iOrderGoodsService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 修改订单商品
     */
    @ApiOperation("修改订单商品")
    @Log(title = "订单商品", businessType = BusinessType.UPDATE)
    @PostMapping("editOrderGood")
    public AjaxResult<Void> editOrderGood(@RequestBody OrderGoodsEditBo bo) {
        return toAjax(iOrderGoodsService.updateByEditBo(bo) ? 1 : 0);
    }

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

    /**
     * 新增订单
     */
    @ApiOperation("新增小程序订单")
    @PostMapping("placeSmallOrder")
    public AjaxResult add(@RequestBody OrderAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iOrderService.placeSmallOrder(bo));
    }

    @ApiOperation("继续支付小程序订单")
    @PostMapping("resumeSmallOrder")
    public AjaxResult resumeSmallOrder(@RequestBody OrderAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iOrderService.resumeSmallOrder(bo));
    }
}
