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.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
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.service.IOrderService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单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;

    private final WxTokenService wxTokenService;

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


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


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

    /**
     * 新增订单
     */
    @ApiOperation("金币支付订单")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@RequestBody OrderAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iOrderService.placeOrder(bo));
    }

    /**
     * 公众号充值订单
     */
    @ApiOperation("公众号金币充值订单")
    @Log(title = "公众号金币充值订单", businessType = BusinessType.INSERT)
    @PostMapping("/credit")
    public AjaxResult credit(@RequestBody OrderAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iOrderService.placeGzhCoinOrder(bo));
    }

    /**
     * 公众号充值订单
     */
    @ApiOperation("小程序金币充值订单")
    @Log(title = "小程序金币充值订单", businessType = BusinessType.INSERT)
    @PostMapping("/smallCoin")
    public AjaxResult smallCoin(@RequestBody OrderAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iOrderService.placeSmallCoinOrder(bo));
    }
}
